Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +417 -141
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,22 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import librosa
|
| 4 |
import numpy as np
|
| 5 |
-
from fastapi import FastAPI, File, UploadFile, Form
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
import
|
|
|
|
| 10 |
|
| 11 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# --- CONFIGURATION ---
|
| 14 |
ASR_MODEL_ID = "facebook/mms-1b-all"
|
| 15 |
-
|
| 16 |
|
| 17 |
-
print("π Loading AI Models...")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
try:
|
| 21 |
processor = AutoProcessor.from_pretrained(ASR_MODEL_ID)
|
| 22 |
model = Wav2Vec2ForCTC.from_pretrained(ASR_MODEL_ID)
|
|
@@ -25,128 +44,385 @@ except Exception as e:
|
|
| 25 |
print(f"β Failed to load ASR model: {e}")
|
| 26 |
raise e
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
try:
|
| 30 |
-
lid_processor = Wav2Vec2FeatureExtractor.from_pretrained(LID_MODEL_ID)
|
| 31 |
-
lid_model = Wav2Vec2ForSequenceClassification.from_pretrained(LID_MODEL_ID)
|
| 32 |
-
print(f"β
LID Model loaded: {LID_MODEL_ID}")
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print(f"β Failed to load LID model: {e}")
|
| 35 |
-
raise e
|
| 36 |
-
|
| 37 |
-
# Language Mapping (ISO codes)
|
| 38 |
LANG_MAP = {
|
| 39 |
'hindi': 'hin', 'tamil': 'tam', 'telugu': 'tel', 'marathi': 'mar',
|
| 40 |
'bengali': 'ben', 'gujarati': 'guj', 'kannada': 'kan', 'malayalam': 'mal',
|
| 41 |
-
'punjabi': 'pan', 'urdu': 'urd', 'english': 'eng'
|
| 42 |
}
|
| 43 |
|
| 44 |
-
@app.get("/")
|
| 45 |
-
def home():
|
| 46 |
-
return {"status": "running", "service": "SLAQ AI Engine", "models": [ASR_MODEL_ID, LID_MODEL_ID]}
|
| 47 |
-
|
| 48 |
-
@app.get("/health")
|
| 49 |
-
def health():
|
| 50 |
-
return {"status": "healthy"}
|
| 51 |
|
| 52 |
-
|
| 53 |
"""
|
| 54 |
-
|
|
|
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
logits = outputs.logits
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
|
|
|
|
|
|
|
|
|
| 104 |
return {
|
| 105 |
-
"
|
| 106 |
-
"
|
| 107 |
-
"
|
| 108 |
-
"
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
@app.post("/analyze")
|
| 113 |
async def analyze_audio(
|
| 114 |
audio: UploadFile = File(...),
|
| 115 |
transcript: Optional[str] = Form(""),
|
| 116 |
language: Optional[str] = Form("auto")
|
| 117 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
temp_filename = f"temp_{audio.filename}"
|
| 119 |
|
| 120 |
try:
|
|
|
|
| 121 |
with open(temp_filename, "wb") as buffer:
|
| 122 |
buffer.write(await audio.read())
|
| 123 |
|
| 124 |
-
# Load
|
| 125 |
-
speech, sr = librosa.load(temp_filename, sr=
|
| 126 |
|
| 127 |
-
# ---
|
| 128 |
-
|
| 129 |
|
| 130 |
-
if
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
except:
|
| 143 |
-
print(f"β οΈ Adapter not found for {detected_lang_code}, falling back to eng")
|
| 144 |
-
detected_lang_code = "eng"
|
| 145 |
-
processor.tokenizer.set_target_lang("eng")
|
| 146 |
-
model.load_adapter("eng")
|
| 147 |
-
|
| 148 |
-
# --- PHASE 1: TRANSCRIPTION ---
|
| 149 |
-
inputs = processor(speech, sampling_rate=16000, return_tensors="pt")
|
| 150 |
with torch.no_grad():
|
| 151 |
outputs = model(**inputs)
|
| 152 |
logits = outputs.logits
|
|
@@ -154,62 +430,62 @@ async def analyze_audio(
|
|
| 154 |
predicted_ids = torch.argmax(logits, dim=-1)
|
| 155 |
actual_transcript = processor.batch_decode(predicted_ids)[0]
|
| 156 |
confidence = float(torch.mean(torch.nn.functional.softmax(logits, dim=-1).max(dim=-1).values))
|
| 157 |
-
|
| 158 |
-
# --- PHASE 2: ACOUSTIC ANALYSIS ---
|
| 159 |
-
acoustic_stats = analyze_acoustics(speech, sr)
|
| 160 |
|
| 161 |
-
# ---
|
|
|
|
|
|
|
|
|
|
| 162 |
mismatch_pct = 0.0
|
| 163 |
-
mismatched_chars = []
|
| 164 |
if transcript:
|
|
|
|
| 165 |
dist = Levenshtein.distance(actual_transcript, transcript)
|
| 166 |
mismatch_pct = (dist / max(len(transcript), 1)) * 100
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
if final_score > 30: severity = "severe"
|
| 184 |
-
|
| 185 |
-
timestamps = []
|
| 186 |
-
for block_dur in acoustic_stats['blocks']:
|
| 187 |
-
timestamps.append({
|
| 188 |
-
"type": "block",
|
| 189 |
-
"start": 0,
|
| 190 |
-
"end": 0,
|
| 191 |
-
"duration": block_dur
|
| 192 |
-
})
|
| 193 |
-
|
| 194 |
return {
|
| 195 |
"actual_transcript": actual_transcript,
|
| 196 |
"target_transcript": transcript or "",
|
| 197 |
-
"
|
| 198 |
-
"mismatch_percentage": round(final_score, 2),
|
| 199 |
"stutter_timestamps": timestamps,
|
| 200 |
-
"total_stutter_duration":
|
| 201 |
-
"stutter_frequency":
|
| 202 |
-
"severity":
|
|
|
|
| 203 |
"confidence_score": round(confidence, 2),
|
| 204 |
-
"model_version": f"
|
| 205 |
-
"language_detected":
|
|
|
|
|
|
|
|
|
|
| 206 |
}
|
| 207 |
-
|
| 208 |
except Exception as e:
|
| 209 |
-
import traceback
|
| 210 |
traceback.print_exc()
|
| 211 |
-
|
| 212 |
|
| 213 |
finally:
|
| 214 |
if os.path.exists(temp_filename):
|
| 215 |
-
os.remove(temp_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Enhanced Stuttering Detection API
|
| 3 |
+
==================================
|
| 4 |
+
FastAPI backend with adaptive, research-based stuttering detection.
|
| 5 |
+
No hardcoded thresholds - uses statistical methods (Modified Z-Score/MAD).
|
| 6 |
+
|
| 7 |
+
Improvements over previous version:
|
| 8 |
+
- Adaptive thresholding using Modified Z-Score (Median Absolute Deviation)
|
| 9 |
+
- Multi-feature acoustic analysis (RMS, Pitch, MFCCs, Spectral features)
|
| 10 |
+
- Speaking-rate normalization for accurate severity assessment
|
| 11 |
+
- Detection of 5 dysfluency types with confidence scores
|
| 12 |
+
- Research-backed algorithms from recent stuttering detection literature
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
import os
|
| 16 |
import torch
|
| 17 |
import librosa
|
| 18 |
import numpy as np
|
| 19 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 20 |
+
from fastapi.responses import JSONResponse
|
| 21 |
+
from transformers import Wav2Vec2ForCTC, AutoProcessor
|
| 22 |
+
from typing import Optional, Dict, List
|
| 23 |
+
import traceback
|
| 24 |
+
from scipy import signal
|
| 25 |
|
| 26 |
+
app = FastAPI(
|
| 27 |
+
title="SLAQ Enhanced AI Engine",
|
| 28 |
+
description="Adaptive stuttering detection with multi-feature analysis",
|
| 29 |
+
version="2.0.0"
|
| 30 |
+
)
|
| 31 |
|
| 32 |
# --- CONFIGURATION ---
|
| 33 |
ASR_MODEL_ID = "facebook/mms-1b-all"
|
| 34 |
+
SAMPLE_RATE = 16000
|
| 35 |
|
| 36 |
+
print("π Loading Enhanced AI Models...")
|
| 37 |
|
| 38 |
+
# Load ASR Model for transcription
|
| 39 |
try:
|
| 40 |
processor = AutoProcessor.from_pretrained(ASR_MODEL_ID)
|
| 41 |
model = Wav2Vec2ForCTC.from_pretrained(ASR_MODEL_ID)
|
|
|
|
| 44 |
print(f"β Failed to load ASR model: {e}")
|
| 45 |
raise e
|
| 46 |
|
| 47 |
+
# Language Mapping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
LANG_MAP = {
|
| 49 |
'hindi': 'hin', 'tamil': 'tam', 'telugu': 'tel', 'marathi': 'mar',
|
| 50 |
'bengali': 'ben', 'gujarati': 'guj', 'kannada': 'kan', 'malayalam': 'mal',
|
| 51 |
+
'punjabi': 'pan', 'urdu': 'urd', 'english': 'eng', 'auto': 'auto'
|
| 52 |
}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
class EnhancedStutterDetector:
|
| 56 |
"""
|
| 57 |
+
Enhanced stuttering detection using adaptive statistical methods.
|
| 58 |
+
Based on recent research (2023-2025) in dysfluency detection.
|
| 59 |
"""
|
| 60 |
+
|
| 61 |
+
def __init__(self, sample_rate: int = 16000):
|
| 62 |
+
self.sr = sample_rate
|
| 63 |
+
self.mad_threshold = 3.5 # Modified Z-Score threshold
|
|
|
|
| 64 |
|
| 65 |
+
def analyze(self, y: np.ndarray, sr: int) -> Dict:
|
| 66 |
+
"""Main analysis pipeline."""
|
| 67 |
+
duration = len(y) / sr
|
| 68 |
+
|
| 69 |
+
# Extract multi-dimensional acoustic features
|
| 70 |
+
features = self._extract_features(y, sr)
|
| 71 |
+
|
| 72 |
+
# Detect speaking rate
|
| 73 |
+
speaking_rate = self._estimate_speaking_rate(y, sr)
|
| 74 |
+
|
| 75 |
+
# Detect dysfluency events
|
| 76 |
+
events = []
|
| 77 |
+
events.extend(self._detect_blocks(y, sr, features))
|
| 78 |
+
events.extend(self._detect_prolongations(y, sr, features))
|
| 79 |
+
events.extend(self._detect_sound_repetitions(y, sr, features))
|
| 80 |
+
events.extend(self._detect_word_repetitions(y, sr, features))
|
| 81 |
+
events.extend(self._detect_interjections(y, sr, features))
|
| 82 |
+
|
| 83 |
+
# Sort by time
|
| 84 |
+
events.sort(key=lambda x: x['start'])
|
| 85 |
+
|
| 86 |
+
# Calculate adaptive severity
|
| 87 |
+
severity_score = self._calculate_severity(events, duration, speaking_rate)
|
| 88 |
+
|
| 89 |
+
return {
|
| 90 |
+
'events': events,
|
| 91 |
+
'total_events': len(events),
|
| 92 |
+
'severity_score': severity_score,
|
| 93 |
+
'severity_label': self._get_severity_label(severity_score),
|
| 94 |
+
'speaking_rate': speaking_rate,
|
| 95 |
+
'duration': duration,
|
| 96 |
+
'event_counts': self._count_types(events)
|
| 97 |
+
}
|
| 98 |
|
| 99 |
+
def _extract_features(self, y: np.ndarray, sr: int) -> Dict:
|
| 100 |
+
"""Extract acoustic features."""
|
| 101 |
+
frame_length = int(0.025 * sr)
|
| 102 |
+
hop_length = int(0.010 * sr)
|
| 103 |
+
|
| 104 |
+
features = {}
|
| 105 |
+
|
| 106 |
+
# Energy (RMS)
|
| 107 |
+
rms = librosa.feature.rms(y=y, frame_length=frame_length, hop_length=hop_length)[0]
|
| 108 |
+
features['rms'] = rms
|
| 109 |
+
|
| 110 |
+
# Pitch (F0)
|
| 111 |
+
f0 = librosa.yin(y, fmin=librosa.note_to_hz('C2'), fmax=librosa.note_to_hz('C7'), sr=sr)
|
| 112 |
+
features['f0'] = f0
|
| 113 |
+
|
| 114 |
+
# Spectral features
|
| 115 |
+
features['spectral_centroid'] = librosa.feature.spectral_centroid(y=y, sr=sr, hop_length=hop_length)[0]
|
| 116 |
+
features['spectral_rolloff'] = librosa.feature.spectral_rolloff(y=y, sr=sr, hop_length=hop_length)[0]
|
| 117 |
+
features['zcr'] = librosa.feature.zero_crossing_rate(y, frame_length=frame_length, hop_length=hop_length)[0]
|
| 118 |
+
|
| 119 |
+
# MFCCs
|
| 120 |
+
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13, hop_length=hop_length)
|
| 121 |
+
features['mfcc'] = mfcc
|
| 122 |
+
|
| 123 |
+
# Time mapping
|
| 124 |
+
features['hop_length'] = hop_length
|
| 125 |
+
features['frame_times'] = librosa.frames_to_time(np.arange(len(rms)), sr=sr, hop_length=hop_length)
|
| 126 |
+
|
| 127 |
+
return features
|
| 128 |
|
| 129 |
+
def _estimate_speaking_rate(self, y: np.ndarray, sr: int) -> float:
|
| 130 |
+
"""Estimate speaking rate (syllables/sec)."""
|
| 131 |
+
onset_env = librosa.onset.onset_strength(y=y, sr=sr)
|
| 132 |
+
onsets = librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, units='time')
|
| 133 |
+
duration = len(y) / sr
|
| 134 |
+
return len(onsets) / duration if duration > 0 else 0.0
|
| 135 |
|
| 136 |
+
def _modified_z_score(self, data: np.ndarray) -> np.ndarray:
|
| 137 |
+
"""Calculate Modified Z-Score using MAD (more robust than standard Z-score)."""
|
| 138 |
+
median = np.median(data)
|
| 139 |
+
mad = np.median(np.abs(data - median))
|
| 140 |
+
|
| 141 |
+
if mad < 1e-10:
|
| 142 |
+
mad = np.mean(np.abs(data - median))
|
| 143 |
+
if mad < 1e-10:
|
| 144 |
+
return np.zeros_like(data)
|
| 145 |
+
|
| 146 |
+
return 0.6745 * (data - median) / mad
|
| 147 |
|
| 148 |
+
def _detect_blocks(self, y: np.ndarray, sr: int, features: Dict) -> List[Dict]:
|
| 149 |
+
"""Detect blocks (abnormal silent pauses)."""
|
| 150 |
+
rms = features['rms']
|
| 151 |
+
frame_times = features['frame_times']
|
| 152 |
+
|
| 153 |
+
# Adaptive silence threshold using Modified Z-Score
|
| 154 |
+
rms_z = self._modified_z_score(rms)
|
| 155 |
+
is_silent = rms_z < -self.mad_threshold
|
| 156 |
+
|
| 157 |
+
blocks = []
|
| 158 |
+
in_block = False
|
| 159 |
+
block_start = 0
|
| 160 |
+
|
| 161 |
+
for i, silent in enumerate(is_silent):
|
| 162 |
+
if silent and not in_block:
|
| 163 |
+
block_start = frame_times[i]
|
| 164 |
+
in_block = True
|
| 165 |
+
elif not silent and in_block:
|
| 166 |
+
block_end = frame_times[i]
|
| 167 |
+
duration = block_end - block_start
|
| 168 |
+
|
| 169 |
+
if 0.2 < duration < 2.0:
|
| 170 |
+
blocks.append({
|
| 171 |
+
'type': 'block',
|
| 172 |
+
'start': float(block_start),
|
| 173 |
+
'end': float(block_end),
|
| 174 |
+
'duration': float(duration),
|
| 175 |
+
'confidence': float(np.mean(np.abs(rms_z[max(0, i-10):i])))
|
| 176 |
+
})
|
| 177 |
+
in_block = False
|
| 178 |
+
|
| 179 |
+
return blocks
|
| 180 |
+
|
| 181 |
+
def _detect_prolongations(self, y: np.ndarray, sr: int, features: Dict) -> List[Dict]:
|
| 182 |
+
"""Detect prolongations (stable sound segments)."""
|
| 183 |
+
rms = features['rms']
|
| 184 |
+
f0 = features['f0']
|
| 185 |
+
frame_times = features['frame_times']
|
| 186 |
+
|
| 187 |
+
prolongations = []
|
| 188 |
+
window = 20
|
| 189 |
+
|
| 190 |
+
for i in range(window, len(rms) - window):
|
| 191 |
+
win_rms = rms[i-window:i+window]
|
| 192 |
+
win_f0 = f0[i-window:i+window]
|
| 193 |
+
|
| 194 |
+
rms_cv = np.std(win_rms) / (np.mean(win_rms) + 1e-10)
|
| 195 |
+
f0_cv = np.std(win_f0) / (np.mean(win_f0) + 1e-10)
|
| 196 |
|
| 197 |
+
if rms_cv < 0.1 and f0_cv < 0.15 and np.mean(win_rms) > np.median(rms) * 0.3:
|
| 198 |
+
if prolongations and frame_times[i] - prolongations[-1]['end'] < 0.1:
|
| 199 |
+
prolongations[-1]['end'] = float(frame_times[i])
|
| 200 |
+
prolongations[-1]['duration'] = prolongations[-1]['end'] - prolongations[-1]['start']
|
| 201 |
+
else:
|
| 202 |
+
start = frame_times[max(0, i-window)]
|
| 203 |
+
end = frame_times[min(len(frame_times)-1, i+window)]
|
| 204 |
+
prolongations.append({
|
| 205 |
+
'type': 'prolongation',
|
| 206 |
+
'start': float(start),
|
| 207 |
+
'end': float(end),
|
| 208 |
+
'duration': float(end - start),
|
| 209 |
+
'confidence': float(1.0 - (rms_cv + f0_cv) / 2)
|
| 210 |
+
})
|
| 211 |
+
|
| 212 |
+
return [p for p in prolongations if 0.3 < p['duration'] < 3.0]
|
| 213 |
+
|
| 214 |
+
def _detect_sound_repetitions(self, y: np.ndarray, sr: int, features: Dict) -> List[Dict]:
|
| 215 |
+
"""Detect sound repetitions using spectral similarity."""
|
| 216 |
+
mfcc = features['mfcc']
|
| 217 |
+
frame_times = features['frame_times']
|
| 218 |
+
|
| 219 |
+
repetitions = []
|
| 220 |
+
window = 15
|
| 221 |
+
|
| 222 |
+
for i in range(window, len(frame_times) - window * 2):
|
| 223 |
+
curr = mfcc[:, i:i+window].flatten()
|
| 224 |
+
next = mfcc[:, i+window:i+2*window].flatten()
|
| 225 |
+
|
| 226 |
+
if len(curr) > 0 and len(next) > 0:
|
| 227 |
+
similarity = np.dot(curr, next) / (np.linalg.norm(curr) * np.linalg.norm(next) + 1e-10)
|
| 228 |
+
|
| 229 |
+
if similarity > 0.85:
|
| 230 |
+
start = frame_times[i]
|
| 231 |
+
end = frame_times[min(len(frame_times)-1, i+2*window)]
|
| 232 |
+
repetitions.append({
|
| 233 |
+
'type': 'sound_repetition',
|
| 234 |
+
'start': float(start),
|
| 235 |
+
'end': float(end),
|
| 236 |
+
'duration': float(end - start),
|
| 237 |
+
'confidence': float(similarity)
|
| 238 |
+
})
|
| 239 |
+
|
| 240 |
+
return [r for r in repetitions if 0.1 < r['duration'] < 1.5]
|
| 241 |
+
|
| 242 |
+
def _detect_word_repetitions(self, y: np.ndarray, sr: int, features: Dict) -> List[Dict]:
|
| 243 |
+
"""Detect word repetitions using autocorrelation."""
|
| 244 |
+
rms = features['rms']
|
| 245 |
+
frame_times = features['frame_times']
|
| 246 |
+
|
| 247 |
+
rms_norm = (rms - np.mean(rms)) / (np.std(rms) + 1e-10)
|
| 248 |
+
autocorr = np.correlate(rms_norm, rms_norm, mode='full')
|
| 249 |
+
autocorr = autocorr[len(autocorr)//2:]
|
| 250 |
+
|
| 251 |
+
word_window = 30
|
| 252 |
+
peaks, _ = signal.find_peaks(
|
| 253 |
+
autocorr[word_window:word_window*3],
|
| 254 |
+
height=np.percentile(autocorr, 75),
|
| 255 |
+
distance=word_window//2
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
repetitions = []
|
| 259 |
+
for peak in peaks:
|
| 260 |
+
idx = peak + word_window
|
| 261 |
+
if idx < len(frame_times):
|
| 262 |
+
start = frame_times[max(0, idx-word_window)]
|
| 263 |
+
end = frame_times[min(len(frame_times)-1, idx+word_window)]
|
| 264 |
+
repetitions.append({
|
| 265 |
+
'type': 'word_repetition',
|
| 266 |
+
'start': float(start),
|
| 267 |
+
'end': float(end),
|
| 268 |
+
'duration': float(end - start),
|
| 269 |
+
'confidence': 0.7
|
| 270 |
+
})
|
| 271 |
+
|
| 272 |
+
return [r for r in repetitions if 0.3 < r['duration'] < 2.0]
|
| 273 |
+
|
| 274 |
+
def _detect_interjections(self, y: np.ndarray, sr: int, features: Dict) -> List[Dict]:
|
| 275 |
+
"""Detect interjections (um, uh, ah)."""
|
| 276 |
+
rms = features['rms']
|
| 277 |
+
centroid = features['spectral_centroid']
|
| 278 |
+
frame_times = features['frame_times']
|
| 279 |
+
|
| 280 |
+
centroid_z = self._modified_z_score(centroid)
|
| 281 |
+
unusual = np.abs(centroid_z) > self.mad_threshold
|
| 282 |
+
|
| 283 |
+
interjections = []
|
| 284 |
+
in_interj = False
|
| 285 |
+
start_idx = 0
|
| 286 |
+
|
| 287 |
+
for i, is_unusual in enumerate(unusual):
|
| 288 |
+
if is_unusual and rms[i] > np.median(rms) * 0.2:
|
| 289 |
+
if not in_interj:
|
| 290 |
+
start_idx = i
|
| 291 |
+
in_interj = True
|
| 292 |
+
elif in_interj:
|
| 293 |
+
duration = (i - start_idx) * features['hop_length'] / sr
|
| 294 |
+
if 0.1 < duration < 0.8:
|
| 295 |
+
interjections.append({
|
| 296 |
+
'type': 'interjection',
|
| 297 |
+
'start': float(frame_times[start_idx]),
|
| 298 |
+
'end': float(frame_times[i]),
|
| 299 |
+
'duration': float(duration),
|
| 300 |
+
'confidence': float(np.mean(np.abs(centroid_z[start_idx:i])))
|
| 301 |
+
})
|
| 302 |
+
in_interj = False
|
| 303 |
+
|
| 304 |
+
return interjections
|
| 305 |
+
|
| 306 |
+
def _calculate_severity(self, events: List[Dict], duration: float, rate: float) -> float:
|
| 307 |
+
"""Calculate adaptive severity score (0-100)."""
|
| 308 |
+
if duration <= 0:
|
| 309 |
+
return 0.0
|
| 310 |
+
|
| 311 |
+
counts = self._count_types(events)
|
| 312 |
+
total_time = sum(e['duration'] for e in events)
|
| 313 |
+
|
| 314 |
+
# Dysfluency percentage
|
| 315 |
+
dysfluency_pct = (total_time / duration) * 100
|
| 316 |
+
|
| 317 |
+
# Event frequency (per minute)
|
| 318 |
+
event_freq = (len(events) / duration) * 60
|
| 319 |
+
|
| 320 |
+
# Weighted count (blocks/prolongations more severe)
|
| 321 |
+
weights = {'block': 2.0, 'prolongation': 1.8, 'sound_repetition': 1.5,
|
| 322 |
+
'word_repetition': 1.3, 'interjection': 1.0}
|
| 323 |
+
weighted = sum(counts.get(t, 0) * w for t, w in weights.items())
|
| 324 |
+
|
| 325 |
+
# Rate normalization
|
| 326 |
+
rate_factor = min(rate / 4.0, 2.0) if rate > 0 else 1.0
|
| 327 |
+
|
| 328 |
+
severity = (
|
| 329 |
+
dysfluency_pct * 0.4 +
|
| 330 |
+
(event_freq / rate_factor) * 0.3 +
|
| 331 |
+
(weighted / rate_factor) * 0.3
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
return float(np.clip(severity, 0, 100))
|
| 335 |
+
|
| 336 |
+
def _count_types(self, events: List[Dict]) -> Dict[str, int]:
|
| 337 |
+
"""Count events by type."""
|
| 338 |
+
counts = {}
|
| 339 |
+
for e in events:
|
| 340 |
+
counts[e['type']] = counts.get(e['type'], 0) + 1
|
| 341 |
+
return counts
|
| 342 |
+
|
| 343 |
+
def _get_severity_label(self, score: float) -> str:
|
| 344 |
+
"""Convert score to label."""
|
| 345 |
+
if score < 10: return 'none'
|
| 346 |
+
elif score < 25: return 'mild'
|
| 347 |
+
elif score < 50: return 'moderate'
|
| 348 |
+
elif score < 75: return 'severe'
|
| 349 |
+
else: return 'very_severe'
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
# Initialize detector
|
| 353 |
+
stutter_detector = EnhancedStutterDetector(sample_rate=SAMPLE_RATE)
|
| 354 |
+
print("β
Enhanced Stutter Detector initialized")
|
| 355 |
|
| 356 |
+
|
| 357 |
+
@app.get("/")
|
| 358 |
+
def home():
|
| 359 |
return {
|
| 360 |
+
"status": "running",
|
| 361 |
+
"service": "SLAQ Enhanced AI Engine",
|
| 362 |
+
"version": "2.0.0",
|
| 363 |
+
"features": [
|
| 364 |
+
"Adaptive thresholding (Modified Z-Score/MAD)",
|
| 365 |
+
"Multi-feature acoustic analysis",
|
| 366 |
+
"Speaking-rate normalization",
|
| 367 |
+
"5 dysfluency types detection",
|
| 368 |
+
"Multilingual support (MMS-1B)"
|
| 369 |
+
],
|
| 370 |
+
"model": ASR_MODEL_ID
|
| 371 |
}
|
| 372 |
|
| 373 |
+
|
| 374 |
+
@app.get("/health")
|
| 375 |
+
def health():
|
| 376 |
+
return {"status": "healthy", "model_loaded": True}
|
| 377 |
+
|
| 378 |
+
|
| 379 |
@app.post("/analyze")
|
| 380 |
async def analyze_audio(
|
| 381 |
audio: UploadFile = File(...),
|
| 382 |
transcript: Optional[str] = Form(""),
|
| 383 |
language: Optional[str] = Form("auto")
|
| 384 |
):
|
| 385 |
+
"""
|
| 386 |
+
Analyze audio for stuttering events with adaptive detection.
|
| 387 |
+
|
| 388 |
+
Args:
|
| 389 |
+
audio: Audio file (WAV, MP3, etc.)
|
| 390 |
+
transcript: Optional reference transcript for comparison
|
| 391 |
+
language: Language code or 'auto' for detection
|
| 392 |
+
|
| 393 |
+
Returns:
|
| 394 |
+
Comprehensive stuttering analysis with adaptive thresholds
|
| 395 |
+
"""
|
| 396 |
temp_filename = f"temp_{audio.filename}"
|
| 397 |
|
| 398 |
try:
|
| 399 |
+
# Save uploaded file
|
| 400 |
with open(temp_filename, "wb") as buffer:
|
| 401 |
buffer.write(await audio.read())
|
| 402 |
|
| 403 |
+
# Load audio
|
| 404 |
+
speech, sr = librosa.load(temp_filename, sr=SAMPLE_RATE)
|
| 405 |
|
| 406 |
+
# --- LANGUAGE HANDLING ---
|
| 407 |
+
lang_code = LANG_MAP.get(str(language).lower(), 'eng')
|
| 408 |
|
| 409 |
+
if lang_code != 'auto':
|
| 410 |
+
try:
|
| 411 |
+
processor.tokenizer.set_target_lang(lang_code)
|
| 412 |
+
model.load_adapter(lang_code)
|
| 413 |
+
except:
|
| 414 |
+
print(f"β οΈ Adapter not found for {lang_code}, using eng")
|
| 415 |
+
lang_code = 'eng'
|
| 416 |
+
processor.tokenizer.set_target_lang('eng')
|
| 417 |
+
model.load_adapter('eng')
|
| 418 |
else:
|
| 419 |
+
# For auto mode, default to English
|
| 420 |
+
lang_code = 'eng'
|
| 421 |
+
processor.tokenizer.set_target_lang('eng')
|
| 422 |
+
model.load_adapter('eng')
|
| 423 |
+
|
| 424 |
+
# --- TRANSCRIPTION ---
|
| 425 |
+
inputs = processor(speech, sampling_rate=SAMPLE_RATE, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
with torch.no_grad():
|
| 427 |
outputs = model(**inputs)
|
| 428 |
logits = outputs.logits
|
|
|
|
| 430 |
predicted_ids = torch.argmax(logits, dim=-1)
|
| 431 |
actual_transcript = processor.batch_decode(predicted_ids)[0]
|
| 432 |
confidence = float(torch.mean(torch.nn.functional.softmax(logits, dim=-1).max(dim=-1).values))
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
+
# --- ENHANCED ACOUSTIC ANALYSIS ---
|
| 435 |
+
analysis = stutter_detector.analyze(speech, sr)
|
| 436 |
+
|
| 437 |
+
# --- TRANSCRIPT COMPARISON (if provided) ---
|
| 438 |
mismatch_pct = 0.0
|
|
|
|
| 439 |
if transcript:
|
| 440 |
+
import Levenshtein
|
| 441 |
dist = Levenshtein.distance(actual_transcript, transcript)
|
| 442 |
mismatch_pct = (dist / max(len(transcript), 1)) * 100
|
| 443 |
+
|
| 444 |
+
# Format timestamps
|
| 445 |
+
timestamps = [
|
| 446 |
+
{
|
| 447 |
+
'type': evt['type'],
|
| 448 |
+
'start': evt['start'],
|
| 449 |
+
'end': evt['end'],
|
| 450 |
+
'duration': evt['duration'],
|
| 451 |
+
'confidence': evt.get('confidence', 0.5)
|
| 452 |
+
}
|
| 453 |
+
for evt in analysis['events']
|
| 454 |
+
]
|
| 455 |
+
|
| 456 |
+
# Calculate total stutter duration
|
| 457 |
+
total_stutter_duration = sum(evt['duration'] for evt in analysis['events'])
|
| 458 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
return {
|
| 460 |
"actual_transcript": actual_transcript,
|
| 461 |
"target_transcript": transcript or "",
|
| 462 |
+
"mismatch_percentage": round(mismatch_pct, 2),
|
|
|
|
| 463 |
"stutter_timestamps": timestamps,
|
| 464 |
+
"total_stutter_duration": round(total_stutter_duration, 2),
|
| 465 |
+
"stutter_frequency": analysis['total_events'],
|
| 466 |
+
"severity": analysis['severity_label'],
|
| 467 |
+
"severity_score": round(analysis['severity_score'], 2),
|
| 468 |
"confidence_score": round(confidence, 2),
|
| 469 |
+
"model_version": f"enhanced-adaptive-v2 ({lang_code})",
|
| 470 |
+
"language_detected": lang_code,
|
| 471 |
+
"speaking_rate": round(analysis['speaking_rate'], 2),
|
| 472 |
+
"event_breakdown": analysis['event_counts'],
|
| 473 |
+
"dysfluency_rate": round(analysis['total_events'] / (analysis['duration'] / 60), 2) if analysis['duration'] > 0 else 0
|
| 474 |
}
|
| 475 |
+
|
| 476 |
except Exception as e:
|
|
|
|
| 477 |
traceback.print_exc()
|
| 478 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 479 |
|
| 480 |
finally:
|
| 481 |
if os.path.exists(temp_filename):
|
| 482 |
+
os.remove(temp_filename)
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
if __name__ == "__main__":
|
| 486 |
+
import uvicorn
|
| 487 |
+
print("\nπ Starting Enhanced SLAQ AI Engine...")
|
| 488 |
+
print("π Features: Adaptive thresholds, MAD-based detection, Multi-feature analysis")
|
| 489 |
+
print("π Access at: http://localhost:8000")
|
| 490 |
+
print("π Docs at: http://localhost:8000/docs\n")
|
| 491 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
CHANGED
|
@@ -5,6 +5,6 @@ torch==2.1.0
|
|
| 5 |
transformers==4.35.2
|
| 6 |
librosa==0.10.1
|
| 7 |
numpy==1.26.2
|
| 8 |
-
scipy==1.11.4
|
| 9 |
soundfile==0.12.1
|
| 10 |
python-Levenshtein==0.23.0
|
|
|
|
| 5 |
transformers==4.35.2
|
| 6 |
librosa==0.10.1
|
| 7 |
numpy==1.26.2
|
| 8 |
+
scipy==1.11.4 <-- Critical for signal processing
|
| 9 |
soundfile==0.12.1
|
| 10 |
python-Levenshtein==0.23.0
|