Created
December 10, 2021 13:07
-
-
Save gmyrianthous/c8d985301d71f460c1286d0b27f9d1c5 to your computer and use it in GitHub Desktop.
Sentiment Analysis with AssemblyAI API and 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 requests | |
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' | |
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 | |
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, | |
'sentiment_analysis': True | |
}, | |
) | |
res_transcript_json = res_transcript.json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment