Spaces:
Running on Zero
Running on Zero
Cheikh Niang commited on
Deploiement GuindiMa AI (interface Gradio)
Browse files
app.py
CHANGED
|
@@ -190,16 +190,14 @@ _COMPTEUR = itertools.count()
|
|
| 190 |
ERREUR_ASR = "\x00ERREUR_ASR "
|
| 191 |
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
"""Transcrit un enregistrement de micro ou un fichier envoyé.
|
| 196 |
|
| 197 |
-
|
| 198 |
-
réservation ne couvre donc que le calcul.
|
| 199 |
"""
|
| 200 |
octets = Path(chemin_audio).read_bytes()
|
| 201 |
try:
|
| 202 |
-
|
| 203 |
except Exception:
|
| 204 |
# Le navigateur enregistre en WebM/Opus, que soundfile ne sait pas lire.
|
| 205 |
# ffmpeg, déclaré dans packages.txt, ramène ça en WAV avant de réessayer.
|
|
@@ -216,7 +214,16 @@ def transcrire_wolof(chemin_audio: str) -> str:
|
|
| 216 |
check=True,
|
| 217 |
capture_output=True,
|
| 218 |
)
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
try:
|
| 221 |
return transcrire(MODELE_WHISPER, audio)
|
| 222 |
except Exception as exc:
|
|
@@ -228,6 +235,19 @@ def transcrire_wolof(chemin_audio: str) -> str:
|
|
| 228 |
return f"{ERREUR_ASR}{type(exc).__name__} : {str(exc)[:300]}"
|
| 229 |
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
def _phrase_correspondance(trajet: dict) -> str:
|
| 232 |
"""La réponse à dire pour un trajet en deux bus."""
|
| 233 |
morceaux = []
|
|
@@ -364,12 +384,21 @@ def trouver_le_bus(audio: str | None, phrase_ecrite: str) -> tuple:
|
|
| 364 |
|
| 365 |
if audio:
|
| 366 |
try:
|
| 367 |
-
|
| 368 |
except Exception as exc:
|
| 369 |
return ("", "", f"### 🎤 Audio illisible\n\n{_court(exc)}", None, "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
if phrase.startswith(ERREUR_ASR):
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
| 373 |
else:
|
| 374 |
phrase = (phrase_ecrite or "").strip()
|
| 375 |
|
|
|
|
| 190 |
ERREUR_ASR = "\x00ERREUR_ASR "
|
| 191 |
|
| 192 |
|
| 193 |
+
def _decoder_audio(chemin_audio: str) -> np.ndarray:
|
| 194 |
+
"""Lit l'enregistrement (micro ou fichier) en 16 kHz mono, sur CPU.
|
|
|
|
| 195 |
|
| 196 |
+
Fait hors de la réservation GPU : décoder un fichier n'a pas besoin du GPU.
|
|
|
|
| 197 |
"""
|
| 198 |
octets = Path(chemin_audio).read_bytes()
|
| 199 |
try:
|
| 200 |
+
return load_audio(octets)
|
| 201 |
except Exception:
|
| 202 |
# Le navigateur enregistre en WebM/Opus, que soundfile ne sait pas lire.
|
| 203 |
# ffmpeg, déclaré dans packages.txt, ramène ça en WAV avant de réessayer.
|
|
|
|
| 214 |
check=True,
|
| 215 |
capture_output=True,
|
| 216 |
)
|
| 217 |
+
return load_audio(converti.read_bytes())
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
@spaces.GPU(duration=30)
|
| 221 |
+
def transcrire_wolof(audio: np.ndarray) -> str:
|
| 222 |
+
"""Transcrit l'audio avec Kiriku sur GPU.
|
| 223 |
+
|
| 224 |
+
Seul endroit de l'app qui réserve le GPU. Le modèle est déjà en mémoire, la
|
| 225 |
+
réservation ne couvre donc que le calcul.
|
| 226 |
+
"""
|
| 227 |
try:
|
| 228 |
return transcrire(MODELE_WHISPER, audio)
|
| 229 |
except Exception as exc:
|
|
|
|
| 235 |
return f"{ERREUR_ASR}{type(exc).__name__} : {str(exc)[:300]}"
|
| 236 |
|
| 237 |
|
| 238 |
+
# Secours quand ZeroGPU ne fournit pas de GPU (« No CUDA GPUs are available ») :
|
| 239 |
+
# whisper-small quantifié sur CPU, chargé seulement le jour où on en a besoin.
|
| 240 |
+
_MODELE_CPU = None
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def _transcrire_cpu(audio: np.ndarray) -> str:
|
| 244 |
+
global _MODELE_CPU
|
| 245 |
+
if _MODELE_CPU is None:
|
| 246 |
+
print("[ASR] GPU indisponible : chargement de whisper-small sur CPU", flush=True)
|
| 247 |
+
_MODELE_CPU = load_model()
|
| 248 |
+
return transcrire(_MODELE_CPU, audio)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
def _phrase_correspondance(trajet: dict) -> str:
|
| 252 |
"""La réponse à dire pour un trajet en deux bus."""
|
| 253 |
morceaux = []
|
|
|
|
| 384 |
|
| 385 |
if audio:
|
| 386 |
try:
|
| 387 |
+
son = _decoder_audio(audio)
|
| 388 |
except Exception as exc:
|
| 389 |
return ("", "", f"### 🎤 Audio illisible\n\n{_court(exc)}", None, "")
|
| 390 |
+
try:
|
| 391 |
+
phrase = transcrire_wolof(son)
|
| 392 |
+
except Exception as exc:
|
| 393 |
+
# Panne côté Hugging Face (GPU non attribué, quota) : on ne laisse pas
|
| 394 |
+
# l'utilisateur sans réponse, on transcrit sur CPU, plus lentement.
|
| 395 |
+
print(f"[ASR] ZeroGPU en échec ({_court(exc)}), secours CPU", flush=True)
|
| 396 |
+
phrase = ERREUR_ASR
|
| 397 |
if phrase.startswith(ERREUR_ASR):
|
| 398 |
+
try:
|
| 399 |
+
phrase = _transcrire_cpu(son)
|
| 400 |
+
except Exception as exc:
|
| 401 |
+
return ("", "", f"### 🎤 Transcription impossible\n\n`{_court(exc)}`", None, "")
|
| 402 |
else:
|
| 403 |
phrase = (phrase_ecrite or "").strip()
|
| 404 |
|