# bodhi132/voiceai architecture

> Architecture diagram of the GitHub repository bodhi132/voiceai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bodhi132/voiceai
- Repository: https://github.com/bodhi132/voiceai

## Overview

1. **Repository shape**
   - This is a two-tier application: a **Next.js client** in `client/` and a **FastAPI Python backend** in `backend/`.
   - The backend owns all AI/audio processing; the frontend is mostly a presentation and upload/results shell.

2. **Frontend boundary**
   - `client/app/page.tsx` is the entry upload/recording surface.
   - `client/app/results/page.tsx` renders the scored pronunciation report.
   - `client/app/loading/page.tsx` and `components/LoadingScreen.tsx` indicate a dedicated waiting state while the backend processes audio.
   - The UI is built with the Next.js App Router and appears designed as a mostly client-side workflow around a single assessment request.

3. **Backend entrypoint and orchestration**
   - `backend/main.py` is the API entrypoint and likely coordinates the full assessment pipeline.
   - The backend is structured as a set of services rather than a large monolith, which is important for diagramming the internal flow.

4. **Audio processing pipeline**
   - The backend depends on **FFmpeg** for audio normalization/conversion before analysis.
   - This is the first preprocessing boundary: user audio is cleaned into a consistent format before transcription or phoneme extraction.

5. **Transcription and word timing**
   - `backend/services/transcription.py` uses the **Groq Whisper API** to transcribe speech and get word-level timestamps.
   - This stage also verifies that the audio is English, which acts as an early gate before deeper analysis.

6. **Expected pronunciation generation**
   - `backend/services/expected_phonemes.py` derives the canonical pronunciation for each word.
   - The readme indicates a dual strategy: **CMUdict** for known words and **g2p_en** as a fallback for proper nouns or out-of-vocabulary words.
   - `backend/vocab.json` likely supports phoneme vocabulary normalization/mapping.

7. **Actual pronunciation extraction**
   - `backend/services/phonemes.py` extracts phonetic output from the audio itself using a **Wav2Vec2-based model**.
   - `backend/export_onnx.py` is a key deployment artifact: it converts the larger PyTorch model into a compressed **INT8 ONNX** version so inference can run on CPU-constrained infrastructure.

8. **Scoring logic**
   - `backend/services/scoring.py` compares expected vs. actual phonemes using **Levenshtein distance** at the word level.
   - The output is a per-word pronunciation score, so the unit of analysis is not just the utterance, but each spoken token.

9. **Feedback generation**
   - `backend/services/feedback.py` turns scores and phonetic error patterns into coaching feedback.
   - The readme says this is generated with **Groq Llama 3**, making the feedback stage separate from both transcription and scoring.

10. **End-to-end data flow**
   - The main pipeline is: **upload audio → normalize audio → transcribe + timestamp words → derive expected phonemes → extract actual phonemes → score words → generate coaching feedback → return structured results**.
   - The frontend then consumes the final response and displays it in the results dashboard.

11. **External service boundaries**
   - The repo depends on two Groq-powered cloud functions:
     - **Whisper-large-v3** for transcription/timestamps
     - **Llama 3** for personalized coaching text
   - These are the only major external AI services; phoneme inference is intentionally local/on-CPU via ONNX.

12. **Deployment and build pipeline**
   - `.github/workflows/main_voiceai-backend.yml` is a significant architectural component because it handles the heavy model preparation step in CI.
   - The design is optimized for constrained backend hosting: model export happens in GitHub Actions, while the deployed backend only serves the lightweight ONNX runtime path.

13. **Operational split**
   - The frontend and backend are independently deployable, with the readme describing the frontend on **Render** and the backend on **Azure App Service**.
   - Architecturally, this means the client talks to the backend over HTTP and does not embed ML logic locally.

14. **Diagramming hint**
   - A good architecture diagram should show three clear zones: **Next.js UI**, **FastAPI AI pipeline**, and **Groq cloud APIs**, with the backend as the control plane that fans out into transcription, phoneme analysis, scoring, and feedback generation.

Main entry points: [`backend/main.py`](https://github.com/bodhi132/voiceai/blob/main/backend/main.py) (API entry), [`backend/services/expected_phonemes.py`](https://github.com/bodhi132/voiceai/blob/main/backend/services/expected_phonemes.py) (Expected phonemes), [`backend/services/feedback.py`](https://github.com/bodhi132/voiceai/blob/main/backend/services/feedback.py) (Feedback), [`backend/services/phonemes.py`](https://github.com/bodhi132/voiceai/blob/main/backend/services/phonemes.py) (Actual phonemes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Next.js client"]
  node_client_page["Upload<br/>next page<br/>[page.tsx]"]
  node_client_loading_page["Loading page<br/>next page<br/>[page.tsx]"]
  node_client_loading_component["Loading UI<br/>ui component<br/>[LoadingScreen.tsx]"]
  node_client_results_page["Results<br/>next page<br/>[page.tsx]"]
end

subgraph group_backend["FastAPI backend"]
  node_backend_main["API entry<br/>fastapi app<br/>[main.py]"]
  node_backend_transcription["Transcription<br/>[transcription.py]"]
  node_backend_expected["Expected phonemes"]
  node_backend_vocab["Vocab map<br/>lexicon data<br/>[vocab.json]"]
  node_backend_phonemes["Actual phonemes<br/>ml inference<br/>[phonemes.py]"]
  node_backend_scoring["Scoring<br/>[scoring.py]"]
  node_backend_feedback["Feedback<br/>[feedback.py]"]
  node_ffmpeg{{"FFmpeg<br/>audio tool"}}
end

subgraph group_cloud["Groq cloud"]
  node_groq_whisper(("Whisper API<br/>external api"))
  node_groq_llama(("Llama 3 API<br/>external api"))
end

subgraph group_ci["Build pipeline"]
  node_onnx_export["ONNX export<br/>build script<br/>[export_onnx.py]"]
  node_github_actions["CI workflow<br/>github actions"]
end

node_client_page -->|"submit audio"| node_backend_main
node_client_page -->|"wait state"| node_client_loading_page
node_client_loading_page -->|"renders"| node_client_loading_component
node_backend_main -->|"normalize"| node_ffmpeg
node_backend_main -->|"transcribe"| node_backend_transcription
node_backend_main -->|"derive target"| node_backend_expected
node_backend_main -->|"infer phonemes"| node_backend_phonemes
node_backend_main -->|"score words"| node_backend_scoring
node_backend_main -->|"coach"| node_backend_feedback
node_backend_transcription -->|"Whisper large-v3"| node_groq_whisper
node_backend_expected -->|"lookup"| node_backend_vocab
node_backend_expected -->|"compare basis"| node_backend_phonemes
node_backend_scoring -->|"score signals"| node_backend_feedback
node_backend_feedback -->|"Llama 3"| node_groq_llama
node_github_actions -->|"build artifact"| node_onnx_export
node_onnx_export -->|"runtime model"| node_backend_phonemes
node_client_results_page -->|"fetch results"| node_backend_main

click node_client_page "https://github.com/bodhi132/voiceai/blob/main/client/app/page.tsx"
click node_client_loading_page "https://github.com/bodhi132/voiceai/blob/main/client/app/loading/page.tsx"
click node_client_loading_component "https://github.com/bodhi132/voiceai/blob/main/client/components/LoadingScreen.tsx"
click node_client_results_page "https://github.com/bodhi132/voiceai/blob/main/client/app/results/page.tsx"
click node_backend_main "https://github.com/bodhi132/voiceai/blob/main/backend/main.py"
click node_backend_transcription "https://github.com/bodhi132/voiceai/blob/main/backend/services/transcription.py"
click node_backend_expected "https://github.com/bodhi132/voiceai/blob/main/backend/services/expected_phonemes.py"
click node_backend_vocab "https://github.com/bodhi132/voiceai/blob/main/backend/vocab.json"
click node_backend_phonemes "https://github.com/bodhi132/voiceai/blob/main/backend/services/phonemes.py"
click node_backend_scoring "https://github.com/bodhi132/voiceai/blob/main/backend/services/scoring.py"
click node_backend_feedback "https://github.com/bodhi132/voiceai/blob/main/backend/services/feedback.py"
click node_onnx_export "https://github.com/bodhi132/voiceai/blob/main/backend/export_onnx.py"
click node_github_actions "https://github.com/bodhi132/voiceai/blob/main/.github/workflows/main_voiceai-backend.yml"

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_client_page,node_client_loading_page,node_client_loading_component,node_client_results_page toneBlue
class node_backend_main,node_backend_transcription,node_backend_expected,node_backend_vocab,node_backend_phonemes,node_backend_scoring,node_backend_feedback,node_ffmpeg toneAmber
class node_groq_whisper,node_groq_llama toneMint
class node_onnx_export,node_github_actions toneRose
```

## Components

### Next.js client

Upload, waiting, and results UI

- [Upload](https://github.com/bodhi132/voiceai/blob/main/client/app/page.tsx): next page · Entry surface for recording or uploading audio · `client/app/page.tsx`
- [Loading page](https://github.com/bodhi132/voiceai/blob/main/client/app/loading/page.tsx): next page · Dedicated waiting route during backend processing · `client/app/loading/page.tsx`
- [Loading UI](https://github.com/bodhi132/voiceai/blob/main/client/components/LoadingScreen.tsx): ui component · Shared loading state shown while scoring runs · `client/components/LoadingScreen.tsx`
- [Results](https://github.com/bodhi132/voiceai/blob/main/client/app/results/page.tsx): next page · Displays pronunciation scores and coaching output · `client/app/results/page.tsx`

### FastAPI backend

Assessment API and AI/audio pipeline

- [API entry](https://github.com/bodhi132/voiceai/blob/main/backend/main.py): fastapi app · HTTP entrypoint and pipeline coordinator · `backend/main.py`
- [Transcription](https://github.com/bodhi132/voiceai/blob/main/backend/services/transcription.py): service · Normalizes speech into words and timestamps · `backend/services/transcription.py`
- [Expected phonemes](https://github.com/bodhi132/voiceai/blob/main/backend/services/expected_phonemes.py): service · Builds canonical pronunciations from vocabulary and fallbacks · `backend/services/expected_phonemes.py`
- [Vocab map](https://github.com/bodhi132/voiceai/blob/main/backend/vocab.json): lexicon data · Pronunciation vocabulary and normalization data · `backend/vocab.json`
- [Actual phonemes](https://github.com/bodhi132/voiceai/blob/main/backend/services/phonemes.py): ml inference · Extracts phonetic output from the audio signal · `backend/services/phonemes.py`
- [Scoring](https://github.com/bodhi132/voiceai/blob/main/backend/services/scoring.py): service · Compares expected and actual phonemes per word · `backend/services/scoring.py`
- [Feedback](https://github.com/bodhi132/voiceai/blob/main/backend/services/feedback.py): service · Turns score patterns into coaching advice · `backend/services/feedback.py`
- **FFmpeg**: audio tool · Audio normalization and conversion boundary

### Groq cloud

External transcription and coaching APIs

- **Whisper API**: external api · Cloud transcription and word timing
- **Llama 3 API**: external api · Cloud-generated personalized coaching text

### Build pipeline

Model export and deployment prep

- [ONNX export](https://github.com/bodhi132/voiceai/blob/main/backend/export_onnx.py): build script · Converts the model to compressed CPU-friendly ONNX · `backend/export_onnx.py`
- [CI workflow](https://github.com/bodhi132/voiceai/blob/main/.github/workflows/main_voiceai-backend.yml): github actions · Prepares and exports backend model artifacts · `.github/workflows/main_voiceai-backend.yml`

## Connections

- Upload → API entry: submit audio · Client sends the assessment request to the API
- Upload → Loading page: wait state · UI transitions into the dedicated processing screen
- Loading page → Loading UI: renders · Loading route uses the shared waiting UI
- API entry → FFmpeg: normalize · Audio is converted to a consistent analysis format
- API entry → Transcription: transcribe · Pipeline starts with speech-to-text and timing
- API entry → Expected phonemes: derive target · Builds expected pronunciations for each word
- API entry → Actual phonemes: infer phonemes · Runs local phoneme extraction on the cleaned audio
- API entry → Scoring: score words · Compares expected and actual pronunciations
- API entry → Feedback: coach · Generates feedback from score patterns
- Transcription → Whisper API: Whisper large-v3 · Uses Groq-hosted transcription service
- Expected phonemes → Vocab map: lookup · Uses pronunciation vocabulary and normalization data
- Expected phonemes → Actual phonemes: compare basis · Expected pronunciations feed phoneme comparison
- Scoring → Feedback: score signals · Per-word errors inform coaching generation
- Feedback → Llama 3 API: Llama 3 · Uses Groq-hosted language model for coaching text
- CI workflow → ONNX export: build artifact · CI prepares the CPU-friendly model export
- ONNX export → Actual phonemes: runtime model · Deployment uses the exported ONNX path
- Results → API entry: fetch results · Results page reads the structured assessment response
