Update app.py
Browse files
app.py
CHANGED
@@ -67,7 +67,8 @@ try:
|
|
67 |
)
|
68 |
from evaluate import (
|
69 |
handle_evaluation_request, handle_upload_reference,
|
70 |
-
init_reference_audio, calculate_similarity
|
|
|
71 |
)
|
72 |
|
73 |
logger.info("β
All required libraries imported successfully")
|
@@ -248,7 +249,7 @@ def home():
|
|
248 |
return jsonify({
|
249 |
"message": "Speech API is running",
|
250 |
"status": "active",
|
251 |
-
"version": "1.
|
252 |
"environment": "Hugging Face Spaces"
|
253 |
})
|
254 |
|
@@ -272,6 +273,9 @@ def health_check():
|
|
272 |
"translation_cache_size": len(translation_cache)
|
273 |
}
|
274 |
|
|
|
|
|
|
|
275 |
return jsonify(health_status)
|
276 |
|
277 |
# ASR with optimizations
|
@@ -409,6 +413,12 @@ def evaluate_pronunciation():
|
|
409 |
user_output_dir = g.user_output_dir if hasattr(g, 'user_output_dir') else OUTPUT_DIR
|
410 |
return handle_evaluation_request(request, REFERENCE_AUDIO_DIR, user_output_dir, SAMPLE_RATE)
|
411 |
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
@app.route("/check_references", methods=["GET"])
|
413 |
def check_references():
|
414 |
"""Optimized endpoint to check if reference files exist"""
|
@@ -433,7 +443,8 @@ def check_references():
|
|
433 |
"directory_exists": os.path.exists(REFERENCE_AUDIO_DIR),
|
434 |
"total_patterns": len(ref_patterns),
|
435 |
"existing_patterns": 0,
|
436 |
-
"total_files": 0
|
|
|
437 |
}
|
438 |
|
439 |
for pattern in ref_patterns:
|
@@ -479,7 +490,8 @@ def check_references_detailed():
|
|
479 |
|
480 |
return jsonify({
|
481 |
"reference_audio_dir": REFERENCE_AUDIO_DIR,
|
482 |
-
"patterns": results
|
|
|
483 |
})
|
484 |
|
485 |
@app.route("/upload_reference", methods=["POST"])
|
@@ -487,6 +499,28 @@ def check_references_detailed():
|
|
487 |
def upload_reference_audio():
|
488 |
return handle_upload_reference(request, REFERENCE_AUDIO_DIR, SAMPLE_RATE)
|
489 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
490 |
# Add a cleanup endpoint
|
491 |
@app.route("/cleanup", methods=["POST"])
|
492 |
def cleanup_files():
|
@@ -548,5 +582,10 @@ if __name__ == "__main__":
|
|
548 |
for lang, model_status in status['tts_models'].items():
|
549 |
logger.info(f"π TTS model {lang}: {'β
' if model_status == 'loaded' else 'β'}")
|
550 |
|
|
|
|
|
|
|
|
|
|
|
551 |
# Use threaded=True for better performance
|
552 |
app.run(host="0.0.0.0", port=7860, debug=False, threaded=True)
|
|
|
67 |
)
|
68 |
from evaluate import (
|
69 |
handle_evaluation_request, handle_upload_reference,
|
70 |
+
init_reference_audio, calculate_similarity, preprocess_all_references,
|
71 |
+
get_preprocessing_status # Import the new function
|
72 |
)
|
73 |
|
74 |
logger.info("β
All required libraries imported successfully")
|
|
|
249 |
return jsonify({
|
250 |
"message": "Speech API is running",
|
251 |
"status": "active",
|
252 |
+
"version": "1.2", # Updated version to reflect reference preprocessing
|
253 |
"environment": "Hugging Face Spaces"
|
254 |
})
|
255 |
|
|
|
273 |
"translation_cache_size": len(translation_cache)
|
274 |
}
|
275 |
|
276 |
+
# Add reference preprocessing status
|
277 |
+
health_status["reference_preprocessing"] = get_preprocessing_status()
|
278 |
+
|
279 |
return jsonify(health_status)
|
280 |
|
281 |
# ASR with optimizations
|
|
|
413 |
user_output_dir = g.user_output_dir if hasattr(g, 'user_output_dir') else OUTPUT_DIR
|
414 |
return handle_evaluation_request(request, REFERENCE_AUDIO_DIR, user_output_dir, SAMPLE_RATE)
|
415 |
|
416 |
+
# New endpoint to check preprocessing status
|
417 |
+
@app.route("/reference_preprocessing_status", methods=["GET"])
|
418 |
+
def reference_preprocessing_status():
|
419 |
+
"""Get the current status of reference audio preprocessing"""
|
420 |
+
return jsonify(get_preprocessing_status())
|
421 |
+
|
422 |
@app.route("/check_references", methods=["GET"])
|
423 |
def check_references():
|
424 |
"""Optimized endpoint to check if reference files exist"""
|
|
|
443 |
"directory_exists": os.path.exists(REFERENCE_AUDIO_DIR),
|
444 |
"total_patterns": len(ref_patterns),
|
445 |
"existing_patterns": 0,
|
446 |
+
"total_files": 0,
|
447 |
+
"preprocessing_status": get_preprocessing_status() # Add preprocessing status
|
448 |
}
|
449 |
|
450 |
for pattern in ref_patterns:
|
|
|
490 |
|
491 |
return jsonify({
|
492 |
"reference_audio_dir": REFERENCE_AUDIO_DIR,
|
493 |
+
"patterns": results,
|
494 |
+
"preprocessing_status": get_preprocessing_status() # Add preprocessing status
|
495 |
})
|
496 |
|
497 |
@app.route("/upload_reference", methods=["POST"])
|
|
|
499 |
def upload_reference_audio():
|
500 |
return handle_upload_reference(request, REFERENCE_AUDIO_DIR, SAMPLE_RATE)
|
501 |
|
502 |
+
# Add an endpoint to manually trigger reference preprocessing
|
503 |
+
@app.route("/preprocess_references", methods=["POST"])
|
504 |
+
def manual_preprocess_references():
|
505 |
+
"""Manually trigger reference audio preprocessing"""
|
506 |
+
# Only allow from local or with API key
|
507 |
+
if not (request.remote_addr == '127.0.0.1' or
|
508 |
+
request.headers.get('X-Admin-Key') == os.environ.get('ADMIN_KEY', 'admin-secret')):
|
509 |
+
return jsonify({"error": "Unauthorized"}), 403
|
510 |
+
|
511 |
+
# Start preprocessing in a background thread to avoid blocking
|
512 |
+
def preprocess_worker():
|
513 |
+
preprocess_all_references(REFERENCE_AUDIO_DIR, SAMPLE_RATE)
|
514 |
+
|
515 |
+
preprocessing_thread = threading.Thread(target=preprocess_worker)
|
516 |
+
preprocessing_thread.daemon = True
|
517 |
+
preprocessing_thread.start()
|
518 |
+
|
519 |
+
return jsonify({
|
520 |
+
"message": "Reference preprocessing started in background",
|
521 |
+
"current_status": get_preprocessing_status()
|
522 |
+
})
|
523 |
+
|
524 |
# Add a cleanup endpoint
|
525 |
@app.route("/cleanup", methods=["POST"])
|
526 |
def cleanup_files():
|
|
|
582 |
for lang, model_status in status['tts_models'].items():
|
583 |
logger.info(f"π TTS model {lang}: {'β
' if model_status == 'loaded' else 'β'}")
|
584 |
|
585 |
+
# Log reference preprocessing status
|
586 |
+
preproc_status = get_preprocessing_status()
|
587 |
+
logger.info(f"π Reference preprocessing: {'β
Complete' if preproc_status['complete'] else 'π In progress'}")
|
588 |
+
logger.info(f"π Preprocessed files: {preproc_status['preprocessed_files']}")
|
589 |
+
|
590 |
# Use threaded=True for better performance
|
591 |
app.run(host="0.0.0.0", port=7860, debug=False, threaded=True)
|