Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions recipes/python/audio-intelligence/v1/sentiment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Sentiment Analysis (Audio Intelligence v1)

Segment-level positive/negative/neutral sentiment scoring on audio content.

## What it does

When `sentiment=True` is set, Deepgram analyses the emotional tone of each segment in the transcript and labels it as positive, negative, or neutral. This is valuable for call-centre analytics, customer feedback processing, or understanding the emotional arc of conversations and recordings.

## Key parameters

| Parameter | Value | Description |
|-----------|-------|-------------|
| `sentiment` | `True` | Enable sentiment analysis |
| `model` | `"nova-3"` | Transcription model |
| `smart_format` | `True` | Format numbers, dates, etc. |

## Example output

```
Transcript: Yeah, as much as it's worth celebrating the 50th anniversary of the spacewalk...

Sentiment segments: 4
[positive] Yeah, as much as it's worth celebrating the 50th anniversary
[neutral] it's also worth noting that we've come a long way since then
```

## Prerequisites

- Python 3.10+
- Set `DEEPGRAM_API_KEY` environment variable
- Install: `pip install -r recipes/python/requirements.txt`

## Run

```bash
python example.py
```

## Test

```bash
pytest example_test.py -v
```
42 changes: 42 additions & 0 deletions recipes/python/audio-intelligence/v1/sentiment/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Recipe: Sentiment Analysis (Audio Intelligence v1)
====================================================
Demonstrates segment-level sentiment analysis on audio content.

When sentiment=True is set, each segment of the transcript is scored as
positive, negative, or neutral with a confidence value. This is useful
for call-centre analytics, customer feedback analysis, or understanding
the emotional tone of conversations.
"""

from deepgram import DeepgramClient

AUDIO_URL = "https://dpgr.am/spacewalk.wav"


def main():
client = DeepgramClient() # reads DEEPGRAM_API_KEY from environment

response = client.listen.v1.media.transcribe_url(
url=AUDIO_URL,
model="nova-3",
smart_format=True,
sentiment=True, # <-- THIS enables sentiment analysis.
)

if response.results and response.results.channels:
transcript = response.results.channels[0].alternatives[0].transcript
print(f"Transcript: {transcript[:150]}...")

# Sentiment results appear in response.results.sentiments.segments
if hasattr(response.results, "sentiments") and response.results.sentiments:
segments = getattr(response.results.sentiments, "segments", [])
print(f"\nSentiment segments: {len(segments)}")
for seg in segments[:5]:
sentiment = getattr(seg, "sentiment", "unknown")
text = getattr(seg, "text", "")
print(f" [{sentiment}] {text[:80]}")


if __name__ == "__main__":
main()
17 changes: 17 additions & 0 deletions recipes/python/audio-intelligence/v1/sentiment/example_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import subprocess
from pathlib import Path

def test_example_runs():
"""Runs the sentiment example and verifies it produces output."""
example = Path(__file__).parent / "example.py"
result = subprocess.run(
["python", str(example)],
capture_output=True,
text=True,
timeout=60,
)
assert result.returncode == 0, (
f"Example failed\nSTDOUT: {result.stdout}\nSTDERR: {result.stderr}"
)
assert result.stdout.strip(), "Example produced no output"
assert len(result.stdout.strip()) > 10, "Output seems too short"
Loading