binaryMao commited on
Commit
bfedbb0
·
verified ·
1 Parent(s): c4470bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # -*- coding: utf-8 -*-
2
  """RobotsMali_ASR_Demo.ipynb - Script Final pour Démo Fluide et Stable
3
- Version optimisée pour la RAM (découpage), la vitesse (warm-up), et la stabilité (correction de la forme audio).
4
  """
5
  import gradio as gr
6
  import time
@@ -20,8 +20,6 @@ ROBOTSMALI_MODELS = [
20
  "RobotsMali/soloba-ctc-0.6b-v0",
21
  "RobotsMali/soloni-114m-tdt-ctc-v1",
22
  "RobotsMali/soloni-114m-tdt-ctc-V0",
23
- # Les modèles suivants ont historiquement eu des problèmes de chargement (vu dans les logs),
24
- # mais sont inclus pour l'exhaustivité si l'utilisateur veut les tester.
25
  "RobotsMali/stt-bm-quartznet5x5-V0",
26
  "RobotsMali/stt-bm-quartznet5x5-v1",
27
  "RobotsMali/soloba-ctc-0.6b-v1"
@@ -57,15 +55,12 @@ def load_pipeline(model_name):
57
  # ----------------------------------------------------
58
  print(f" [Warmup] Exécution d'une inférence à blanc...")
59
 
60
- # Création et écriture d'un segment mono de 1 seconde
61
- dummy_audio = np.random.randn(SR_TARGET).astype(np.float32)
62
  sf.write(temp_warmup_file, dummy_audio, SR_TARGET)
63
 
64
- # Lancement de l'inférence
65
  model_instance.transcribe([temp_warmup_file], batch_size=1)
66
 
67
  print(f" [Warmup] Terminé.")
68
- # ----------------------------------------------------
69
 
70
  except Exception as e:
71
  if model_name in asr_pipelines:
@@ -85,7 +80,6 @@ def load_pipeline(model_name):
85
  def transcribe_audio(model_name: str, audio_path: str):
86
  """
87
  Effectue la transcription ASR avec découpage (chunking) et streaming d'état.
88
- Intègre la correction pour la forme audio (squeeze) et gère l'objet Hypothesis.
89
  """
90
  if audio_path is None:
91
  yield "⚠️ Veuillez d'abord télécharger ou enregistrer un fichier audio."
@@ -104,7 +98,6 @@ def transcribe_audio(model_name: str, audio_path: str):
104
  # ----------------------------------------------------------------
105
  yield f"**[1/4] CHARGEMENT AUDIO...** Préparation du fichier original (Mono @ 16kHz). ⚙️"
106
 
107
- # Le mono=True de librosa garantit le canal unique (dimension (T,))
108
  full_audio_data, sr = librosa.load(audio_path, sr=SR_TARGET, mono=True)
109
 
110
  total_duration = len(full_audio_data) / SR_TARGET
@@ -131,11 +124,9 @@ def transcribe_audio(model_name: str, audio_path: str):
131
 
132
  for idx, segment_data in enumerate(audio_segments):
133
 
134
- # Message d'état
135
  yield f"**[3/4] TRANSCRIPTION EN COURS...** Analyse du segment {idx + 1}/{num_chunks}. ⏳"
136
 
137
- # --- CORRECTION CRITIQUE DE LA FORME DE L'AUDIO (shape mismatch fix) ---
138
- # S'assurer que l'array NumPy est strictement à 1 dimension (mono)
139
  segment_data = segment_data.squeeze()
140
 
141
  # Écriture du chunk temporaire
@@ -151,7 +142,6 @@ def transcribe_audio(model_name: str, audio_path: str):
151
  if transcriptions and transcriptions[0]:
152
  hyp_object = transcriptions[0]
153
 
154
- # Accède à l'attribut .text de l'objet Hypothesis ou à la chaîne si déjà simple
155
  if hasattr(hyp_object, 'text'):
156
  segment_text = hyp_object.text.strip()
157
  elif isinstance(hyp_object, str):
@@ -162,7 +152,6 @@ def transcribe_audio(model_name: str, audio_path: str):
162
  if not segment_text:
163
  segment_text = "[Transcription vide]"
164
 
165
- # Ajout d'un double saut de ligne pour le format "Lyrics"
166
  full_transcription_text += segment_text + "\n\n"
167
 
168
  # ----------------------------------------------------
@@ -182,22 +171,27 @@ def transcribe_audio(model_name: str, audio_path: str):
182
 
183
  # 2. PRÉSENTATION LYRICS PROPRE
184
  output += "**RÉSULTAT DE LA TRANSCRIPTION (Lyrics) :**\n"
185
- # Utilisation du bloc de citation Markdown pour une mise en évidence structurée
186
- output += ">>> " + transcription_text_final.replace('\n\n', '\n>>> ')
 
187
 
188
  # 3. NOTE FINALE
189
  output += "\n\n*Note : Audio converti en **Mono @ 16kHz** pour la transcription.*"
190
 
191
- # Le dernier 'yield' envoie le résultat final
192
  yield output
193
 
194
  except RuntimeError as e:
195
  yield f"❌ Erreur critique lors du chargement : {str(e)}"
 
196
  except Exception as e:
 
197
  # Affiche le texte partiel en cas d'erreur
198
  if 'full_transcription_text' in locals() and full_transcription_text:
199
- yield f"❌ Erreur lors de la transcription, le traitement s'est arrêté. Texte partiel:\n>>> {full_transcription_text.strip().replace('\n\n', '\n>>> ')}"
 
 
200
  yield f"❌ Erreur générale : {e}"
 
201
  finally:
202
  # Nettoyage
203
  for chunk_path in temp_chunk_paths:
@@ -254,4 +248,4 @@ interface = gr.Interface(
254
  allow_flagging="never")
255
 
256
  print("Lancement de l'interface Gradio...")
257
- interface.launch(share=True)
 
1
  # -*- coding: utf-8 -*-
2
  """RobotsMali_ASR_Demo.ipynb - Script Final pour Démo Fluide et Stable
3
+ Version corrigée du SyntaxError.
4
  """
5
  import gradio as gr
6
  import time
 
20
  "RobotsMali/soloba-ctc-0.6b-v0",
21
  "RobotsMali/soloni-114m-tdt-ctc-v1",
22
  "RobotsMali/soloni-114m-tdt-ctc-V0",
 
 
23
  "RobotsMali/stt-bm-quartznet5x5-V0",
24
  "RobotsMali/stt-bm-quartznet5x5-v1",
25
  "RobotsMali/soloba-ctc-0.6b-v1"
 
55
  # ----------------------------------------------------
56
  print(f" [Warmup] Exécution d'une inférence à blanc...")
57
 
58
+ dummy_audio = np.random.randn(SR_TARGET).astype(np.float32)
 
59
  sf.write(temp_warmup_file, dummy_audio, SR_TARGET)
60
 
 
61
  model_instance.transcribe([temp_warmup_file], batch_size=1)
62
 
63
  print(f" [Warmup] Terminé.")
 
64
 
65
  except Exception as e:
66
  if model_name in asr_pipelines:
 
80
  def transcribe_audio(model_name: str, audio_path: str):
81
  """
82
  Effectue la transcription ASR avec découpage (chunking) et streaming d'état.
 
83
  """
84
  if audio_path is None:
85
  yield "⚠️ Veuillez d'abord télécharger ou enregistrer un fichier audio."
 
98
  # ----------------------------------------------------------------
99
  yield f"**[1/4] CHARGEMENT AUDIO...** Préparation du fichier original (Mono @ 16kHz). ⚙️"
100
 
 
101
  full_audio_data, sr = librosa.load(audio_path, sr=SR_TARGET, mono=True)
102
 
103
  total_duration = len(full_audio_data) / SR_TARGET
 
124
 
125
  for idx, segment_data in enumerate(audio_segments):
126
 
 
127
  yield f"**[3/4] TRANSCRIPTION EN COURS...** Analyse du segment {idx + 1}/{num_chunks}. ⏳"
128
 
129
+ # --- CORRECTION DE LA FORME AUDIO (squeeze) ---
 
130
  segment_data = segment_data.squeeze()
131
 
132
  # Écriture du chunk temporaire
 
142
  if transcriptions and transcriptions[0]:
143
  hyp_object = transcriptions[0]
144
 
 
145
  if hasattr(hyp_object, 'text'):
146
  segment_text = hyp_object.text.strip()
147
  elif isinstance(hyp_object, str):
 
152
  if not segment_text:
153
  segment_text = "[Transcription vide]"
154
 
 
155
  full_transcription_text += segment_text + "\n\n"
156
 
157
  # ----------------------------------------------------
 
171
 
172
  # 2. PRÉSENTATION LYRICS PROPRE
173
  output += "**RÉSULTAT DE LA TRANSCRIPTION (Lyrics) :**\n"
174
+ # Préparation du texte pour le Markdown (Remplacement avant le yield)
175
+ formatted_lyrics = transcription_text_final.replace('\n\n', '\n>>> ')
176
+ output += f">>> {formatted_lyrics}"
177
 
178
  # 3. NOTE FINALE
179
  output += "\n\n*Note : Audio converti en **Mono @ 16kHz** pour la transcription.*"
180
 
 
181
  yield output
182
 
183
  except RuntimeError as e:
184
  yield f"❌ Erreur critique lors du chargement : {str(e)}"
185
+
186
  except Exception as e:
187
+ # --- CORRECTION DE SYNTAXE APPLIQUÉE ICI ---
188
  # Affiche le texte partiel en cas d'erreur
189
  if 'full_transcription_text' in locals() and full_transcription_text:
190
+ partial_text = full_transcription_text.strip().replace('\n\n', '\n>>> ')
191
+ yield f"❌ Erreur lors de la transcription, le traitement s'est arrêté. Texte partiel:\n>>> {partial_text}"
192
+
193
  yield f"❌ Erreur générale : {e}"
194
+
195
  finally:
196
  # Nettoyage
197
  for chunk_path in temp_chunk_paths:
 
248
  allow_flagging="never")
249
 
250
  print("Lancement de l'interface Gradio...")
251
+ interface.launch(share=True)