Skip to content

Instantly share code, notes, and snippets.

@AIAnytime
Created May 14, 2023 10:29
Show Gist options
  • Save AIAnytime/14e6affec09fd2de9fedf5eb8c2b1914 to your computer and use it in GitHub Desktop.
Save AIAnytime/14e6affec09fd2de9fedf5eb8c2b1914 to your computer and use it in GitHub Desktop.
Speech to Text Assembly AI
# Assembly AI speech to text
def assemblyai_stt(audio_filename):
with open(audio_filename , "rb") as f:
response = requests.post(base_url + "/upload",
headers=headers,
data=f)
upload_url = response.json()["upload_url"]
data = {
"audio_url": upload_url
}
url = base_url + "/transcript"
response = requests.post(url, json=data, headers=headers)
transcript_id = response.json()['id']
polling_endpoint = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"
while True:
transcription_result = requests.get(polling_endpoint, headers=headers).json()
if transcription_result['status'] == 'completed':
break
elif transcription_result['status'] == 'error':
raise RuntimeError(f"Transcription failed: {transcription_result['error']}")
else:
print("Processing...")
time.sleep(3)
print(transcription_result['text'])
file = open('docs/transcription.txt', 'w')
file.write(transcription_result['text'])
file.close()
return transcription_result['text']
@AIAnytime
Copy link
Author

base_url = "https://api.assemblyai.com/v2"

headers = {
"authorization": api_token,
"content-type": "application/json"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment