binaryMao commited on
Commit
4ff231b
·
verified ·
1 Parent(s): 1e5b3af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -21
app.py CHANGED
@@ -1,35 +1,139 @@
1
- # app.py
 
 
 
 
 
 
 
2
  import gradio as gr
3
- import nemo.collections.asr as nemo_asr
4
  import torch
 
5
  import warnings
 
 
6
 
7
  warnings.filterwarnings("ignore")
8
 
9
- MODEL = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- def load_model():
12
- global MODEL
13
- if MODEL is None:
14
- MODEL = nemo_asr.models.EncDecCTCModel.from_pretrained(
15
- "nvidia/stt_en_conformer_ctc_small"
16
- )
17
- MODEL.eval()
18
- return MODEL
19
 
20
- def transcribe(audio_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  if audio_path is None:
22
- return "Aucun audio fourni"
 
 
 
 
 
23
 
24
- model = load_model()
25
- with torch.no_grad():
26
- text = model.transcribe([audio_path])[0]
27
- return text
28
 
 
 
 
 
 
 
29
  with gr.Blocks() as demo:
30
- gr.Markdown("## 🎙️ ASR Demo – Safe Mode")
31
- audio = gr.Audio(type="filepath")
32
- out = gr.Textbox()
33
- gr.Button("Transcrire").click(transcribe, audio, out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
 
 
 
35
  demo.launch()
 
1
+ # =====================================================
2
+ # HF SPACES – ROBOTSMALI ASR (SAFE MULTI-MODELS)
3
+ # Default: Soloni (lightweight)
4
+ # =====================================================
5
+
6
+ import os
7
+ os.environ["GRADIO_DISABLE_API"] = "1"
8
+
9
  import gradio as gr
 
10
  import torch
11
+ import nemo.collections.asr as nemo_asr
12
  import warnings
13
+ import gc
14
+ import time
15
 
16
  warnings.filterwarnings("ignore")
17
 
18
+ # =====================================================
19
+ # MODELS REGISTRY
20
+ # =====================================================
21
+ MODELS = {
22
+ "Soloni V3 (TDT-CTC) – Rapide (défaut)": ("RobotsMali/soloni-114m-tdt-ctc-v3", "rnnt"),
23
+ "Soloni V2 (TDT-CTC)": ("RobotsMali/soloni-114m-tdt-ctc-v2", "rnnt"),
24
+ "Soloni V1 (TDT-CTC)": ("RobotsMali/soloni-114m-tdt-ctc-v1", "rnnt"),
25
+
26
+ "Soloba V3 (CTC) – Haute précision": ("RobotsMali/soloba-ctc-0.6b-v3", "ctc"),
27
+ "Soloba V2 (CTC)": ("RobotsMali/soloba-ctc-0.6b-v2", "ctc"),
28
+ "Soloba V1 (CTC)": ("RobotsMali/soloba-ctc-0.6b-v1", "ctc"),
29
+
30
+ "Soloba V1.5 (TDT)": ("RobotsMali/soloba-tdt-0.6b-v1.5", "rnnt"),
31
+ "Soloba V0.5 (TDT)": ("RobotsMali/soloba-tdt-0.6b-v0.5", "rnnt"),
32
+ }
33
+
34
+ DEFAULT_MODEL = "Soloni V3 (TDT-CTC) – Rapide (défaut)"
35
+
36
+ current_model = None
37
+ current_model_name = None
38
+
39
+ # =====================================================
40
+ # MODEL LOADER (ONE MODEL AT A TIME)
41
+ # =====================================================
42
+ def load_model(model_name):
43
+ global current_model, current_model_name
44
+
45
+ # Already loaded
46
+ if current_model_name == model_name:
47
+ return current_model
48
+
49
+ # Clean previous model from memory
50
+ if current_model is not None:
51
+ del current_model
52
+ current_model = None
53
+ gc.collect()
54
+ if torch.cuda.is_available():
55
+ torch.cuda.empty_cache()
56
+
57
+ model_id, model_type = MODELS[model_name]
58
+
59
+ start = time.time()
60
 
61
+ if model_type == "ctc":
62
+ model = nemo_asr.models.EncDecCTCModel.from_pretrained(model_id)
63
+ else:
64
+ model = nemo_asr.models.EncDecRNNTModel.from_pretrained(model_id)
 
 
 
 
65
 
66
+ model.eval()
67
+
68
+ if torch.cuda.is_available():
69
+ model = model.cuda()
70
+
71
+ if hasattr(model, "decoding"):
72
+ model.decoding.strategy = "greedy"
73
+
74
+ current_model = model
75
+ current_model_name = model_name
76
+
77
+ load_time = round(time.time() - start, 2)
78
+ print(f"[INFO] Model loaded: {model_name} in {load_time}s")
79
+
80
+ return model
81
+
82
+ # =====================================================
83
+ # TRANSCRIPTION
84
+ # =====================================================
85
+ def transcribe(model_name, audio_path):
86
  if audio_path is None:
87
+ return "Aucun fichier audio fourni."
88
+
89
+ try:
90
+ asr_model = load_model(model_name)
91
+ with torch.no_grad():
92
+ result = asr_model.transcribe([audio_path])
93
 
94
+ return f"✅ Transcription :\n\n{result[0]}"
 
 
 
95
 
96
+ except Exception as e:
97
+ return f"❌ Erreur pendant la transcription :\n{e}"
98
+
99
+ # =====================================================
100
+ # UI
101
+ # =====================================================
102
  with gr.Blocks() as demo:
103
+ gr.Markdown(
104
+ """
105
+ ## 🤖 RobotsMali ASR – Démo officielle
106
+
107
+ 🟢 **Soloni** (léger) est utilisé par défaut pour un démarrage rapide
108
+ ⚠️ **Soloba** est plus précis mais peut prendre plusieurs minutes à charger
109
+ ℹ️ Un seul modèle est chargé à la fois pour éviter les crashs mémoire
110
+ """
111
+ )
112
+
113
+ model_selector = gr.Dropdown(
114
+ choices=list(MODELS.keys()),
115
+ value=DEFAULT_MODEL,
116
+ label="Choisir le modèle ASR"
117
+ )
118
+
119
+ audio = gr.Audio(
120
+ type="filepath",
121
+ label="Téléverser un fichier audio (wav, mp3, flac)"
122
+ )
123
+
124
+ output = gr.Textbox(
125
+ label="Résultat de la transcription",
126
+ lines=8
127
+ )
128
+
129
+ btn = gr.Button("🎙️ Transcrire")
130
+ btn.click(
131
+ fn=transcribe,
132
+ inputs=[model_selector, audio],
133
+ outputs=output
134
+ )
135
 
136
+ # =====================================================
137
+ # HF SPACES LAUNCH (MANDATORY)
138
+ # =====================================================
139
  demo.launch()