Cheikh Niang commited on
Commit
327d56a
·
verified ·
1 Parent(s): 0412868

Deploiement GuindiMa AI (interface Gradio)

Browse files
Files changed (2) hide show
  1. app.py +15 -1
  2. src/asr/transcribe.py +10 -8
app.py CHANGED
@@ -187,6 +187,9 @@ DOSSIER_VOIX = Path(tempfile.mkdtemp(prefix="guindima_voix_"))
187
  _COMPTEUR = itertools.count()
188
 
189
 
 
 
 
190
  @spaces.GPU(duration=30)
191
  def transcrire_wolof(chemin_audio: str) -> str:
192
  """Transcrit un enregistrement de micro ou un fichier envoyé.
@@ -214,7 +217,15 @@ def transcrire_wolof(chemin_audio: str) -> str:
214
  capture_output=True,
215
  )
216
  audio = load_audio(converti.read_bytes())
217
- return transcrire(MODELE_WHISPER, audio)
 
 
 
 
 
 
 
 
218
 
219
 
220
  def _phrase_correspondance(trajet: dict) -> str:
@@ -356,6 +367,9 @@ def trouver_le_bus(audio: str | None, phrase_ecrite: str) -> tuple:
356
  phrase = transcrire_wolof(audio)
357
  except Exception as exc:
358
  return ("", "", f"### 🎤 Audio illisible\n\n{_court(exc)}", None, "")
 
 
 
359
  else:
360
  phrase = (phrase_ecrite or "").strip()
361
 
 
187
  _COMPTEUR = itertools.count()
188
 
189
 
190
+ ERREUR_ASR = "\x00ERREUR_ASR "
191
+
192
+
193
  @spaces.GPU(duration=30)
194
  def transcrire_wolof(chemin_audio: str) -> str:
195
  """Transcrit un enregistrement de micro ou un fichier envoyé.
 
217
  capture_output=True,
218
  )
219
  audio = load_audio(converti.read_bytes())
220
+ try:
221
+ return transcrire(MODELE_WHISPER, audio)
222
+ except Exception as exc:
223
+ # ZeroGPU ne renvoie que le nom de l'erreur (« RuntimeError ») : on garde le
224
+ # détail en texte pour l'afficher et le voir dans les logs.
225
+ import traceback
226
+
227
+ traceback.print_exc()
228
+ return f"{ERREUR_ASR}{type(exc).__name__} : {str(exc)[:300]}"
229
 
230
 
231
  def _phrase_correspondance(trajet: dict) -> str:
 
367
  phrase = transcrire_wolof(audio)
368
  except Exception as exc:
369
  return ("", "", f"### 🎤 Audio illisible\n\n{_court(exc)}", None, "")
370
+ if phrase.startswith(ERREUR_ASR):
371
+ detail = phrase[len(ERREUR_ASR):]
372
+ return ("", "", f"### 🎤 Transcription impossible\n\n`{detail}`", None, "")
373
  else:
374
  phrase = (phrase_ecrite or "").strip()
375
 
src/asr/transcribe.py CHANGED
@@ -111,19 +111,21 @@ def transcrire(model, audio_16k: np.ndarray) -> str:
111
  "max_new_tokens": MAX_NEW_TOKENS,
112
  "num_beams": 1,
113
  }
114
- with torch.inference_mode():
115
- try:
116
  prediction = model(
117
  {"array": audio_16k, "sampling_rate": SAMPLE_RATE},
118
  generate_kwargs=reglages,
119
  )
120
- except ValueError:
121
- # Certains fine-tunes (Kiriku compris, selon sa generation_config)
122
- # refusent « task » : on relance avec les seuls réglages validés au banc d'essai.
123
- reglages.pop("task")
 
 
124
  prediction = model(
125
- {"array": audio_16k, "sampling_rate": SAMPLE_RATE},
126
- generate_kwargs=reglages,
127
  )
128
  texte = prediction.get("text", "") if isinstance(prediction, dict) else str(prediction)
129
  return re.sub(r"\s+", " ", texte).strip()
 
111
  "max_new_tokens": MAX_NEW_TOKENS,
112
  "num_beams": 1,
113
  }
114
+ try:
115
+ with torch.inference_mode():
116
  prediction = model(
117
  {"array": audio_16k, "sampling_rate": SAMPLE_RATE},
118
  generate_kwargs=reglages,
119
  )
120
+ except Exception as premiere:
121
+ # Kiriku n'accepte pas forcément « task » ni l'inference_mode : on relance
122
+ # exactement comme au banc d'essai Colab (seul max_new_tokens, sans
123
+ # inference_mode), qui a fonctionné.
124
+ print(f"[ASR] 1er essai échoué ({type(premiere).__name__}: {premiere}), nouvel essai", flush=True)
125
+ with torch.no_grad():
126
  prediction = model(
127
+ {"raw": audio_16k, "sampling_rate": SAMPLE_RATE},
128
+ generate_kwargs={"max_new_tokens": MAX_NEW_TOKENS},
129
  )
130
  texte = prediction.get("text", "") if isinstance(prediction, dict) else str(prediction)
131
  return re.sub(r"\s+", " ", texte).strip()