Created
May 14, 2023 10:29
-
-
Save AIAnytime/14e6affec09fd2de9fedf5eb8c2b1914 to your computer and use it in GitHub Desktop.
Speech to Text Assembly AI
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
# 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'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
base_url = "https://api.assemblyai.com/v2"
headers = {
"authorization": api_token,
"content-type": "application/json"
}