# jnr-10/speechemotionassistant-audioml architecture

> Architecture diagram of the GitHub repository jnr-10/speechemotionassistant-audioml, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jnr-10/speechemotionassistant-audioml
- Repository: https://github.com/jnr-10/speechemotionassistant-audioml

## Overview

1. **Overall shape**
   - This repository has two main planes: an **offline ML training/evaluation pipeline** and an **online Flask-based inference/assistant application**.
   - The offline side produces serialized TensorFlow/Keras models and preprocessing artifacts stored under `models/`.
   - The online side serves a browser UI, accepts audio, runs emotion inference, and optionally calls Gemini for natural-language responses.

2. **User-facing application layer**
   - `app.py` is the central web entrypoint.
   - `templates/index.html` provides the main browser interface: audio capture/upload, live emotion display, language selection, and session/report interactions.
   - The README indicates WebSocket-style real-time updates and live visualization, so the frontend is not just a static form; it supports streaming-style interaction with the backend.

3. **Audio ingestion boundary**
   - Audio enters the system in two ways:
     - **Microphone/live recording** through the voice assistant flow.
     - **Uploaded audio files** via the analysis API.
   - `voice_assistant.py` likely handles microphone/audio conversion and related orchestration.
   - This is the first major boundary: raw audio is normalized before any ML processing.

4. **Signal preprocessing and feature extraction**
   - `data_loader.py` is responsible for loading audio and transforming it into **mel spectrogram features**.
   - The repo’s documented pipeline is: raw audio → resampling to 22,050 Hz → mel spectrogram with 128 bands and ~130 frames → model input.
   - The preprocessing stage is a critical architectural boundary because every model variant expects the same feature representation, with saved scalers/encoders used for consistent inference.

5. **Emotion inference subsystem**
   - `model.py` defines the CNN-LSTM architecture used for speech emotion recognition.
   - Pre-trained variants are already packaged in `models/`:
     - CNN
     - LSTM
     - CNN-LSTM hybrid
   - Associated artifacts include `.keras` weights plus `label_encoder.pkl`, `scaler.pkl`, and model-specific config/encoder/scaler files. These artifacts form the inference bundle.

6. **Prediction outputs**
   - The model outputs one of the supported emotion classes: neutral, calm, happy, sad, angry, fearful, disgust, surprised.
   - The backend uses these predictions to drive:
     - per-chunk emotion analysis
     - confidence display
     - session emotion trends
     - summary statistics for reports

7. **LLM / assistant integration**
   - `llm_service.py` is the boundary to **Google Gemini API**.
   - Emotion results are passed into the LLM layer to generate context-aware, empathetic responses.
   - This is a separate subsystem from emotion recognition: the ML model classifies speech, while Gemini turns that classification into conversational assistance.

8. **Reporting subsystem**
   - `report_generator.py` produces PDF session reports.
   - Based on the README, reports include charts, emotion breakdowns, and session insights.
   - This subsystem consumes accumulated session data rather than single inference results, so it sits downstream of the analysis pipeline.

9. **API surface**
   - The backend exposes endpoints for:
     - audio analysis
     - chat/assistant interaction
     - system status
     - supported languages
     - PDF report generation
   - These endpoints are the integration layer between the frontend, the emotion model, the LLM service, and the report generator.

10. **Training and evaluation pipeline**
    - `train.py` is the local training entrypoint.
    - The notebooks under `notebooks/` and the top-level `.ipynb` files are the research/training environment for model development and benchmarking.
    - This offline workflow uses combined emotion datasets such as RAVDESS and CREMA-D, plus augmentation, to produce the artifacts stored in `models/`.

11. **Configuration and environment**
    - `config.py` centralizes audio/model parameters such as sample rate, clip duration, mel band count, epochs, and batch size.
    - `.env.example` shows that runtime secrets, especially the Gemini API key, are injected through environment variables rather than hardcoded.
    - `requirements.txt` defines the Python stack, with TensorFlow and Flask as the main runtime dependencies.

12. **Data/artifact flow for architecture diagrams**
    - The key flow to draw is:
      - browser or uploaded audio → Flask endpoint / voice assistant layer → preprocessing (`data_loader.py`) → emotion model (`model.py` + saved artifacts) → emotion result
      - then optionally → `llm_service.py` for Gemini response
      - and/or → `report_generator.py` for PDF output
    - Training is a separate flow:
      - dataset files/notebooks → feature extraction/preprocessing → `train.py` / notebook training → saved model artifacts in `models/`

13. **Important architectural boundary**
    - The repository mixes **runtime inference** and **research/training assets** in one codebase, but they should be drawn as separate subsystems.
    - The runtime application depends on prebuilt artifacts; it does not require retraining to function.
    - The notebooks and plots are supporting assets for experimentation, evaluation, and documentation rather than part of the production request path.

Main entry points: [`app.py`](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/app.py) (App), [`train.py`](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/train.py) (Training), [`model.py`](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/model.py) (SER Model), [`config.py`](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/config.py) (Config)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime App"]
  node_app["App<br/>Flask entrypoint<br/>[app.py]"]
  node_ui["Web UI<br/>Frontend template<br/>[index.html]"]
  node_voice["Voice Assistant<br/>Audio ingress<br/>[voice_assistant.py]"]
  node_llm["Gemini Layer<br/>LLM service<br/>[llm_service.py]"]
  node_report["Reports<br/>PDF generator"]
  node_config["Config<br/>[config.py]"]
  node_stack["Python Stack<br/>Dependencies<br/>[requirements.txt]"]
end

subgraph group_ml["ML Pipeline"]
  node_loader["Feature Loader<br/>Mel preprocessing<br/>[data_loader.py]"]
  node_model["SER Model<br/>CNN-LSTM model<br/>[model.py]"]
  node_models[("Model Bundles<br/>Saved artifacts")]
end

subgraph group_research["Research & Artifacts"]
  node_train["Training<br/>Offline trainer<br/>[train.py]"]
  node_nb1["Colab Train<br/>Notebook"]
  node_nb2["Optimized Models<br/>Notebook"]
  node_nb3["Multi-Dataset<br/>Notebook"]
  node_nb4["Training Colab<br/>Notebook"]
  node_dataset["Feature Data<br/>Dataset metadata"]
end

node_ui -->|"user input"| node_app
node_voice -->|"audio upload"| node_app
node_app -->|"preprocess"| node_loader
node_loader -->|"features"| node_model
node_model -->|"weights"| node_models
node_model -->|"predictions"| node_app
node_app -->|"context"| node_llm
node_llm -->|"reply"| node_app
node_app -->|"session data"| node_report
node_report -->|"pdf"| node_app
node_config -.->|"audio params"| node_voice
node_config -.->|"feature params"| node_loader
node_config -.->|"train params"| node_train
node_train -->|"data"| node_dataset
node_train -->|"builds"| node_model
node_nb1 -.->|"experiments"| node_train
node_nb2 -.->|"benchmarks"| node_train
node_nb3 -.->|"datasets"| node_train
node_nb4 -.->|"colab flow"| node_train
node_stack -.->|"runtime deps"| node_app

click node_app "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/app.py"
click node_ui "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/templates/index.html"
click node_voice "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/voice_assistant.py"
click node_llm "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/llm_service.py"
click node_report "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/report_generator.py"
click node_config "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/config.py"
click node_loader "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/data_loader.py"
click node_model "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/model.py"
click node_models "https://github.com/jnr-10/speechemotionassistant-audioml/tree/main/models"
click node_train "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/train.py"
click node_nb1 "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/SER_Training_Colab.ipynb"
click node_nb2 "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/SER_Optimized_Models.ipynb"
click node_nb3 "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/notebooks/SER_Multi_Dataset_Training.ipynb"
click node_nb4 "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/notebooks/SER_Training_Colab.ipynb"
click node_dataset "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/features/ravdess_mel_metadata.csv"
click node_stack "https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_ui,node_voice,node_llm,node_report,node_config,node_stack toneBlue
class node_loader,node_model,node_models toneAmber
class node_train,node_nb1,node_nb2,node_nb3,node_nb4,node_dataset toneMint
```

## Components

### Runtime App

Online Flask app, audio ingestion, inference, assistant responses, and reports.

- [App](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/app.py): Flask entrypoint · Central web entrypoint for the browser UI and API surface. · `app.py`
- [Web UI](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/templates/index.html): Frontend template · Browser interface for audio capture, emotion display, language selection, and reports. · `templates/index.html`
- [Voice Assistant](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/voice_assistant.py): Audio ingress · Handles microphone/live recording and audio normalization for the online flow. · `voice_assistant.py`
- [Gemini Layer](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/llm_service.py): LLM service · Wraps the Gemini API to turn emotion results into conversational responses. · `llm_service.py`
- [Reports](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/report_generator.py): PDF generator · Produces session reports with charts, emotion breakdowns, and insights. · `report_generator.py`
- [Config](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/config.py): Runtime config · Centralizes audio, model, and training parameters. · `config.py`
- [Python Stack](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/requirements.txt): Dependencies · Declared runtime dependencies for Flask, TensorFlow, and supporting libraries. · `requirements.txt`

### ML Pipeline

Preprocessing and emotion classification used by runtime inference and offline training.

- [Feature Loader](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/data_loader.py): Mel preprocessing · Loads audio and converts it into mel-spectrogram features for inference and training. · `data_loader.py`
- [SER Model](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/model.py): CNN-LSTM model · Defines the speech emotion recognition architecture. · `model.py`
- [Model Bundles](https://github.com/jnr-10/speechemotionassistant-audioml/tree/main/models): Saved artifacts · Serialized Keras models plus encoders, scalers, and configs used at runtime. · `models`

### Research & Artifacts

Training notebooks, datasets, and saved model bundles.

- [Training](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/train.py): Offline trainer · Local training entrypoint for building and evaluating emotion models. · `train.py`
- [Colab Train](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/SER_Training_Colab.ipynb): Notebook · Research notebook for model training in Colab. · `SER_Training_Colab.ipynb`
- [Optimized Models](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/SER_Optimized_Models.ipynb): Notebook · Notebook for model optimization and benchmarking. · `SER_Optimized_Models.ipynb`
- [Multi-Dataset](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/notebooks/SER_Multi_Dataset_Training.ipynb): Notebook · Notebook covering multi-dataset training experiments. · `notebooks/SER_Multi_Dataset_Training.ipynb`
- [Training Colab](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/notebooks/SER_Training_Colab.ipynb): Notebook · Notebook for training workflows in Colab. · `notebooks/SER_Training_Colab.ipynb`
- [Feature Data](https://github.com/jnr-10/speechemotionassistant-audioml/blob/main/features/ravdess_mel_metadata.csv): Dataset metadata · Precomputed dataset metadata and derived feature assets. · `features/ravdess_mel_metadata.csv`

## Connections

- Web UI → App: user input · Browser interactions are handled by the Flask app.
- Voice Assistant → App: audio upload · Live audio is routed into the application backend.
- App → Feature Loader: preprocess · Incoming audio is normalized into model features.
- Feature Loader → SER Model: features · Mel features are fed into the emotion model.
- SER Model → Model Bundles: weights · The runtime uses packaged model artifacts and encoders.
- SER Model → App: predictions · Emotion labels and confidence scores return to the web app.
- App → Gemini Layer: context · Predicted emotion is sent to Gemini for assistant responses.
- Gemini Layer → App: reply · Generated text is returned for display in the assistant flow.
- App → Reports: session data · Accumulated analysis results are converted into PDF reports.
- Reports → App: pdf · Generated reports are exposed through the application.
- Config → Voice Assistant: audio params · Recording and normalization use shared audio settings.
- Config → Feature Loader: feature params · Feature extraction uses shared sample-rate and mel settings.
- Config → Training: train params · Training uses shared epochs, batch size, and clip settings.
- Training → Feature Data: data · Training and evaluation consume dataset metadata and derived features.
- Training → SER Model: builds · Training produces the SER architecture and learned parameters.
- Colab Train → Training: experiments · Notebook workflows mirror or extend the training entrypoint.
- Optimized Models → Training: benchmarks · Optimization experiments inform the packaged model variants.
- Multi-Dataset → Training: datasets · Multi-dataset experiments feed model development.
- Training Colab → Training: colab flow · Notebook-based training supports the offline pipeline.
- Python Stack → App: runtime deps · The Flask application depends on the declared Python stack.
