Created
April 27, 2023 00:49
-
-
Save pranavsharma/2330b26d3506e99f929d8b818ae242d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <curl/curl.h> | |
int main(void) { | |
CURL *curl; | |
CURLcode res; | |
// Set the API endpoint and the authentication token | |
const char *url = "https://api.openai.com/v1/audio/transcriptions"; | |
const char *token = "your_api_token_here"; | |
// Set the model to use for transcription | |
const char *model = "whisper-1"; | |
// Set the file path and read its binary data | |
const char *file_path = "/home/pranav/test.m4a"; | |
FILE *fp = fopen(file_path, "rb"); | |
fseek(fp, 0L, SEEK_END); | |
const size_t file_size = ftell(fp); | |
rewind(fp); | |
char *file_data = static_cast<char*>(malloc(file_size)); | |
fread(file_data, 1, file_size, fp); | |
fclose(fp); | |
// Initialize curl | |
curl = curl_easy_init(); | |
if(curl) { | |
// Set the headers for the request | |
struct curl_slist *headers = NULL; | |
headers = curl_slist_append(headers, "Content-Type: multipart/form-data"); | |
headers = curl_slist_append(headers, "Authorization: Bearer TOKEN"); | |
headers = curl_slist_append(headers, token); | |
// Set the multipart form data | |
struct curl_httppost *post = NULL; | |
struct curl_httppost *last = NULL; | |
curl_formadd(&post, &last, CURLFORM_COPYNAME, "model", CURLFORM_COPYCONTENTS, model, CURLFORM_END); | |
curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_BUFFER, file_path, CURLFORM_BUFFERPTR, file_data, CURLFORM_BUFFERLENGTH, file_size, CURLFORM_END); | |
// Set the request options | |
curl_easy_setopt(curl, CURLOPT_URL, url); | |
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); | |
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); | |
// Perform the request | |
res = curl_easy_perform(curl); | |
// Check for errors | |
if(res != CURLE_OK) { | |
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); | |
} | |
// Clean up | |
curl_slist_free_all(headers); | |
curl_formfree(post); | |
curl_easy_cleanup(curl); | |
free(file_data); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment