speechkid-api / README.md
yoavzamir's picture
Update README with current algorithm, tech stack, and architecture
020dc73
|
Raw
History Blame Contribute Delete
4.9 kB
metadata
title: Speechkid API
emoji: πŸŽ™οΈ
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false

SpeechKid API - Hebrew 'Sh' Sound Pronunciation Trainer

AI-powered pronunciation trainer for children learning the Hebrew 'Sh' (Χ©) sound. Uses Wav2Vec2 forced alignment + acoustic physics to grade pronunciation as CORRECT or INCORRECT in real-time. No reference audio needed β€” pure AI inference.

Technology Stack

Layer Technology
AI Model Wav2Vec2 (imvladikon/wav2vec2-large-xlsr-53-hebrew) β€” CTC forced alignment
Audio Processing librosa, torchaudio, scipy β€” spectral analysis, bandpass filtering
Backend FastAPI + Uvicorn
Deployment Hugging Face Spaces (Docker SDK), PyTorch CPU-only
Clinical Engine OpenAI GPT-4o Structured Outputs β€” diagnostic report generation
Frontend Expo React Native app (separate repo)

How the Algorithm Works

Processing Pipeline

Recording β†’ Silence Trimming β†’ Wav2Vec2 Forced Alignment β†’ Χ© Segment Extraction
         β†’ Bandpass Filter (3-8kHz) β†’ Spectral Centroid β†’ Binary Diagnosis
  1. Silence Trimming β€” Removes leading silence (librosa.effects.trim, top_db=25) so the AI model sees speech immediately. Mobile recordings often have 1-2s of dead air.

  2. Wav2Vec2 Forced Alignment β€” CTC forced alignment (torchaudio.functional.forced_align) maps each Hebrew character to a time span + confidence score. The model identifies exactly where the Χ© sound starts.

  3. Segment Extraction β€” Extracts 120ms forward from the Wav2Vec2 onset of Χ©. Forward-only extraction avoids capturing pre-speech silence. Falls back to energy-peak scanning if alignment fails.

  4. Bandpass Filter β€” 4th-order Butterworth bandpass (3-8kHz) isolates the fricative frequency range.

  5. Spectral Centroid β€” Computes the frequency center of gravity on the bandpassed audio. This is device-invariant β€” doesn't change with recording volume or distance.

  6. Binary Diagnosis β€” Two gates, both must pass:

Diagnosis Gates

Gate Threshold What it catches
AI Score Wav2Vec2 confidence β‰₯ 0.70 Lisps, substitutions, wet CH, omission
Centroid 2700–5500 Hz Too low = wet/lateral distortion; too high = S substitution

Result: CORRECT (score 100) or INCORRECT (score 0).

Key Techniques

  • CTC Forced Alignment: Aligns Hebrew characters to audio frames using Connectionist Temporal Classification, providing per-character timestamps and confidence scores
  • Spectral Centroid Analysis: Frequency center of gravity β€” robust across devices, volumes, and recording distances (unlike energy-based measures)
  • Forward-Only Extraction: Always extract audio forward from the detected onset, never backward into silence
  • Leading Silence Trimming: Prevents alignment failure caused by long pre-speech silence on mobile recordings
  • Model Warmup: Pre-loads Wav2Vec2 at startup via FastAPI lifespan to eliminate cold-start latency (~10s warmup vs ~2min on first request)

API Endpoints

POST /evaluate

Score a pronunciation recording.

  • file: Audio file (WAV, M4A, AAC, WebM, etc.)
  • word: Target word key (shalom, shemesh, shir, shuk, geshem, shshshsh, shaon, shulchan)
  • Returns: { score, status, diagnosis, feedback, details, evidence }

POST /diagnose_profile

Generate a clinical diagnostic report via GPT-4o.

  • Body: Intake questionnaire (child age, error patterns, history)
  • Returns: Clinical hypothesis, intervention recommendation, exercises, app roadmap

Supported Words

Χ©ΧœΧ•Χ (shalom), שמש (shemesh), Χ©Χ™Χ¨ (shir), Χ©Χ•Χ§ (shuk), גשם (geshem), Χ©Χ©Χ©Χ© (shshshsh), Χ©Χ’Χ•ΧŸ (shaon), Χ©Χ•ΧœΧ—ΧŸ (shulchan)

Project Structure

speechkid-api/
β”œβ”€β”€ score_engine.py       # Main scoring pipeline β€” trimming, extraction, diagnosis
β”œβ”€β”€ phoneme_extractor.py  # Wav2Vec2 forced alignment & Χ© isolation
β”œβ”€β”€ main.py               # FastAPI backend (HF Spaces deployment)
β”œβ”€β”€ simulate_remote.py    # Terminal simulator β€” records mic, calls HF API
β”œβ”€β”€ simulate.py           # Local terminal simulator β€” scores without API
β”œβ”€β”€ calibrate.py          # Batch calibration tool
β”œβ”€β”€ Dockerfile            # HF Spaces Docker deployment
β”œβ”€β”€ requirements.txt      # Python dependencies
└── CLAUDE.md             # AI assistant context file

Quick Start

# Install
pip install -r requirements.txt

# Test via terminal (calls HF Spaces API)
python simulate_remote.py

# Score a single file locally
python score_engine.py "recording.wav" shalom --pretty

Deployment

Deployed on Hugging Face Spaces (Docker SDK) at: https://yoavzamir-speechkid-api.hf.space

License

MIT License