Spaces:
Running
Running
Commit
·
753a150
1
Parent(s):
ef4d3d8
[UPDATE] performize error log
Browse files
app.py
CHANGED
@@ -7,15 +7,12 @@ from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
|
7 |
from huggingface_hub import login
|
8 |
import logging
|
9 |
|
10 |
-
# Configuration
|
11 |
MODEL_NAME = "Ronaldodev/speech-to-text-fongbe"
|
12 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
13 |
|
14 |
-
# Variables globales
|
15 |
model = None
|
16 |
processor = None
|
17 |
|
18 |
-
# Configuration logging
|
19 |
logging.basicConfig(level=logging.INFO)
|
20 |
logger = logging.getLogger(__name__)
|
21 |
|
@@ -30,11 +27,9 @@ def load_model():
|
|
30 |
if not HF_TOKEN:
|
31 |
raise ValueError("HF_TOKEN non configuré dans les secrets")
|
32 |
|
33 |
-
# Login avec token privé
|
34 |
login(token=HF_TOKEN)
|
35 |
logger.info("✅ Authentification HF réussie")
|
36 |
|
37 |
-
# Charger le modèle et processeur
|
38 |
model = AutoModelForSpeechSeq2Seq.from_pretrained(MODEL_NAME)
|
39 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
40 |
|
@@ -49,18 +44,14 @@ def load_model():
|
|
49 |
def transcribe(audio):
|
50 |
"""Fonction principale de transcription"""
|
51 |
|
52 |
-
# Vérifier si le modèle est chargé
|
53 |
if model is None or processor is None:
|
54 |
return "❌ Erreur: Modèle non chargé. Vérifiez les logs."
|
55 |
|
56 |
-
# Vérifier si un audio est fourni
|
57 |
if audio is None:
|
58 |
return "❌ Aucun fichier audio fourni"
|
59 |
|
60 |
try:
|
61 |
logger.info(f"🎵 Traitement audio: {audio}")
|
62 |
-
|
63 |
-
# Charger l'audio avec fallback
|
64 |
try:
|
65 |
waveform, sample_rate = torchaudio.load(audio)
|
66 |
logger.info(f"✅ Audio chargé avec torchaudio: {sample_rate}Hz")
|
@@ -70,25 +61,21 @@ def transcribe(audio):
|
|
70 |
waveform = torch.tensor(waveform).unsqueeze(0)
|
71 |
logger.info(f"✅ Audio chargé avec librosa: {sample_rate}Hz")
|
72 |
|
73 |
-
# Conversion mono si nécessaire
|
74 |
if waveform.shape[0] > 1:
|
75 |
waveform = waveform.mean(dim=0, keepdim=True)
|
76 |
logger.info("🔄 Conversion stéréo → mono")
|
77 |
|
78 |
-
# Resampling à 16kHz si nécessaire
|
79 |
if sample_rate != 16000:
|
80 |
logger.info(f"🔄 Resampling {sample_rate}Hz → 16000Hz")
|
81 |
resampler = torchaudio.transforms.Resample(sample_rate, 16000)
|
82 |
waveform = resampler(waveform)
|
83 |
|
84 |
-
# Préparation des inputs
|
85 |
inputs = processor(
|
86 |
waveform.squeeze(),
|
87 |
sampling_rate=16000,
|
88 |
return_tensors="pt"
|
89 |
)
|
90 |
|
91 |
-
# Génération de la transcription
|
92 |
logger.info("🔄 Génération de la transcription...")
|
93 |
with torch.no_grad():
|
94 |
result = model.generate(
|
@@ -98,7 +85,6 @@ def transcribe(audio):
|
|
98 |
num_beams=1
|
99 |
)
|
100 |
|
101 |
-
# Décodage
|
102 |
transcription = processor.batch_decode(result, skip_special_tokens=True)[0]
|
103 |
|
104 |
logger.info(f"✅ Transcription réussie: '{transcription}'")
|
@@ -110,7 +96,6 @@ def transcribe(audio):
|
|
110 |
return error_msg
|
111 |
|
112 |
|
113 |
-
# Charger le modèle au démarrage
|
114 |
print("🚀 DÉMARRAGE API STT FONGBÉ - RONALDODEV")
|
115 |
print("=" * 50)
|
116 |
|
@@ -121,7 +106,6 @@ else:
|
|
121 |
print("❌ Erreur de chargement du modèle")
|
122 |
model_status = "❌ Erreur de chargement"
|
123 |
|
124 |
-
# Interface Gradio simple
|
125 |
demo = gr.Interface(
|
126 |
fn=transcribe,
|
127 |
inputs=gr.Audio(
|
@@ -164,13 +148,13 @@ demo = gr.Interface(
|
|
164 |
```
|
165 |
|
166 |
**Pour Flutter:** Utilisez MultipartRequest avec l'endpoint ci-dessus.
|
|
|
|
|
167 |
""",
|
168 |
examples=[
|
169 |
-
# Vous pouvez ajouter des fichiers d'exemple si vous en avez
|
170 |
],
|
171 |
theme=gr.themes.Soft(),
|
172 |
allow_flagging="never"
|
173 |
)
|
174 |
|
175 |
-
# Lancement de l'interface
|
176 |
demo.launch()
|
|
|
7 |
from huggingface_hub import login
|
8 |
import logging
|
9 |
|
|
|
10 |
MODEL_NAME = "Ronaldodev/speech-to-text-fongbe"
|
11 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
|
|
|
13 |
model = None
|
14 |
processor = None
|
15 |
|
|
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
|
|
27 |
if not HF_TOKEN:
|
28 |
raise ValueError("HF_TOKEN non configuré dans les secrets")
|
29 |
|
|
|
30 |
login(token=HF_TOKEN)
|
31 |
logger.info("✅ Authentification HF réussie")
|
32 |
|
|
|
33 |
model = AutoModelForSpeechSeq2Seq.from_pretrained(MODEL_NAME)
|
34 |
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
35 |
|
|
|
44 |
def transcribe(audio):
|
45 |
"""Fonction principale de transcription"""
|
46 |
|
|
|
47 |
if model is None or processor is None:
|
48 |
return "❌ Erreur: Modèle non chargé. Vérifiez les logs."
|
49 |
|
|
|
50 |
if audio is None:
|
51 |
return "❌ Aucun fichier audio fourni"
|
52 |
|
53 |
try:
|
54 |
logger.info(f"🎵 Traitement audio: {audio}")
|
|
|
|
|
55 |
try:
|
56 |
waveform, sample_rate = torchaudio.load(audio)
|
57 |
logger.info(f"✅ Audio chargé avec torchaudio: {sample_rate}Hz")
|
|
|
61 |
waveform = torch.tensor(waveform).unsqueeze(0)
|
62 |
logger.info(f"✅ Audio chargé avec librosa: {sample_rate}Hz")
|
63 |
|
|
|
64 |
if waveform.shape[0] > 1:
|
65 |
waveform = waveform.mean(dim=0, keepdim=True)
|
66 |
logger.info("🔄 Conversion stéréo → mono")
|
67 |
|
|
|
68 |
if sample_rate != 16000:
|
69 |
logger.info(f"🔄 Resampling {sample_rate}Hz → 16000Hz")
|
70 |
resampler = torchaudio.transforms.Resample(sample_rate, 16000)
|
71 |
waveform = resampler(waveform)
|
72 |
|
|
|
73 |
inputs = processor(
|
74 |
waveform.squeeze(),
|
75 |
sampling_rate=16000,
|
76 |
return_tensors="pt"
|
77 |
)
|
78 |
|
|
|
79 |
logger.info("🔄 Génération de la transcription...")
|
80 |
with torch.no_grad():
|
81 |
result = model.generate(
|
|
|
85 |
num_beams=1
|
86 |
)
|
87 |
|
|
|
88 |
transcription = processor.batch_decode(result, skip_special_tokens=True)[0]
|
89 |
|
90 |
logger.info(f"✅ Transcription réussie: '{transcription}'")
|
|
|
96 |
return error_msg
|
97 |
|
98 |
|
|
|
99 |
print("🚀 DÉMARRAGE API STT FONGBÉ - RONALDODEV")
|
100 |
print("=" * 50)
|
101 |
|
|
|
106 |
print("❌ Erreur de chargement du modèle")
|
107 |
model_status = "❌ Erreur de chargement"
|
108 |
|
|
|
109 |
demo = gr.Interface(
|
110 |
fn=transcribe,
|
111 |
inputs=gr.Audio(
|
|
|
148 |
```
|
149 |
|
150 |
**Pour Flutter:** Utilisez MultipartRequest avec l'endpoint ci-dessus.
|
151 |
+
|
152 |
+
**L'utilisation de l'api est gratuite pour l'instant**
|
153 |
""",
|
154 |
examples=[
|
|
|
155 |
],
|
156 |
theme=gr.themes.Soft(),
|
157 |
allow_flagging="never"
|
158 |
)
|
159 |
|
|
|
160 |
demo.launch()
|