Update app.py
Browse files
app.py
CHANGED
|
@@ -75,6 +75,29 @@ print("Gemma pret !")
|
|
| 75 |
current_audio = {"path": None, "filename": None}
|
| 76 |
current_exchange = {"transcription": None, "reponse": None}
|
| 77 |
stop_generation = {"value": False}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# ── Helpers ───────────────────────────────────────────────────────
|
| 80 |
def generate_audio_name():
|
|
@@ -262,12 +285,25 @@ def generate_gemma_response(transcription: str, progress=None):
|
|
| 262 |
def transcribe_only(audio, progress=gr.Progress()):
|
| 263 |
path = process_audio(audio)
|
| 264 |
if path is None:
|
| 265 |
-
|
|
|
|
| 266 |
"Aucun audio recu.",
|
| 267 |
gr.update(visible=False),
|
| 268 |
gr.update(visible=False),
|
| 269 |
-
|
|
|
|
| 270 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
try:
|
| 273 |
progress(0.3, desc="🎙 Transcription audio...")
|
|
@@ -281,19 +317,22 @@ def transcribe_only(audio, progress=gr.Progress()):
|
|
| 281 |
|
| 282 |
progress(1.0, desc="✅ Transcription terminée")
|
| 283 |
|
| 284 |
-
|
| 285 |
text,
|
| 286 |
gr.update(visible=False),
|
| 287 |
gr.update(visible=False),
|
| 288 |
gr.update(interactive=False),
|
|
|
|
| 289 |
)
|
| 290 |
|
| 291 |
except Exception as e:
|
| 292 |
-
|
|
|
|
| 293 |
f"Erreur Whisper : {str(e)}",
|
| 294 |
gr.update(visible=False),
|
| 295 |
gr.update(visible=False),
|
| 296 |
-
|
|
|
|
| 297 |
)
|
| 298 |
|
| 299 |
|
|
@@ -301,11 +340,13 @@ def transcribe_only(audio, progress=gr.Progress()):
|
|
| 301 |
def respond_gemma(progress=gr.Progress()):
|
| 302 |
transcription = current_exchange.get("transcription")
|
| 303 |
if not transcription:
|
|
|
|
| 304 |
yield (
|
| 305 |
"⚠️ Pas de transcription disponible.",
|
| 306 |
gr.update(visible=False),
|
| 307 |
gr.update(visible=False),
|
| 308 |
-
|
|
|
|
| 309 |
)
|
| 310 |
return
|
| 311 |
|
|
@@ -322,6 +363,7 @@ def respond_gemma(progress=gr.Progress()):
|
|
| 322 |
gr.update(visible=False),
|
| 323 |
gr.update(visible=False),
|
| 324 |
gr.update(interactive=False),
|
|
|
|
| 325 |
)
|
| 326 |
|
| 327 |
if stop_generation["value"]:
|
|
@@ -330,19 +372,23 @@ def respond_gemma(progress=gr.Progress()):
|
|
| 330 |
current_exchange["reponse"] = last_partial
|
| 331 |
progress(1.0, desc="✅ Terminé")
|
| 332 |
|
|
|
|
| 333 |
yield (
|
| 334 |
last_partial,
|
| 335 |
gr.update(visible=True),
|
| 336 |
gr.update(visible=True),
|
| 337 |
-
|
|
|
|
| 338 |
)
|
| 339 |
|
| 340 |
except Exception as e:
|
|
|
|
| 341 |
yield (
|
| 342 |
f"Erreur Gemma : {str(e)}",
|
| 343 |
gr.update(visible=False),
|
| 344 |
gr.update(visible=False),
|
| 345 |
-
|
|
|
|
| 346 |
)
|
| 347 |
|
| 348 |
|
|
@@ -350,9 +396,14 @@ def respond_gemma(progress=gr.Progress()):
|
|
| 350 |
def regenerate_gemma_only(progress=gr.Progress()):
|
| 351 |
transcription = current_exchange.get("transcription")
|
| 352 |
if not transcription:
|
| 353 |
-
|
|
|
|
| 354 |
return
|
| 355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
try:
|
| 357 |
progress(0.3, desc="🔄 Régénération...")
|
| 358 |
|
|
@@ -361,17 +412,19 @@ def regenerate_gemma_only(progress=gr.Progress()):
|
|
| 361 |
if stop_generation["value"]:
|
| 362 |
return
|
| 363 |
last_partial = partial
|
| 364 |
-
yield partial, gr.update(visible=False)
|
| 365 |
|
| 366 |
if stop_generation["value"]:
|
| 367 |
return
|
| 368 |
|
| 369 |
current_exchange["reponse"] = last_partial
|
| 370 |
progress(1.0, desc="✅ Prêt")
|
| 371 |
-
|
|
|
|
| 372 |
|
| 373 |
except Exception as e:
|
| 374 |
-
|
|
|
|
| 375 |
|
| 376 |
|
| 377 |
# ── Gestion de l'enregistrement micro (attente de chargement) ─────
|
|
@@ -383,12 +436,15 @@ def on_audio_recorded(audio):
|
|
| 383 |
réactive le bouton (uniquement si un audio est bien présent).
|
| 384 |
"""
|
| 385 |
if audio is None:
|
|
|
|
| 386 |
# Audio supprimé -> bouton inactif
|
| 387 |
return gr.update(interactive=False, value="🎤 Transcrire la voix")
|
| 388 |
|
|
|
|
| 389 |
# On affiche un état "chargement" pendant l'attente
|
| 390 |
yield gr.update(interactive=False, value="⏳ Chargement de l'audio...")
|
| 391 |
time.sleep(2)
|
|
|
|
| 392 |
yield gr.update(interactive=True, value="🎤 Transcrire la voix")
|
| 393 |
|
| 394 |
|
|
@@ -400,10 +456,13 @@ def on_file_uploaded(audio):
|
|
| 400 |
de se charger côté navigateur.
|
| 401 |
"""
|
| 402 |
if audio is None:
|
|
|
|
| 403 |
return gr.update(interactive=False, value="📂 Transcrire le fichier")
|
| 404 |
|
|
|
|
| 405 |
yield gr.update(interactive=False, value="⏳ Chargement du fichier...")
|
| 406 |
time.sleep(2)
|
|
|
|
| 407 |
yield gr.update(interactive=True, value="📂 Transcrire le fichier")
|
| 408 |
|
| 409 |
|
|
@@ -414,6 +473,8 @@ def reset_all():
|
|
| 414 |
current_audio["filename"] = None
|
| 415 |
current_exchange["transcription"] = None
|
| 416 |
current_exchange["reponse"] = None
|
|
|
|
|
|
|
| 417 |
return (
|
| 418 |
None, None, "", "",
|
| 419 |
gr.update(visible=False), gr.update(visible=False),
|
|
@@ -651,27 +712,27 @@ with gr.Blocks(title="Whisper + Gemma · Darija", css=css, js=js) as demo:
|
|
| 651 |
mic_btn.click(
|
| 652 |
fn=transcribe_only,
|
| 653 |
inputs=[mic_input],
|
| 654 |
-
outputs=[whisper_out, fb_whisper, fb_gemma, mic_btn],
|
| 655 |
).then(
|
| 656 |
fn=respond_gemma,
|
| 657 |
inputs=[],
|
| 658 |
-
outputs=[gemma_out, fb_whisper, fb_gemma, mic_btn],
|
| 659 |
)
|
| 660 |
|
| 661 |
file_btn.click(
|
| 662 |
fn=transcribe_only,
|
| 663 |
inputs=[file_input],
|
| 664 |
-
outputs=[whisper_out, fb_whisper, fb_gemma, file_btn],
|
| 665 |
).then(
|
| 666 |
fn=respond_gemma,
|
| 667 |
inputs=[],
|
| 668 |
-
outputs=[gemma_out, fb_whisper, fb_gemma, file_btn],
|
| 669 |
)
|
| 670 |
|
| 671 |
regenerate_btn.click(
|
| 672 |
fn=regenerate_gemma_only,
|
| 673 |
inputs=[],
|
| 674 |
-
outputs=[gemma_out, fb_gemma],
|
| 675 |
)
|
| 676 |
|
| 677 |
btn_whisper.click(fn=save_feedback_whisper, inputs=[whisper_out, note_whisper], outputs=[status_whisper])
|
|
|
|
| 75 |
current_audio = {"path": None, "filename": None}
|
| 76 |
current_exchange = {"transcription": None, "reponse": None}
|
| 77 |
stop_generation = {"value": False}
|
| 78 |
+
audio_state = {"mic_ready": False, "file_ready": False}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def btn_states():
|
| 82 |
+
"""
|
| 83 |
+
Retourne les mises à jour (interactive=True/False) pour mic_btn et file_btn
|
| 84 |
+
en fonction de la présence d'un audio micro / fichier, à utiliser une fois
|
| 85 |
+
qu'un traitement (transcription/génération) est terminé.
|
| 86 |
+
"""
|
| 87 |
+
mic_label = "🎤 Transcrire la voix"
|
| 88 |
+
file_label = "📂 Transcrire le fichier"
|
| 89 |
+
return (
|
| 90 |
+
gr.update(interactive=audio_state["mic_ready"], value=mic_label),
|
| 91 |
+
gr.update(interactive=audio_state["file_ready"], value=file_label),
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def btn_states_disabled():
|
| 96 |
+
"""Désactive les deux boutons de transcription (traitement en cours)."""
|
| 97 |
+
return (
|
| 98 |
+
gr.update(interactive=False, value="🎤 Transcrire la voix"),
|
| 99 |
+
gr.update(interactive=False, value="📂 Transcrire le fichier"),
|
| 100 |
+
)
|
| 101 |
|
| 102 |
# ── Helpers ───────────────────────────────────────────────────────
|
| 103 |
def generate_audio_name():
|
|
|
|
| 285 |
def transcribe_only(audio, progress=gr.Progress()):
|
| 286 |
path = process_audio(audio)
|
| 287 |
if path is None:
|
| 288 |
+
mic_u, file_u = btn_states()
|
| 289 |
+
yield (
|
| 290 |
"Aucun audio recu.",
|
| 291 |
gr.update(visible=False),
|
| 292 |
gr.update(visible=False),
|
| 293 |
+
mic_u,
|
| 294 |
+
file_u,
|
| 295 |
)
|
| 296 |
+
return
|
| 297 |
+
|
| 298 |
+
# Désactive les deux boutons pendant la transcription + génération Gemma
|
| 299 |
+
mic_u, file_u = btn_states_disabled()
|
| 300 |
+
yield (
|
| 301 |
+
gr.update(),
|
| 302 |
+
gr.update(visible=False),
|
| 303 |
+
gr.update(visible=False),
|
| 304 |
+
mic_u,
|
| 305 |
+
file_u,
|
| 306 |
+
)
|
| 307 |
|
| 308 |
try:
|
| 309 |
progress(0.3, desc="🎙 Transcription audio...")
|
|
|
|
| 317 |
|
| 318 |
progress(1.0, desc="✅ Transcription terminée")
|
| 319 |
|
| 320 |
+
yield (
|
| 321 |
text,
|
| 322 |
gr.update(visible=False),
|
| 323 |
gr.update(visible=False),
|
| 324 |
gr.update(interactive=False),
|
| 325 |
+
gr.update(interactive=False),
|
| 326 |
)
|
| 327 |
|
| 328 |
except Exception as e:
|
| 329 |
+
mic_u, file_u = btn_states()
|
| 330 |
+
yield (
|
| 331 |
f"Erreur Whisper : {str(e)}",
|
| 332 |
gr.update(visible=False),
|
| 333 |
gr.update(visible=False),
|
| 334 |
+
mic_u,
|
| 335 |
+
file_u,
|
| 336 |
)
|
| 337 |
|
| 338 |
|
|
|
|
| 340 |
def respond_gemma(progress=gr.Progress()):
|
| 341 |
transcription = current_exchange.get("transcription")
|
| 342 |
if not transcription:
|
| 343 |
+
mic_u, file_u = btn_states()
|
| 344 |
yield (
|
| 345 |
"⚠️ Pas de transcription disponible.",
|
| 346 |
gr.update(visible=False),
|
| 347 |
gr.update(visible=False),
|
| 348 |
+
mic_u,
|
| 349 |
+
file_u,
|
| 350 |
)
|
| 351 |
return
|
| 352 |
|
|
|
|
| 363 |
gr.update(visible=False),
|
| 364 |
gr.update(visible=False),
|
| 365 |
gr.update(interactive=False),
|
| 366 |
+
gr.update(interactive=False),
|
| 367 |
)
|
| 368 |
|
| 369 |
if stop_generation["value"]:
|
|
|
|
| 372 |
current_exchange["reponse"] = last_partial
|
| 373 |
progress(1.0, desc="✅ Terminé")
|
| 374 |
|
| 375 |
+
mic_u, file_u = btn_states()
|
| 376 |
yield (
|
| 377 |
last_partial,
|
| 378 |
gr.update(visible=True),
|
| 379 |
gr.update(visible=True),
|
| 380 |
+
mic_u,
|
| 381 |
+
file_u,
|
| 382 |
)
|
| 383 |
|
| 384 |
except Exception as e:
|
| 385 |
+
mic_u, file_u = btn_states()
|
| 386 |
yield (
|
| 387 |
f"Erreur Gemma : {str(e)}",
|
| 388 |
gr.update(visible=False),
|
| 389 |
gr.update(visible=False),
|
| 390 |
+
mic_u,
|
| 391 |
+
file_u,
|
| 392 |
)
|
| 393 |
|
| 394 |
|
|
|
|
| 396 |
def regenerate_gemma_only(progress=gr.Progress()):
|
| 397 |
transcription = current_exchange.get("transcription")
|
| 398 |
if not transcription:
|
| 399 |
+
mic_u, file_u = btn_states()
|
| 400 |
+
yield "", gr.update(visible=False), mic_u, file_u
|
| 401 |
return
|
| 402 |
|
| 403 |
+
# Désactive les boutons de transcription pendant la régénération
|
| 404 |
+
mic_u, file_u = btn_states_disabled()
|
| 405 |
+
yield gr.update(), gr.update(visible=False), mic_u, file_u
|
| 406 |
+
|
| 407 |
try:
|
| 408 |
progress(0.3, desc="🔄 Régénération...")
|
| 409 |
|
|
|
|
| 412 |
if stop_generation["value"]:
|
| 413 |
return
|
| 414 |
last_partial = partial
|
| 415 |
+
yield partial, gr.update(visible=False), gr.update(interactive=False), gr.update(interactive=False)
|
| 416 |
|
| 417 |
if stop_generation["value"]:
|
| 418 |
return
|
| 419 |
|
| 420 |
current_exchange["reponse"] = last_partial
|
| 421 |
progress(1.0, desc="✅ Prêt")
|
| 422 |
+
mic_u, file_u = btn_states()
|
| 423 |
+
yield last_partial, gr.update(visible=True), mic_u, file_u
|
| 424 |
|
| 425 |
except Exception as e:
|
| 426 |
+
mic_u, file_u = btn_states()
|
| 427 |
+
yield f"Erreur : {str(e)}", gr.update(visible=False), mic_u, file_u
|
| 428 |
|
| 429 |
|
| 430 |
# ── Gestion de l'enregistrement micro (attente de chargement) ─────
|
|
|
|
| 436 |
réactive le bouton (uniquement si un audio est bien présent).
|
| 437 |
"""
|
| 438 |
if audio is None:
|
| 439 |
+
audio_state["mic_ready"] = False
|
| 440 |
# Audio supprimé -> bouton inactif
|
| 441 |
return gr.update(interactive=False, value="🎤 Transcrire la voix")
|
| 442 |
|
| 443 |
+
audio_state["mic_ready"] = False
|
| 444 |
# On affiche un état "chargement" pendant l'attente
|
| 445 |
yield gr.update(interactive=False, value="⏳ Chargement de l'audio...")
|
| 446 |
time.sleep(2)
|
| 447 |
+
audio_state["mic_ready"] = True
|
| 448 |
yield gr.update(interactive=True, value="🎤 Transcrire la voix")
|
| 449 |
|
| 450 |
|
|
|
|
| 456 |
de se charger côté navigateur.
|
| 457 |
"""
|
| 458 |
if audio is None:
|
| 459 |
+
audio_state["file_ready"] = False
|
| 460 |
return gr.update(interactive=False, value="📂 Transcrire le fichier")
|
| 461 |
|
| 462 |
+
audio_state["file_ready"] = False
|
| 463 |
yield gr.update(interactive=False, value="⏳ Chargement du fichier...")
|
| 464 |
time.sleep(2)
|
| 465 |
+
audio_state["file_ready"] = True
|
| 466 |
yield gr.update(interactive=True, value="📂 Transcrire le fichier")
|
| 467 |
|
| 468 |
|
|
|
|
| 473 |
current_audio["filename"] = None
|
| 474 |
current_exchange["transcription"] = None
|
| 475 |
current_exchange["reponse"] = None
|
| 476 |
+
audio_state["mic_ready"] = False
|
| 477 |
+
audio_state["file_ready"] = False
|
| 478 |
return (
|
| 479 |
None, None, "", "",
|
| 480 |
gr.update(visible=False), gr.update(visible=False),
|
|
|
|
| 712 |
mic_btn.click(
|
| 713 |
fn=transcribe_only,
|
| 714 |
inputs=[mic_input],
|
| 715 |
+
outputs=[whisper_out, fb_whisper, fb_gemma, mic_btn, file_btn],
|
| 716 |
).then(
|
| 717 |
fn=respond_gemma,
|
| 718 |
inputs=[],
|
| 719 |
+
outputs=[gemma_out, fb_whisper, fb_gemma, mic_btn, file_btn],
|
| 720 |
)
|
| 721 |
|
| 722 |
file_btn.click(
|
| 723 |
fn=transcribe_only,
|
| 724 |
inputs=[file_input],
|
| 725 |
+
outputs=[whisper_out, fb_whisper, fb_gemma, mic_btn, file_btn],
|
| 726 |
).then(
|
| 727 |
fn=respond_gemma,
|
| 728 |
inputs=[],
|
| 729 |
+
outputs=[gemma_out, fb_whisper, fb_gemma, mic_btn, file_btn],
|
| 730 |
)
|
| 731 |
|
| 732 |
regenerate_btn.click(
|
| 733 |
fn=regenerate_gemma_only,
|
| 734 |
inputs=[],
|
| 735 |
+
outputs=[gemma_out, fb_gemma, mic_btn, file_btn],
|
| 736 |
)
|
| 737 |
|
| 738 |
btn_whisper.click(fn=save_feedback_whisper, inputs=[whisper_out, note_whisper], outputs=[status_whisper])
|