For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
PlaygroundChangelogSign In
OverviewAPI ReferencePre-recorded STTStreaming STTVoice AgentsSpeech UnderstandingGuardrailsLLM GatewayFAQ
OverviewAPI ReferencePre-recorded STTStreaming STTVoice AgentsSpeech UnderstandingGuardrailsLLM GatewayFAQ
  • Models & features
    • Getting started
    • Identify speakers by name/role
    • Translate transcripts
    • Format transcripts with custom rules
    • Detect entities in transcript
    • Analyze sentiment of speech
    • Create summarized chapters
    • Identify highlights
    • Detect discussion topics
    • Summarize transcripts
LogoLogo
PlaygroundChangelogSign In
On this page
  • Quickstart
  • Example output
  • Add speaker labels to sentiments
  • API reference
  • Request
  • Response
  • Frequently asked questions
Models & features

Sentiment Analysis

Was this page helpful?
Previous

Auto Chapters

Next
Built with
Supported languages
Global Englishen
Australian Englishen_au
British Englishen_uk
US Englishen_us

Supported models
Universal-3 Prouniversal-3-pro
Universal-2universal-2

Supported regions

US & EU

The Sentiment Analysis model detects the sentiment of each spoken sentence in the transcript text. Use Sentiment Analysis to get a detailed analysis of the positive, negative, or neutral sentiment conveyed in the audio, along with a confidence score for each result.

Quickstart

Python
Python SDK
JavaScript
JavaScript SDK

Enable Sentiment Analysis by setting sentiment_analysis to True in the JSON payload.

1import requests
2import time
3
4base_url = "https://api.assemblyai.com"
5
6headers = {
7 "authorization": "<YOUR_API_KEY>"
8}
9
10with open("./local_file.mp3", "rb") as f:
11 response = requests.post(base_url + "/v2/upload",
12 headers=headers,
13 data=f)
14
15upload_url = response.json()["upload_url"]
16
17data = {
18 "audio_url": upload_url, # You can also use a URL to an audio or video file on the web
19 "speech_models": ["universal-3-pro", "universal-2"],
20 "language_detection": True,
21 "sentiment_analysis": True
22}
23
24url = base_url + "/v2/transcript"
25response = requests.post(url, json=data, headers=headers)
26
27transcript_id = response.json()['id']
28polling_endpoint = base_url + "/v2/transcript/" + transcript_id
29
30print(f"Transcript ID: {transcript_id}")
31
32while True:
33 transcription_result = requests.get(polling_endpoint, headers=headers).json()
34
35 if transcription_result['status'] == 'completed':
36 for sentiment_result in transcription_result['sentiment_analysis_results']:
37 print(sentiment_result['text'])
38 print(sentiment_result['sentiment']) # POSITIVE, NEUTRAL, or NEGATIVE
39 print(sentiment_result['confidence'])
40 print(f"Timestamp: {sentiment_result['start']} - {sentiment_result['end']}")
41 break
42 elif transcription_result['status'] == 'error':
43 raise RuntimeError(f"Transcription failed: {transcription_result['error']}")
44 else:
45 time.sleep(3)

Example output

1Smoke from hundreds of wildfires in Canada is triggering air quality alerts throughout the US.
2NEGATIVE
30.8181032538414001
4Timestamp: 250 - 6350
5...
Sentiment Analysis Using LLM Gateway

Check out this cookbook LLM Gateway for Customer Call Sentiment Analysis for an example of how to use LLM Gateway to analyze the sentiment of a customer call.

Add speaker labels to sentiments

Python
Python SDK
JavaScript
JavaScript SDK

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

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

1data = {
2 "audio_url": upload_url,
3 "sentiment_analysis": True,
4 "speaker_labels": True
5}
6# ...
7 for sentiment_result in transcription_result['sentiment_analysis_results']:
8 print(sentiment_result['speaker'])
9 break

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

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

What if the model predicts the wrong sentiment label for a sentence?

The Sentiment Analysis model is based on the interpretation of the transcript and may not always accurately capture the intended sentiment of the speaker. It’s recommended to take into account the context of the transcript and to validate the sentiment analysis results with human judgment when possible.

What if the transcript contains sensitive or offensive content?

The Content Moderation model can be used to identify and filter out sensitive or offensive content from the transcript.

What if the sentiment analysis results aren't consistent with my expectations?

It’s important to ensure that the audio being analyzed is relevant to your use case. Additionally, it’s recommended to take into account the context of the transcript and to evaluate the confidence score for each sentiment label.

What if the sentiment analysis is taking too long to process?

The Sentiment Analysis model is designed to be fast and efficient, but processing times may vary depending on the size of the audio file and the complexity of the language used. If you experience longer processing times than expected, don’t hesitate to contact our support team.