Update evaluate.py
Browse files- evaluate.py +369 -340
evaluate.py
CHANGED
|
@@ -1,341 +1,370 @@
|
|
| 1 |
-
# evaluate.py - Handles evaluation and comparing tasks
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import glob
|
| 5 |
-
import logging
|
| 6 |
-
import traceback
|
| 7 |
-
import tempfile
|
| 8 |
-
import shutil
|
| 9 |
-
from difflib import SequenceMatcher
|
| 10 |
-
import torch
|
| 11 |
-
import torchaudio
|
| 12 |
-
from pydub import AudioSegment
|
| 13 |
-
from flask import jsonify
|
| 14 |
-
from werkzeug.utils import secure_filename
|
| 15 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 16 |
-
|
| 17 |
-
# Import necessary functions from translator.py
|
| 18 |
-
from translator import asr_model, asr_processor, LANGUAGE_CODES
|
| 19 |
-
|
| 20 |
-
# Configure logging
|
| 21 |
-
logger = logging.getLogger("speech_api")
|
| 22 |
-
|
| 23 |
-
def calculate_similarity(text1, text2):
|
| 24 |
-
"""Calculate text similarity percentage."""
|
| 25 |
-
def clean_text(text):
|
| 26 |
-
return text.lower()
|
| 27 |
-
|
| 28 |
-
clean1 = clean_text(text1)
|
| 29 |
-
clean2 = clean_text(text2)
|
| 30 |
-
|
| 31 |
-
matcher = SequenceMatcher(None, clean1, clean2)
|
| 32 |
-
return matcher.ratio() * 100
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
f"[{request_id}]
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
logger.
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
"
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
return jsonify({"error": f"Internal server error: {str(e)}"}), 500
|
|
|
|
| 1 |
+
# evaluate.py - Handles evaluation and comparing tasks
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import glob
|
| 5 |
+
import logging
|
| 6 |
+
import traceback
|
| 7 |
+
import tempfile
|
| 8 |
+
import shutil
|
| 9 |
+
from difflib import SequenceMatcher
|
| 10 |
+
import torch
|
| 11 |
+
import torchaudio
|
| 12 |
+
from pydub import AudioSegment
|
| 13 |
+
from flask import jsonify
|
| 14 |
+
from werkzeug.utils import secure_filename
|
| 15 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 16 |
+
|
| 17 |
+
# Import necessary functions from translator.py
|
| 18 |
+
from translator import asr_model, asr_processor, LANGUAGE_CODES
|
| 19 |
+
|
| 20 |
+
# Configure logging
|
| 21 |
+
logger = logging.getLogger("speech_api")
|
| 22 |
+
|
| 23 |
+
def calculate_similarity(text1, text2):
|
| 24 |
+
"""Calculate text similarity percentage."""
|
| 25 |
+
def clean_text(text):
|
| 26 |
+
return text.lower()
|
| 27 |
+
|
| 28 |
+
clean1 = clean_text(text1)
|
| 29 |
+
clean2 = clean_text(text2)
|
| 30 |
+
|
| 31 |
+
matcher = SequenceMatcher(None, clean1, clean2)
|
| 32 |
+
return matcher.ratio() * 100
|
| 33 |
+
|
| 34 |
+
# In evaluate.py, modify the init_reference_audio function
|
| 35 |
+
|
| 36 |
+
def init_reference_audio(reference_dir, output_dir):
|
| 37 |
+
try:
|
| 38 |
+
# Create the output directory first
|
| 39 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 40 |
+
logger.info(f"π Created output directory: {output_dir}")
|
| 41 |
+
|
| 42 |
+
# Change reference_dir to be inside /tmp if the original location doesn't work
|
| 43 |
+
if not os.path.exists(reference_dir) or not os.access(os.path.dirname(reference_dir), os.W_OK):
|
| 44 |
+
# Use a directory in /tmp instead
|
| 45 |
+
new_reference_dir = os.path.join('/tmp', 'reference_audio')
|
| 46 |
+
logger.warning(f"β οΈ Changing reference directory from {reference_dir} to {new_reference_dir}")
|
| 47 |
+
reference_dir = new_reference_dir
|
| 48 |
+
|
| 49 |
+
# Check if the reference audio directory exists
|
| 50 |
+
if os.path.exists(reference_dir):
|
| 51 |
+
logger.info(f"β
Found reference audio directory: {reference_dir}")
|
| 52 |
+
|
| 53 |
+
# Log the contents to verify
|
| 54 |
+
pattern_dirs = [d for d in os.listdir(reference_dir)
|
| 55 |
+
if os.path.isdir(os.path.join(reference_dir, d))]
|
| 56 |
+
logger.info(f"π Found reference patterns: {pattern_dirs}")
|
| 57 |
+
|
| 58 |
+
# Check each pattern directory for wav files
|
| 59 |
+
for pattern_dir_name in pattern_dirs:
|
| 60 |
+
pattern_path = os.path.join(reference_dir, pattern_dir_name)
|
| 61 |
+
wav_files = glob.glob(os.path.join(pattern_path, "*.wav"))
|
| 62 |
+
logger.info(f"π Found {len(wav_files)} wav files in {pattern_dir_name}")
|
| 63 |
+
else:
|
| 64 |
+
logger.warning(f"β οΈ Reference audio directory not found: {reference_dir}")
|
| 65 |
+
# Create the directory if it doesn't exist
|
| 66 |
+
try:
|
| 67 |
+
os.makedirs(reference_dir, exist_ok=True)
|
| 68 |
+
logger.info(f"π Created reference audio directory: {reference_dir}")
|
| 69 |
+
except PermissionError:
|
| 70 |
+
logger.error(f"β Permission denied when creating {reference_dir}")
|
| 71 |
+
# Try alternate location in /tmp
|
| 72 |
+
reference_dir = os.path.join('/tmp', 'reference_audio')
|
| 73 |
+
os.makedirs(reference_dir, exist_ok=True)
|
| 74 |
+
logger.info(f"π Created reference audio directory in alternate location: {reference_dir}")
|
| 75 |
+
|
| 76 |
+
# Return the (possibly updated) reference directory path
|
| 77 |
+
return reference_dir
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"β Failed to set up reference audio directory: {str(e)}")
|
| 81 |
+
# Use /tmp as fallback
|
| 82 |
+
fallback_dir = os.path.join('/tmp', 'reference_audio')
|
| 83 |
+
try:
|
| 84 |
+
os.makedirs(fallback_dir, exist_ok=True)
|
| 85 |
+
logger.info(f"π Created fallback reference audio directory: {fallback_dir}")
|
| 86 |
+
return fallback_dir
|
| 87 |
+
except Exception as e2:
|
| 88 |
+
logger.critical(f"β Failed to create fallback directory: {str(e2)}")
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
def handle_upload_reference(request, reference_dir, sample_rate):
|
| 92 |
+
"""Handle upload of reference audio files"""
|
| 93 |
+
try:
|
| 94 |
+
if "audio" not in request.files:
|
| 95 |
+
logger.warning("β οΈ Reference upload missing audio file")
|
| 96 |
+
return jsonify({"error": "No audio file uploaded"}), 400
|
| 97 |
+
|
| 98 |
+
reference_word = request.form.get("reference_word", "").strip()
|
| 99 |
+
if not reference_word:
|
| 100 |
+
logger.warning("β οΈ Reference upload missing reference word")
|
| 101 |
+
return jsonify({"error": "No reference word provided"}), 400
|
| 102 |
+
|
| 103 |
+
# Validate reference word
|
| 104 |
+
reference_patterns = [
|
| 105 |
+
"mayap_a_abak", "mayap_a_ugtu", "mayap_a_gatpanapun", "mayap_a_bengi",
|
| 106 |
+
"komusta_ka", "malaus_ko_pu", "malaus_kayu", "agaganaka_da_ka",
|
| 107 |
+
"pagdulapan_da_ka", "kaluguran_da_ka", "dakal_a_salamat", "panapaya_mu_ku"
|
| 108 |
+
]
|
| 109 |
+
|
| 110 |
+
if reference_word not in reference_patterns:
|
| 111 |
+
logger.warning(f"β οΈ Invalid reference word: {reference_word}")
|
| 112 |
+
return jsonify({"error": f"Invalid reference word. Available: {reference_patterns}"}), 400
|
| 113 |
+
|
| 114 |
+
# Create directory for reference pattern if it doesn't exist
|
| 115 |
+
pattern_dir = os.path.join(reference_dir, reference_word)
|
| 116 |
+
os.makedirs(pattern_dir, exist_ok=True)
|
| 117 |
+
|
| 118 |
+
# Save the reference audio file
|
| 119 |
+
audio_file = request.files["audio"]
|
| 120 |
+
file_path = os.path.join(pattern_dir, secure_filename(audio_file.filename))
|
| 121 |
+
audio_file.save(file_path)
|
| 122 |
+
|
| 123 |
+
# Convert to WAV if not already in that format
|
| 124 |
+
if not file_path.lower().endswith('.wav'):
|
| 125 |
+
base_path = os.path.splitext(file_path)[0]
|
| 126 |
+
wav_path = f"{base_path}.wav"
|
| 127 |
+
try:
|
| 128 |
+
audio = AudioSegment.from_file(file_path)
|
| 129 |
+
audio = audio.set_frame_rate(sample_rate).set_channels(1)
|
| 130 |
+
audio.export(wav_path, format="wav")
|
| 131 |
+
# Remove original file if conversion successful
|
| 132 |
+
os.unlink(file_path)
|
| 133 |
+
file_path = wav_path
|
| 134 |
+
except Exception as e:
|
| 135 |
+
logger.error(f"β Reference audio conversion failed: {str(e)}")
|
| 136 |
+
return jsonify({"error": f"Audio conversion failed: {str(e)}"}), 500
|
| 137 |
+
|
| 138 |
+
logger.info(f"β
Reference audio saved successfully for {reference_word}: {file_path}")
|
| 139 |
+
|
| 140 |
+
# Count how many references we have now
|
| 141 |
+
references = glob.glob(os.path.join(pattern_dir, "*.wav"))
|
| 142 |
+
return jsonify({
|
| 143 |
+
"message": "Reference audio uploaded successfully",
|
| 144 |
+
"reference_word": reference_word,
|
| 145 |
+
"file": os.path.basename(file_path),
|
| 146 |
+
"total_references": len(references)
|
| 147 |
+
})
|
| 148 |
+
|
| 149 |
+
except Exception as e:
|
| 150 |
+
logger.error(f"β Unhandled exception in reference upload: {str(e)}")
|
| 151 |
+
logger.debug(f"Stack trace: {traceback.format_exc()}")
|
| 152 |
+
return jsonify({"error": f"Internal server error: {str(e)}"}), 500
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def handle_evaluation_request(request, reference_dir, output_dir, sample_rate):
|
| 156 |
+
"""Handle pronunciation evaluation requests"""
|
| 157 |
+
request_id = f"req-{id(request)}" # Create unique ID for this request
|
| 158 |
+
logger.info(f"[{request_id}] π Starting new pronunciation evaluation request")
|
| 159 |
+
|
| 160 |
+
temp_dir = None
|
| 161 |
+
|
| 162 |
+
if asr_model is None or asr_processor is None:
|
| 163 |
+
logger.error(f"[{request_id}] β Evaluation endpoint called but ASR models aren't loaded")
|
| 164 |
+
return jsonify({"error": "ASR model not available"}), 503
|
| 165 |
+
|
| 166 |
+
try:
|
| 167 |
+
if "audio" not in request.files:
|
| 168 |
+
logger.warning(f"[{request_id}] β οΈ Evaluation request missing audio file")
|
| 169 |
+
return jsonify({"error": "No audio file uploaded"}), 400
|
| 170 |
+
|
| 171 |
+
audio_file = request.files["audio"]
|
| 172 |
+
reference_locator = request.form.get("reference_locator", "").strip()
|
| 173 |
+
language = request.form.get("language", "kapampangan").lower()
|
| 174 |
+
|
| 175 |
+
# Validate reference locator
|
| 176 |
+
if not reference_locator:
|
| 177 |
+
logger.warning(f"[{request_id}] β οΈ No reference locator provided")
|
| 178 |
+
return jsonify({"error": "Reference locator is required"}), 400
|
| 179 |
+
|
| 180 |
+
# Construct full reference directory path
|
| 181 |
+
reference_dir_path = os.path.join(reference_dir, reference_locator)
|
| 182 |
+
logger.info(f"[{request_id}] π Reference directory path: {reference_dir_path}")
|
| 183 |
+
|
| 184 |
+
if not os.path.exists(reference_dir_path):
|
| 185 |
+
logger.warning(f"[{request_id}] β οΈ Reference directory not found: {reference_dir_path}")
|
| 186 |
+
return jsonify({"error": f"Reference audio directory not found: {reference_locator}"}), 404
|
| 187 |
+
|
| 188 |
+
reference_files = glob.glob(os.path.join(reference_dir_path, "*.wav"))
|
| 189 |
+
logger.info(f"[{request_id}] π Found {len(reference_files)} reference files")
|
| 190 |
+
|
| 191 |
+
if not reference_files:
|
| 192 |
+
logger.warning(f"[{request_id}] β οΈ No reference audio files found in {reference_dir_path}")
|
| 193 |
+
return jsonify({"error": f"No reference audio found for {reference_locator}"}), 404
|
| 194 |
+
|
| 195 |
+
lang_code = LANGUAGE_CODES.get(language, language)
|
| 196 |
+
logger.info(
|
| 197 |
+
f"[{request_id}] π Evaluating pronunciation for reference: {reference_locator} with language code: {lang_code}")
|
| 198 |
+
|
| 199 |
+
# Create a request-specific temp directory to avoid conflicts
|
| 200 |
+
temp_dir = os.path.join(output_dir, f"temp_{request_id}")
|
| 201 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 202 |
+
|
| 203 |
+
# Process user audio
|
| 204 |
+
user_audio_path = os.path.join(temp_dir, "user_audio_input.wav")
|
| 205 |
+
with open(user_audio_path, 'wb') as f:
|
| 206 |
+
f.write(audio_file.read())
|
| 207 |
+
|
| 208 |
+
try:
|
| 209 |
+
logger.info(f"[{request_id}] π Processing user audio file")
|
| 210 |
+
audio = AudioSegment.from_file(user_audio_path)
|
| 211 |
+
audio = audio.set_frame_rate(sample_rate).set_channels(1)
|
| 212 |
+
|
| 213 |
+
processed_path = os.path.join(temp_dir, "processed_user_audio.wav")
|
| 214 |
+
audio.export(processed_path, format="wav")
|
| 215 |
+
|
| 216 |
+
user_waveform, sr = torchaudio.load(processed_path)
|
| 217 |
+
user_waveform = user_waveform.squeeze().numpy()
|
| 218 |
+
logger.info(f"[{request_id}] β
User audio processed: {sr}Hz, length: {len(user_waveform)} samples")
|
| 219 |
+
|
| 220 |
+
user_audio_path = processed_path
|
| 221 |
+
except Exception as e:
|
| 222 |
+
logger.error(f"[{request_id}] β Audio processing failed: {str(e)}")
|
| 223 |
+
return jsonify({"error": f"Audio processing failed: {str(e)}"}), 500
|
| 224 |
+
|
| 225 |
+
# Transcribe user audio
|
| 226 |
+
try:
|
| 227 |
+
logger.info(f"[{request_id}] π Transcribing user audio")
|
| 228 |
+
inputs = asr_processor(
|
| 229 |
+
user_waveform,
|
| 230 |
+
sampling_rate=sample_rate,
|
| 231 |
+
return_tensors="pt",
|
| 232 |
+
language=lang_code
|
| 233 |
+
)
|
| 234 |
+
inputs = {k: v.to(asr_model.device) for k, v in inputs.items()}
|
| 235 |
+
|
| 236 |
+
with torch.no_grad():
|
| 237 |
+
logits = asr_model(**inputs).logits
|
| 238 |
+
ids = torch.argmax(logits, dim=-1)[0]
|
| 239 |
+
user_transcription = asr_processor.decode(ids)
|
| 240 |
+
|
| 241 |
+
logger.info(f"[{request_id}] β
User transcription: '{user_transcription}'")
|
| 242 |
+
except Exception as e:
|
| 243 |
+
logger.error(f"[{request_id}] β ASR inference failed: {str(e)}")
|
| 244 |
+
return jsonify({"error": f"ASR inference failed: {str(e)}"}), 500
|
| 245 |
+
|
| 246 |
+
# Process reference files in batches
|
| 247 |
+
batch_size = 2 # Process 2 files at a time - adjust based on your hardware
|
| 248 |
+
results = []
|
| 249 |
+
best_score = 0
|
| 250 |
+
best_reference = None
|
| 251 |
+
best_transcription = None
|
| 252 |
+
|
| 253 |
+
# Use this if you want to limit the number of files to process
|
| 254 |
+
max_files_to_check = min(5, len(reference_files)) # Check at most 5 files
|
| 255 |
+
reference_files = reference_files[:max_files_to_check]
|
| 256 |
+
|
| 257 |
+
logger.info(f"[{request_id}] π Processing {len(reference_files)} reference files in batches of {batch_size}")
|
| 258 |
+
|
| 259 |
+
# Function to process a single reference file
|
| 260 |
+
def process_reference_file(ref_file):
|
| 261 |
+
ref_filename = os.path.basename(ref_file)
|
| 262 |
+
try:
|
| 263 |
+
# Load and resample reference audio
|
| 264 |
+
ref_waveform, ref_sr = torchaudio.load(ref_file)
|
| 265 |
+
if ref_sr != sample_rate:
|
| 266 |
+
ref_waveform = torchaudio.transforms.Resample(ref_sr, sample_rate)(ref_waveform)
|
| 267 |
+
ref_waveform = ref_waveform.squeeze().numpy()
|
| 268 |
+
|
| 269 |
+
# Transcribe reference audio
|
| 270 |
+
inputs = asr_processor(
|
| 271 |
+
ref_waveform,
|
| 272 |
+
sampling_rate=sample_rate,
|
| 273 |
+
return_tensors="pt",
|
| 274 |
+
language=lang_code
|
| 275 |
+
)
|
| 276 |
+
inputs = {k: v.to(asr_model.device) for k, v in inputs.items()}
|
| 277 |
+
|
| 278 |
+
with torch.no_grad():
|
| 279 |
+
logits = asr_model(**inputs).logits
|
| 280 |
+
ids = torch.argmax(logits, dim=-1)[0]
|
| 281 |
+
ref_transcription = asr_processor.decode(ids)
|
| 282 |
+
|
| 283 |
+
# Calculate similarity
|
| 284 |
+
similarity = calculate_similarity(user_transcription, ref_transcription)
|
| 285 |
+
|
| 286 |
+
logger.info(
|
| 287 |
+
f"[{request_id}] π Similarity with {ref_filename}: {similarity:.2f}%, transcription: '{ref_transcription}'")
|
| 288 |
+
|
| 289 |
+
return {
|
| 290 |
+
"reference_file": ref_filename,
|
| 291 |
+
"reference_text": ref_transcription,
|
| 292 |
+
"similarity_score": similarity
|
| 293 |
+
}
|
| 294 |
+
except Exception as e:
|
| 295 |
+
logger.error(f"[{request_id}] β Error processing {ref_filename}: {str(e)}")
|
| 296 |
+
return {
|
| 297 |
+
"reference_file": ref_filename,
|
| 298 |
+
"reference_text": "Error",
|
| 299 |
+
"similarity_score": 0,
|
| 300 |
+
"error": str(e)
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
# Process files in batches using ThreadPoolExecutor
|
| 304 |
+
with ThreadPoolExecutor(max_workers=batch_size) as executor:
|
| 305 |
+
batch_results = list(executor.map(process_reference_file, reference_files))
|
| 306 |
+
results.extend(batch_results)
|
| 307 |
+
|
| 308 |
+
# Find the best result
|
| 309 |
+
for result in batch_results:
|
| 310 |
+
if result["similarity_score"] > best_score:
|
| 311 |
+
best_score = result["similarity_score"]
|
| 312 |
+
best_reference = result["reference_file"]
|
| 313 |
+
best_transcription = result["reference_text"]
|
| 314 |
+
|
| 315 |
+
# Exit early if we found a very good match (optional)
|
| 316 |
+
if best_score > 80.0:
|
| 317 |
+
logger.info(f"[{request_id}] π Found excellent match: {best_score:.2f}%")
|
| 318 |
+
break
|
| 319 |
+
|
| 320 |
+
# Clean up temp files
|
| 321 |
+
try:
|
| 322 |
+
if temp_dir and os.path.exists(temp_dir):
|
| 323 |
+
shutil.rmtree(temp_dir)
|
| 324 |
+
logger.debug(f"[{request_id}] π§Ή Cleaned up temporary directory")
|
| 325 |
+
except Exception as e:
|
| 326 |
+
logger.warning(f"[{request_id}] β οΈ Failed to clean up temp files: {str(e)}")
|
| 327 |
+
|
| 328 |
+
# Determine feedback based on score
|
| 329 |
+
is_correct = best_score >= 70.0
|
| 330 |
+
|
| 331 |
+
if best_score >= 90.0:
|
| 332 |
+
feedback = "Perfect pronunciation! Excellent job!"
|
| 333 |
+
elif best_score >= 80.0:
|
| 334 |
+
feedback = "Great pronunciation! Your accent is very good."
|
| 335 |
+
elif best_score >= 70.0:
|
| 336 |
+
feedback = "Good pronunciation. Keep practicing!"
|
| 337 |
+
elif best_score >= 50.0:
|
| 338 |
+
feedback = "Fair attempt. Try focusing on the syllables that differ from the sample."
|
| 339 |
+
else:
|
| 340 |
+
feedback = "Try again. Listen carefully to the sample pronunciation."
|
| 341 |
+
|
| 342 |
+
logger.info(f"[{request_id}] π Final evaluation results: score={best_score:.2f}%, is_correct={is_correct}")
|
| 343 |
+
logger.info(f"[{request_id}] π Feedback: '{feedback}'")
|
| 344 |
+
logger.info(f"[{request_id}] β
Evaluation complete")
|
| 345 |
+
|
| 346 |
+
# Sort results by score descending
|
| 347 |
+
results.sort(key=lambda x: x["similarity_score"], reverse=True)
|
| 348 |
+
|
| 349 |
+
return jsonify({
|
| 350 |
+
"is_correct": is_correct,
|
| 351 |
+
"score": best_score,
|
| 352 |
+
"feedback": feedback,
|
| 353 |
+
"user_transcription": user_transcription,
|
| 354 |
+
"best_reference_transcription": best_transcription,
|
| 355 |
+
"reference_locator": reference_locator,
|
| 356 |
+
"details": results
|
| 357 |
+
})
|
| 358 |
+
|
| 359 |
+
except Exception as e:
|
| 360 |
+
logger.error(f"[{request_id}] β Unhandled exception in evaluation endpoint: {str(e)}")
|
| 361 |
+
logger.debug(f"[{request_id}] Stack trace: {traceback.format_exc()}")
|
| 362 |
+
|
| 363 |
+
# Clean up on error
|
| 364 |
+
try:
|
| 365 |
+
if temp_dir and os.path.exists(temp_dir):
|
| 366 |
+
shutil.rmtree(temp_dir)
|
| 367 |
+
except:
|
| 368 |
+
pass
|
| 369 |
+
|
| 370 |
return jsonify({"error": f"Internal server error: {str(e)}"}), 500
|