binaryMao commited on
Commit
7d87345
·
verified ·
1 Parent(s): d4a14e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -30
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # =====================================================
2
- # HF SPACES – ROBOTSMALI ASR (SAFE MULTI-MODELS)
3
- # Default: Soloni (lightweight)
4
  # =====================================================
5
 
6
  import os
@@ -8,22 +8,36 @@ 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
  }
28
 
29
  DEFAULT_MODEL = "Soloni V3 (TDT-CTC) – Rapide (défaut)"
@@ -32,16 +46,15 @@ current_model = None
32
  current_model_name = None
33
 
34
  # =====================================================
35
- # MODEL LOADER (ONE MODEL AT A TIME)
36
  # =====================================================
37
  def load_model(model_name):
38
  global current_model, current_model_name
39
 
40
- # Already loaded
41
  if current_model_name == model_name:
42
  return current_model
43
 
44
- # Clean previous model from memory
45
  if current_model is not None:
46
  del current_model
47
  current_model = None
@@ -49,22 +62,30 @@ def load_model(model_name):
49
  if torch.cuda.is_available():
50
  torch.cuda.empty_cache()
51
 
52
- model_id, model_type = MODELS[model_name]
53
 
54
  start = time.time()
55
 
56
- if model_type == "ctc":
57
- model = nemo_asr.models.EncDecCTCModel.from_pretrained(model_id)
58
- else:
59
- model = nemo_asr.models.EncDecRNNTModel.from_pretrained(model_id)
60
 
61
- model.eval()
 
 
 
 
 
62
 
63
- if torch.cuda.is_available():
64
- model = model.cuda()
 
 
 
65
 
66
- if hasattr(model, "decoding"):
67
- model.decoding.strategy = "greedy"
 
 
68
 
69
  current_model = model
70
  current_model_name = model_name
@@ -82,14 +103,21 @@ def transcribe(model_name, audio_path):
82
  return "❌ Aucun fichier audio fourni."
83
 
84
  try:
85
- asr_model = load_model(model_name)
86
- with torch.no_grad():
87
- result = asr_model.transcribe([audio_path])
 
 
 
 
 
88
 
89
- return f"✅ Transcription :\n\n{result[0]}"
 
 
90
 
91
  except Exception as e:
92
- return f"❌ Erreur pendant la transcription :\n{e}"
93
 
94
  # =====================================================
95
  # UI
@@ -99,9 +127,9 @@ with gr.Blocks() as demo:
99
  """
100
  ## 🤖 RobotsMali ASR – Démo officielle
101
 
102
- 🟢 **Soloni** (léger) est utilisé par défaut pour un démarrage rapide
103
- ⚠️ **Soloba** est plus précis mais peut prendre plusieurs minutes à charger
104
- ℹ️ Un seul modèle est chargé à la fois pour éviter les crashs mémoire
105
  """
106
  )
107
 
@@ -122,6 +150,7 @@ with gr.Blocks() as demo:
122
  )
123
 
124
  btn = gr.Button("🎙️ Transcrire")
 
125
  btn.click(
126
  fn=transcribe,
127
  inputs=[model_selector, audio],
@@ -129,6 +158,6 @@ with gr.Blocks() as demo:
129
  )
130
 
131
  # =====================================================
132
- # HF SPACES LAUNCH (MANDATORY)
133
  # =====================================================
134
- demo.launch()
 
1
  # =====================================================
2
+ # HF SPACES – ROBOTSMALI ASR (STABLE MULTI-MODELS)
3
+ # Basé sur le pipeline vidéo fonctionnel
4
  # =====================================================
5
 
6
  import os
 
8
 
9
  import gradio as gr
10
  import torch
 
11
  import warnings
12
  import gc
13
  import time
14
+ from huggingface_hub import snapshot_download
15
+ from nemo.collections import asr as nemo_asr
16
 
17
  warnings.filterwarnings("ignore")
18
 
19
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
20
+
21
  # =====================================================
22
+ # MODELS REGISTRY (TOUS LES MODÈLES VIDÉO)
23
  # =====================================================
24
  MODELS = {
25
+ # Soloba CTC
26
+ "Soloba V3 (CTC)": ("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 TDT
31
+ "Soloba V1.5 (TDT)": ("RobotsMali/soloba-tdt-0.6b-v1.5", "rnnt"),
32
+ "Soloba V0.5 (TDT)": ("RobotsMali/soloba-tdt-0.6b-v0.5", "rnnt"),
33
+
34
+ # Soloni
35
  "Soloni V3 (TDT-CTC) – Rapide (défaut)": ("RobotsMali/soloni-114m-tdt-ctc-v3", "rnnt"),
36
+ "Soloni V2 (TDT-CTC)": ("RobotsMali/soloni-114m-tdt-ctc-v2", "rnnt"),
37
+ "Soloni V1 (TDT-CTC)": ("RobotsMali/soloni-114m-tdt-ctc-v1", "rnnt"),
38
 
39
+ # Traduction
40
+ "Traduction Soloni (ST)": ("RobotsMali/st-soloni-114m-tdt-ctc", "rnnt"),
41
  }
42
 
43
  DEFAULT_MODEL = "Soloni V3 (TDT-CTC) – Rapide (défaut)"
 
46
  current_model_name = None
47
 
48
  # =====================================================
49
+ # MODEL LOADER (ROBUSTE)
50
  # =====================================================
51
  def load_model(model_name):
52
  global current_model, current_model_name
53
 
 
54
  if current_model_name == model_name:
55
  return current_model
56
 
57
+ # Nettoyage mémoire
58
  if current_model is not None:
59
  del current_model
60
  current_model = None
 
62
  if torch.cuda.is_available():
63
  torch.cuda.empty_cache()
64
 
65
+ repo, _ = MODELS[model_name]
66
 
67
  start = time.time()
68
 
69
+ # Téléchargement manuel du repo
70
+ folder = snapshot_download(repo, local_dir_use_symlinks=False)
 
 
71
 
72
+ # Recherche du fichier .nemo
73
+ nemo_file = next(
74
+ os.path.join(folder, f)
75
+ for f in os.listdir(folder)
76
+ if f.endswith(".nemo")
77
+ )
78
 
79
+ # Chargement stable
80
+ model = nemo_asr.models.ASRModel.restore_from(
81
+ nemo_file,
82
+ map_location=torch.device(DEVICE)
83
+ )
84
 
85
+ model.eval()
86
+
87
+ if DEVICE == "cuda":
88
+ model = model.half()
89
 
90
  current_model = model
91
  current_model_name = model_name
 
103
  return "❌ Aucun fichier audio fourni."
104
 
105
  try:
106
+ model = load_model(model_name)
107
+
108
+ with torch.inference_mode():
109
+ result = model.transcribe(
110
+ [audio_path],
111
+ batch_size=1,
112
+ num_workers=0
113
+ )
114
 
115
+ text = result[0] if isinstance(result, list) else str(result)
116
+
117
+ return f"✅ Transcription :\n\n{text}"
118
 
119
  except Exception as e:
120
+ return f"❌ Erreur pendant la transcription :\n{str(e)}"
121
 
122
  # =====================================================
123
  # UI
 
127
  """
128
  ## 🤖 RobotsMali ASR – Démo officielle
129
 
130
+ 🟢 **Soloni** démarre plus rapidement
131
+ ⚠️ **Soloba 0.6B** peut prendre du temps à charger
132
+ ℹ️ Un seul modèle est chargé à la fois pour stabilité maximale
133
  """
134
  )
135
 
 
150
  )
151
 
152
  btn = gr.Button("🎙️ Transcrire")
153
+
154
  btn.click(
155
  fn=transcribe,
156
  inputs=[model_selector, audio],
 
158
  )
159
 
160
  # =====================================================
161
+ # HF SPACES LAUNCH
162
  # =====================================================
163
+ demo.launch(server_name="0.0.0.0", server_port=7860)