You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learning-paths/mobile-graphics-and-gaming/voice-sentiment-analysis-with-llm/1_overview.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ layout: learningpathall
5
5
---
6
6
Voice-based LLM applications often rely primarily on transcribed text from speech input, such as in interactions with non-player characters in games or voice assistants. This approach can overlook vocal cues—like tone, pitch, and emotion—present in a speaker’s voice. As a result, responses may feel less natural and may not fully capture the user’s underlying intent.
7
7
8
-
To address this, voice-based sentiment classification analyzes audio input to determine the user’s emotional state, which is then incorporated into the LLM prompt to enable more context-aware responses. In this Learning Path, we will build a sentiment-aware voice assistant that runs entirely on-device. The application records audio, performs transcription—converting speech into written text—using Whisper, classifies sentiment directly from the voice signal, and combines the transcript and voice-based sentiment to guide responses from a local LLM running with llama.cpp.
8
+
To address this, voice-based sentiment classification analyzes audio input to determine the user’s emotional state, which is then incorporated into the LLM prompt to enable more context-aware responses. In this Learning Path, you will build a sentiment-aware voice assistant that runs entirely on-device. The application records audio, performs transcription—converting speech into written text—using Whisper, classifies sentiment directly from the voice signal, and combines the transcript and voice-based sentiment to guide responses from a local LLM running with llama.cpp.
Copy file name to clipboardExpand all lines: content/learning-paths/mobile-graphics-and-gaming/voice-sentiment-analysis-with-llm/2_set-up-your-environment.md
+75-39Lines changed: 75 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,43 +10,69 @@ Before building the voice assistant, create a project workspace and set up an is
10
10
11
11
These instructions support Ubuntu, macOS, and Windows, with Python 3.9 or later and a working microphone.
12
12
13
-
Check your Python version before continuing:
13
+
Install required system tools first. `ffmpeg` is required by Whisper for audio decoding.
Install `UV` first from PyPI using `pip`. `UV` is a fast Python package and environment manager that we will use throughout this Learning Path to create the project environment and install dependencies.
30
-
31
-
**Ubuntu or macOS**
32
-
33
-
```bash
45
+
Install `UV` first so the `uv` command is available in your terminal. `UV` is a fast Python package and environment manager that you will use throughout this Learning Path to create the project environment and install dependencies.
46
+
47
+
{{< tabpane code=true >}}
48
+
{{< tab header="Ubuntu/macOS" language="bash">}}
49
+
curl -LsSf https://astral.sh/uv/install.sh | sh
50
+
# Start a new shell, or source your shell rc file so `uv` is on PATH.
Keep this virtual environment activated while you complete the rest of the Learning Path.
52
78
@@ -63,6 +89,7 @@ numpy
63
89
librosa
64
90
scikit-learn
65
91
onnx
92
+
onnxscript
66
93
onnxruntime
67
94
```
68
95
@@ -78,41 +105,50 @@ This installs the libraries needed for the Gradio interface, Whisper transcripti
78
105
79
106
Next, clone the [llama.cpp GitHub repository](https://github.com/ggml-org/llama.cpp), build the local inference server, and start it. This server exposes an OpenAI-compatible API that the Python application will call later in the Learning Path.
80
107
81
-
**Ubuntu or macOS**
108
+
{{% notice Pre-built llama.cpp %}}
109
+
If you prefer not to build from source, you can use pre-built binaries from [llama.cpp releases](https://github.com/ggml-org/llama.cpp/releases). Download the package for your platform, extract it, and use the `llama-server` executable from that package in the run commands later in this section.
When the build completes, the `llama-server` executable should be available in the build output directory.
129
+
130
+
```
131
+
ls ./build/bin/llama-server
97
132
```
98
133
99
-
When the build completes, the `llama-server` executable should be available in the build output directory. This Learning Path uses a quantized [Gemma 3 1B instruction-tuned model](https://huggingface.co/google/gemma-3-1b-it) served locally through `llama.cpp`.
134
+
{{% notice Note %}}
135
+
The file verification commands in this Learning Path uses syntax for Ubuntu and macOS. If you're on Windows, adjust the commands to use PowerShell equivalents like `Test-Path .\build\bin\llama-server.exe` or `dir .\build\bin\`. The file location remains the same across all platforms.
136
+
{{% /notice %}}
100
137
101
-
The first time you run this command, `llama.cpp` will download the model from Hugging Face. This can take several minutes depending on your network connection.
138
+
This Learning Path uses a quantized [Gemma 3 1B instruction-tuned model](https://huggingface.co/google/gemma-3-1b-it) served locally through `llama.cpp`.
102
139
103
-
**Ubuntu or macOS**
140
+
The first time you run this command, `llama.cpp` will download the model from Hugging Face. This can take several minutes depending on your network connection.
104
141
105
142
Run the following command from the `llama.cpp` directory:
Leave this terminal running while you test the application in later steps. The server listens on a local OpenAI-compatible endpoint that your app will call to generate responses.
Copy file name to clipboardExpand all lines: content/learning-paths/mobile-graphics-and-gaming/voice-sentiment-analysis-with-llm/3_build-baseline-voice-to-llm-pipeline.md
+8-1Lines changed: 8 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,14 @@ Gradio is a Python library for building simple browser-based interfaces. Here, y
24
24
25
25
This is a good first step because it lets you confirm that microphone capture works before you add transcription and model inference.
26
26
27
-
Create a file called `app.py`:
27
+
Using a new terminal, navigate to the project directory you created in the last step and make sure the virtual environment is active:
28
+
29
+
```
30
+
cd $HOME/voice-sentiment-assistant
31
+
source .venv/bin/activate
32
+
```
33
+
34
+
Create a file called `app.py` with the following contents:
Copy file name to clipboardExpand all lines: content/learning-paths/mobile-graphics-and-gaming/voice-sentiment-analysis-with-llm/4_train-voice-sentiment-model.md
+35-16Lines changed: 35 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,12 +11,22 @@ Before you begin, make sure you have completed the environment setup from the pr
11
11
12
12
This example uses a simple per-sample training loop for clarity. On CPU, training can take 10 to 30 minutes or more depending on your machine. Apple Silicon `mps` and CUDA-enabled GPUs are often faster.
13
13
14
+
All code snippets in Steps 2.1 to 2.5 are meant to be added to a single Python file: `train_sentiment_model.py` in your project root (`$HOME/voice-sentiment-assistant`).
15
+
16
+
Make sure you are located in the project directory:
17
+
18
+
```bash
19
+
cd$HOME/voice-sentiment-assistant
20
+
```
21
+
14
22
### Step 2.1 - Prepare the dataset
15
23
16
-
This step downloads the RAVDESS speech dataset, extracts it locally, and builds a dataframe that maps each audio file to a sentiment label. RAVDESS encodes emotion labels in the filename format, which we use to select the classes for this demo.
24
+
This step downloads the RAVDESS speech dataset, extracts it locally, and builds a dataframe that maps each audio file to a sentiment label. RAVDESS encodes emotion labels in the filename format, which you use to select the classes for this demo.
17
25
18
26
In practice, this turns a folder of audio files into a structured table that your training code can work with. Each row is one example, and each column stores details such as the file path, actor ID, and sentiment label.
19
27
28
+
Add this to a new file called `train_sentiment_model.py`:
29
+
20
30
```python
21
31
import os
22
32
import urllib.request
@@ -25,25 +35,24 @@ import pandas as pd
25
35
26
36
# Download and unpack the dataset into the local project directory.
@@ -73,6 +82,8 @@ Next, split the dataset by speaker so the same speaker does not appear in both t
73
82
This matters because a speech model can accidentally learn speaker-specific traits instead of sentiment if the same speakers appear in both splits. Separating speakers gives you a better measure of how well the model generalizes.
74
83
The training split is the data the model learns from. The validation split is held back and used only to test how well the model performs on unseen examples.
75
84
85
+
Append this to the same `train_sentiment_model.py` file:
This step loads the HuBERT feature extractor and initializes a sequence classification model. The pre-trained HuBERT base model provides a strong starting point for transfer learning on speech sentiment classification.
98
109
The feature extractor prepares raw audio in the format expected by the model. The HuBERT model then uses those inputs to predict one of the target sentiment classes.
99
110
111
+
Append this below the previous code in `train_sentiment_model.py`:
112
+
100
113
```python
101
114
from transformers import AutoFeatureExtractor, HubertForSequenceClassification
102
115
@@ -122,6 +135,8 @@ During training, the model compares its predictions with the correct labels and
122
135
123
136
For better performance in a production training pipeline, you would typically use batching and a PyTorch `DataLoader`.
124
137
138
+
Append this below the previous code in `train_sentiment_model.py`:
139
+
125
140
```python
126
141
import librosa
127
142
import torch
@@ -200,23 +215,27 @@ At the end of this step, you will have a trained model and a simple validation a
200
215
Save both the trained model and the feature extractor so they can be reused later for inference and ONNX export. Keeping them together helps avoid preprocessing mismatches.
201
216
This is an important final step because the model weights alone are not enough. You also need the matching feature extractor so inference uses the same preprocessing as training.
202
217
203
-
```python
204
-
import os
218
+
Append this at the end of `train_sentiment_model.py`:
205
219
220
+
```python
206
221
SAVE_DIR="models/hubert_vsa_ravdess"
207
222
os.makedirs(SAVE_DIR, exist_ok=True)
208
223
209
224
# Save both the trained model and the preprocessing configuration.
210
225
model.save_pretrained(SAVE_DIR)
211
226
feature_extractor.save_pretrained(SAVE_DIR)
227
+
228
+
print("Training complete. Model saved successfully.")
229
+
print(f"Saved model to {SAVE_DIR}")
212
230
```
213
231
214
232
After this step, the trained model should be saved in `models/hubert_vsa_ravdess`.
215
233
216
234
## Full training script
217
235
218
-
The following script combines the previous steps into one runnable example. Save it as `train_sentiment_model.py` and run it from the root of your project.
219
-
Use this script if you want to run the full workflow without copying each block separately.
236
+
The following script combines the previous steps into one runnable example.
237
+
If you already built `train_sentiment_model.py` using Steps 2.1 to 2.5, you can skip this section.
238
+
If not, copy this full script into `train_sentiment_model.py` and run it from the root of your project.
0 commit comments