Created
January 16, 2023 06:55
-
-
Save ankushagarwal/c84a2e26e7980374dcd3b2997c3357e6 to your computer and use it in GitHub Desktop.
Open AI Whisper API
This file contains hidden or 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
import requests | |
from requests_toolbelt import MultipartEncoder | |
import os, random, string | |
# Some basics | |
req_url = "https://api.openai.com/v1/engines/audio-transcribe-001/transcriptions" | |
openai_key = 'OPEN_AI_KEY_GOES_HERE' | |
# Load file as bytes and add it as a form field | |
file_path = "test.mp3" | |
file = open(file_path, "rb").read() | |
form_fields = { | |
'file': (file_path, file, 'audio/mpeg'), | |
} | |
# Encode our fake form | |
boundary = '--WebKitFormBoundary' + ''.join(random.sample(string.ascii_letters + string.digits, 16)) | |
m = MultipartEncoder (fields=form_fields, boundary=boundary) | |
# Send the request to OpenAI | |
response = requests.post( | |
req_url, | |
headers={ | |
"Authorization": f"Bearer {openai_key}", | |
"Content-Type": m.content_type | |
}, data=m | |
) | |
# Et voila! | |
print(response.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment