Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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, la vitesse et
|
| 4 |
"""
|
| 5 |
import gradio as gr
|
| 6 |
import time
|
|
@@ -20,6 +20,8 @@ ROBOTSMALI_MODELS = [
|
|
| 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,9 +57,11 @@ def load_pipeline(model_name):
|
|
| 55 |
# ----------------------------------------------------
|
| 56 |
print(f" [Warmup] Exécution d'une inférence à blanc...")
|
| 57 |
|
| 58 |
-
|
|
|
|
| 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é.")
|
|
@@ -81,6 +85,7 @@ def load_pipeline(model_name):
|
|
| 81 |
def transcribe_audio(model_name: str, audio_path: str):
|
| 82 |
"""
|
| 83 |
Effectue la transcription ASR avec découpage (chunking) et streaming d'état.
|
|
|
|
| 84 |
"""
|
| 85 |
if audio_path is None:
|
| 86 |
yield "⚠️ Veuillez d'abord télécharger ou enregistrer un fichier audio."
|
|
@@ -91,7 +96,7 @@ def transcribe_audio(model_name: str, audio_path: str):
|
|
| 91 |
|
| 92 |
start_time = time.time()
|
| 93 |
model_short_name = model_name.split('/')[-1]
|
| 94 |
-
temp_chunk_paths = []
|
| 95 |
|
| 96 |
try:
|
| 97 |
# ----------------------------------------------------------------
|
|
@@ -99,6 +104,7 @@ def transcribe_audio(model_name: str, audio_path: str):
|
|
| 99 |
# ----------------------------------------------------------------
|
| 100 |
yield f"**[1/4] CHARGEMENT AUDIO...** Préparation du fichier original (Mono @ 16kHz). ⚙️"
|
| 101 |
|
|
|
|
| 102 |
full_audio_data, sr = librosa.load(audio_path, sr=SR_TARGET, mono=True)
|
| 103 |
|
| 104 |
total_duration = len(full_audio_data) / SR_TARGET
|
|
@@ -125,9 +131,13 @@ def transcribe_audio(model_name: str, audio_path: str):
|
|
| 125 |
|
| 126 |
for idx, segment_data in enumerate(audio_segments):
|
| 127 |
|
| 128 |
-
# Message d'état
|
| 129 |
yield f"**[3/4] TRANSCRIPTION EN COURS...** Analyse du segment {idx + 1}/{num_chunks}. ⏳"
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# Écriture du chunk temporaire
|
| 132 |
chunk_path = f"{os.path.splitext(os.path.basename(audio_path))[0]}_chunk_{idx}.wav"
|
| 133 |
sf.write(chunk_path, segment_data, SR_TARGET)
|
|
@@ -136,24 +146,23 @@ def transcribe_audio(model_name: str, audio_path: str):
|
|
| 136 |
# 🚀 INFÉRENCE NEMO
|
| 137 |
transcriptions = asr_model.transcribe([chunk_path], batch_size=1)
|
| 138 |
|
| 139 |
-
# --- GESTION DE L'OBJET HYPOTHESIS
|
| 140 |
segment_text = ""
|
| 141 |
if transcriptions and transcriptions[0]:
|
| 142 |
hyp_object = transcriptions[0]
|
| 143 |
|
| 144 |
-
# Accède à l'attribut .text de l'objet Hypothesis
|
| 145 |
if hasattr(hyp_object, 'text'):
|
| 146 |
segment_text = hyp_object.text.strip()
|
| 147 |
elif isinstance(hyp_object, str):
|
| 148 |
segment_text = hyp_object.strip()
|
| 149 |
-
# Gère le cas où transcribe retourne une liste de listes
|
| 150 |
elif isinstance(hyp_object, list) and hasattr(hyp_object[0], 'text'):
|
| 151 |
segment_text = hyp_object[0].text.strip()
|
| 152 |
|
| 153 |
if not segment_text:
|
| 154 |
segment_text = "[Transcription vide]"
|
| 155 |
|
| 156 |
-
# Ajout d'un double saut de ligne pour le format "Lyrics"
|
| 157 |
full_transcription_text += segment_text + "\n\n"
|
| 158 |
|
| 159 |
# ----------------------------------------------------
|
|
@@ -173,7 +182,7 @@ def transcribe_audio(model_name: str, audio_path: str):
|
|
| 173 |
|
| 174 |
# 2. PRÉSENTATION LYRICS PROPRE
|
| 175 |
output += "**RÉSULTAT DE LA TRANSCRIPTION (Lyrics) :**\n"
|
| 176 |
-
# Utilisation du bloc de citation Markdown pour
|
| 177 |
output += ">>> " + transcription_text_final.replace('\n\n', '\n>>> ')
|
| 178 |
|
| 179 |
# 3. NOTE FINALE
|
|
|
|
| 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 |
"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 |
# ----------------------------------------------------
|
| 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é.")
|
|
|
|
| 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."
|
|
|
|
| 96 |
|
| 97 |
start_time = time.time()
|
| 98 |
model_short_name = model_name.split('/')[-1]
|
| 99 |
+
temp_chunk_paths = []
|
| 100 |
|
| 101 |
try:
|
| 102 |
# ----------------------------------------------------------------
|
|
|
|
| 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 |
|
| 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
|
| 142 |
chunk_path = f"{os.path.splitext(os.path.basename(audio_path))[0]}_chunk_{idx}.wav"
|
| 143 |
sf.write(chunk_path, segment_data, SR_TARGET)
|
|
|
|
| 146 |
# 🚀 INFÉRENCE NEMO
|
| 147 |
transcriptions = asr_model.transcribe([chunk_path], batch_size=1)
|
| 148 |
|
| 149 |
+
# --- GESTION DE L'OBJET HYPOTHESIS ---
|
| 150 |
segment_text = ""
|
| 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):
|
| 158 |
segment_text = hyp_object.strip()
|
|
|
|
| 159 |
elif isinstance(hyp_object, list) and hasattr(hyp_object[0], 'text'):
|
| 160 |
segment_text = hyp_object[0].text.strip()
|
| 161 |
|
| 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 |
|
| 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
|