Quickstart

Enable Sentiment Analysis by setting sentiment_analysis to true in the transcription config.

import assemblyai as aai

aai.settings.api_key = "YOUR_API_KEY"

# audio_file = "./local_file.mp3"
audio_file = "https://assembly.ai/wildfires.mp3"

config = aai.TranscriptionConfig(sentiment_analysis=True)

transcript = aai.Transcriber().transcribe(audio_file, config)

for sentiment_result in transcript.sentiment_analysis:
    print(sentiment_result.text)
    print(sentiment_result.sentiment)  # POSITIVE, NEUTRAL, or NEGATIVE
    print(sentiment_result.confidence)
    print(f"Timestamp: {sentiment_result.start} - {sentiment_result.end}")

Example output

Smoke from hundreds of wildfires in Canada is triggering air quality alerts throughout the US.
SentimentType.negative
0.8181032538414001
Timestamp: 250 - 6350
...

Add speaker labels to sentiments

To add speaker labels to each sentiment analysis result, using Speaker Diarization, enable speaker_labels in the transcription config.

Each sentiment result will then have a speaker field that contains the speaker label.

config = aai.TranscriptionConfig(
  sentiment_analysis=True,
  speaker_labels=True
)

# ...

for sentiment_result in transcript.sentiment_analysis:
  print(sentiment_result.speaker)

API reference

Request

curl https://api.assemblyai.com/v2/transcript \
--header "Authorization: YOUR_API_KEY" \
--header "Content-Type: application/json" \
--data '{
  "audio_url": "YOUR_AUDIO_URL",
  "sentiment_analysis": true
}'
KeyTypeDescription
sentiment_analysisbooleanEnable Sentiment Analysis.

Response

{
  "sentiment_analysis_results": [...]
}
KeyTypeDescription
sentiment_analysis_resultsarrayA temporal sequence of Sentiment Analysis results for the audio file, one element for each sentence in the file.
sentiment_analysis_results[i].textstringThe transcript of the i-th sentence.
sentiment_analysis_results[i].startnumberThe starting time, in milliseconds, of the i-th sentence.
sentiment_analysis_results[i].endnumberThe ending time, in milliseconds, of the i-th sentence.
sentiment_analysis_results[i].sentimentstringThe detected sentiment for the i-th sentence, one of POSITIVE, NEUTRAL, NEGATIVE.
sentiment_analysis_results[i].confidencenumberThe confidence score for the detected sentiment of the i-th sentence, from 0 to 1.
sentiment_analysis_results[i].speakerstring or nullThe speaker of the i-th sentence if Speaker Diarization is enabled, else null.

Frequently asked questions