Spaces:
Runtime error
Runtime error
Create ui/handlers.py
Browse files- ui/handlers.py +89 -0
ui/handlers.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from core.audio_verifier import verify_audio
|
| 3 |
+
from core.uploader import upload_audio
|
| 4 |
+
from core.model_loader import is_ready
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def handle_verify(audio_path: str):
|
| 8 |
+
"""
|
| 9 |
+
Kiểm tra mẫu âm thanh.
|
| 10 |
+
Returns: (status_text, btn_send_update, score)
|
| 11 |
+
"""
|
| 12 |
+
if not is_ready():
|
| 13 |
+
return (
|
| 14 |
+
"⏳ Model đang khởi động, thử lại sau vài giây...",
|
| 15 |
+
gr.update(interactive=False),
|
| 16 |
+
0.0
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if not audio_path:
|
| 20 |
+
return (
|
| 21 |
+
"⚠️ Hãy ghi âm trước khi kiểm tra!",
|
| 22 |
+
gr.update(interactive=False),
|
| 23 |
+
0.0
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
passed, score = verify_audio(audio_path)
|
| 28 |
+
except RuntimeError as e:
|
| 29 |
+
return str(e), gr.update(interactive=False), 0.0
|
| 30 |
+
except ValueError as e:
|
| 31 |
+
return str(e), gr.update(interactive=False), 0.0
|
| 32 |
+
|
| 33 |
+
if passed:
|
| 34 |
+
return (
|
| 35 |
+
f"✅ Hợp lệ! Điểm: {score:.2f} — Nhấn GỬI để lưu mẫu",
|
| 36 |
+
gr.update(interactive=True),
|
| 37 |
+
score
|
| 38 |
+
)
|
| 39 |
+
else:
|
| 40 |
+
return (
|
| 41 |
+
f"❌ Chưa đạt (Điểm: {score:.2f}). Hãy nói rõ hơn và thử lại!",
|
| 42 |
+
gr.update(interactive=False),
|
| 43 |
+
score
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def handle_upload(audio_path: str, score: float):
|
| 48 |
+
"""
|
| 49 |
+
Upload mẫu lên HuggingFace.
|
| 50 |
+
Returns: (status_text, btn_send_update, audio_reset)
|
| 51 |
+
"""
|
| 52 |
+
if not audio_path:
|
| 53 |
+
return (
|
| 54 |
+
"❌ Không có file để gửi.",
|
| 55 |
+
gr.update(interactive=False),
|
| 56 |
+
None
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
filename = upload_audio(audio_path, score)
|
| 61 |
+
return (
|
| 62 |
+
f"🎉 Gửi thành công ({filename})! Bạn có thể thu mẫu tiếp theo.",
|
| 63 |
+
gr.update(interactive=False),
|
| 64 |
+
None
|
| 65 |
+
)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return (
|
| 68 |
+
f"❌ Lỗi gửi: {e}",
|
| 69 |
+
gr.update(interactive=True),
|
| 70 |
+
audio_path
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def handle_reset():
|
| 75 |
+
"""
|
| 76 |
+
Reset toàn bộ UI về trạng thái ban đầu.
|
| 77 |
+
Returns: (audio_reset, status_text, btn_send_update, score_reset)
|
| 78 |
+
"""
|
| 79 |
+
return (
|
| 80 |
+
None,
|
| 81 |
+
'🎙️ Sẵn sàng — Nhấn mic và đọc "Lumi ơi"',
|
| 82 |
+
gr.update(interactive=False),
|
| 83 |
+
0.0
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def handle_audio_change():
|
| 88 |
+
"""Khi có file audio mới inject vào → disable nút Gửi."""
|
| 89 |
+
return gr.update(interactive=False)
|