# Prepare data for API request
files = {
'audio': (filename, audio, f'audio/{file_ext[1:]}'), # Specify audio file type
'toggle_diarization': (None, True), # Toggle diarization option
'diarization_max_speakers': (None, 2), # Set the maximum number of speakers for diarization
'output_format': (None, 'txt') # Specify output format as text
}
print('Sending request to Gladia API')
# Make a POST request to Gladia API
response = requests.post('https://api.gladia.io/audio/text/audio-transcription/', headers=headers, files=files)
if response.status_code == 200:
# If the request is successful, parse the JSON response
response = response.json()
# Extract the transcription from the response
prediction = response['prediction']
# Write the transcription to a text file
with open('transcription.txt', 'w') as f:
f.write(prediction)
return response
else:
# If the request fails, print an error message and return the JSON response
print('Request failed')
return response.json()