#I am working on a project where I
4 messages · Page 1 of 1 (latest)
Code:
`import requests
import io
import subprocess
file_url = #.opus file url
api_key = #WATI API Keu
def transcribe_audio_to_text():
Fetch the audio file and convert to wav format
headers = {'Authorization': f'Bearer {api_key}'}
response = requests.get(file_url, headers=headers)
audio_bytes = io.BytesIO(response.content)
process = subprocess.Popen(['ffmpeg', '-i', '-', '-f', 'wav', '-acodec', 'libmp3lame', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
wav_audio, _ = process.communicate(input=audio_bytes.read())
Set the Whisper API endpoint and headers
WHISPER_API_ENDPOINT = 'https://api.openai.com/v1/audio/translations'
whisper_api_headers = {'Authorization': 'Bearer ' + WHISPER_API_KEY,
'Content-Type': 'application/json'}
print(whisper_api_headers)
Send the audio file for transcription
payload = {'model': 'whisper-1'}
files = {'file': ('audio.wav', io.BytesIO(wav_audio), 'audio/wav')}
files = {'file': ('audio.wav', io.BytesIO(wav_audio), 'application/octet-stream')}
files = {'file': ('audio.mp3', io.BytesIO(mp3_audio), 'audio/mp3')}
response = requests.post(WHISPER_API_ENDPOINT, headers=whisper_api_headers, data=payload)
print(response)
Get the transcription text
if response.status_code == 200:
result = response.json()
text = result['text']
print(response, text)
else:
print('Error:', response)
err = response.json()
print(response.status_code)
print(err)
print(response.headers)
transcribe_audio_to_text()`
Output:
Error: <Response [400]> 400 {'error': {'message': "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)", 'type': 'invalid_request_error', 'param': None, 'code': None}}
Adjust your request to send multipart/form-data content type instead of application/json. Not sure about other parts of implementation but I would first build a request with a file you are sure whisper can transcribe. When you get success there if you still have issues I would look into a .wav output