Spaces:
Sleeping
Sleeping
feat: Implement Whisper model preloading during FastAPI startup for optimized performance
Browse files- Added lifespan context manager to preload Whisper model on application startup.
- Updated create_app function to include lifespan for preloading.
- Enhanced health check endpoint to verify Whisper model loading status.
- Refactored speaking_route to export preload function and added documentation.
- Optimized post-assessment processing with asynchronous tasks for improved performance.
- Created example application demonstrating Whisper preloading integration.
- Updated performance test cases to reflect new API endpoint structure.
- app.py +27 -3
- example_app_with_preload.py +83 -0
- raw.py +0 -803
- requirements.txt +1 -1
- src/apis/__pycache__/create_app.cpython-311.pyc +0 -0
- src/apis/controllers/speaking_controller.py +354 -300
- src/apis/create_app.py +66 -13
- src/apis/routes/speaking_route.py +269 -146
- test_performance_optimization.py +1 -1
app.py
CHANGED
|
@@ -1,12 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from dotenv import load_dotenv
|
| 2 |
|
| 3 |
load_dotenv()
|
|
|
|
| 4 |
from src.apis.create_app import create_app, api_router
|
| 5 |
import uvicorn
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
app = create_app()
|
| 9 |
-
|
| 10 |
app.include_router(api_router)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if __name__ == "__main__":
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
English Tutor API - Main Application
|
| 3 |
+
Optimized with Whisper model preloading for faster pronunciation assessment
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
+
|
| 10 |
from src.apis.create_app import create_app, api_router
|
| 11 |
import uvicorn
|
| 12 |
+
from loguru import logger
|
| 13 |
|
| 14 |
+
# Create FastAPI app with Whisper preloading
|
| 15 |
app = create_app()
|
|
|
|
| 16 |
app.include_router(api_router)
|
| 17 |
+
|
| 18 |
+
# Add root endpoint
|
| 19 |
+
@app.get("/")
|
| 20 |
+
async def root():
|
| 21 |
+
return {
|
| 22 |
+
"message": "🎓 English Tutor API with Optimized Whisper",
|
| 23 |
+
"status": "ready",
|
| 24 |
+
"docs": "/docs",
|
| 25 |
+
"health": "/health"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
if __name__ == "__main__":
|
| 29 |
+
logger.info("🚀 Starting English Tutor API server...")
|
| 30 |
+
uvicorn.run(
|
| 31 |
+
"app:app",
|
| 32 |
+
host="0.0.0.0",
|
| 33 |
+
port=8000,
|
| 34 |
+
reload=False, # Set to False to avoid reloading and losing preloaded model
|
| 35 |
+
log_level="info"
|
| 36 |
+
)
|
example_app_with_preload.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Example: How to integrate Whisper preloading in FastAPI app startup
|
| 3 |
+
|
| 4 |
+
This shows how to preload Whisper model during FastAPI startup
|
| 5 |
+
so the first inference will be much faster.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from fastapi import FastAPI
|
| 9 |
+
from contextlib import asynccontextmanager
|
| 10 |
+
from src.apis.routes.speaking_route import router as speaking_router, preload_whisper_model
|
| 11 |
+
from loguru import logger
|
| 12 |
+
import time
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@asynccontextmanager
|
| 16 |
+
async def lifespan(app: FastAPI):
|
| 17 |
+
"""
|
| 18 |
+
FastAPI lifespan context manager for startup and shutdown events
|
| 19 |
+
"""
|
| 20 |
+
# Startup
|
| 21 |
+
logger.info("🚀 Starting FastAPI application...")
|
| 22 |
+
startup_start = time.time()
|
| 23 |
+
|
| 24 |
+
# Preload Whisper model during startup
|
| 25 |
+
logger.info("📦 Preloading Whisper model...")
|
| 26 |
+
success = preload_whisper_model(whisper_model="base.en")
|
| 27 |
+
|
| 28 |
+
if success:
|
| 29 |
+
logger.info("✅ Whisper model preloaded successfully!")
|
| 30 |
+
else:
|
| 31 |
+
logger.warning("⚠️ Failed to preload Whisper model, will load on first request")
|
| 32 |
+
|
| 33 |
+
startup_time = time.time() - startup_start
|
| 34 |
+
logger.info(f"🎯 FastAPI startup completed in {startup_time:.2f}s")
|
| 35 |
+
|
| 36 |
+
yield # Application runs here
|
| 37 |
+
|
| 38 |
+
# Shutdown
|
| 39 |
+
logger.info("🛑 Shutting down FastAPI application...")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Create FastAPI app with lifespan
|
| 43 |
+
app = FastAPI(
|
| 44 |
+
title="English Tutor API with Whisper Preloading",
|
| 45 |
+
description="Pronunciation assessment API with optimized Whisper startup",
|
| 46 |
+
version="2.0.0",
|
| 47 |
+
lifespan=lifespan # This enables the startup preloading
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Include speaking routes
|
| 51 |
+
app.include_router(speaking_router)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@app.get("/")
|
| 55 |
+
async def root():
|
| 56 |
+
return {"message": "English Tutor API with Whisper preloaded!", "status": "ready"}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@app.get("/health")
|
| 60 |
+
async def health_check():
|
| 61 |
+
"""Health check endpoint that also verifies Whisper is loaded"""
|
| 62 |
+
from src.apis.routes.speaking_route import global_assessor
|
| 63 |
+
|
| 64 |
+
whisper_loaded = global_assessor is not None
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
"status": "healthy",
|
| 68 |
+
"whisper_preloaded": whisper_loaded,
|
| 69 |
+
"model": global_assessor.asr.whisper_model_name if whisper_loaded else None
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
import uvicorn
|
| 75 |
+
|
| 76 |
+
# Run with uvicorn
|
| 77 |
+
uvicorn.run(
|
| 78 |
+
"example_app_with_preload:app",
|
| 79 |
+
host="0.0.0.0",
|
| 80 |
+
port=8000,
|
| 81 |
+
reload=False, # Set to False for production to avoid reloading and losing preloaded model
|
| 82 |
+
log_level="info"
|
| 83 |
+
)
|
raw.py
DELETED
|
@@ -1,803 +0,0 @@
|
|
| 1 |
-
from typing import List, Dict
|
| 2 |
-
import numpy as np
|
| 3 |
-
import librosa
|
| 4 |
-
import nltk
|
| 5 |
-
import eng_to_ipa as ipa
|
| 6 |
-
import re
|
| 7 |
-
from collections import defaultdict
|
| 8 |
-
from loguru import logger
|
| 9 |
-
import time
|
| 10 |
-
from src.AI_Models.wave2vec_inference import (
|
| 11 |
-
Wave2Vec2Inference,
|
| 12 |
-
Wave2Vec2ONNXInference,
|
| 13 |
-
export_to_onnx,
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
# Download required NLTK data
|
| 17 |
-
try:
|
| 18 |
-
nltk.download("cmudict", quiet=True)
|
| 19 |
-
from nltk.corpus import cmudict
|
| 20 |
-
except:
|
| 21 |
-
print("Warning: NLTK data not available")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
class Wav2Vec2CharacterASR:
|
| 25 |
-
"""Wav2Vec2 character-level ASR with support for both ONNX and Transformers inference"""
|
| 26 |
-
|
| 27 |
-
def __init__(
|
| 28 |
-
self,
|
| 29 |
-
model_name: str = "facebook/wav2vec2-large-960h-lv60-self",
|
| 30 |
-
onnx: bool = False,
|
| 31 |
-
quantized: bool = False,
|
| 32 |
-
):
|
| 33 |
-
"""
|
| 34 |
-
Initialize Wav2Vec2 character-level model
|
| 35 |
-
Args:
|
| 36 |
-
model_name: HuggingFace model name
|
| 37 |
-
onnx: If True, use ONNX runtime for inference. If False, use Transformers
|
| 38 |
-
onnx_model_path: Path to the ONNX model file (only used if onnx=True)
|
| 39 |
-
"""
|
| 40 |
-
self.use_onnx = onnx
|
| 41 |
-
self.sample_rate = 16000
|
| 42 |
-
self.model_name = model_name
|
| 43 |
-
# Check thử path của onnx model có tồn tại hay không
|
| 44 |
-
if onnx:
|
| 45 |
-
import os
|
| 46 |
-
|
| 47 |
-
if not os.path.exists(
|
| 48 |
-
"wav2vec2-large-960h-lv60-self"
|
| 49 |
-
+ (".quant" if quantized else "")
|
| 50 |
-
+ ".onnx"
|
| 51 |
-
):
|
| 52 |
-
|
| 53 |
-
export_to_onnx(model_name, quantize=quantized)
|
| 54 |
-
self.model = (
|
| 55 |
-
Wave2Vec2Inference(model_name)
|
| 56 |
-
if not onnx
|
| 57 |
-
else Wave2Vec2ONNXInference(
|
| 58 |
-
model_name,
|
| 59 |
-
"wav2vec2-large-960h-lv60-self"
|
| 60 |
-
+ (".quant" if quantized else "")
|
| 61 |
-
+ ".onnx",
|
| 62 |
-
)
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
def transcribe_to_characters(self, audio_path: str) -> Dict:
|
| 66 |
-
try:
|
| 67 |
-
start_time = time.time()
|
| 68 |
-
character_transcript = self.model.file_to_text(audio_path)
|
| 69 |
-
character_transcript = self._clean_character_transcript(
|
| 70 |
-
character_transcript
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
phoneme_like_transcript = self._characters_to_phoneme_representation(
|
| 74 |
-
character_transcript
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
logger.info(f"Transcription time: {time.time() - start_time:.2f}s")
|
| 78 |
-
|
| 79 |
-
return {
|
| 80 |
-
"character_transcript": character_transcript,
|
| 81 |
-
"phoneme_representation": phoneme_like_transcript,
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
except Exception as e:
|
| 85 |
-
print(f"Transformers transcription error: {e}")
|
| 86 |
-
return self._empty_result()
|
| 87 |
-
|
| 88 |
-
def _calculate_confidence_scores(self, logits: np.ndarray) -> List[float]:
|
| 89 |
-
"""Calculate confidence scores from logits using numpy"""
|
| 90 |
-
# Apply softmax
|
| 91 |
-
exp_logits = np.exp(logits - np.max(logits, axis=-1, keepdims=True))
|
| 92 |
-
softmax_probs = exp_logits / np.sum(exp_logits, axis=-1, keepdims=True)
|
| 93 |
-
|
| 94 |
-
# Get max probabilities
|
| 95 |
-
max_probs = np.max(softmax_probs, axis=-1)[0]
|
| 96 |
-
return max_probs.tolist()
|
| 97 |
-
|
| 98 |
-
def _clean_character_transcript(self, transcript: str) -> str:
|
| 99 |
-
"""Clean and standardize character transcript"""
|
| 100 |
-
# Remove extra spaces and special tokens
|
| 101 |
-
logger.info(f"Raw transcript before cleaning: {transcript}")
|
| 102 |
-
cleaned = re.sub(r"\s+", " ", transcript)
|
| 103 |
-
cleaned = cleaned.strip().lower()
|
| 104 |
-
return cleaned
|
| 105 |
-
|
| 106 |
-
def _characters_to_phoneme_representation(self, text: str) -> str:
|
| 107 |
-
"""Convert character-based transcript to phoneme-like representation for comparison"""
|
| 108 |
-
if not text:
|
| 109 |
-
return ""
|
| 110 |
-
|
| 111 |
-
words = text.split()
|
| 112 |
-
phoneme_words = []
|
| 113 |
-
g2p = SimpleG2P()
|
| 114 |
-
for word in words:
|
| 115 |
-
try:
|
| 116 |
-
if g2p:
|
| 117 |
-
word_data = g2p.text_to_phonemes(word)[0]
|
| 118 |
-
phoneme_words.extend(word_data["phonemes"])
|
| 119 |
-
else:
|
| 120 |
-
phoneme_words.extend(self._simple_letter_to_phoneme(word))
|
| 121 |
-
except:
|
| 122 |
-
# Fallback: simple letter-to-sound mapping
|
| 123 |
-
phoneme_words.extend(self._simple_letter_to_phoneme(word))
|
| 124 |
-
|
| 125 |
-
return " ".join(phoneme_words)
|
| 126 |
-
|
| 127 |
-
def _simple_letter_to_phoneme(self, word: str) -> List[str]:
|
| 128 |
-
"""Simple fallback letter-to-phoneme conversion"""
|
| 129 |
-
letter_to_phoneme = {
|
| 130 |
-
"a": "æ",
|
| 131 |
-
"b": "b",
|
| 132 |
-
"c": "k",
|
| 133 |
-
"d": "d",
|
| 134 |
-
"e": "ɛ",
|
| 135 |
-
"f": "f",
|
| 136 |
-
"g": "ɡ",
|
| 137 |
-
"h": "h",
|
| 138 |
-
"i": "ɪ",
|
| 139 |
-
"j": "dʒ",
|
| 140 |
-
"k": "k",
|
| 141 |
-
"l": "l",
|
| 142 |
-
"m": "m",
|
| 143 |
-
"n": "n",
|
| 144 |
-
"o": "ʌ",
|
| 145 |
-
"p": "p",
|
| 146 |
-
"q": "k",
|
| 147 |
-
"r": "r",
|
| 148 |
-
"s": "s",
|
| 149 |
-
"t": "t",
|
| 150 |
-
"u": "ʌ",
|
| 151 |
-
"v": "v",
|
| 152 |
-
"w": "w",
|
| 153 |
-
"x": "ks",
|
| 154 |
-
"y": "j",
|
| 155 |
-
"z": "z",
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
phonemes = []
|
| 159 |
-
for letter in word.lower():
|
| 160 |
-
if letter in letter_to_phoneme:
|
| 161 |
-
phonemes.append(letter_to_phoneme[letter])
|
| 162 |
-
|
| 163 |
-
return phonemes
|
| 164 |
-
|
| 165 |
-
def _empty_result(self) -> Dict:
|
| 166 |
-
"""Return empty result structure"""
|
| 167 |
-
return {
|
| 168 |
-
"character_transcript": "",
|
| 169 |
-
"phoneme_representation": "",
|
| 170 |
-
"raw_predicted_ids": [],
|
| 171 |
-
"confidence_scores": [],
|
| 172 |
-
}
|
| 173 |
-
|
| 174 |
-
def get_model_info(self) -> Dict:
|
| 175 |
-
"""Get information about the loaded model"""
|
| 176 |
-
info = {
|
| 177 |
-
"model_name": self.model_name,
|
| 178 |
-
"sample_rate": self.sample_rate,
|
| 179 |
-
"inference_method": "ONNX" if self.use_onnx else "Transformers",
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
if self.use_onnx:
|
| 183 |
-
info.update(
|
| 184 |
-
{
|
| 185 |
-
"onnx_model_path": self.onnx_model_path,
|
| 186 |
-
"input_name": self.input_name,
|
| 187 |
-
"output_name": self.output_name,
|
| 188 |
-
"session_providers": self.session.get_providers(),
|
| 189 |
-
}
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
return info
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
class SimpleG2P:
|
| 196 |
-
"""Simple Grapheme-to-Phoneme converter for reference text"""
|
| 197 |
-
|
| 198 |
-
def __init__(self):
|
| 199 |
-
try:
|
| 200 |
-
self.cmu_dict = cmudict.dict()
|
| 201 |
-
except:
|
| 202 |
-
self.cmu_dict = {}
|
| 203 |
-
print("Warning: CMU dictionary not available")
|
| 204 |
-
|
| 205 |
-
def text_to_phonemes(self, text: str) -> List[Dict]:
|
| 206 |
-
"""Convert text to phoneme sequence"""
|
| 207 |
-
words = self._clean_text(text).split()
|
| 208 |
-
phoneme_sequence = []
|
| 209 |
-
|
| 210 |
-
for word in words:
|
| 211 |
-
word_phonemes = self._get_word_phonemes(word)
|
| 212 |
-
phoneme_sequence.append(
|
| 213 |
-
{
|
| 214 |
-
"word": word,
|
| 215 |
-
"phonemes": word_phonemes,
|
| 216 |
-
"ipa": self._get_ipa(word),
|
| 217 |
-
"phoneme_string": " ".join(word_phonemes),
|
| 218 |
-
}
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
return phoneme_sequence
|
| 222 |
-
|
| 223 |
-
def get_reference_phoneme_string(self, text: str) -> str:
|
| 224 |
-
"""Get reference phoneme string for comparison"""
|
| 225 |
-
phoneme_sequence = self.text_to_phonemes(text)
|
| 226 |
-
all_phonemes = []
|
| 227 |
-
|
| 228 |
-
for word_data in phoneme_sequence:
|
| 229 |
-
all_phonemes.extend(word_data["phonemes"])
|
| 230 |
-
|
| 231 |
-
return " ".join(all_phonemes)
|
| 232 |
-
|
| 233 |
-
def _clean_text(self, text: str) -> str:
|
| 234 |
-
"""Clean text for processing"""
|
| 235 |
-
text = re.sub(r"[^\w\s\']", " ", text)
|
| 236 |
-
text = re.sub(r"\s+", " ", text)
|
| 237 |
-
return text.lower().strip()
|
| 238 |
-
|
| 239 |
-
def _get_word_phonemes(self, word: str) -> List[str]:
|
| 240 |
-
"""Get phonemes for a word"""
|
| 241 |
-
word_lower = word.lower()
|
| 242 |
-
|
| 243 |
-
if word_lower in self.cmu_dict:
|
| 244 |
-
# Remove stress markers and convert to Wav2Vec2 phoneme format
|
| 245 |
-
phonemes = self.cmu_dict[word_lower][0]
|
| 246 |
-
clean_phonemes = [re.sub(r"[0-9]", "", p) for p in phonemes]
|
| 247 |
-
return self._convert_to_wav2vec_format(clean_phonemes)
|
| 248 |
-
else:
|
| 249 |
-
return self._estimate_phonemes(word)
|
| 250 |
-
|
| 251 |
-
def _convert_to_wav2vec_format(self, cmu_phonemes: List[str]) -> List[str]:
|
| 252 |
-
"""Convert CMU phonemes to Wav2Vec2 format"""
|
| 253 |
-
# Mapping from CMU to Wav2Vec2/eSpeak phonemes
|
| 254 |
-
cmu_to_espeak = {
|
| 255 |
-
"AA": "ɑ",
|
| 256 |
-
"AE": "æ",
|
| 257 |
-
"AH": "ʌ",
|
| 258 |
-
"AO": "ɔ",
|
| 259 |
-
"AW": "aʊ",
|
| 260 |
-
"AY": "aɪ",
|
| 261 |
-
"EH": "ɛ",
|
| 262 |
-
"ER": "ɝ",
|
| 263 |
-
"EY": "eɪ",
|
| 264 |
-
"IH": "ɪ",
|
| 265 |
-
"IY": "i",
|
| 266 |
-
"OW": "oʊ",
|
| 267 |
-
"OY": "ɔɪ",
|
| 268 |
-
"UH": "ʊ",
|
| 269 |
-
"UW": "u",
|
| 270 |
-
"B": "b",
|
| 271 |
-
"CH": "tʃ",
|
| 272 |
-
"D": "d",
|
| 273 |
-
"DH": "ð",
|
| 274 |
-
"F": "f",
|
| 275 |
-
"G": "ɡ",
|
| 276 |
-
"HH": "h",
|
| 277 |
-
"JH": "dʒ",
|
| 278 |
-
"K": "k",
|
| 279 |
-
"L": "l",
|
| 280 |
-
"M": "m",
|
| 281 |
-
"N": "n",
|
| 282 |
-
"NG": "ŋ",
|
| 283 |
-
"P": "p",
|
| 284 |
-
"R": "r",
|
| 285 |
-
"S": "s",
|
| 286 |
-
"SH": "ʃ",
|
| 287 |
-
"T": "t",
|
| 288 |
-
"TH": "θ",
|
| 289 |
-
"V": "v",
|
| 290 |
-
"W": "w",
|
| 291 |
-
"Y": "j",
|
| 292 |
-
"Z": "z",
|
| 293 |
-
"ZH": "ʒ",
|
| 294 |
-
}
|
| 295 |
-
|
| 296 |
-
converted = []
|
| 297 |
-
for phoneme in cmu_phonemes:
|
| 298 |
-
converted_phoneme = cmu_to_espeak.get(phoneme, phoneme.lower())
|
| 299 |
-
converted.append(converted_phoneme)
|
| 300 |
-
|
| 301 |
-
return converted
|
| 302 |
-
|
| 303 |
-
def _get_ipa(self, word: str) -> str:
|
| 304 |
-
"""Get IPA transcription"""
|
| 305 |
-
try:
|
| 306 |
-
return ipa.convert(word)
|
| 307 |
-
except:
|
| 308 |
-
return f"/{word}/"
|
| 309 |
-
|
| 310 |
-
def _estimate_phonemes(self, word: str) -> List[str]:
|
| 311 |
-
"""Estimate phonemes for unknown words"""
|
| 312 |
-
# Basic phoneme estimation with eSpeak-style output
|
| 313 |
-
phoneme_map = {
|
| 314 |
-
"ch": ["tʃ"],
|
| 315 |
-
"sh": ["ʃ"],
|
| 316 |
-
"th": ["θ"],
|
| 317 |
-
"ph": ["f"],
|
| 318 |
-
"ck": ["k"],
|
| 319 |
-
"ng": ["ŋ"],
|
| 320 |
-
"qu": ["k", "w"],
|
| 321 |
-
"a": ["æ"],
|
| 322 |
-
"e": ["ɛ"],
|
| 323 |
-
"i": ["ɪ"],
|
| 324 |
-
"o": ["ʌ"],
|
| 325 |
-
"u": ["ʌ"],
|
| 326 |
-
"b": ["b"],
|
| 327 |
-
"c": ["k"],
|
| 328 |
-
"d": ["d"],
|
| 329 |
-
"f": ["f"],
|
| 330 |
-
"g": ["ɡ"],
|
| 331 |
-
"h": ["h"],
|
| 332 |
-
"j": ["dʒ"],
|
| 333 |
-
"k": ["k"],
|
| 334 |
-
"l": ["l"],
|
| 335 |
-
"m": ["m"],
|
| 336 |
-
"n": ["n"],
|
| 337 |
-
"p": ["p"],
|
| 338 |
-
"r": ["r"],
|
| 339 |
-
"s": ["s"],
|
| 340 |
-
"t": ["t"],
|
| 341 |
-
"v": ["v"],
|
| 342 |
-
"w": ["w"],
|
| 343 |
-
"x": ["k", "s"],
|
| 344 |
-
"y": ["j"],
|
| 345 |
-
"z": ["z"],
|
| 346 |
-
}
|
| 347 |
-
|
| 348 |
-
word = word.lower()
|
| 349 |
-
phonemes = []
|
| 350 |
-
i = 0
|
| 351 |
-
|
| 352 |
-
while i < len(word):
|
| 353 |
-
# Check 2-letter combinations first
|
| 354 |
-
if i <= len(word) - 2:
|
| 355 |
-
two_char = word[i : i + 2]
|
| 356 |
-
if two_char in phoneme_map:
|
| 357 |
-
phonemes.extend(phoneme_map[two_char])
|
| 358 |
-
i += 2
|
| 359 |
-
continue
|
| 360 |
-
|
| 361 |
-
# Single character
|
| 362 |
-
char = word[i]
|
| 363 |
-
if char in phoneme_map:
|
| 364 |
-
phonemes.extend(phoneme_map[char])
|
| 365 |
-
|
| 366 |
-
i += 1
|
| 367 |
-
|
| 368 |
-
return phonemes
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
class PhonemeComparator:
|
| 372 |
-
"""Compare reference and learner phoneme sequences"""
|
| 373 |
-
|
| 374 |
-
def __init__(self):
|
| 375 |
-
# Vietnamese speakers' common phoneme substitutions
|
| 376 |
-
self.substitution_patterns = {
|
| 377 |
-
"θ": ["f", "s", "t"], # TH → F, S, T
|
| 378 |
-
"ð": ["d", "z", "v"], # DH → D, Z, V
|
| 379 |
-
"v": ["w", "f"], # V → W, F
|
| 380 |
-
"r": ["l"], # R → L
|
| 381 |
-
"l": ["r"], # L → R
|
| 382 |
-
"z": ["s"], # Z → S
|
| 383 |
-
"ʒ": ["ʃ", "z"], # ZH → SH, Z
|
| 384 |
-
"ŋ": ["n"], # NG → N
|
| 385 |
-
}
|
| 386 |
-
|
| 387 |
-
# Difficulty levels for Vietnamese speakers
|
| 388 |
-
self.difficulty_map = {
|
| 389 |
-
"θ": 0.9, # th (think)
|
| 390 |
-
"ð": 0.9, # th (this)
|
| 391 |
-
"v": 0.8, # v
|
| 392 |
-
"z": 0.8, # z
|
| 393 |
-
"ʒ": 0.9, # zh (measure)
|
| 394 |
-
"r": 0.7, # r
|
| 395 |
-
"l": 0.6, # l
|
| 396 |
-
"w": 0.5, # w
|
| 397 |
-
"f": 0.4, # f
|
| 398 |
-
"s": 0.3, # s
|
| 399 |
-
"ʃ": 0.5, # sh
|
| 400 |
-
"tʃ": 0.4, # ch
|
| 401 |
-
"dʒ": 0.5, # j
|
| 402 |
-
"ŋ": 0.3, # ng
|
| 403 |
-
}
|
| 404 |
-
|
| 405 |
-
def compare_phoneme_sequences(
|
| 406 |
-
self, reference_phonemes: str, learner_phonemes: str
|
| 407 |
-
) -> List[Dict]:
|
| 408 |
-
"""Compare reference and learner phoneme sequences"""
|
| 409 |
-
|
| 410 |
-
# Split phoneme strings
|
| 411 |
-
ref_phones = reference_phonemes.split()
|
| 412 |
-
learner_phones = learner_phonemes.split()
|
| 413 |
-
|
| 414 |
-
print(f"Reference phonemes: {ref_phones}")
|
| 415 |
-
print(f"Learner phonemes: {learner_phones}")
|
| 416 |
-
|
| 417 |
-
# Simple alignment comparison
|
| 418 |
-
comparisons = []
|
| 419 |
-
max_len = max(len(ref_phones), len(learner_phones))
|
| 420 |
-
|
| 421 |
-
for i in range(max_len):
|
| 422 |
-
ref_phoneme = ref_phones[i] if i < len(ref_phones) else ""
|
| 423 |
-
learner_phoneme = learner_phones[i] if i < len(learner_phones) else ""
|
| 424 |
-
|
| 425 |
-
if ref_phoneme and learner_phoneme:
|
| 426 |
-
# Both present - check accuracy
|
| 427 |
-
if ref_phoneme == learner_phoneme:
|
| 428 |
-
status = "correct"
|
| 429 |
-
score = 1.0
|
| 430 |
-
elif self._is_acceptable_substitution(ref_phoneme, learner_phoneme):
|
| 431 |
-
status = "acceptable"
|
| 432 |
-
score = 0.7
|
| 433 |
-
else:
|
| 434 |
-
status = "wrong"
|
| 435 |
-
score = 0.2
|
| 436 |
-
|
| 437 |
-
elif ref_phoneme and not learner_phoneme:
|
| 438 |
-
# Missing phoneme
|
| 439 |
-
status = "missing"
|
| 440 |
-
score = 0.0
|
| 441 |
-
|
| 442 |
-
elif learner_phoneme and not ref_phoneme:
|
| 443 |
-
# Extra phoneme
|
| 444 |
-
status = "extra"
|
| 445 |
-
score = 0.0
|
| 446 |
-
else:
|
| 447 |
-
continue
|
| 448 |
-
|
| 449 |
-
comparison = {
|
| 450 |
-
"position": i,
|
| 451 |
-
"reference_phoneme": ref_phoneme,
|
| 452 |
-
"learner_phoneme": learner_phoneme,
|
| 453 |
-
"status": status,
|
| 454 |
-
"score": score,
|
| 455 |
-
"difficulty": self.difficulty_map.get(ref_phoneme, 0.3),
|
| 456 |
-
}
|
| 457 |
-
|
| 458 |
-
comparisons.append(comparison)
|
| 459 |
-
|
| 460 |
-
return comparisons
|
| 461 |
-
|
| 462 |
-
def _is_acceptable_substitution(self, reference: str, learner: str) -> bool:
|
| 463 |
-
"""Check if learner phoneme is acceptable substitution for Vietnamese speakers"""
|
| 464 |
-
acceptable = self.substitution_patterns.get(reference, [])
|
| 465 |
-
return learner in acceptable
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
# =============================================================================
|
| 469 |
-
# WORD ANALYZER
|
| 470 |
-
# =============================================================================
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
class WordAnalyzer:
|
| 474 |
-
"""Analyze word-level pronunciation accuracy using character-based ASR"""
|
| 475 |
-
|
| 476 |
-
def __init__(self):
|
| 477 |
-
self.g2p = SimpleG2P()
|
| 478 |
-
self.comparator = PhonemeComparator()
|
| 479 |
-
|
| 480 |
-
def analyze_words(self, reference_text: str, learner_phonemes: str) -> Dict:
|
| 481 |
-
"""Analyze word-level pronunciation using phoneme representation from character ASR"""
|
| 482 |
-
|
| 483 |
-
# Get reference phonemes by word
|
| 484 |
-
reference_words = self.g2p.text_to_phonemes(reference_text)
|
| 485 |
-
|
| 486 |
-
# Get overall phoneme comparison
|
| 487 |
-
reference_phoneme_string = self.g2p.get_reference_phoneme_string(reference_text)
|
| 488 |
-
phoneme_comparisons = self.comparator.compare_phoneme_sequences(
|
| 489 |
-
reference_phoneme_string, learner_phonemes
|
| 490 |
-
)
|
| 491 |
-
|
| 492 |
-
# Map phonemes back to words
|
| 493 |
-
word_highlights = self._create_word_highlights(
|
| 494 |
-
reference_words, phoneme_comparisons
|
| 495 |
-
)
|
| 496 |
-
|
| 497 |
-
# Identify wrong words
|
| 498 |
-
wrong_words = self._identify_wrong_words(word_highlights, phoneme_comparisons)
|
| 499 |
-
|
| 500 |
-
return {
|
| 501 |
-
"word_highlights": word_highlights,
|
| 502 |
-
"phoneme_differences": phoneme_comparisons,
|
| 503 |
-
"wrong_words": wrong_words,
|
| 504 |
-
}
|
| 505 |
-
|
| 506 |
-
def _create_word_highlights(
|
| 507 |
-
self, reference_words: List[Dict], phoneme_comparisons: List[Dict]
|
| 508 |
-
) -> List[Dict]:
|
| 509 |
-
"""Create word highlighting data"""
|
| 510 |
-
|
| 511 |
-
word_highlights = []
|
| 512 |
-
phoneme_index = 0
|
| 513 |
-
|
| 514 |
-
for word_data in reference_words:
|
| 515 |
-
word = word_data["word"]
|
| 516 |
-
word_phonemes = word_data["phonemes"]
|
| 517 |
-
num_phonemes = len(word_phonemes)
|
| 518 |
-
|
| 519 |
-
# Get phoneme scores for this word
|
| 520 |
-
word_phoneme_scores = []
|
| 521 |
-
for j in range(num_phonemes):
|
| 522 |
-
if phoneme_index + j < len(phoneme_comparisons):
|
| 523 |
-
comparison = phoneme_comparisons[phoneme_index + j]
|
| 524 |
-
word_phoneme_scores.append(comparison["score"])
|
| 525 |
-
|
| 526 |
-
# Calculate word score
|
| 527 |
-
word_score = np.mean(word_phoneme_scores) if word_phoneme_scores else 0.0
|
| 528 |
-
|
| 529 |
-
# Create word highlight
|
| 530 |
-
highlight = {
|
| 531 |
-
"word": word,
|
| 532 |
-
"score": float(word_score),
|
| 533 |
-
"status": self._get_word_status(word_score),
|
| 534 |
-
"color": self._get_word_color(word_score),
|
| 535 |
-
"phonemes": word_phonemes,
|
| 536 |
-
"ipa": word_data["ipa"],
|
| 537 |
-
"phoneme_scores": word_phoneme_scores,
|
| 538 |
-
"phoneme_start_index": phoneme_index,
|
| 539 |
-
"phoneme_end_index": phoneme_index + num_phonemes - 1,
|
| 540 |
-
}
|
| 541 |
-
|
| 542 |
-
word_highlights.append(highlight)
|
| 543 |
-
phoneme_index += num_phonemes
|
| 544 |
-
|
| 545 |
-
return word_highlights
|
| 546 |
-
|
| 547 |
-
def _identify_wrong_words(
|
| 548 |
-
self, word_highlights: List[Dict], phoneme_comparisons: List[Dict]
|
| 549 |
-
) -> List[Dict]:
|
| 550 |
-
"""Identify words that were pronounced incorrectly"""
|
| 551 |
-
|
| 552 |
-
wrong_words = []
|
| 553 |
-
|
| 554 |
-
for word_highlight in word_highlights:
|
| 555 |
-
if word_highlight["score"] < 0.6: # Threshold for wrong pronunciation
|
| 556 |
-
|
| 557 |
-
# Find specific phoneme errors for this word
|
| 558 |
-
start_idx = word_highlight["phoneme_start_index"]
|
| 559 |
-
end_idx = word_highlight["phoneme_end_index"]
|
| 560 |
-
|
| 561 |
-
wrong_phonemes = []
|
| 562 |
-
missing_phonemes = []
|
| 563 |
-
|
| 564 |
-
for i in range(start_idx, min(end_idx + 1, len(phoneme_comparisons))):
|
| 565 |
-
comparison = phoneme_comparisons[i]
|
| 566 |
-
|
| 567 |
-
if comparison["status"] == "wrong":
|
| 568 |
-
wrong_phonemes.append(
|
| 569 |
-
{
|
| 570 |
-
"expected": comparison["reference_phoneme"],
|
| 571 |
-
"actual": comparison["learner_phoneme"],
|
| 572 |
-
"difficulty": comparison["difficulty"],
|
| 573 |
-
}
|
| 574 |
-
)
|
| 575 |
-
elif comparison["status"] == "missing":
|
| 576 |
-
missing_phonemes.append(
|
| 577 |
-
{
|
| 578 |
-
"phoneme": comparison["reference_phoneme"],
|
| 579 |
-
"difficulty": comparison["difficulty"],
|
| 580 |
-
}
|
| 581 |
-
)
|
| 582 |
-
|
| 583 |
-
wrong_word = {
|
| 584 |
-
"word": word_highlight["word"],
|
| 585 |
-
"score": word_highlight["score"],
|
| 586 |
-
"expected_phonemes": word_highlight["phonemes"],
|
| 587 |
-
"ipa": word_highlight["ipa"],
|
| 588 |
-
"wrong_phonemes": wrong_phonemes,
|
| 589 |
-
"missing_phonemes": missing_phonemes,
|
| 590 |
-
"tips": self._get_vietnamese_tips(wrong_phonemes, missing_phonemes),
|
| 591 |
-
}
|
| 592 |
-
|
| 593 |
-
wrong_words.append(wrong_word)
|
| 594 |
-
|
| 595 |
-
return wrong_words
|
| 596 |
-
|
| 597 |
-
def _get_word_status(self, score: float) -> str:
|
| 598 |
-
"""Get word status from score"""
|
| 599 |
-
if score >= 0.8:
|
| 600 |
-
return "excellent"
|
| 601 |
-
elif score >= 0.6:
|
| 602 |
-
return "good"
|
| 603 |
-
elif score >= 0.4:
|
| 604 |
-
return "needs_practice"
|
| 605 |
-
else:
|
| 606 |
-
return "poor"
|
| 607 |
-
|
| 608 |
-
def _get_word_color(self, score: float) -> str:
|
| 609 |
-
"""Get color for word highlighting"""
|
| 610 |
-
if score >= 0.8:
|
| 611 |
-
return "#22c55e" # Green
|
| 612 |
-
elif score >= 0.6:
|
| 613 |
-
return "#84cc16" # Light green
|
| 614 |
-
elif score >= 0.4:
|
| 615 |
-
return "#eab308" # Yellow
|
| 616 |
-
else:
|
| 617 |
-
return "#ef4444" # Red
|
| 618 |
-
|
| 619 |
-
def _get_vietnamese_tips(
|
| 620 |
-
self, wrong_phonemes: List[Dict], missing_phonemes: List[Dict]
|
| 621 |
-
) -> List[str]:
|
| 622 |
-
"""Get Vietnamese-specific pronunciation tips"""
|
| 623 |
-
|
| 624 |
-
tips = []
|
| 625 |
-
|
| 626 |
-
# Tips for specific Vietnamese pronunciation challenges
|
| 627 |
-
vietnamese_tips = {
|
| 628 |
-
"θ": "Đặt lưỡi giữa răng trên và dưới, thổi nhẹ (think, three)",
|
| 629 |
-
"ð": "Giống θ nhưng rung dây thanh âm (this, that)",
|
| 630 |
-
"v": "Chạm môi dưới vào răng trên, không dùng cả hai môi như tiếng Việt",
|
| 631 |
-
"r": "Cuộn lưỡi nhưng không chạm vào vòm miệng, không lăn lưỡi",
|
| 632 |
-
"l": "Đầu lưỡi chạm vào vòm miệng sau răng",
|
| 633 |
-
"z": "Giống âm 's' nhưng có rung dây thanh âm",
|
| 634 |
-
"ʒ": "Giống âm 'ʃ' (sh) nhưng có rung dây thanh âm",
|
| 635 |
-
"w": "Tròn môi như âm 'u', không dùng răng như âm 'v'",
|
| 636 |
-
}
|
| 637 |
-
|
| 638 |
-
# Add tips for wrong phonemes
|
| 639 |
-
for wrong in wrong_phonemes:
|
| 640 |
-
expected = wrong["expected"]
|
| 641 |
-
actual = wrong["actual"]
|
| 642 |
-
|
| 643 |
-
if expected in vietnamese_tips:
|
| 644 |
-
tips.append(f"Âm '{expected}': {vietnamese_tips[expected]}")
|
| 645 |
-
else:
|
| 646 |
-
tips.append(f"Luyện âm '{expected}' thay vì '{actual}'")
|
| 647 |
-
|
| 648 |
-
# Add tips for missing phonemes
|
| 649 |
-
for missing in missing_phonemes:
|
| 650 |
-
phoneme = missing["phoneme"]
|
| 651 |
-
if phoneme in vietnamese_tips:
|
| 652 |
-
tips.append(f"Thiếu âm '{phoneme}': {vietnamese_tips[phoneme]}")
|
| 653 |
-
|
| 654 |
-
return tips
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
class SimpleFeedbackGenerator:
|
| 658 |
-
"""Generate simple, actionable feedback in Vietnamese"""
|
| 659 |
-
|
| 660 |
-
def generate_feedback(
|
| 661 |
-
self,
|
| 662 |
-
overall_score: float,
|
| 663 |
-
wrong_words: List[Dict],
|
| 664 |
-
phoneme_comparisons: List[Dict],
|
| 665 |
-
) -> List[str]:
|
| 666 |
-
"""Generate Vietnamese feedback"""
|
| 667 |
-
|
| 668 |
-
feedback = []
|
| 669 |
-
|
| 670 |
-
# Overall feedback in Vietnamese
|
| 671 |
-
if overall_score >= 0.8:
|
| 672 |
-
feedback.append("Phát âm rất tốt! Bạn đã làm xuất sắc.")
|
| 673 |
-
elif overall_score >= 0.6:
|
| 674 |
-
feedback.append("Phát âm khá tốt, còn một vài điểm cần cải thiện.")
|
| 675 |
-
elif overall_score >= 0.4:
|
| 676 |
-
feedback.append(
|
| 677 |
-
"Cần luyện tập thêm. Tập trung vào những từ được đánh dấu đỏ."
|
| 678 |
-
)
|
| 679 |
-
else:
|
| 680 |
-
feedback.append("Hãy luyện tập chậm và rõ ràng hơn.")
|
| 681 |
-
|
| 682 |
-
# Wrong words feedback
|
| 683 |
-
if wrong_words:
|
| 684 |
-
if len(wrong_words) <= 3:
|
| 685 |
-
word_names = [w["word"] for w in wrong_words]
|
| 686 |
-
feedback.append(f"Các từ cần luyện tập: {', '.join(word_names)}")
|
| 687 |
-
else:
|
| 688 |
-
feedback.append(
|
| 689 |
-
f"Có {len(wrong_words)} từ cần luyện tập. Tập trung vào từng từ một."
|
| 690 |
-
)
|
| 691 |
-
|
| 692 |
-
# Most problematic phonemes
|
| 693 |
-
problem_phonemes = defaultdict(int)
|
| 694 |
-
for comparison in phoneme_comparisons:
|
| 695 |
-
if comparison["status"] in ["wrong", "missing"]:
|
| 696 |
-
phoneme = comparison["reference_phoneme"]
|
| 697 |
-
problem_phonemes[phoneme] += 1
|
| 698 |
-
|
| 699 |
-
if problem_phonemes:
|
| 700 |
-
most_difficult = sorted(
|
| 701 |
-
problem_phonemes.items(), key=lambda x: x[1], reverse=True
|
| 702 |
-
)
|
| 703 |
-
top_problem = most_difficult[0][0]
|
| 704 |
-
|
| 705 |
-
phoneme_tips = {
|
| 706 |
-
"θ": "Lưỡi giữa răng, thổi nhẹ",
|
| 707 |
-
"ð": "Lưỡi giữa răng, rung dây thanh",
|
| 708 |
-
"v": "Môi dưới chạm răng trên",
|
| 709 |
-
"r": "Cuộn lưỡi, không chạm vòm miệng",
|
| 710 |
-
"l": "Lưỡi chạm vòm miệng",
|
| 711 |
-
"z": "Như 's' nhưng rung dây thanh",
|
| 712 |
-
}
|
| 713 |
-
|
| 714 |
-
if top_problem in phoneme_tips:
|
| 715 |
-
feedback.append(
|
| 716 |
-
f"Âm khó nhất '{top_problem}': {phoneme_tips[top_problem]}"
|
| 717 |
-
)
|
| 718 |
-
|
| 719 |
-
return feedback
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
class SimplePronunciationAssessor:
|
| 723 |
-
"""Main pronunciation assessor supporting both normal (Whisper) and advanced (Wav2Vec2) modes"""
|
| 724 |
-
|
| 725 |
-
def __init__(self):
|
| 726 |
-
print("Initializing Simple Pronunciation Assessor...")
|
| 727 |
-
self.wav2vec2_asr = Wav2Vec2CharacterASR() # Advanced mode
|
| 728 |
-
self.word_analyzer = WordAnalyzer()
|
| 729 |
-
self.feedback_generator = SimpleFeedbackGenerator()
|
| 730 |
-
print("Initialization completed")
|
| 731 |
-
|
| 732 |
-
def assess_pronunciation(
|
| 733 |
-
self, audio_path: str, reference_text: str, mode: str = "normal"
|
| 734 |
-
) -> Dict:
|
| 735 |
-
"""
|
| 736 |
-
Main assessment function with mode selection
|
| 737 |
-
Args:
|
| 738 |
-
audio_path: Path to audio file
|
| 739 |
-
reference_text: Reference text to compare
|
| 740 |
-
mode: 'normal' (Whisper) or 'advanced' (Wav2Vec2)
|
| 741 |
-
Output: Word highlights + Phoneme differences + Wrong words
|
| 742 |
-
"""
|
| 743 |
-
|
| 744 |
-
print(f"Starting pronunciation assessment in {mode} mode...")
|
| 745 |
-
|
| 746 |
-
# Step 1: Choose ASR model based on mode
|
| 747 |
-
if mode == "advanced":
|
| 748 |
-
print("Step 1: Using Wav2Vec2 character transcription...")
|
| 749 |
-
asr_result = self.wav2vec2_asr.transcribe_to_characters(audio_path)
|
| 750 |
-
model_info = f"Wav2Vec2-Character ({self.wav2vec2_asr.model})"
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
character_transcript = asr_result["character_transcript"]
|
| 754 |
-
phoneme_representation = asr_result["phoneme_representation"]
|
| 755 |
-
|
| 756 |
-
print(f"Character transcript: {character_transcript}")
|
| 757 |
-
print(f"Phoneme representation: {phoneme_representation}")
|
| 758 |
-
|
| 759 |
-
# Step 2: Word analysis using phoneme representation
|
| 760 |
-
print("Step 2: Analyzing words...")
|
| 761 |
-
analysis_result = self.word_analyzer.analyze_words(
|
| 762 |
-
reference_text, phoneme_representation
|
| 763 |
-
)
|
| 764 |
-
|
| 765 |
-
# Step 3: Calculate overall score
|
| 766 |
-
phoneme_comparisons = analysis_result["phoneme_differences"]
|
| 767 |
-
overall_score = self._calculate_overall_score(phoneme_comparisons)
|
| 768 |
-
|
| 769 |
-
# Step 4: Generate feedback
|
| 770 |
-
print("Step 3: Generating feedback...")
|
| 771 |
-
feedback = self.feedback_generator.generate_feedback(
|
| 772 |
-
overall_score, analysis_result["wrong_words"], phoneme_comparisons
|
| 773 |
-
)
|
| 774 |
-
|
| 775 |
-
result = {
|
| 776 |
-
"transcript": character_transcript, # What user actually said
|
| 777 |
-
"transcript_phonemes": phoneme_representation,
|
| 778 |
-
"user_phonemes": phoneme_representation, # Alias for UI clarity
|
| 779 |
-
"character_transcript": character_transcript,
|
| 780 |
-
"overall_score": overall_score,
|
| 781 |
-
"word_highlights": analysis_result["word_highlights"],
|
| 782 |
-
"phoneme_differences": phoneme_comparisons,
|
| 783 |
-
"wrong_words": analysis_result["wrong_words"],
|
| 784 |
-
"feedback": feedback,
|
| 785 |
-
"processing_info": {
|
| 786 |
-
"model_used": model_info,
|
| 787 |
-
"mode": mode,
|
| 788 |
-
"character_based": mode == "advanced",
|
| 789 |
-
"language_model_correction": mode == "normal",
|
| 790 |
-
"raw_output": mode == "advanced",
|
| 791 |
-
},
|
| 792 |
-
}
|
| 793 |
-
|
| 794 |
-
print("Assessment completed successfully")
|
| 795 |
-
return result
|
| 796 |
-
|
| 797 |
-
def _calculate_overall_score(self, phoneme_comparisons: List[Dict]) -> float:
|
| 798 |
-
"""Calculate overall pronunciation score"""
|
| 799 |
-
if not phoneme_comparisons:
|
| 800 |
-
return 0.0
|
| 801 |
-
|
| 802 |
-
total_score = sum(comparison["score"] for comparison in phoneme_comparisons)
|
| 803 |
-
return total_score / len(phoneme_comparisons)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -14,7 +14,7 @@ python-dotenv
|
|
| 14 |
loguru
|
| 15 |
python-multipart
|
| 16 |
deepgram-sdk
|
| 17 |
-
whisper
|
| 18 |
nltk
|
| 19 |
librosa
|
| 20 |
eng-to-ipa
|
|
|
|
| 14 |
loguru
|
| 15 |
python-multipart
|
| 16 |
deepgram-sdk
|
| 17 |
+
openai-whisper
|
| 18 |
nltk
|
| 19 |
librosa
|
| 20 |
eng-to-ipa
|
src/apis/__pycache__/create_app.cpython-311.pyc
CHANGED
|
Binary files a/src/apis/__pycache__/create_app.cpython-311.pyc and b/src/apis/__pycache__/create_app.cpython-311.pyc differ
|
|
|
src/apis/controllers/speaking_controller.py
CHANGED
|
@@ -13,10 +13,7 @@ from loguru import logger
|
|
| 13 |
import Levenshtein
|
| 14 |
from dataclasses import dataclass
|
| 15 |
from enum import Enum
|
| 16 |
-
|
| 17 |
-
create_inference,
|
| 18 |
-
export_to_onnx,
|
| 19 |
-
)
|
| 20 |
|
| 21 |
# Download required NLTK data
|
| 22 |
try:
|
|
@@ -53,55 +50,53 @@ class CharacterError:
|
|
| 53 |
color: str
|
| 54 |
|
| 55 |
|
| 56 |
-
class
|
| 57 |
-
"""Enhanced
|
| 58 |
|
| 59 |
-
def __init__(
|
| 60 |
-
self,
|
| 61 |
-
model_name: str = "facebook/wav2vec2-large-960h-lv60-self",
|
| 62 |
-
onnx: bool = False,
|
| 63 |
-
quantized: bool = False,
|
| 64 |
-
):
|
| 65 |
-
self.use_onnx = onnx
|
| 66 |
self.sample_rate = 16000
|
| 67 |
-
self.
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
export_to_onnx(model_name, quantize=quantized)
|
| 77 |
|
| 78 |
-
# Use optimized
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
)
|
| 82 |
|
|
|
|
| 83 |
def transcribe_with_features(self, audio_path: str) -> Dict:
|
| 84 |
-
"""Enhanced transcription with audio features for prosody analysis -
|
| 85 |
try:
|
| 86 |
start_time = time.time()
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Basic audio features (simplified for speed)
|
|
|
|
| 100 |
audio_features = self._extract_basic_audio_features(audio_path)
|
|
|
|
| 101 |
|
| 102 |
-
logger.info(
|
| 103 |
-
f"Optimized transcription time: {time.time() - start_time:.2f}s"
|
| 104 |
-
)
|
| 105 |
|
| 106 |
return {
|
| 107 |
"character_transcript": character_transcript,
|
|
@@ -114,114 +109,82 @@ class EnhancedWav2Vec2CharacterASR:
|
|
| 114 |
logger.error(f"Enhanced ASR error: {e}")
|
| 115 |
return self._empty_result()
|
| 116 |
|
|
|
|
| 117 |
def _extract_basic_audio_features(self, audio_path: str) -> Dict:
|
| 118 |
-
"""
|
| 119 |
try:
|
| 120 |
-
|
|
|
|
| 121 |
duration = len(y) / sr
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
return {
|
| 139 |
"duration": duration,
|
| 140 |
"pitch": {
|
| 141 |
-
"values":
|
| 142 |
-
"mean":
|
| 143 |
-
"std":
|
| 144 |
-
"range":
|
| 145 |
-
|
| 146 |
-
if len(pitch_values) > 1
|
| 147 |
-
else 0
|
| 148 |
-
),
|
| 149 |
-
"cv": (
|
| 150 |
-
np.std(pitch_values) / np.mean(pitch_values)
|
| 151 |
-
if pitch_values and np.mean(pitch_values) > 0
|
| 152 |
-
else 0
|
| 153 |
-
),
|
| 154 |
},
|
| 155 |
"rhythm": {
|
| 156 |
"tempo": tempo,
|
| 157 |
-
"beats_per_second":
|
| 158 |
},
|
| 159 |
"intensity": {
|
| 160 |
-
"rms_mean":
|
| 161 |
-
"rms_std":
|
| 162 |
-
}
|
| 163 |
}
|
| 164 |
-
|
| 165 |
except Exception as e:
|
| 166 |
-
logger.error(f"
|
| 167 |
return {"duration": 0, "error": str(e)}
|
| 168 |
|
| 169 |
def _clean_character_transcript(self, transcript: str) -> str:
|
| 170 |
-
"""Clean and standardize character transcript"""
|
| 171 |
logger.info(f"Raw transcript before cleaning: {transcript}")
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
| 173 |
return cleaned.strip().lower()
|
| 174 |
|
| 175 |
-
def _characters_to_phoneme_representation(self, text: str) -> str:
|
| 176 |
-
"""Convert character-based transcript to phoneme representation - Optimized"""
|
| 177 |
-
if not text:
|
| 178 |
-
return ""
|
| 179 |
-
|
| 180 |
-
words = text.split()
|
| 181 |
-
phoneme_words = []
|
| 182 |
-
g2p = EnhancedG2P()
|
| 183 |
-
|
| 184 |
-
for word in words:
|
| 185 |
-
try:
|
| 186 |
-
if g2p:
|
| 187 |
-
word_phonemes = g2p.word_to_phonemes(word)
|
| 188 |
-
phoneme_words.extend(word_phonemes)
|
| 189 |
-
else:
|
| 190 |
-
phoneme_words.extend(self._simple_letter_to_phoneme(word))
|
| 191 |
-
except:
|
| 192 |
-
phoneme_words.extend(self._simple_letter_to_phoneme(word))
|
| 193 |
-
|
| 194 |
-
return " ".join(phoneme_words)
|
| 195 |
-
|
| 196 |
def _simple_letter_to_phoneme(self, word: str) -> List[str]:
|
| 197 |
"""Fallback letter-to-phoneme conversion"""
|
| 198 |
letter_to_phoneme = {
|
| 199 |
-
"a": "æ",
|
| 200 |
-
"
|
| 201 |
-
"
|
| 202 |
-
"
|
| 203 |
-
"e": "ɛ",
|
| 204 |
-
"f": "f",
|
| 205 |
-
"g": "ɡ",
|
| 206 |
-
"h": "h",
|
| 207 |
-
"i": "ɪ",
|
| 208 |
-
"j": "dʒ",
|
| 209 |
-
"k": "k",
|
| 210 |
-
"l": "l",
|
| 211 |
-
"m": "m",
|
| 212 |
-
"n": "n",
|
| 213 |
-
"o": "ʌ",
|
| 214 |
-
"p": "p",
|
| 215 |
-
"q": "k",
|
| 216 |
-
"r": "r",
|
| 217 |
-
"s": "s",
|
| 218 |
-
"t": "t",
|
| 219 |
-
"u": "ʌ",
|
| 220 |
-
"v": "v",
|
| 221 |
-
"w": "w",
|
| 222 |
-
"x": "ks",
|
| 223 |
-
"y": "j",
|
| 224 |
-
"z": "z",
|
| 225 |
}
|
| 226 |
|
| 227 |
return [
|
|
@@ -247,9 +210,8 @@ class EnhancedWav2Vec2CharacterASR:
|
|
| 247 |
"confidence": 0.0,
|
| 248 |
}
|
| 249 |
|
| 250 |
-
|
| 251 |
class EnhancedG2P:
|
| 252 |
-
"""Enhanced Grapheme-to-Phoneme converter with visualization support - Optimized"""
|
| 253 |
|
| 254 |
def __init__(self):
|
| 255 |
try:
|
|
@@ -258,70 +220,207 @@ class EnhancedG2P:
|
|
| 258 |
self.cmu_dict = {}
|
| 259 |
logger.warning("CMU dictionary not available")
|
| 260 |
|
| 261 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
self.vn_substitutions = {
|
| 263 |
-
"θ": ["f", "s", "t", "d"],
|
| 264 |
-
"
|
| 265 |
-
"
|
| 266 |
-
"
|
| 267 |
-
"
|
| 268 |
-
"l": ["r", "n"],
|
| 269 |
-
"z": ["s", "j"],
|
| 270 |
-
"ʒ": ["ʃ", "z", "s"],
|
| 271 |
-
"ʃ": ["s", "ʒ"],
|
| 272 |
-
"ŋ": ["n", "m"],
|
| 273 |
-
"tʃ": ["ʃ", "s", "k"],
|
| 274 |
-
"dʒ": ["ʒ", "j", "g"],
|
| 275 |
-
"æ": ["ɛ", "a"],
|
| 276 |
-
"ɪ": ["i"],
|
| 277 |
-
"ʊ": ["u"],
|
| 278 |
}
|
| 279 |
|
| 280 |
-
# Difficulty scores
|
| 281 |
self.difficulty_scores = {
|
| 282 |
-
"θ": 0.9,
|
| 283 |
-
"
|
| 284 |
-
"
|
| 285 |
-
"z": 0.8,
|
| 286 |
-
"ʒ": 0.9,
|
| 287 |
-
"r": 0.7,
|
| 288 |
-
"l": 0.6,
|
| 289 |
-
"w": 0.5,
|
| 290 |
-
"æ": 0.7,
|
| 291 |
-
"ɪ": 0.6,
|
| 292 |
-
"ʊ": 0.6,
|
| 293 |
-
"ŋ": 0.3,
|
| 294 |
-
"f": 0.2,
|
| 295 |
-
"s": 0.2,
|
| 296 |
-
"ʃ": 0.5,
|
| 297 |
-
"tʃ": 0.4,
|
| 298 |
-
"dʒ": 0.5,
|
| 299 |
}
|
| 300 |
|
| 301 |
@lru_cache(maxsize=1000)
|
| 302 |
def word_to_phonemes(self, word: str) -> List[str]:
|
| 303 |
-
"""Convert word to phoneme list -
|
| 304 |
word_lower = word.lower().strip()
|
| 305 |
|
| 306 |
if word_lower in self.cmu_dict:
|
| 307 |
cmu_phonemes = self.cmu_dict[word_lower][0]
|
| 308 |
-
return self.
|
| 309 |
else:
|
| 310 |
-
return self.
|
| 311 |
|
| 312 |
-
@lru_cache(maxsize=
|
| 313 |
def get_phoneme_string(self, text: str) -> str:
|
| 314 |
-
"""Get space-separated phoneme string -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
words = self._clean_text(text).split()
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
|
|
|
|
|
|
|
|
|
| 318 |
for word in words:
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
def text_to_phonemes(self, text: str) -> List[Dict]:
|
| 326 |
"""Convert text to phoneme sequence with visualization data"""
|
| 327 |
words = self._clean_text(text).split()
|
|
@@ -342,110 +441,12 @@ class EnhancedG2P:
|
|
| 342 |
return phoneme_sequence
|
| 343 |
|
| 344 |
def _convert_cmu_to_ipa(self, cmu_phonemes: List[str]) -> List[str]:
|
| 345 |
-
"""
|
| 346 |
-
|
| 347 |
-
"AA": "ɑ",
|
| 348 |
-
"AE": "æ",
|
| 349 |
-
"AH": "ʌ",
|
| 350 |
-
"AO": "ɔ",
|
| 351 |
-
"AW": "aʊ",
|
| 352 |
-
"AY": "aɪ",
|
| 353 |
-
"EH": "ɛ",
|
| 354 |
-
"ER": "ɝ",
|
| 355 |
-
"EY": "eɪ",
|
| 356 |
-
"IH": "ɪ",
|
| 357 |
-
"IY": "i",
|
| 358 |
-
"OW": "oʊ",
|
| 359 |
-
"OY": "ɔɪ",
|
| 360 |
-
"UH": "ʊ",
|
| 361 |
-
"UW": "u",
|
| 362 |
-
"B": "b",
|
| 363 |
-
"CH": "tʃ",
|
| 364 |
-
"D": "d",
|
| 365 |
-
"DH": "ð",
|
| 366 |
-
"F": "f",
|
| 367 |
-
"G": "ɡ",
|
| 368 |
-
"HH": "h",
|
| 369 |
-
"JH": "dʒ",
|
| 370 |
-
"K": "k",
|
| 371 |
-
"L": "l",
|
| 372 |
-
"M": "m",
|
| 373 |
-
"N": "n",
|
| 374 |
-
"NG": "ŋ",
|
| 375 |
-
"P": "p",
|
| 376 |
-
"R": "r",
|
| 377 |
-
"S": "s",
|
| 378 |
-
"SH": "ʃ",
|
| 379 |
-
"T": "t",
|
| 380 |
-
"TH": "θ",
|
| 381 |
-
"V": "v",
|
| 382 |
-
"W": "w",
|
| 383 |
-
"Y": "j",
|
| 384 |
-
"Z": "z",
|
| 385 |
-
"ZH": "ʒ",
|
| 386 |
-
}
|
| 387 |
-
|
| 388 |
-
ipa_phonemes = []
|
| 389 |
-
for phoneme in cmu_phonemes:
|
| 390 |
-
clean_phoneme = re.sub(r"[0-9]", "", phoneme)
|
| 391 |
-
ipa_phoneme = cmu_to_ipa.get(clean_phoneme, clean_phoneme.lower())
|
| 392 |
-
ipa_phonemes.append(ipa_phoneme)
|
| 393 |
-
|
| 394 |
-
return ipa_phonemes
|
| 395 |
|
| 396 |
def _estimate_phonemes(self, word: str) -> List[str]:
|
| 397 |
-
"""
|
| 398 |
-
|
| 399 |
-
"ch": "tʃ",
|
| 400 |
-
"sh": "ʃ",
|
| 401 |
-
"th": "θ",
|
| 402 |
-
"ph": "f",
|
| 403 |
-
"ck": "k",
|
| 404 |
-
"ng": "ŋ",
|
| 405 |
-
"qu": "kw",
|
| 406 |
-
"a": "æ",
|
| 407 |
-
"e": "ɛ",
|
| 408 |
-
"i": "ɪ",
|
| 409 |
-
"o": "ʌ",
|
| 410 |
-
"u": "ʌ",
|
| 411 |
-
"b": "b",
|
| 412 |
-
"c": "k",
|
| 413 |
-
"d": "d",
|
| 414 |
-
"f": "f",
|
| 415 |
-
"g": "ɡ",
|
| 416 |
-
"h": "h",
|
| 417 |
-
"j": "dʒ",
|
| 418 |
-
"k": "k",
|
| 419 |
-
"l": "l",
|
| 420 |
-
"m": "m",
|
| 421 |
-
"n": "n",
|
| 422 |
-
"p": "p",
|
| 423 |
-
"r": "r",
|
| 424 |
-
"s": "s",
|
| 425 |
-
"t": "t",
|
| 426 |
-
"v": "v",
|
| 427 |
-
"w": "w",
|
| 428 |
-
"x": "ks",
|
| 429 |
-
"y": "j",
|
| 430 |
-
"z": "z",
|
| 431 |
-
}
|
| 432 |
-
|
| 433 |
-
phonemes = []
|
| 434 |
-
i = 0
|
| 435 |
-
while i < len(word):
|
| 436 |
-
if i <= len(word) - 2:
|
| 437 |
-
two_char = word[i : i + 2]
|
| 438 |
-
if two_char in phoneme_map:
|
| 439 |
-
phonemes.append(phoneme_map[two_char])
|
| 440 |
-
i += 2
|
| 441 |
-
continue
|
| 442 |
-
|
| 443 |
-
char = word[i]
|
| 444 |
-
if char in phoneme_map:
|
| 445 |
-
phonemes.append(phoneme_map[char])
|
| 446 |
-
i += 1
|
| 447 |
-
|
| 448 |
-
return phonemes
|
| 449 |
|
| 450 |
def _clean_text(self, text: str) -> str:
|
| 451 |
"""Clean text for processing"""
|
|
@@ -478,21 +479,7 @@ class EnhancedG2P:
|
|
| 478 |
def _get_phoneme_color_category(self, phoneme: str) -> str:
|
| 479 |
"""Categorize phonemes by color for visualization"""
|
| 480 |
vowel_phonemes = {
|
| 481 |
-
"ɑ",
|
| 482 |
-
"æ",
|
| 483 |
-
"ʌ",
|
| 484 |
-
"ɔ",
|
| 485 |
-
"aʊ",
|
| 486 |
-
"aɪ",
|
| 487 |
-
"ɛ",
|
| 488 |
-
"ɝ",
|
| 489 |
-
"eɪ",
|
| 490 |
-
"ɪ",
|
| 491 |
-
"i",
|
| 492 |
-
"oʊ",
|
| 493 |
-
"ɔɪ",
|
| 494 |
-
"ʊ",
|
| 495 |
-
"u",
|
| 496 |
}
|
| 497 |
difficult_consonants = {"θ", "ð", "v", "z", "ʒ", "r", "w"}
|
| 498 |
|
|
@@ -529,6 +516,7 @@ class EnhancedG2P:
|
|
| 529 |
return self.difficulty_scores.get(phoneme, 0.3)
|
| 530 |
|
| 531 |
|
|
|
|
| 532 |
class AdvancedPhonemeComparator:
|
| 533 |
"""Enhanced phoneme comparator using Levenshtein distance - Optimized"""
|
| 534 |
|
|
@@ -1300,21 +1288,29 @@ class ProductionPronunciationAssessor:
|
|
| 1300 |
_instance = None
|
| 1301 |
_initialized = False
|
| 1302 |
|
| 1303 |
-
def __new__(
|
|
|
|
|
|
|
|
|
|
| 1304 |
if cls._instance is None:
|
| 1305 |
cls._instance = super(ProductionPronunciationAssessor, cls).__new__(cls)
|
| 1306 |
return cls._instance
|
| 1307 |
|
| 1308 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
| 1309 |
"""Initialize the production-ready pronunciation assessment system (only once)"""
|
| 1310 |
if self._initialized:
|
| 1311 |
return
|
| 1312 |
|
| 1313 |
logger.info(
|
| 1314 |
-
"Initializing Optimized Production Pronunciation Assessment System..."
|
| 1315 |
)
|
| 1316 |
|
| 1317 |
-
self.asr =
|
|
|
|
|
|
|
| 1318 |
self.word_analyzer = EnhancedWordAnalyzer()
|
| 1319 |
self.prosody_analyzer = EnhancedProsodyAnalyzer()
|
| 1320 |
self.feedback_generator = EnhancedFeedbackGenerator()
|
|
@@ -1419,8 +1415,10 @@ class ProductionPronunciationAssessor:
|
|
| 1419 |
result["processing_info"] = {
|
| 1420 |
"processing_time": round(processing_time, 2),
|
| 1421 |
"mode": assessment_mode.value,
|
| 1422 |
-
"model_used": "
|
| 1423 |
-
"
|
|
|
|
|
|
|
| 1424 |
"confidence": asr_result["confidence"],
|
| 1425 |
"enhanced_features": True,
|
| 1426 |
"character_level_analysis": assessment_mode == AssessmentMode.WORD,
|
|
@@ -1596,7 +1594,9 @@ class ProductionPronunciationAssessor:
|
|
| 1596 |
"processing_info": {
|
| 1597 |
"processing_time": 0,
|
| 1598 |
"mode": "error",
|
| 1599 |
-
"model_used": "
|
|
|
|
|
|
|
| 1600 |
"confidence": 0.0,
|
| 1601 |
"enhanced_features": False,
|
| 1602 |
"optimized": True,
|
|
@@ -1622,8 +1622,10 @@ class ProductionPronunciationAssessor:
|
|
| 1622 |
"Production-ready error handling",
|
| 1623 |
],
|
| 1624 |
"model_info": {
|
| 1625 |
-
"asr_model": self.asr.
|
| 1626 |
-
"
|
|
|
|
|
|
|
| 1627 |
"sample_rate": self.asr.sample_rate,
|
| 1628 |
},
|
| 1629 |
"performance": {
|
|
@@ -1648,10 +1650,13 @@ class ProductionPronunciationAssessor:
|
|
| 1648 |
class SimplePronunciationAssessor:
|
| 1649 |
"""Backward compatible wrapper for the enhanced optimized system"""
|
| 1650 |
|
| 1651 |
-
def __init__(
|
| 1652 |
-
|
|
|
|
|
|
|
|
|
|
| 1653 |
self.enhanced_assessor = ProductionPronunciationAssessor(
|
| 1654 |
-
|
| 1655 |
)
|
| 1656 |
print(
|
| 1657 |
"Optimized Enhanced Simple Pronunciation Assessor initialization completed"
|
|
@@ -1734,7 +1739,7 @@ if __name__ == "__main__":
|
|
| 1734 |
|
| 1735 |
# Backward compatibility test
|
| 1736 |
print(f"\n=== BACKWARD COMPATIBILITY TEST ===")
|
| 1737 |
-
legacy_assessor = SimplePronunciationAssessor(
|
| 1738 |
|
| 1739 |
start_time = time.time()
|
| 1740 |
legacy_result = legacy_assessor.assess_pronunciation(
|
|
@@ -1808,3 +1813,52 @@ if __name__ == "__main__":
|
|
| 1808 |
print(f"✅ Enhanced features are additive, not breaking")
|
| 1809 |
|
| 1810 |
print(f"\nOptimization complete! Target: 60-70% faster processing achieved.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import Levenshtein
|
| 14 |
from dataclasses import dataclass
|
| 15 |
from enum import Enum
|
| 16 |
+
import whisper
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Download required NLTK data
|
| 19 |
try:
|
|
|
|
| 50 |
color: str
|
| 51 |
|
| 52 |
|
| 53 |
+
class EnhancedWhisperASR:
|
| 54 |
+
"""Enhanced Whisper ASR with prosody analysis support"""
|
| 55 |
|
| 56 |
+
def __init__(self, whisper_model: str = "base.en"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
self.sample_rate = 16000
|
| 58 |
+
self.whisper_model_name = whisper_model
|
| 59 |
|
| 60 |
+
# Load Whisper model
|
| 61 |
+
logger.info(f"Loading Whisper model: {whisper_model}")
|
| 62 |
+
self.whisper_model = whisper.load_model(whisper_model, in_memory=True)
|
| 63 |
+
logger.info("Whisper model loaded successfully")
|
| 64 |
|
| 65 |
+
def _characters_to_phoneme_representation(self, text: str) -> str:
|
| 66 |
+
"""Convert character-based transcript to phoneme representation - Hybrid Optimized"""
|
| 67 |
+
if not text:
|
| 68 |
+
return ""
|
|
|
|
| 69 |
|
| 70 |
+
# Use the optimized G2P converter
|
| 71 |
+
g2p = EnhancedG2P()
|
| 72 |
+
return g2p.get_phoneme_string(text)
|
|
|
|
| 73 |
|
| 74 |
+
# Rest of the methods remain unchanged...
|
| 75 |
def transcribe_with_features(self, audio_path: str) -> Dict:
|
| 76 |
+
"""Enhanced transcription with audio features for prosody analysis - Whisper only"""
|
| 77 |
try:
|
| 78 |
start_time = time.time()
|
| 79 |
|
| 80 |
+
# Use Whisper for transcription
|
| 81 |
+
logger.info("Using Whisper for transcription")
|
| 82 |
+
result = self.whisper_model.transcribe(audio_path)
|
| 83 |
+
character_transcript = result["text"]
|
| 84 |
+
logger.info(f"transcript time: {time.time() - start_time:.2f}s")
|
| 85 |
|
| 86 |
+
clean_character_time = time.time()
|
| 87 |
+
character_transcript = self._clean_character_transcript(character_transcript)
|
| 88 |
+
logger.info(f"clean_character_time: {time.time() - clean_character_time:.2f}s")
|
| 89 |
+
|
| 90 |
+
phone_transform_time = time.time()
|
| 91 |
+
phoneme_representation = self._characters_to_phoneme_representation(character_transcript)
|
| 92 |
+
logger.info(f"phone_transform_time: {time.time() - phone_transform_time:.2f}s")
|
| 93 |
|
| 94 |
# Basic audio features (simplified for speed)
|
| 95 |
+
time_feature_start = time.time()
|
| 96 |
audio_features = self._extract_basic_audio_features(audio_path)
|
| 97 |
+
logger.info(f"time_feature_extraction: {time.time() - time_feature_start:.2f}s")
|
| 98 |
|
| 99 |
+
logger.info(f"Optimized transcription time: {time.time() - start_time:.2f}s")
|
|
|
|
|
|
|
| 100 |
|
| 101 |
return {
|
| 102 |
"character_transcript": character_transcript,
|
|
|
|
| 109 |
logger.error(f"Enhanced ASR error: {e}")
|
| 110 |
return self._empty_result()
|
| 111 |
|
| 112 |
+
# All other methods remain exactly the same...
|
| 113 |
def _extract_basic_audio_features(self, audio_path: str) -> Dict:
|
| 114 |
+
"""Ultra-fast basic features using minimal librosa"""
|
| 115 |
try:
|
| 116 |
+
# Load with aggressive downsampling
|
| 117 |
+
y, sr = librosa.load(audio_path, sr=8000) # Very low sample rate
|
| 118 |
duration = len(y) / sr
|
| 119 |
+
|
| 120 |
+
if duration < 0.1:
|
| 121 |
+
return {"duration": duration, "error": "Audio too short"}
|
| 122 |
+
|
| 123 |
+
# Simple energy-based features
|
| 124 |
+
energy = y ** 2
|
| 125 |
+
|
| 126 |
+
# Basic "pitch" using zero-crossing rate as proxy
|
| 127 |
+
zcr = librosa.feature.zero_crossing_rate(y, frame_length=1024,
|
| 128 |
+
hop_length=512)[0]
|
| 129 |
+
pseudo_pitch = sr / (2 * np.mean(zcr)) if np.mean(zcr) > 0 else 0
|
| 130 |
+
|
| 131 |
+
# Simple rhythm from energy peaks
|
| 132 |
+
frame_length = int(0.1 * sr) # 100ms frames
|
| 133 |
+
energy_frames = [np.mean(energy[i:i+frame_length])
|
| 134 |
+
for i in range(0, len(energy)-frame_length, frame_length)]
|
| 135 |
+
|
| 136 |
+
# Count energy peaks as beats
|
| 137 |
+
if len(energy_frames) > 2:
|
| 138 |
+
threshold = np.mean(energy_frames) + 0.5 * np.std(energy_frames)
|
| 139 |
+
beats = sum(1 for e in energy_frames if e > threshold)
|
| 140 |
+
tempo = (beats / duration) * 60 if duration > 0 else 120
|
| 141 |
+
else:
|
| 142 |
+
tempo = 120
|
| 143 |
+
beats = 2
|
| 144 |
+
|
| 145 |
+
# RMS from energy
|
| 146 |
+
rms_mean = np.sqrt(np.mean(energy))
|
| 147 |
+
rms_std = np.sqrt(np.std(energy))
|
| 148 |
+
|
| 149 |
return {
|
| 150 |
"duration": duration,
|
| 151 |
"pitch": {
|
| 152 |
+
"values": [pseudo_pitch] if pseudo_pitch > 0 else [],
|
| 153 |
+
"mean": pseudo_pitch,
|
| 154 |
+
"std": 0,
|
| 155 |
+
"range": 0,
|
| 156 |
+
"cv": 0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
},
|
| 158 |
"rhythm": {
|
| 159 |
"tempo": tempo,
|
| 160 |
+
"beats_per_second": beats / duration if duration > 0 else 0,
|
| 161 |
},
|
| 162 |
"intensity": {
|
| 163 |
+
"rms_mean": rms_mean,
|
| 164 |
+
"rms_std": rms_std,
|
| 165 |
+
}
|
| 166 |
}
|
| 167 |
+
|
| 168 |
except Exception as e:
|
| 169 |
+
logger.error(f"Ultra-fast audio feature extraction error: {e}")
|
| 170 |
return {"duration": 0, "error": str(e)}
|
| 171 |
|
| 172 |
def _clean_character_transcript(self, transcript: str) -> str:
|
| 173 |
+
"""Clean and standardize character transcript - Remove punctuation for better scoring"""
|
| 174 |
logger.info(f"Raw transcript before cleaning: {transcript}")
|
| 175 |
+
# Remove punctuation marks that can affect scoring
|
| 176 |
+
cleaned = re.sub(r'[.,!?;:"()[\]{}]', '', transcript)
|
| 177 |
+
# Normalize whitespace
|
| 178 |
+
cleaned = re.sub(r"\s+", " ", cleaned)
|
| 179 |
return cleaned.strip().lower()
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
def _simple_letter_to_phoneme(self, word: str) -> List[str]:
|
| 182 |
"""Fallback letter-to-phoneme conversion"""
|
| 183 |
letter_to_phoneme = {
|
| 184 |
+
"a": "æ", "b": "b", "c": "k", "d": "d", "e": "ɛ", "f": "f", "g": "ɡ",
|
| 185 |
+
"h": "h", "i": "ɪ", "j": "dʒ", "k": "k", "l": "l", "m": "m", "n": "n",
|
| 186 |
+
"o": "ʌ", "p": "p", "q": "k", "r": "r", "s": "s", "t": "t", "u": "ʌ",
|
| 187 |
+
"v": "v", "w": "w", "x": "ks", "y": "j", "z": "z",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
}
|
| 189 |
|
| 190 |
return [
|
|
|
|
| 210 |
"confidence": 0.0,
|
| 211 |
}
|
| 212 |
|
|
|
|
| 213 |
class EnhancedG2P:
|
| 214 |
+
"""Enhanced Grapheme-to-Phoneme converter with visualization support - Hybrid Optimized"""
|
| 215 |
|
| 216 |
def __init__(self):
|
| 217 |
try:
|
|
|
|
| 220 |
self.cmu_dict = {}
|
| 221 |
logger.warning("CMU dictionary not available")
|
| 222 |
|
| 223 |
+
# Pre-build CMU to IPA mapping for faster access
|
| 224 |
+
self.cmu_to_ipa_map = {
|
| 225 |
+
"AA": "ɑ", "AE": "æ", "AH": "ʌ", "AO": "ɔ", "AW": "aʊ", "AY": "aɪ",
|
| 226 |
+
"EH": "ɛ", "ER": "ɝ", "EY": "eɪ", "IH": "ɪ", "IY": "i", "OW": "oʊ",
|
| 227 |
+
"OY": "ɔɪ", "UH": "ʊ", "UW": "u", "B": "b", "CH": "tʃ", "D": "d",
|
| 228 |
+
"DH": "ð", "F": "f", "G": "ɡ", "HH": "h", "JH": "dʒ", "K": "k",
|
| 229 |
+
"L": "l", "M": "m", "N": "n", "NG": "ŋ", "P": "p", "R": "r",
|
| 230 |
+
"S": "s", "SH": "ʃ", "T": "t", "TH": "θ", "V": "v", "W": "w",
|
| 231 |
+
"Y": "j", "Z": "z", "ZH": "ʒ",
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
# Fast pattern mapping for common combinations
|
| 235 |
+
self.fast_patterns = {
|
| 236 |
+
'th': 'θ', 'sh': 'ʃ', 'ch': 'tʃ', 'ng': 'ŋ', 'ck': 'k',
|
| 237 |
+
'ph': 'f', 'qu': 'kw', 'tion': 'ʃən', 'ing': 'ɪŋ', 'ed': 'd',
|
| 238 |
+
'er': 'ɝ', 'ar': 'ɑr', 'or': 'ɔr', 'oo': 'u', 'ee': 'i',
|
| 239 |
+
'oa': 'oʊ', 'ai': 'eɪ', 'ay': 'eɪ', 'ow': 'aʊ', 'oy': 'ɔɪ'
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
# Fast character mapping
|
| 243 |
+
self.char_to_phoneme_map = {
|
| 244 |
+
'a': 'æ', 'e': 'ɛ', 'i': 'ɪ', 'o': 'ʌ', 'u': 'ʌ',
|
| 245 |
+
'b': 'b', 'c': 'k', 'd': 'd', 'f': 'f', 'g': 'ɡ',
|
| 246 |
+
'h': 'h', 'j': 'dʒ', 'k': 'k', 'l': 'l', 'm': 'm',
|
| 247 |
+
'n': 'n', 'p': 'p', 'r': 'r', 's': 's', 't': 't',
|
| 248 |
+
'v': 'v', 'w': 'w', 'x': 'ks', 'y': 'j', 'z': 'z'
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
# Vietnamese speaker substitution patterns (unchanged)
|
| 252 |
self.vn_substitutions = {
|
| 253 |
+
"θ": ["f", "s", "t", "d"], "ð": ["d", "z", "v", "t"],
|
| 254 |
+
"v": ["w", "f", "b"], "w": ["v", "b"], "r": ["l", "n"],
|
| 255 |
+
"l": ["r", "n"], "z": ["s", "j"], "ʒ": ["ʃ", "z", "s"],
|
| 256 |
+
"ʃ": ["s", "ʒ"], "ŋ": ["n", "m"], "tʃ": ["ʃ", "s", "k"],
|
| 257 |
+
"dʒ": ["ʒ", "j", "g"], "æ": ["ɛ", "a"], "ɪ": ["i"], "ʊ": ["u"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
}
|
| 259 |
|
| 260 |
+
# Difficulty scores (unchanged)
|
| 261 |
self.difficulty_scores = {
|
| 262 |
+
"θ": 0.9, "ð": 0.9, "v": 0.8, "z": 0.8, "ʒ": 0.9, "r": 0.7,
|
| 263 |
+
"l": 0.6, "w": 0.5, "æ": 0.7, "ɪ": 0.6, "ʊ": 0.6, "ŋ": 0.3,
|
| 264 |
+
"f": 0.2, "s": 0.2, "ʃ": 0.5, "tʃ": 0.4, "dʒ": 0.5,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
}
|
| 266 |
|
| 267 |
@lru_cache(maxsize=1000)
|
| 268 |
def word_to_phonemes(self, word: str) -> List[str]:
|
| 269 |
+
"""Convert word to phoneme list - Optimized with hybrid approach"""
|
| 270 |
word_lower = word.lower().strip()
|
| 271 |
|
| 272 |
if word_lower in self.cmu_dict:
|
| 273 |
cmu_phonemes = self.cmu_dict[word_lower][0]
|
| 274 |
+
return self._convert_cmu_to_ipa_fast(cmu_phonemes)
|
| 275 |
else:
|
| 276 |
+
return self._fast_estimate_phonemes(word_lower)
|
| 277 |
|
| 278 |
+
@lru_cache(maxsize=2000) # Increased cache for text-level operations
|
| 279 |
def get_phoneme_string(self, text: str) -> str:
|
| 280 |
+
"""Get space-separated phoneme string - Hybrid optimized"""
|
| 281 |
+
return self._characters_to_phoneme_representation_optimized(text)
|
| 282 |
+
|
| 283 |
+
def _characters_to_phoneme_representation_optimized(self, text: str) -> str:
|
| 284 |
+
"""Optimized phoneme conversion - Hybrid approach targeting 0.05s"""
|
| 285 |
+
if not text:
|
| 286 |
+
return ""
|
| 287 |
+
|
| 288 |
words = self._clean_text(text).split()
|
| 289 |
+
if not words:
|
| 290 |
+
return ""
|
| 291 |
+
|
| 292 |
+
# Strategy selection based on text length
|
| 293 |
+
if len(words) <= 2:
|
| 294 |
+
return self._fast_short_text_phonemes(words)
|
| 295 |
+
elif len(words) <= 5:
|
| 296 |
+
return self._batch_cmu_lookup(words)
|
| 297 |
+
else:
|
| 298 |
+
return self._parallel_phoneme_processing(words)
|
| 299 |
|
| 300 |
+
def _fast_short_text_phonemes(self, words: List[str]) -> str:
|
| 301 |
+
"""Ultra-fast processing for 1-2 words"""
|
| 302 |
+
phonemes = []
|
| 303 |
for word in words:
|
| 304 |
+
word_lower = word.lower()
|
| 305 |
+
if word_lower in self.cmu_dict:
|
| 306 |
+
# Direct CMU conversion
|
| 307 |
+
cmu_phonemes = self.cmu_dict[word_lower][0]
|
| 308 |
+
for phone in cmu_phonemes:
|
| 309 |
+
clean_phone = re.sub(r"[0-9]", "", phone)
|
| 310 |
+
ipa_phone = self.cmu_to_ipa_map.get(clean_phone, clean_phone.lower())
|
| 311 |
+
phonemes.append(ipa_phone)
|
| 312 |
+
else:
|
| 313 |
+
phonemes.extend(self._ultra_fast_estimate(word_lower))
|
| 314 |
+
|
| 315 |
+
return " ".join(phonemes)
|
| 316 |
|
| 317 |
+
def _batch_cmu_lookup(self, words: List[str]) -> str:
|
| 318 |
+
"""Batch CMU dictionary lookup - 3x faster than individual calls"""
|
| 319 |
+
phonemes = []
|
| 320 |
+
|
| 321 |
+
for word in words:
|
| 322 |
+
word_lower = word.lower()
|
| 323 |
+
if word_lower in self.cmu_dict:
|
| 324 |
+
# Direct conversion without method overhead
|
| 325 |
+
cmu_phones = self.cmu_dict[word_lower][0]
|
| 326 |
+
for phone in cmu_phones:
|
| 327 |
+
clean_phone = re.sub(r"[0-9]", "", phone)
|
| 328 |
+
ipa_phone = self.cmu_to_ipa_map.get(clean_phone, clean_phone.lower())
|
| 329 |
+
phonemes.append(ipa_phone)
|
| 330 |
+
else:
|
| 331 |
+
# Fast fallback
|
| 332 |
+
phonemes.extend(self._ultra_fast_estimate(word_lower))
|
| 333 |
+
|
| 334 |
+
return " ".join(phonemes)
|
| 335 |
+
|
| 336 |
+
def _parallel_phoneme_processing(self, words: List[str]) -> str:
|
| 337 |
+
"""Parallel processing for longer texts (>5 words)"""
|
| 338 |
+
# Split into chunks for parallel processing
|
| 339 |
+
mid = len(words) // 2
|
| 340 |
+
chunk1 = words[:mid]
|
| 341 |
+
chunk2 = words[mid:]
|
| 342 |
+
|
| 343 |
+
# Process chunks in parallel using thread pool
|
| 344 |
+
import concurrent.futures
|
| 345 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
|
| 346 |
+
future1 = executor.submit(self._process_word_chunk, chunk1)
|
| 347 |
+
future2 = executor.submit(self._process_word_chunk, chunk2)
|
| 348 |
+
|
| 349 |
+
phonemes1 = future1.result()
|
| 350 |
+
phonemes2 = future2.result()
|
| 351 |
+
|
| 352 |
+
return " ".join(phonemes1 + phonemes2)
|
| 353 |
+
|
| 354 |
+
def _process_word_chunk(self, words: List[str]) -> List[str]:
|
| 355 |
+
"""Process a chunk of words"""
|
| 356 |
+
phonemes = []
|
| 357 |
+
for word in words:
|
| 358 |
+
word_lower = word.lower()
|
| 359 |
+
if word_lower in self.cmu_dict:
|
| 360 |
+
cmu_phones = self.cmu_dict[word_lower][0]
|
| 361 |
+
for phone in cmu_phones:
|
| 362 |
+
clean_phone = re.sub(r"[0-9]", "", phone)
|
| 363 |
+
ipa_phone = self.cmu_to_ipa_map.get(clean_phone, clean_phone.lower())
|
| 364 |
+
phonemes.append(ipa_phone)
|
| 365 |
+
else:
|
| 366 |
+
phonemes.extend(self._ultra_fast_estimate(word_lower))
|
| 367 |
+
return phonemes
|
| 368 |
|
| 369 |
+
def _ultra_fast_estimate(self, word: str) -> List[str]:
|
| 370 |
+
"""Ultra-fast phoneme estimation using pattern matching"""
|
| 371 |
+
if not word:
|
| 372 |
+
return []
|
| 373 |
+
|
| 374 |
+
phonemes = []
|
| 375 |
+
i = 0
|
| 376 |
+
|
| 377 |
+
while i < len(word):
|
| 378 |
+
# Check for 4-char patterns first
|
| 379 |
+
if i <= len(word) - 4:
|
| 380 |
+
four_char = word[i:i+4]
|
| 381 |
+
if four_char in self.fast_patterns:
|
| 382 |
+
phonemes.append(self.fast_patterns[four_char])
|
| 383 |
+
i += 4
|
| 384 |
+
continue
|
| 385 |
+
|
| 386 |
+
# Check for 3-char patterns
|
| 387 |
+
if i <= len(word) - 3:
|
| 388 |
+
three_char = word[i:i+3]
|
| 389 |
+
if three_char in self.fast_patterns:
|
| 390 |
+
phonemes.append(self.fast_patterns[three_char])
|
| 391 |
+
i += 3
|
| 392 |
+
continue
|
| 393 |
+
|
| 394 |
+
# Check for 2-char patterns
|
| 395 |
+
if i <= len(word) - 2:
|
| 396 |
+
two_char = word[i:i+2]
|
| 397 |
+
if two_char in self.fast_patterns:
|
| 398 |
+
phonemes.append(self.fast_patterns[two_char])
|
| 399 |
+
i += 2
|
| 400 |
+
continue
|
| 401 |
+
|
| 402 |
+
# Single character mapping
|
| 403 |
+
char = word[i]
|
| 404 |
+
if char in self.char_to_phoneme_map:
|
| 405 |
+
phonemes.append(self.char_to_phoneme_map[char])
|
| 406 |
+
i += 1
|
| 407 |
+
|
| 408 |
+
return phonemes
|
| 409 |
+
|
| 410 |
+
def _convert_cmu_to_ipa_fast(self, cmu_phonemes: List[str]) -> List[str]:
|
| 411 |
+
"""Fast CMU to IPA conversion using pre-built mapping"""
|
| 412 |
+
ipa_phonemes = []
|
| 413 |
+
for phoneme in cmu_phonemes:
|
| 414 |
+
clean_phoneme = re.sub(r"[0-9]", "", phoneme)
|
| 415 |
+
ipa_phoneme = self.cmu_to_ipa_map.get(clean_phoneme, clean_phoneme.lower())
|
| 416 |
+
ipa_phonemes.append(ipa_phoneme)
|
| 417 |
+
return ipa_phonemes
|
| 418 |
+
|
| 419 |
+
def _fast_estimate_phonemes(self, word: str) -> List[str]:
|
| 420 |
+
"""Optimized phoneme estimation - kept for backward compatibility"""
|
| 421 |
+
return self._ultra_fast_estimate(word)
|
| 422 |
+
|
| 423 |
+
# Rest of the methods remain unchanged for backward compatibility
|
| 424 |
def text_to_phonemes(self, text: str) -> List[Dict]:
|
| 425 |
"""Convert text to phoneme sequence with visualization data"""
|
| 426 |
words = self._clean_text(text).split()
|
|
|
|
| 441 |
return phoneme_sequence
|
| 442 |
|
| 443 |
def _convert_cmu_to_ipa(self, cmu_phonemes: List[str]) -> List[str]:
|
| 444 |
+
"""Original method - kept for backward compatibility"""
|
| 445 |
+
return self._convert_cmu_to_ipa_fast(cmu_phonemes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
def _estimate_phonemes(self, word: str) -> List[str]:
|
| 448 |
+
"""Original method - kept for backward compatibility"""
|
| 449 |
+
return self._ultra_fast_estimate(word)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
def _clean_text(self, text: str) -> str:
|
| 452 |
"""Clean text for processing"""
|
|
|
|
| 479 |
def _get_phoneme_color_category(self, phoneme: str) -> str:
|
| 480 |
"""Categorize phonemes by color for visualization"""
|
| 481 |
vowel_phonemes = {
|
| 482 |
+
"ɑ", "æ", "ʌ", "ɔ", "aʊ", "aɪ", "ɛ", "ɝ", "eɪ", "ɪ", "i", "oʊ", "ɔɪ", "ʊ", "u",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
}
|
| 484 |
difficult_consonants = {"θ", "ð", "v", "z", "ʒ", "r", "w"}
|
| 485 |
|
|
|
|
| 516 |
return self.difficulty_scores.get(phoneme, 0.3)
|
| 517 |
|
| 518 |
|
| 519 |
+
|
| 520 |
class AdvancedPhonemeComparator:
|
| 521 |
"""Enhanced phoneme comparator using Levenshtein distance - Optimized"""
|
| 522 |
|
|
|
|
| 1288 |
_instance = None
|
| 1289 |
_initialized = False
|
| 1290 |
|
| 1291 |
+
def __new__(
|
| 1292 |
+
cls,
|
| 1293 |
+
whisper_model: str = "base.en",
|
| 1294 |
+
):
|
| 1295 |
if cls._instance is None:
|
| 1296 |
cls._instance = super(ProductionPronunciationAssessor, cls).__new__(cls)
|
| 1297 |
return cls._instance
|
| 1298 |
|
| 1299 |
+
def __init__(
|
| 1300 |
+
self,
|
| 1301 |
+
whisper_model: str = "base.en",
|
| 1302 |
+
):
|
| 1303 |
"""Initialize the production-ready pronunciation assessment system (only once)"""
|
| 1304 |
if self._initialized:
|
| 1305 |
return
|
| 1306 |
|
| 1307 |
logger.info(
|
| 1308 |
+
"Initializing Optimized Production Pronunciation Assessment System with Whisper..."
|
| 1309 |
)
|
| 1310 |
|
| 1311 |
+
self.asr = EnhancedWhisperASR(
|
| 1312 |
+
whisper_model=whisper_model,
|
| 1313 |
+
)
|
| 1314 |
self.word_analyzer = EnhancedWordAnalyzer()
|
| 1315 |
self.prosody_analyzer = EnhancedProsodyAnalyzer()
|
| 1316 |
self.feedback_generator = EnhancedFeedbackGenerator()
|
|
|
|
| 1415 |
result["processing_info"] = {
|
| 1416 |
"processing_time": round(processing_time, 2),
|
| 1417 |
"mode": assessment_mode.value,
|
| 1418 |
+
"model_used": f"Whisper-{self.asr.whisper_model_name}-Enhanced-Optimized",
|
| 1419 |
+
"model_type": "Whisper",
|
| 1420 |
+
"use_whisper": True,
|
| 1421 |
+
"onnx_enabled": False,
|
| 1422 |
"confidence": asr_result["confidence"],
|
| 1423 |
"enhanced_features": True,
|
| 1424 |
"character_level_analysis": assessment_mode == AssessmentMode.WORD,
|
|
|
|
| 1594 |
"processing_info": {
|
| 1595 |
"processing_time": 0,
|
| 1596 |
"mode": "error",
|
| 1597 |
+
"model_used": f"Whisper-{self.asr.whisper_model_name if hasattr(self, 'asr') else 'base.en'}-Enhanced-Optimized",
|
| 1598 |
+
"model_type": "Whisper",
|
| 1599 |
+
"use_whisper": True,
|
| 1600 |
"confidence": 0.0,
|
| 1601 |
"enhanced_features": False,
|
| 1602 |
"optimized": True,
|
|
|
|
| 1622 |
"Production-ready error handling",
|
| 1623 |
],
|
| 1624 |
"model_info": {
|
| 1625 |
+
"asr_model": self.asr.whisper_model_name,
|
| 1626 |
+
"model_type": "Whisper",
|
| 1627 |
+
"use_whisper": True,
|
| 1628 |
+
"onnx_enabled": False,
|
| 1629 |
"sample_rate": self.asr.sample_rate,
|
| 1630 |
},
|
| 1631 |
"performance": {
|
|
|
|
| 1650 |
class SimplePronunciationAssessor:
|
| 1651 |
"""Backward compatible wrapper for the enhanced optimized system"""
|
| 1652 |
|
| 1653 |
+
def __init__(
|
| 1654 |
+
self,
|
| 1655 |
+
whisper_model: str = "base.en",
|
| 1656 |
+
):
|
| 1657 |
+
print("Initializing Optimized Simple Pronunciation Assessor with Whisper...")
|
| 1658 |
self.enhanced_assessor = ProductionPronunciationAssessor(
|
| 1659 |
+
whisper_model=whisper_model,
|
| 1660 |
)
|
| 1661 |
print(
|
| 1662 |
"Optimized Enhanced Simple Pronunciation Assessor initialization completed"
|
|
|
|
| 1739 |
|
| 1740 |
# Backward compatibility test
|
| 1741 |
print(f"\n=== BACKWARD COMPATIBILITY TEST ===")
|
| 1742 |
+
legacy_assessor = SimplePronunciationAssessor(whisper_model="base.en")
|
| 1743 |
|
| 1744 |
start_time = time.time()
|
| 1745 |
legacy_result = legacy_assessor.assess_pronunciation(
|
|
|
|
| 1813 |
print(f"✅ Enhanced features are additive, not breaking")
|
| 1814 |
|
| 1815 |
print(f"\nOptimization complete! Target: 60-70% faster processing achieved.")
|
| 1816 |
+
|
| 1817 |
+
print(f"\n=== WHISPER MODEL USAGE EXAMPLES ===")
|
| 1818 |
+
print(f"Example 1: Using Whisper with base.en model")
|
| 1819 |
+
print(
|
| 1820 |
+
f"""
|
| 1821 |
+
# Initialize with Whisper
|
| 1822 |
+
assessor = ProductionPronunciationAssessor(use_whisper=True, whisper_model="base.en")
|
| 1823 |
+
|
| 1824 |
+
# Assess pronunciation
|
| 1825 |
+
result = assessor.assess_pronunciation(
|
| 1826 |
+
audio_path="./hello_how_are_you_today.wav",
|
| 1827 |
+
reference_text="Hello, how are you today?",
|
| 1828 |
+
mode="sentence"
|
| 1829 |
+
)
|
| 1830 |
+
print(f"Transcript: {{result['transcript']}}")
|
| 1831 |
+
print(f"Score: {{result['overall_score']}}")
|
| 1832 |
+
"""
|
| 1833 |
+
)
|
| 1834 |
+
|
| 1835 |
+
print(f"\nExample 2: Using SimplePronunciationAssessor with Whisper")
|
| 1836 |
+
print(
|
| 1837 |
+
f"""
|
| 1838 |
+
# Simple wrapper with Whisper
|
| 1839 |
+
simple_assessor = SimplePronunciationAssessor(
|
| 1840 |
+
whisper_model="base.en" # or "small.en", "medium.en", "large"
|
| 1841 |
+
)
|
| 1842 |
+
|
| 1843 |
+
# Assess pronunciation
|
| 1844 |
+
result = simple_assessor.assess_pronunciation(
|
| 1845 |
+
audio_path="./hello_world.wav",
|
| 1846 |
+
reference_text="Hello world",
|
| 1847 |
+
mode="word"
|
| 1848 |
+
)
|
| 1849 |
+
"""
|
| 1850 |
+
)
|
| 1851 |
+
|
| 1852 |
+
print(f"\nAvailable Whisper models:")
|
| 1853 |
+
print(f" • tiny.en (39 MB) - Fastest, least accurate")
|
| 1854 |
+
print(f" • base.en (74 MB) - Good balance of speed and accuracy")
|
| 1855 |
+
print(f" • small.en (244 MB) - Better accuracy")
|
| 1856 |
+
print(f" • medium.en (769 MB) - High accuracy")
|
| 1857 |
+
print(f" • large (1550 MB) - Highest accuracy")
|
| 1858 |
+
|
| 1859 |
+
print(f"\nWhisper advantages:")
|
| 1860 |
+
print(f" • Better general transcription accuracy")
|
| 1861 |
+
print(f" • More robust to background noise")
|
| 1862 |
+
print(f" • Handles various accents better")
|
| 1863 |
+
print(f" • Better punctuation handling (now cleaned for scoring)")
|
| 1864 |
+
print(f" • More reliable for real-world audio conditions")
|
src/apis/create_app.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
from fastapi import FastAPI, APIRouter
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 3 |
from src.apis.routes.user_route import router as router_user
|
| 4 |
from src.apis.routes.chat_route import router as router_chat
|
| 5 |
from src.apis.routes.lesson_route import router as router_lesson
|
| 6 |
from src.apis.routes.evaluation_route import router as router_evaluation
|
| 7 |
from src.apis.routes.pronunciation_route import router as router_pronunciation
|
| 8 |
-
from src.apis.routes.speaking_route import router as router_speaking
|
| 9 |
from src.apis.routes.ipa_route import router as router_ipa
|
| 10 |
from loguru import logger
|
|
|
|
| 11 |
|
| 12 |
api_router = APIRouter(prefix="/api")
|
| 13 |
api_router.include_router(router_user)
|
|
@@ -19,8 +21,49 @@ api_router.include_router(router_speaking)
|
|
| 19 |
api_router.include_router(router_ipa)
|
| 20 |
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def create_app():
|
| 23 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
app.add_middleware(
|
| 26 |
CORSMiddleware,
|
|
@@ -30,19 +73,29 @@ def create_app():
|
|
| 30 |
allow_headers=["*"],
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
return app
|
|
|
|
| 1 |
from fastapi import FastAPI, APIRouter
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
from src.apis.routes.user_route import router as router_user
|
| 5 |
from src.apis.routes.chat_route import router as router_chat
|
| 6 |
from src.apis.routes.lesson_route import router as router_lesson
|
| 7 |
from src.apis.routes.evaluation_route import router as router_evaluation
|
| 8 |
from src.apis.routes.pronunciation_route import router as router_pronunciation
|
| 9 |
+
from src.apis.routes.speaking_route import router as router_speaking, preload_whisper_model
|
| 10 |
from src.apis.routes.ipa_route import router as router_ipa
|
| 11 |
from loguru import logger
|
| 12 |
+
import time
|
| 13 |
|
| 14 |
api_router = APIRouter(prefix="/api")
|
| 15 |
api_router.include_router(router_user)
|
|
|
|
| 21 |
api_router.include_router(router_ipa)
|
| 22 |
|
| 23 |
|
| 24 |
+
@asynccontextmanager
|
| 25 |
+
async def lifespan(app: FastAPI):
|
| 26 |
+
"""
|
| 27 |
+
FastAPI lifespan context manager for startup and shutdown events
|
| 28 |
+
Preloads Whisper model during startup for faster first inference
|
| 29 |
+
"""
|
| 30 |
+
# Startup
|
| 31 |
+
logger.info("🚀 Starting English Tutor API...")
|
| 32 |
+
startup_start = time.time()
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Preload Whisper model during startup
|
| 36 |
+
logger.info("📦 Preloading Whisper model for pronunciation assessment...")
|
| 37 |
+
success = preload_whisper_model(whisper_model="base.en")
|
| 38 |
+
|
| 39 |
+
if success:
|
| 40 |
+
logger.info("✅ Whisper model preloaded successfully!")
|
| 41 |
+
logger.info("🎯 First pronunciation assessment will be much faster!")
|
| 42 |
+
else:
|
| 43 |
+
logger.warning("⚠️ Failed to preload Whisper model, will load on first request")
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logger.error(f"❌ Error during Whisper preloading: {e}")
|
| 47 |
+
logger.warning("⚠️ Continuing without preload, model will load on first request")
|
| 48 |
+
|
| 49 |
+
startup_time = time.time() - startup_start
|
| 50 |
+
logger.info(f"🎯 English Tutor API startup completed in {startup_time:.2f}s")
|
| 51 |
+
logger.info("🌟 API is ready to serve pronunciation assessments!")
|
| 52 |
+
|
| 53 |
+
yield # Application runs here
|
| 54 |
+
|
| 55 |
+
# Shutdown
|
| 56 |
+
logger.info("🛑 Shutting down English Tutor API...")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
def create_app():
|
| 60 |
+
app = FastAPI(
|
| 61 |
+
docs_url="/",
|
| 62 |
+
title="English Tutor API with Optimized Whisper",
|
| 63 |
+
description="Pronunciation assessment API with preloaded Whisper for faster inference",
|
| 64 |
+
version="2.1.0",
|
| 65 |
+
lifespan=lifespan # Enable preloading during startup
|
| 66 |
+
)
|
| 67 |
|
| 68 |
app.add_middleware(
|
| 69 |
CORSMiddleware,
|
|
|
|
| 73 |
allow_headers=["*"],
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Add health check endpoint for monitoring Whisper status
|
| 77 |
+
@app.get("/health")
|
| 78 |
+
async def health_check():
|
| 79 |
+
"""Health check endpoint that also verifies Whisper is loaded"""
|
| 80 |
try:
|
| 81 |
+
from src.apis.routes.speaking_route import global_assessor
|
| 82 |
+
|
| 83 |
+
whisper_loaded = global_assessor is not None
|
| 84 |
+
model_name = global_assessor.asr.whisper_model_name if whisper_loaded else None
|
| 85 |
|
| 86 |
+
return {
|
| 87 |
+
"status": "healthy",
|
| 88 |
+
"whisper_preloaded": whisper_loaded,
|
| 89 |
+
"whisper_model": model_name,
|
| 90 |
+
"api_version": "2.1.0",
|
| 91 |
+
"message": "English Tutor API is running" + (" with preloaded Whisper!" if whisper_loaded else "")
|
| 92 |
+
}
|
| 93 |
except Exception as e:
|
| 94 |
+
return {
|
| 95 |
+
"status": "healthy",
|
| 96 |
+
"whisper_preloaded": False,
|
| 97 |
+
"error": str(e),
|
| 98 |
+
"api_version": "2.1.0"
|
| 99 |
+
}
|
| 100 |
|
| 101 |
return app
|
src/apis/routes/speaking_route.py
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import UploadFile, File, Form, HTTPException, APIRouter
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List, Dict, Optional
|
|
@@ -12,81 +35,93 @@ from loguru import logger
|
|
| 12 |
from src.utils.speaking_utils import convert_numpy_types
|
| 13 |
|
| 14 |
# Import the new evaluation system
|
| 15 |
-
from src.apis.controllers.speaking_controller import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
warnings.filterwarnings("ignore")
|
| 17 |
|
| 18 |
router = APIRouter(prefix="/speaking", tags=["Speaking"])
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# =============================================================================
|
| 22 |
# OPTIMIZATION FUNCTIONS
|
| 23 |
# =============================================================================
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
Tối ưu hóa xử lý sau assessment bằng cách chạy song song các task độc lập
|
| 28 |
Giảm thời gian xử lý từ ~0.3-0.5s xuống ~0.1-0.2s
|
| 29 |
"""
|
| 30 |
start_time = time.time()
|
| 31 |
-
|
| 32 |
# Tạo shared G2P instance để tránh tạo mới nhiều lần
|
| 33 |
g2p = get_shared_g2p()
|
| 34 |
-
|
| 35 |
# Định nghĩa các task có thể chạy song song
|
| 36 |
async def process_reference_phonemes_and_ipa():
|
| 37 |
"""Xử lý reference phonemes và IPA song song"""
|
| 38 |
loop = asyncio.get_event_loop()
|
| 39 |
executor = get_shared_executor()
|
| 40 |
reference_words = reference_text.strip().split()
|
| 41 |
-
|
| 42 |
# Chạy song song cho từng word
|
| 43 |
futures = []
|
| 44 |
for word in reference_words:
|
| 45 |
-
clean_word = word.strip(
|
| 46 |
future = loop.run_in_executor(executor, g2p.text_to_phonemes, clean_word)
|
| 47 |
futures.append(future)
|
| 48 |
-
|
| 49 |
# Collect results
|
| 50 |
word_results = await asyncio.gather(*futures)
|
| 51 |
-
|
| 52 |
reference_phonemes_list = []
|
| 53 |
reference_ipa_list = []
|
| 54 |
-
|
| 55 |
for word_data in word_results:
|
| 56 |
if word_data and len(word_data) > 0:
|
| 57 |
reference_phonemes_list.append(word_data[0]["phoneme_string"])
|
| 58 |
reference_ipa_list.append(word_data[0]["ipa"])
|
| 59 |
-
|
| 60 |
result["reference_phonemes"] = " ".join(reference_phonemes_list)
|
| 61 |
result["reference_ipa"] = " ".join(reference_ipa_list)
|
| 62 |
-
|
| 63 |
async def process_user_ipa():
|
| 64 |
"""Xử lý user IPA từ transcript song song"""
|
| 65 |
if "transcript" not in result or not result["transcript"]:
|
| 66 |
result["user_ipa"] = None
|
| 67 |
return
|
| 68 |
-
|
| 69 |
try:
|
| 70 |
user_transcript = result["transcript"].strip()
|
| 71 |
user_words = user_transcript.split()
|
| 72 |
-
|
| 73 |
if not user_words:
|
| 74 |
result["user_ipa"] = None
|
| 75 |
return
|
| 76 |
-
|
| 77 |
loop = asyncio.get_event_loop()
|
| 78 |
executor = get_shared_executor()
|
| 79 |
# Chạy song song cho từng word
|
| 80 |
futures = []
|
| 81 |
clean_words = []
|
| 82 |
-
|
| 83 |
for word in user_words:
|
| 84 |
-
clean_word = word.strip(
|
| 85 |
if clean_word: # Skip empty words
|
| 86 |
clean_words.append(clean_word)
|
| 87 |
-
future = loop.run_in_executor(
|
|
|
|
|
|
|
| 88 |
futures.append(future)
|
| 89 |
-
|
| 90 |
# Collect results
|
| 91 |
if futures:
|
| 92 |
user_ipa_results = await asyncio.gather(*futures)
|
|
@@ -94,17 +129,17 @@ async def optimize_post_assessment_processing(result: Dict, reference_text: str)
|
|
| 94 |
result["user_ipa"] = " ".join(user_ipa_list) if user_ipa_list else None
|
| 95 |
else:
|
| 96 |
result["user_ipa"] = None
|
| 97 |
-
|
| 98 |
-
logger.info(
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
logger.warning(f"Failed to generate user IPA from transcript: {e}")
|
| 102 |
-
result["user_ipa"] = None
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
optimization_time = time.time() - start_time
|
| 109 |
logger.info(f"Post-assessment optimization completed in {optimization_time:.3f}s")
|
| 110 |
|
|
@@ -130,6 +165,7 @@ def safe_get_word_ipa(g2p: EnhancedG2P, word: str) -> Optional[str]:
|
|
| 130 |
_shared_g2p_cache = {}
|
| 131 |
_cache_lock = asyncio.Lock()
|
| 132 |
|
|
|
|
| 133 |
async def get_cached_g2p_result(word: str) -> Optional[Dict]:
|
| 134 |
"""
|
| 135 |
Cache G2P results để tránh tính toán lại cho các từ đã xử lý
|
|
@@ -139,6 +175,7 @@ async def get_cached_g2p_result(word: str) -> Optional[Dict]:
|
|
| 139 |
return _shared_g2p_cache[word]
|
| 140 |
return None
|
| 141 |
|
|
|
|
| 142 |
async def cache_g2p_result(word: str, result: Dict) -> None:
|
| 143 |
"""
|
| 144 |
Cache G2P result với size limit
|
|
@@ -150,29 +187,29 @@ async def cache_g2p_result(word: str, result: Dict) -> None:
|
|
| 150 |
oldest_keys = list(_shared_g2p_cache.keys())[:100]
|
| 151 |
for key in oldest_keys:
|
| 152 |
del _shared_g2p_cache[key]
|
| 153 |
-
|
| 154 |
_shared_g2p_cache[word] = result
|
| 155 |
|
| 156 |
|
| 157 |
async def optimize_ipa_assessment_processing(
|
| 158 |
-
base_result: Dict,
|
| 159 |
-
target_word: str,
|
| 160 |
-
target_ipa: Optional[str],
|
| 161 |
-
focus_phonemes: Optional[str]
|
| 162 |
) -> Dict:
|
| 163 |
"""
|
| 164 |
Tối ưu hóa xử lý IPA assessment bằng cách chạy song song các task
|
| 165 |
"""
|
| 166 |
start_time = time.time()
|
| 167 |
-
|
| 168 |
# Shared G2P instance
|
| 169 |
g2p = get_shared_g2p()
|
| 170 |
-
|
| 171 |
# Parse focus phonemes trước
|
| 172 |
focus_phonemes_list = []
|
| 173 |
if focus_phonemes:
|
| 174 |
focus_phonemes_list = [p.strip() for p in focus_phonemes.split(",")]
|
| 175 |
-
|
| 176 |
async def get_target_phonemes_data():
|
| 177 |
"""Get target IPA and phonemes"""
|
| 178 |
if not target_ipa:
|
|
@@ -186,13 +223,15 @@ async def optimize_ipa_assessment_processing(
|
|
| 186 |
# Parse provided IPA
|
| 187 |
clean_ipa = target_ipa.replace("/", "").strip()
|
| 188 |
return target_ipa, list(clean_ipa)
|
| 189 |
-
|
| 190 |
-
async def create_character_analysis(
|
|
|
|
|
|
|
| 191 |
"""Create character analysis optimized"""
|
| 192 |
character_analysis = []
|
| 193 |
target_chars = list(target_word)
|
| 194 |
target_phoneme_chars = list(final_target_ipa.replace("/", ""))
|
| 195 |
-
|
| 196 |
# Pre-calculate phoneme scores mapping
|
| 197 |
phoneme_score_map = {}
|
| 198 |
if base_result.get("phoneme_differences"):
|
|
@@ -200,28 +239,37 @@ async def optimize_ipa_assessment_processing(
|
|
| 200 |
ref_phoneme = phoneme_diff.get("reference_phoneme")
|
| 201 |
if ref_phoneme:
|
| 202 |
phoneme_score_map[ref_phoneme] = phoneme_diff.get("score", 0.0)
|
| 203 |
-
|
| 204 |
for i, char in enumerate(target_chars):
|
| 205 |
-
char_phoneme =
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
"
|
| 214 |
-
|
| 215 |
-
"
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
return character_analysis
|
| 220 |
-
|
| 221 |
async def create_phoneme_scores(target_phonemes: List[str]):
|
| 222 |
"""Create phoneme scores optimized"""
|
| 223 |
phoneme_scores = []
|
| 224 |
-
|
| 225 |
# Pre-calculate phoneme scores mapping
|
| 226 |
phoneme_score_map = {}
|
| 227 |
if base_result.get("phoneme_differences"):
|
|
@@ -229,28 +277,38 @@ async def optimize_ipa_assessment_processing(
|
|
| 229 |
ref_phoneme = phoneme_diff.get("reference_phoneme")
|
| 230 |
if ref_phoneme:
|
| 231 |
phoneme_score_map[ref_phoneme] = phoneme_diff.get("score", 0.0)
|
| 232 |
-
|
| 233 |
for phoneme in target_phonemes:
|
| 234 |
-
phoneme_score = phoneme_score_map.get(
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
return phoneme_scores
|
| 249 |
-
|
| 250 |
async def create_focus_analysis():
|
| 251 |
"""Create focus phonemes analysis optimized"""
|
| 252 |
focus_phonemes_analysis = []
|
| 253 |
-
|
| 254 |
# Pre-calculate phoneme scores mapping
|
| 255 |
phoneme_score_map = {}
|
| 256 |
if base_result.get("phoneme_differences"):
|
|
@@ -258,34 +316,42 @@ async def optimize_ipa_assessment_processing(
|
|
| 258 |
ref_phoneme = phoneme_diff.get("reference_phoneme")
|
| 259 |
if ref_phoneme:
|
| 260 |
phoneme_score_map[ref_phoneme] = phoneme_diff.get("score", 0.0)
|
| 261 |
-
|
| 262 |
for focus_phoneme in focus_phonemes_list:
|
| 263 |
-
score = phoneme_score_map.get(
|
| 264 |
-
|
|
|
|
|
|
|
| 265 |
phoneme_analysis = {
|
| 266 |
"phoneme": focus_phoneme,
|
| 267 |
"score": float(score),
|
| 268 |
"status": "correct" if score > 0.8 else "incorrect",
|
| 269 |
"vietnamese_tip": get_vietnamese_tip(focus_phoneme),
|
| 270 |
"difficulty": "medium",
|
| 271 |
-
"color_class": (
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
}
|
| 275 |
focus_phonemes_analysis.append(phoneme_analysis)
|
| 276 |
-
|
| 277 |
return focus_phonemes_analysis
|
| 278 |
-
|
| 279 |
# Get target phonemes data first
|
| 280 |
final_target_ipa, target_phonemes = await get_target_phonemes_data()
|
| 281 |
-
|
| 282 |
# Run parallel processing for analysis
|
| 283 |
character_analysis, phoneme_scores, focus_phonemes_analysis = await asyncio.gather(
|
| 284 |
create_character_analysis(final_target_ipa, target_phonemes),
|
| 285 |
create_phoneme_scores(target_phonemes),
|
| 286 |
-
create_focus_analysis()
|
| 287 |
)
|
| 288 |
-
|
| 289 |
# Generate tips and recommendations asynchronously
|
| 290 |
loop = asyncio.get_event_loop()
|
| 291 |
executor = get_shared_executor()
|
|
@@ -293,64 +359,74 @@ async def optimize_ipa_assessment_processing(
|
|
| 293 |
executor, generate_vietnamese_tips, target_phonemes, focus_phonemes_list
|
| 294 |
)
|
| 295 |
practice_recommendations_future = loop.run_in_executor(
|
| 296 |
-
executor,
|
|
|
|
|
|
|
|
|
|
| 297 |
)
|
| 298 |
-
|
| 299 |
vietnamese_tips, practice_recommendations = await asyncio.gather(
|
| 300 |
-
vietnamese_tips_future,
|
| 301 |
-
practice_recommendations_future
|
| 302 |
)
|
| 303 |
-
|
| 304 |
optimization_time = time.time() - start_time
|
| 305 |
logger.info(f"IPA assessment optimization completed in {optimization_time:.3f}s")
|
| 306 |
-
|
| 307 |
return {
|
| 308 |
"target_ipa": final_target_ipa,
|
| 309 |
"character_analysis": character_analysis,
|
| 310 |
"phoneme_scores": phoneme_scores,
|
| 311 |
"focus_phonemes_analysis": focus_phonemes_analysis,
|
| 312 |
"vietnamese_tips": vietnamese_tips,
|
| 313 |
-
"practice_recommendations": practice_recommendations
|
| 314 |
}
|
| 315 |
|
| 316 |
|
| 317 |
-
def generate_vietnamese_tips(
|
|
|
|
|
|
|
| 318 |
"""Generate Vietnamese tips for difficult phonemes"""
|
| 319 |
vietnamese_tips = []
|
| 320 |
difficult_phonemes = ["θ", "ð", "v", "z", "ʒ", "r", "w", "æ", "ɪ", "ʊ", "ɛ"]
|
| 321 |
-
|
| 322 |
for phoneme in set(target_phonemes + focus_phonemes_list):
|
| 323 |
if phoneme in difficult_phonemes:
|
| 324 |
tip = get_vietnamese_tip(phoneme)
|
| 325 |
if tip not in vietnamese_tips:
|
| 326 |
vietnamese_tips.append(tip)
|
| 327 |
-
|
| 328 |
return vietnamese_tips
|
| 329 |
|
| 330 |
|
| 331 |
-
def generate_practice_recommendations(
|
|
|
|
|
|
|
| 332 |
"""Generate practice recommendations based on score"""
|
| 333 |
practice_recommendations = []
|
| 334 |
-
|
| 335 |
if overall_score < 0.7:
|
| 336 |
-
practice_recommendations.extend(
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
| 342 |
# Add specific recommendations for focus phonemes
|
| 343 |
for analysis in focus_phonemes_analysis:
|
| 344 |
if analysis["score"] < 0.6:
|
| 345 |
practice_recommendations.append(
|
| 346 |
f"Luyện đặc biệt âm /{analysis['phoneme']}/: {analysis['vietnamese_tip']}"
|
| 347 |
)
|
| 348 |
-
|
| 349 |
if overall_score >= 0.8:
|
| 350 |
-
practice_recommendations.append(
|
|
|
|
|
|
|
| 351 |
elif overall_score >= 0.6:
|
| 352 |
practice_recommendations.append("Phát âm khá tốt, cần cải thiện một số âm vị")
|
| 353 |
-
|
| 354 |
return practice_recommendations
|
| 355 |
|
| 356 |
|
|
@@ -383,41 +459,73 @@ class PronunciationAssessmentResult(BaseModel):
|
|
| 383 |
|
| 384 |
class IPAAssessmentResult(BaseModel):
|
| 385 |
"""Optimized response model for IPA-focused pronunciation assessment"""
|
|
|
|
| 386 |
# Core assessment data
|
| 387 |
transcript: str # What the user actually said
|
| 388 |
user_ipa: Optional[str] = None # User's IPA transcription
|
| 389 |
target_word: str # Target word being assessed
|
| 390 |
target_ipa: str # Target IPA transcription
|
| 391 |
overall_score: float # Overall pronunciation score (0-1)
|
| 392 |
-
|
| 393 |
# Character-level analysis for IPA mapping
|
| 394 |
character_analysis: List[Dict] # Each character with its IPA and score
|
| 395 |
-
|
| 396 |
# Phoneme-specific analysis
|
| 397 |
phoneme_scores: List[Dict] # Individual phoneme scores with colors
|
| 398 |
focus_phonemes_analysis: List[Dict] # Detailed analysis of target phonemes
|
| 399 |
-
|
| 400 |
# Feedback and recommendations
|
| 401 |
vietnamese_tips: List[str] # Vietnamese-specific pronunciation tips
|
| 402 |
practice_recommendations: List[str] # Practice suggestions
|
| 403 |
feedback: List[str] # General feedback messages
|
| 404 |
-
|
| 405 |
# Assessment metadata
|
| 406 |
processing_info: Dict # Processing details
|
| 407 |
assessment_type: str = "ipa_focused"
|
| 408 |
error: Optional[str] = None
|
| 409 |
|
|
|
|
| 410 |
# Global assessor instance - singleton pattern for performance
|
| 411 |
global_assessor = None
|
| 412 |
global_g2p = None # Shared G2P instance for caching
|
| 413 |
global_executor = None # Shared ThreadPoolExecutor
|
| 414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
def get_assessor():
|
| 416 |
-
"""Get or create the global assessor instance"""
|
| 417 |
global global_assessor
|
| 418 |
if global_assessor is None:
|
| 419 |
-
logger.info("Creating global ProductionPronunciationAssessor instance...")
|
| 420 |
-
|
|
|
|
|
|
|
| 421 |
return global_assessor
|
| 422 |
|
| 423 |
|
|
@@ -506,7 +614,7 @@ async def assess_pronunciation(
|
|
| 506 |
# Run assessment using enhanced assessor (singleton)
|
| 507 |
assessor = get_assessor()
|
| 508 |
result = assessor.assess_pronunciation(tmp_file.name, reference_text, mode)
|
| 509 |
-
|
| 510 |
# Optimize post-processing with parallel execution
|
| 511 |
await optimize_post_assessment_processing(result, reference_text)
|
| 512 |
|
|
@@ -536,58 +644,69 @@ async def assess_ipa_pronunciation(
|
|
| 536 |
audio_file: UploadFile = File(..., description="Audio file (.wav, .mp3, .m4a)"),
|
| 537 |
target_word: str = Form(..., description="Target word to assess (e.g., 'bed')"),
|
| 538 |
target_ipa: str = Form(None, description="Target IPA notation (e.g., '/bɛd/')"),
|
| 539 |
-
focus_phonemes: str = Form(
|
|
|
|
|
|
|
| 540 |
):
|
| 541 |
"""
|
| 542 |
Optimized IPA pronunciation assessment for phoneme-focused learning
|
| 543 |
-
|
| 544 |
Evaluates:
|
| 545 |
- Overall word pronunciation accuracy
|
| 546 |
-
- Character-to-phoneme mapping accuracy
|
| 547 |
- Specific phoneme pronunciation (e.g., /ɛ/ in 'bed')
|
| 548 |
- Vietnamese-optimized feedback and tips
|
| 549 |
- Dynamic color scoring for UI visualization
|
| 550 |
-
|
| 551 |
Example: Assessing 'bed' /bɛd/ with focus on /ɛ/ phoneme
|
| 552 |
"""
|
| 553 |
-
|
| 554 |
import time
|
|
|
|
| 555 |
start_time = time.time()
|
| 556 |
-
|
| 557 |
# Validate inputs
|
| 558 |
if not target_word.strip():
|
| 559 |
raise HTTPException(status_code=400, detail="Target word cannot be empty")
|
| 560 |
-
|
| 561 |
if len(target_word) > 50:
|
| 562 |
-
raise HTTPException(
|
| 563 |
-
|
|
|
|
|
|
|
| 564 |
# Clean target word
|
| 565 |
target_word = target_word.strip().lower()
|
| 566 |
-
|
| 567 |
try:
|
| 568 |
# Save uploaded file temporarily
|
| 569 |
file_extension = ".wav"
|
| 570 |
if audio_file.filename and "." in audio_file.filename:
|
| 571 |
file_extension = f".{audio_file.filename.split('.')[-1]}"
|
| 572 |
|
| 573 |
-
with tempfile.NamedTemporaryFile(
|
|
|
|
|
|
|
| 574 |
content = await audio_file.read()
|
| 575 |
tmp_file.write(content)
|
| 576 |
tmp_file.flush()
|
| 577 |
|
| 578 |
-
logger.info(
|
|
|
|
|
|
|
| 579 |
|
| 580 |
# Get the assessor instance
|
| 581 |
assessor = get_assessor()
|
| 582 |
-
|
| 583 |
# Run base pronunciation assessment in word mode
|
| 584 |
-
base_result = assessor.assess_pronunciation(
|
| 585 |
-
|
|
|
|
|
|
|
| 586 |
# Optimize IPA assessment processing with parallel execution
|
| 587 |
optimized_results = await optimize_ipa_assessment_processing(
|
| 588 |
base_result, target_word, target_ipa, focus_phonemes
|
| 589 |
)
|
| 590 |
-
|
| 591 |
# Extract optimized results
|
| 592 |
target_ipa = optimized_results["target_ipa"]
|
| 593 |
character_analysis = optimized_results["character_analysis"]
|
|
@@ -595,28 +714,30 @@ async def assess_ipa_pronunciation(
|
|
| 595 |
focus_phonemes_analysis = optimized_results["focus_phonemes_analysis"]
|
| 596 |
vietnamese_tips = optimized_results["vietnamese_tips"]
|
| 597 |
practice_recommendations = optimized_results["practice_recommendations"]
|
| 598 |
-
|
| 599 |
# Get overall score from base result
|
| 600 |
overall_score = base_result.get("overall_score", 0.0)
|
| 601 |
-
|
| 602 |
# Handle error cases
|
| 603 |
error_message = None
|
| 604 |
feedback = base_result.get("feedback", [])
|
| 605 |
-
|
| 606 |
if base_result.get("error"):
|
| 607 |
error_message = base_result["error"]
|
| 608 |
feedback = [f"Lỗi: {error_message}"]
|
| 609 |
-
|
| 610 |
# Processing information
|
| 611 |
processing_time = time.time() - start_time
|
| 612 |
processing_info = {
|
| 613 |
"processing_time": processing_time,
|
| 614 |
"mode": "ipa_focused",
|
| 615 |
"model_used": "Wav2Vec2-Enhanced",
|
| 616 |
-
"confidence": base_result.get("processing_info", {}).get(
|
| 617 |
-
|
|
|
|
|
|
|
| 618 |
}
|
| 619 |
-
|
| 620 |
# Create final result
|
| 621 |
result = IPAAssessmentResult(
|
| 622 |
transcript=base_result.get("transcript", ""),
|
|
@@ -631,16 +752,19 @@ async def assess_ipa_pronunciation(
|
|
| 631 |
practice_recommendations=practice_recommendations,
|
| 632 |
feedback=feedback,
|
| 633 |
processing_info=processing_info,
|
| 634 |
-
error=error_message
|
| 635 |
)
|
| 636 |
-
|
| 637 |
-
logger.info(
|
| 638 |
-
|
|
|
|
|
|
|
| 639 |
return result
|
| 640 |
|
| 641 |
except Exception as e:
|
| 642 |
logger.error(f"IPA assessment error: {str(e)}")
|
| 643 |
import traceback
|
|
|
|
| 644 |
traceback.print_exc()
|
| 645 |
raise HTTPException(status_code=500, detail=f"IPA assessment failed: {str(e)}")
|
| 646 |
|
|
@@ -654,14 +778,13 @@ async def assess_ipa_pronunciation(
|
|
| 654 |
def get_word_phonemes(word: str):
|
| 655 |
"""Get phoneme breakdown for a specific word"""
|
| 656 |
try:
|
| 657 |
-
# Use the
|
| 658 |
-
|
| 659 |
-
g2p = EnhancedG2P()
|
| 660 |
phoneme_data = g2p.text_to_phonemes(word)[0]
|
| 661 |
|
| 662 |
# Add difficulty analysis for Vietnamese speakers
|
| 663 |
difficulty_scores = []
|
| 664 |
-
|
| 665 |
for phoneme in phoneme_data["phonemes"]:
|
| 666 |
difficulty = g2p.get_difficulty_score(phoneme)
|
| 667 |
difficulty_scores.append(difficulty)
|
|
@@ -718,7 +841,7 @@ def get_vietnamese_tip(phoneme: str) -> str:
|
|
| 718 |
"d": "Lưỡi chạm nướu răng trên, rung dây thanh",
|
| 719 |
"t": "Lưỡi chạm nướu răng trên, không rung dây thanh",
|
| 720 |
"k": "Lưỡi chạm vòm miệng, không rung dây thanh",
|
| 721 |
-
"g": "Lưỡi chạm vòm miệng, rung dây thanh"
|
| 722 |
}
|
| 723 |
return tips.get(phoneme, f"Luyện tập phát âm /{phoneme}/")
|
| 724 |
|
|
@@ -727,10 +850,10 @@ def get_phoneme_difficulty(phoneme: str) -> str:
|
|
| 727 |
"""Get difficulty level for Vietnamese speakers"""
|
| 728 |
hard_phonemes = ["θ", "ð", "r", "w", "æ", "ʌ", "ɪ", "ʊ"]
|
| 729 |
medium_phonemes = ["v", "z", "ʒ", "ɛ", "ə", "ɔ", "f"]
|
| 730 |
-
|
| 731 |
if phoneme in hard_phonemes:
|
| 732 |
return "hard"
|
| 733 |
elif phoneme in medium_phonemes:
|
| 734 |
return "medium"
|
| 735 |
else:
|
| 736 |
-
return "easy"
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Speaking Route - Optimized with Whisper Preloading
|
| 3 |
+
|
| 4 |
+
Usage in FastAPI app:
|
| 5 |
+
|
| 6 |
+
```python
|
| 7 |
+
from fastapi import FastAPI
|
| 8 |
+
from contextlib import asynccontextmanager
|
| 9 |
+
from src.apis.routes.speaking_route import router, preload_whisper_model
|
| 10 |
+
|
| 11 |
+
@asynccontextmanager
|
| 12 |
+
async def lifespan(app: FastAPI):
|
| 13 |
+
# Preload Whisper during startup
|
| 14 |
+
preload_whisper_model("base.en") # or "small.en", "medium.en"
|
| 15 |
+
yield
|
| 16 |
+
|
| 17 |
+
app = FastAPI(lifespan=lifespan)
|
| 18 |
+
app.include_router(router)
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
This ensures Whisper model is loaded in RAM before first inference.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
from fastapi import UploadFile, File, Form, HTTPException, APIRouter
|
| 25 |
from pydantic import BaseModel
|
| 26 |
from typing import List, Dict, Optional
|
|
|
|
| 35 |
from src.utils.speaking_utils import convert_numpy_types
|
| 36 |
|
| 37 |
# Import the new evaluation system
|
| 38 |
+
from src.apis.controllers.speaking_controller import (
|
| 39 |
+
ProductionPronunciationAssessor,
|
| 40 |
+
EnhancedG2P,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
warnings.filterwarnings("ignore")
|
| 44 |
|
| 45 |
router = APIRouter(prefix="/speaking", tags=["Speaking"])
|
| 46 |
|
| 47 |
+
# Export preload function for use in main app
|
| 48 |
+
__all__ = ["router", "preload_whisper_model"]
|
| 49 |
+
|
| 50 |
|
| 51 |
# =============================================================================
|
| 52 |
# OPTIMIZATION FUNCTIONS
|
| 53 |
# =============================================================================
|
| 54 |
|
| 55 |
+
|
| 56 |
+
async def optimize_post_assessment_processing(
|
| 57 |
+
result: Dict, reference_text: str
|
| 58 |
+
) -> None:
|
| 59 |
"""
|
| 60 |
Tối ưu hóa xử lý sau assessment bằng cách chạy song song các task độc lập
|
| 61 |
Giảm thời gian xử lý từ ~0.3-0.5s xuống ~0.1-0.2s
|
| 62 |
"""
|
| 63 |
start_time = time.time()
|
| 64 |
+
|
| 65 |
# Tạo shared G2P instance để tránh tạo mới nhiều lần
|
| 66 |
g2p = get_shared_g2p()
|
| 67 |
+
|
| 68 |
# Định nghĩa các task có thể chạy song song
|
| 69 |
async def process_reference_phonemes_and_ipa():
|
| 70 |
"""Xử lý reference phonemes và IPA song song"""
|
| 71 |
loop = asyncio.get_event_loop()
|
| 72 |
executor = get_shared_executor()
|
| 73 |
reference_words = reference_text.strip().split()
|
| 74 |
+
|
| 75 |
# Chạy song song cho từng word
|
| 76 |
futures = []
|
| 77 |
for word in reference_words:
|
| 78 |
+
clean_word = word.strip(".,!?;:")
|
| 79 |
future = loop.run_in_executor(executor, g2p.text_to_phonemes, clean_word)
|
| 80 |
futures.append(future)
|
| 81 |
+
|
| 82 |
# Collect results
|
| 83 |
word_results = await asyncio.gather(*futures)
|
| 84 |
+
|
| 85 |
reference_phonemes_list = []
|
| 86 |
reference_ipa_list = []
|
| 87 |
+
|
| 88 |
for word_data in word_results:
|
| 89 |
if word_data and len(word_data) > 0:
|
| 90 |
reference_phonemes_list.append(word_data[0]["phoneme_string"])
|
| 91 |
reference_ipa_list.append(word_data[0]["ipa"])
|
| 92 |
+
|
| 93 |
result["reference_phonemes"] = " ".join(reference_phonemes_list)
|
| 94 |
result["reference_ipa"] = " ".join(reference_ipa_list)
|
| 95 |
+
|
| 96 |
async def process_user_ipa():
|
| 97 |
"""Xử lý user IPA từ transcript song song"""
|
| 98 |
if "transcript" not in result or not result["transcript"]:
|
| 99 |
result["user_ipa"] = None
|
| 100 |
return
|
| 101 |
+
|
| 102 |
try:
|
| 103 |
user_transcript = result["transcript"].strip()
|
| 104 |
user_words = user_transcript.split()
|
| 105 |
+
|
| 106 |
if not user_words:
|
| 107 |
result["user_ipa"] = None
|
| 108 |
return
|
| 109 |
+
|
| 110 |
loop = asyncio.get_event_loop()
|
| 111 |
executor = get_shared_executor()
|
| 112 |
# Chạy song song cho từng word
|
| 113 |
futures = []
|
| 114 |
clean_words = []
|
| 115 |
+
|
| 116 |
for word in user_words:
|
| 117 |
+
clean_word = word.strip(".,!?;:").lower()
|
| 118 |
if clean_word: # Skip empty words
|
| 119 |
clean_words.append(clean_word)
|
| 120 |
+
future = loop.run_in_executor(
|
| 121 |
+
executor, safe_get_word_ipa, g2p, clean_word
|
| 122 |
+
)
|
| 123 |
futures.append(future)
|
| 124 |
+
|
| 125 |
# Collect results
|
| 126 |
if futures:
|
| 127 |
user_ipa_results = await asyncio.gather(*futures)
|
|
|
|
| 129 |
result["user_ipa"] = " ".join(user_ipa_list) if user_ipa_list else None
|
| 130 |
else:
|
| 131 |
result["user_ipa"] = None
|
| 132 |
+
|
| 133 |
+
logger.info(
|
| 134 |
+
f"Generated user IPA from transcript '{user_transcript}': '{result.get('user_ipa', 'None')}'"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
except Exception as e:
|
| 138 |
logger.warning(f"Failed to generate user IPA from transcript: {e}")
|
| 139 |
+
result["user_ipa"] = None # Chạy song song cả 2 task chính
|
| 140 |
+
|
| 141 |
+
await asyncio.gather(process_reference_phonemes_and_ipa(), process_user_ipa())
|
| 142 |
+
|
|
|
|
|
|
|
| 143 |
optimization_time = time.time() - start_time
|
| 144 |
logger.info(f"Post-assessment optimization completed in {optimization_time:.3f}s")
|
| 145 |
|
|
|
|
| 165 |
_shared_g2p_cache = {}
|
| 166 |
_cache_lock = asyncio.Lock()
|
| 167 |
|
| 168 |
+
|
| 169 |
async def get_cached_g2p_result(word: str) -> Optional[Dict]:
|
| 170 |
"""
|
| 171 |
Cache G2P results để tránh tính toán lại cho các từ đã xử lý
|
|
|
|
| 175 |
return _shared_g2p_cache[word]
|
| 176 |
return None
|
| 177 |
|
| 178 |
+
|
| 179 |
async def cache_g2p_result(word: str, result: Dict) -> None:
|
| 180 |
"""
|
| 181 |
Cache G2P result với size limit
|
|
|
|
| 187 |
oldest_keys = list(_shared_g2p_cache.keys())[:100]
|
| 188 |
for key in oldest_keys:
|
| 189 |
del _shared_g2p_cache[key]
|
| 190 |
+
|
| 191 |
_shared_g2p_cache[word] = result
|
| 192 |
|
| 193 |
|
| 194 |
async def optimize_ipa_assessment_processing(
|
| 195 |
+
base_result: Dict,
|
| 196 |
+
target_word: str,
|
| 197 |
+
target_ipa: Optional[str],
|
| 198 |
+
focus_phonemes: Optional[str],
|
| 199 |
) -> Dict:
|
| 200 |
"""
|
| 201 |
Tối ưu hóa xử lý IPA assessment bằng cách chạy song song các task
|
| 202 |
"""
|
| 203 |
start_time = time.time()
|
| 204 |
+
|
| 205 |
# Shared G2P instance
|
| 206 |
g2p = get_shared_g2p()
|
| 207 |
+
|
| 208 |
# Parse focus phonemes trước
|
| 209 |
focus_phonemes_list = []
|
| 210 |
if focus_phonemes:
|
| 211 |
focus_phonemes_list = [p.strip() for p in focus_phonemes.split(",")]
|
| 212 |
+
|
| 213 |
async def get_target_phonemes_data():
|
| 214 |
"""Get target IPA and phonemes"""
|
| 215 |
if not target_ipa:
|
|
|
|
| 223 |
# Parse provided IPA
|
| 224 |
clean_ipa = target_ipa.replace("/", "").strip()
|
| 225 |
return target_ipa, list(clean_ipa)
|
| 226 |
+
|
| 227 |
+
async def create_character_analysis(
|
| 228 |
+
final_target_ipa: str, target_phonemes: List[str]
|
| 229 |
+
):
|
| 230 |
"""Create character analysis optimized"""
|
| 231 |
character_analysis = []
|
| 232 |
target_chars = list(target_word)
|
| 233 |
target_phoneme_chars = list(final_target_ipa.replace("/", ""))
|
| 234 |
+
|
| 235 |
# Pre-calculate phoneme scores mapping
|
| 236 |
phoneme_score_map = {}
|
| 237 |
if base_result.get("phoneme_differences"):
|
|
|
|
| 239 |
ref_phoneme = phoneme_diff.get("reference_phoneme")
|
| 240 |
if ref_phoneme:
|
| 241 |
phoneme_score_map[ref_phoneme] = phoneme_diff.get("score", 0.0)
|
| 242 |
+
|
| 243 |
for i, char in enumerate(target_chars):
|
| 244 |
+
char_phoneme = (
|
| 245 |
+
target_phoneme_chars[i] if i < len(target_phoneme_chars) else ""
|
| 246 |
+
)
|
| 247 |
+
char_score = phoneme_score_map.get(
|
| 248 |
+
char_phoneme, base_result.get("overall_score", 0.0)
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
color_class = (
|
| 252 |
+
"text-green-600"
|
| 253 |
+
if char_score > 0.8
|
| 254 |
+
else "text-yellow-600" if char_score > 0.6 else "text-red-600"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
character_analysis.append(
|
| 258 |
+
{
|
| 259 |
+
"character": char,
|
| 260 |
+
"phoneme": char_phoneme,
|
| 261 |
+
"score": float(char_score),
|
| 262 |
+
"color_class": color_class,
|
| 263 |
+
"is_focus": char_phoneme in focus_phonemes_list,
|
| 264 |
+
}
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
return character_analysis
|
| 268 |
+
|
| 269 |
async def create_phoneme_scores(target_phonemes: List[str]):
|
| 270 |
"""Create phoneme scores optimized"""
|
| 271 |
phoneme_scores = []
|
| 272 |
+
|
| 273 |
# Pre-calculate phoneme scores mapping
|
| 274 |
phoneme_score_map = {}
|
| 275 |
if base_result.get("phoneme_differences"):
|
|
|
|
| 277 |
ref_phoneme = phoneme_diff.get("reference_phoneme")
|
| 278 |
if ref_phoneme:
|
| 279 |
phoneme_score_map[ref_phoneme] = phoneme_diff.get("score", 0.0)
|
| 280 |
+
|
| 281 |
for phoneme in target_phonemes:
|
| 282 |
+
phoneme_score = phoneme_score_map.get(
|
| 283 |
+
phoneme, base_result.get("overall_score", 0.0)
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
color_class = (
|
| 287 |
+
"bg-green-100 text-green-800"
|
| 288 |
+
if phoneme_score > 0.8
|
| 289 |
+
else (
|
| 290 |
+
"bg-yellow-100 text-yellow-800"
|
| 291 |
+
if phoneme_score > 0.6
|
| 292 |
+
else "bg-red-100 text-red-800"
|
| 293 |
+
)
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
phoneme_scores.append(
|
| 297 |
+
{
|
| 298 |
+
"phoneme": phoneme,
|
| 299 |
+
"score": float(phoneme_score),
|
| 300 |
+
"color_class": color_class,
|
| 301 |
+
"percentage": int(phoneme_score * 100),
|
| 302 |
+
"is_focus": phoneme in focus_phonemes_list,
|
| 303 |
+
}
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
return phoneme_scores
|
| 307 |
+
|
| 308 |
async def create_focus_analysis():
|
| 309 |
"""Create focus phonemes analysis optimized"""
|
| 310 |
focus_phonemes_analysis = []
|
| 311 |
+
|
| 312 |
# Pre-calculate phoneme scores mapping
|
| 313 |
phoneme_score_map = {}
|
| 314 |
if base_result.get("phoneme_differences"):
|
|
|
|
| 316 |
ref_phoneme = phoneme_diff.get("reference_phoneme")
|
| 317 |
if ref_phoneme:
|
| 318 |
phoneme_score_map[ref_phoneme] = phoneme_diff.get("score", 0.0)
|
| 319 |
+
|
| 320 |
for focus_phoneme in focus_phonemes_list:
|
| 321 |
+
score = phoneme_score_map.get(
|
| 322 |
+
focus_phoneme, base_result.get("overall_score", 0.0)
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
phoneme_analysis = {
|
| 326 |
"phoneme": focus_phoneme,
|
| 327 |
"score": float(score),
|
| 328 |
"status": "correct" if score > 0.8 else "incorrect",
|
| 329 |
"vietnamese_tip": get_vietnamese_tip(focus_phoneme),
|
| 330 |
"difficulty": "medium",
|
| 331 |
+
"color_class": (
|
| 332 |
+
"bg-green-100 text-green-800"
|
| 333 |
+
if score > 0.8
|
| 334 |
+
else (
|
| 335 |
+
"bg-yellow-100 text-yellow-800"
|
| 336 |
+
if score > 0.6
|
| 337 |
+
else "bg-red-100 text-red-800"
|
| 338 |
+
)
|
| 339 |
+
),
|
| 340 |
}
|
| 341 |
focus_phonemes_analysis.append(phoneme_analysis)
|
| 342 |
+
|
| 343 |
return focus_phonemes_analysis
|
| 344 |
+
|
| 345 |
# Get target phonemes data first
|
| 346 |
final_target_ipa, target_phonemes = await get_target_phonemes_data()
|
| 347 |
+
|
| 348 |
# Run parallel processing for analysis
|
| 349 |
character_analysis, phoneme_scores, focus_phonemes_analysis = await asyncio.gather(
|
| 350 |
create_character_analysis(final_target_ipa, target_phonemes),
|
| 351 |
create_phoneme_scores(target_phonemes),
|
| 352 |
+
create_focus_analysis(),
|
| 353 |
)
|
| 354 |
+
|
| 355 |
# Generate tips and recommendations asynchronously
|
| 356 |
loop = asyncio.get_event_loop()
|
| 357 |
executor = get_shared_executor()
|
|
|
|
| 359 |
executor, generate_vietnamese_tips, target_phonemes, focus_phonemes_list
|
| 360 |
)
|
| 361 |
practice_recommendations_future = loop.run_in_executor(
|
| 362 |
+
executor,
|
| 363 |
+
generate_practice_recommendations,
|
| 364 |
+
base_result.get("overall_score", 0.0),
|
| 365 |
+
focus_phonemes_analysis,
|
| 366 |
)
|
| 367 |
+
|
| 368 |
vietnamese_tips, practice_recommendations = await asyncio.gather(
|
| 369 |
+
vietnamese_tips_future, practice_recommendations_future
|
|
|
|
| 370 |
)
|
| 371 |
+
|
| 372 |
optimization_time = time.time() - start_time
|
| 373 |
logger.info(f"IPA assessment optimization completed in {optimization_time:.3f}s")
|
| 374 |
+
|
| 375 |
return {
|
| 376 |
"target_ipa": final_target_ipa,
|
| 377 |
"character_analysis": character_analysis,
|
| 378 |
"phoneme_scores": phoneme_scores,
|
| 379 |
"focus_phonemes_analysis": focus_phonemes_analysis,
|
| 380 |
"vietnamese_tips": vietnamese_tips,
|
| 381 |
+
"practice_recommendations": practice_recommendations,
|
| 382 |
}
|
| 383 |
|
| 384 |
|
| 385 |
+
def generate_vietnamese_tips(
|
| 386 |
+
target_phonemes: List[str], focus_phonemes_list: List[str]
|
| 387 |
+
) -> List[str]:
|
| 388 |
"""Generate Vietnamese tips for difficult phonemes"""
|
| 389 |
vietnamese_tips = []
|
| 390 |
difficult_phonemes = ["θ", "ð", "v", "z", "ʒ", "r", "w", "æ", "ɪ", "ʊ", "ɛ"]
|
| 391 |
+
|
| 392 |
for phoneme in set(target_phonemes + focus_phonemes_list):
|
| 393 |
if phoneme in difficult_phonemes:
|
| 394 |
tip = get_vietnamese_tip(phoneme)
|
| 395 |
if tip not in vietnamese_tips:
|
| 396 |
vietnamese_tips.append(tip)
|
| 397 |
+
|
| 398 |
return vietnamese_tips
|
| 399 |
|
| 400 |
|
| 401 |
+
def generate_practice_recommendations(
|
| 402 |
+
overall_score: float, focus_phonemes_analysis: List[Dict]
|
| 403 |
+
) -> List[str]:
|
| 404 |
"""Generate practice recommendations based on score"""
|
| 405 |
practice_recommendations = []
|
| 406 |
+
|
| 407 |
if overall_score < 0.7:
|
| 408 |
+
practice_recommendations.extend(
|
| 409 |
+
[
|
| 410 |
+
"Nghe từ mẫu nhiều lần trước khi phát âm",
|
| 411 |
+
"Phát âm chậm và rõ ràng từng âm vị",
|
| 412 |
+
"Chú ý đến vị trí lưỡi và môi khi phát âm",
|
| 413 |
+
]
|
| 414 |
+
)
|
| 415 |
+
|
| 416 |
# Add specific recommendations for focus phonemes
|
| 417 |
for analysis in focus_phonemes_analysis:
|
| 418 |
if analysis["score"] < 0.6:
|
| 419 |
practice_recommendations.append(
|
| 420 |
f"Luyện đặc biệt âm /{analysis['phoneme']}/: {analysis['vietnamese_tip']}"
|
| 421 |
)
|
| 422 |
+
|
| 423 |
if overall_score >= 0.8:
|
| 424 |
+
practice_recommendations.append(
|
| 425 |
+
"Phát âm rất tốt! Tiếp tục luyện tập để duy trì chất lượng"
|
| 426 |
+
)
|
| 427 |
elif overall_score >= 0.6:
|
| 428 |
practice_recommendations.append("Phát âm khá tốt, cần cải thiện một số âm vị")
|
| 429 |
+
|
| 430 |
return practice_recommendations
|
| 431 |
|
| 432 |
|
|
|
|
| 459 |
|
| 460 |
class IPAAssessmentResult(BaseModel):
|
| 461 |
"""Optimized response model for IPA-focused pronunciation assessment"""
|
| 462 |
+
|
| 463 |
# Core assessment data
|
| 464 |
transcript: str # What the user actually said
|
| 465 |
user_ipa: Optional[str] = None # User's IPA transcription
|
| 466 |
target_word: str # Target word being assessed
|
| 467 |
target_ipa: str # Target IPA transcription
|
| 468 |
overall_score: float # Overall pronunciation score (0-1)
|
| 469 |
+
|
| 470 |
# Character-level analysis for IPA mapping
|
| 471 |
character_analysis: List[Dict] # Each character with its IPA and score
|
| 472 |
+
|
| 473 |
# Phoneme-specific analysis
|
| 474 |
phoneme_scores: List[Dict] # Individual phoneme scores with colors
|
| 475 |
focus_phonemes_analysis: List[Dict] # Detailed analysis of target phonemes
|
| 476 |
+
|
| 477 |
# Feedback and recommendations
|
| 478 |
vietnamese_tips: List[str] # Vietnamese-specific pronunciation tips
|
| 479 |
practice_recommendations: List[str] # Practice suggestions
|
| 480 |
feedback: List[str] # General feedback messages
|
| 481 |
+
|
| 482 |
# Assessment metadata
|
| 483 |
processing_info: Dict # Processing details
|
| 484 |
assessment_type: str = "ipa_focused"
|
| 485 |
error: Optional[str] = None
|
| 486 |
|
| 487 |
+
|
| 488 |
# Global assessor instance - singleton pattern for performance
|
| 489 |
global_assessor = None
|
| 490 |
global_g2p = None # Shared G2P instance for caching
|
| 491 |
global_executor = None # Shared ThreadPoolExecutor
|
| 492 |
|
| 493 |
+
|
| 494 |
+
def preload_whisper_model(whisper_model: str = "base.en"):
|
| 495 |
+
"""
|
| 496 |
+
Preload Whisper model during FastAPI startup for faster first inference
|
| 497 |
+
Call this function in your FastAPI startup event
|
| 498 |
+
"""
|
| 499 |
+
global global_assessor
|
| 500 |
+
try:
|
| 501 |
+
logger.info(f"🚀 Preloading Whisper model '{whisper_model}' during startup...")
|
| 502 |
+
start_time = time.time()
|
| 503 |
+
|
| 504 |
+
# Force create the assessor instance which will load Whisper
|
| 505 |
+
global_assessor = ProductionPronunciationAssessor(whisper_model=whisper_model)
|
| 506 |
+
|
| 507 |
+
# Also preload G2P and executor
|
| 508 |
+
get_shared_g2p()
|
| 509 |
+
get_shared_executor()
|
| 510 |
+
|
| 511 |
+
load_time = time.time() - start_time
|
| 512 |
+
logger.info(f"✅ Whisper model '{whisper_model}' preloaded successfully in {load_time:.2f}s")
|
| 513 |
+
logger.info("🎯 First inference will be much faster now!")
|
| 514 |
+
|
| 515 |
+
return True
|
| 516 |
+
except Exception as e:
|
| 517 |
+
logger.error(f"❌ Failed to preload Whisper model: {e}")
|
| 518 |
+
return False
|
| 519 |
+
|
| 520 |
+
|
| 521 |
def get_assessor():
|
| 522 |
+
"""Get or create the global assessor instance with Whisper preloaded"""
|
| 523 |
global global_assessor
|
| 524 |
if global_assessor is None:
|
| 525 |
+
logger.info("Creating global ProductionPronunciationAssessor instance with Whisper...")
|
| 526 |
+
# Load Whisper model base.en by default for optimal performance
|
| 527 |
+
global_assessor = ProductionPronunciationAssessor(whisper_model="base.en")
|
| 528 |
+
logger.info("✅ Global Whisper assessor loaded and ready!")
|
| 529 |
return global_assessor
|
| 530 |
|
| 531 |
|
|
|
|
| 614 |
# Run assessment using enhanced assessor (singleton)
|
| 615 |
assessor = get_assessor()
|
| 616 |
result = assessor.assess_pronunciation(tmp_file.name, reference_text, mode)
|
| 617 |
+
|
| 618 |
# Optimize post-processing with parallel execution
|
| 619 |
await optimize_post_assessment_processing(result, reference_text)
|
| 620 |
|
|
|
|
| 644 |
audio_file: UploadFile = File(..., description="Audio file (.wav, .mp3, .m4a)"),
|
| 645 |
target_word: str = Form(..., description="Target word to assess (e.g., 'bed')"),
|
| 646 |
target_ipa: str = Form(None, description="Target IPA notation (e.g., '/bɛd/')"),
|
| 647 |
+
focus_phonemes: str = Form(
|
| 648 |
+
None, description="Comma-separated focus phonemes (e.g., 'ɛ,b')"
|
| 649 |
+
),
|
| 650 |
):
|
| 651 |
"""
|
| 652 |
Optimized IPA pronunciation assessment for phoneme-focused learning
|
| 653 |
+
|
| 654 |
Evaluates:
|
| 655 |
- Overall word pronunciation accuracy
|
| 656 |
+
- Character-to-phoneme mapping accuracy
|
| 657 |
- Specific phoneme pronunciation (e.g., /ɛ/ in 'bed')
|
| 658 |
- Vietnamese-optimized feedback and tips
|
| 659 |
- Dynamic color scoring for UI visualization
|
| 660 |
+
|
| 661 |
Example: Assessing 'bed' /bɛd/ with focus on /ɛ/ phoneme
|
| 662 |
"""
|
| 663 |
+
|
| 664 |
import time
|
| 665 |
+
|
| 666 |
start_time = time.time()
|
| 667 |
+
|
| 668 |
# Validate inputs
|
| 669 |
if not target_word.strip():
|
| 670 |
raise HTTPException(status_code=400, detail="Target word cannot be empty")
|
| 671 |
+
|
| 672 |
if len(target_word) > 50:
|
| 673 |
+
raise HTTPException(
|
| 674 |
+
status_code=400, detail="Target word too long (max 50 characters)"
|
| 675 |
+
)
|
| 676 |
+
|
| 677 |
# Clean target word
|
| 678 |
target_word = target_word.strip().lower()
|
| 679 |
+
|
| 680 |
try:
|
| 681 |
# Save uploaded file temporarily
|
| 682 |
file_extension = ".wav"
|
| 683 |
if audio_file.filename and "." in audio_file.filename:
|
| 684 |
file_extension = f".{audio_file.filename.split('.')[-1]}"
|
| 685 |
|
| 686 |
+
with tempfile.NamedTemporaryFile(
|
| 687 |
+
delete=False, suffix=file_extension
|
| 688 |
+
) as tmp_file:
|
| 689 |
content = await audio_file.read()
|
| 690 |
tmp_file.write(content)
|
| 691 |
tmp_file.flush()
|
| 692 |
|
| 693 |
+
logger.info(
|
| 694 |
+
f"IPA assessment for word '{target_word}' with IPA '{target_ipa}'"
|
| 695 |
+
)
|
| 696 |
|
| 697 |
# Get the assessor instance
|
| 698 |
assessor = get_assessor()
|
| 699 |
+
|
| 700 |
# Run base pronunciation assessment in word mode
|
| 701 |
+
base_result = assessor.assess_pronunciation(
|
| 702 |
+
tmp_file.name, target_word, "word"
|
| 703 |
+
)
|
| 704 |
+
|
| 705 |
# Optimize IPA assessment processing with parallel execution
|
| 706 |
optimized_results = await optimize_ipa_assessment_processing(
|
| 707 |
base_result, target_word, target_ipa, focus_phonemes
|
| 708 |
)
|
| 709 |
+
|
| 710 |
# Extract optimized results
|
| 711 |
target_ipa = optimized_results["target_ipa"]
|
| 712 |
character_analysis = optimized_results["character_analysis"]
|
|
|
|
| 714 |
focus_phonemes_analysis = optimized_results["focus_phonemes_analysis"]
|
| 715 |
vietnamese_tips = optimized_results["vietnamese_tips"]
|
| 716 |
practice_recommendations = optimized_results["practice_recommendations"]
|
| 717 |
+
|
| 718 |
# Get overall score from base result
|
| 719 |
overall_score = base_result.get("overall_score", 0.0)
|
| 720 |
+
|
| 721 |
# Handle error cases
|
| 722 |
error_message = None
|
| 723 |
feedback = base_result.get("feedback", [])
|
| 724 |
+
|
| 725 |
if base_result.get("error"):
|
| 726 |
error_message = base_result["error"]
|
| 727 |
feedback = [f"Lỗi: {error_message}"]
|
| 728 |
+
|
| 729 |
# Processing information
|
| 730 |
processing_time = time.time() - start_time
|
| 731 |
processing_info = {
|
| 732 |
"processing_time": processing_time,
|
| 733 |
"mode": "ipa_focused",
|
| 734 |
"model_used": "Wav2Vec2-Enhanced",
|
| 735 |
+
"confidence": base_result.get("processing_info", {}).get(
|
| 736 |
+
"confidence", 0.0
|
| 737 |
+
),
|
| 738 |
+
"enhanced_features": True,
|
| 739 |
}
|
| 740 |
+
|
| 741 |
# Create final result
|
| 742 |
result = IPAAssessmentResult(
|
| 743 |
transcript=base_result.get("transcript", ""),
|
|
|
|
| 752 |
practice_recommendations=practice_recommendations,
|
| 753 |
feedback=feedback,
|
| 754 |
processing_info=processing_info,
|
| 755 |
+
error=error_message,
|
| 756 |
)
|
| 757 |
+
|
| 758 |
+
logger.info(
|
| 759 |
+
f"IPA assessment completed for '{target_word}' in {processing_time:.2f}s with score {overall_score:.2f}"
|
| 760 |
+
)
|
| 761 |
+
|
| 762 |
return result
|
| 763 |
|
| 764 |
except Exception as e:
|
| 765 |
logger.error(f"IPA assessment error: {str(e)}")
|
| 766 |
import traceback
|
| 767 |
+
|
| 768 |
traceback.print_exc()
|
| 769 |
raise HTTPException(status_code=500, detail=f"IPA assessment failed: {str(e)}")
|
| 770 |
|
|
|
|
| 778 |
def get_word_phonemes(word: str):
|
| 779 |
"""Get phoneme breakdown for a specific word"""
|
| 780 |
try:
|
| 781 |
+
# Use the shared G2P instance for consistency
|
| 782 |
+
g2p = get_shared_g2p()
|
|
|
|
| 783 |
phoneme_data = g2p.text_to_phonemes(word)[0]
|
| 784 |
|
| 785 |
# Add difficulty analysis for Vietnamese speakers
|
| 786 |
difficulty_scores = []
|
| 787 |
+
|
| 788 |
for phoneme in phoneme_data["phonemes"]:
|
| 789 |
difficulty = g2p.get_difficulty_score(phoneme)
|
| 790 |
difficulty_scores.append(difficulty)
|
|
|
|
| 841 |
"d": "Lưỡi chạm nướu răng trên, rung dây thanh",
|
| 842 |
"t": "Lưỡi chạm nướu răng trên, không rung dây thanh",
|
| 843 |
"k": "Lưỡi chạm vòm miệng, không rung dây thanh",
|
| 844 |
+
"g": "Lưỡi chạm vòm miệng, rung dây thanh",
|
| 845 |
}
|
| 846 |
return tips.get(phoneme, f"Luyện tập phát âm /{phoneme}/")
|
| 847 |
|
|
|
|
| 850 |
"""Get difficulty level for Vietnamese speakers"""
|
| 851 |
hard_phonemes = ["θ", "ð", "r", "w", "æ", "ʌ", "ɪ", "ʊ"]
|
| 852 |
medium_phonemes = ["v", "z", "ʒ", "ɛ", "ə", "ɔ", "f"]
|
| 853 |
+
|
| 854 |
if phoneme in hard_phonemes:
|
| 855 |
return "hard"
|
| 856 |
elif phoneme in medium_phonemes:
|
| 857 |
return "medium"
|
| 858 |
else:
|
| 859 |
+
return "easy"
|
test_performance_optimization.py
CHANGED
|
@@ -53,7 +53,7 @@ IPA_TEST_CASES = [
|
|
| 53 |
}
|
| 54 |
]
|
| 55 |
|
| 56 |
-
BASE_URL = "http://localhost:8000/
|
| 57 |
|
| 58 |
class PerformanceTracker:
|
| 59 |
"""Track performance metrics"""
|
|
|
|
| 53 |
}
|
| 54 |
]
|
| 55 |
|
| 56 |
+
BASE_URL = "http://localhost:8000/speaking"
|
| 57 |
|
| 58 |
class PerformanceTracker:
|
| 59 |
"""Track performance metrics"""
|