Created
February 8, 2022 16:02
-
-
Save gmyrianthous/cd0e214916a127a955630d1d42f96570 to your computer and use it in GitHub Desktop.
Topic Detection with Python - Full Code
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
import os | |
import sys | |
import requests | |
from time import sleep | |
API_KEY = <your AssemblyAI API key goes here> | |
AUDIO_FILE = '/path/to/your/audio/file.mp3' | |
UPLOAD_ENDPOINT = 'https://api.assemblyai.com/v2/upload' | |
TRANSCRIPT_ENDPOINT = 'https://api.assemblyai.com/v2/transcript' | |
OUTPUT_TRANSCRIPT_FILE = 'speech-to-text-tutorial.txt' | |
def read_audio_file(file): | |
"""Helper method that reads in audio files""" | |
with open(file, 'rb') as f: | |
while True: | |
data = f.read(5242880) | |
if not data: | |
break | |
yield data | |
# Create the headers for request | |
headers = { | |
'authorization': API_KEY, | |
'content-type': 'application/json' | |
} | |
res_upload = requests.post( | |
UPLOAD_ENDPOINT, | |
headers=headers, | |
data=read_audio_file(AUDIO_FILE) | |
) | |
upload_url = res_upload.json()['upload_url'] | |
res_transcript = requests.post( | |
TRANSCRIPT_ENDPOINT, | |
headers=headers, | |
json={ | |
'audio_url': upload_url, | |
'iab_categories': True, | |
}, | |
) | |
res_transcript_json = res_transcript.json() | |
status = '' | |
while status != 'completed': | |
res_result = requests.get( | |
os.path.join(TRANSCRIPT_ENDPOINT, res_transcript_json['id']), | |
headers=headers | |
) | |
status = res_result.json()['status'] | |
print(f'Status: {status}') | |
if status == 'error': | |
sys.exit('Audio file failed to process.') | |
elif status != 'completed': | |
sleep(10) | |
with open(OUTPUT_TRANSCRIPT_FILE, 'w') as f: | |
f.write(res_result.json()['text']) | |
print(f'Transcript file saved under {OUTPUT_TRANSCRIPT_FILE}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment