Skip to content

Instantly share code, notes, and snippets.

@gmyrianthous
Created December 10, 2021 13:07
Show Gist options
  • Save gmyrianthous/c8d985301d71f460c1286d0b27f9d1c5 to your computer and use it in GitHub Desktop.
Save gmyrianthous/c8d985301d71f460c1286d0b27f9d1c5 to your computer and use it in GitHub Desktop.
Sentiment Analysis with AssemblyAI API and Python - Full Code
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