binaryMao commited on
Commit
b01f955
·
verified ·
1 Parent(s): 3b5085e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -40
app.py CHANGED
@@ -8,7 +8,7 @@ import gradio as gr
8
  # 1. CONFIGURATION MATÉRIEL
9
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
- # 2. DICTIONNAIRE DES MODÈLES (Mis à jour selon votre capture d'écran)
12
  MODELS = {
13
  "Soloba V3 (CTC)": ("RobotsMali/soloba-ctc-0.6b-v3", "ctc"),
14
  "Soloba V1.5 (TDT)": ("RobotsMali/soloba-tdt-0.6b-v1.5", "rnnt"),
@@ -21,19 +21,18 @@ MODELS = {
21
  _cache = {}
22
 
23
  def clear_memory():
24
- """Nettoie la VRAM et la RAM pour éviter les débordements."""
25
  _cache.clear()
26
  gc.collect()
27
  if torch.cuda.is_available():
28
  torch.cuda.empty_cache()
29
 
30
- def load_model(name):
31
- """Charge le modèle avec optimisation FP16 si possible."""
32
- if name in _cache: return _cache[name]
 
33
 
34
- yield f"⏳ Chargement du modèle {name}..."
35
  clear_memory()
36
-
37
  repo, mode = MODELS[name]
38
  folder = snapshot_download(repo, local_dir_use_symlinks=False)
39
  nemo_file = next((os.path.join(folder, f) for f in os.listdir(folder) if f.endswith(".nemo")), None)
@@ -45,7 +44,7 @@ def load_model(name):
45
 
46
  model.to(DEVICE).eval()
47
  if DEVICE == "cuda":
48
- model.half() # Utilisation de la demi-précision pour gagner 50% de VRAM
49
 
50
  _cache[name] = model
51
  return model
@@ -58,37 +57,35 @@ def format_srt_time(sec):
58
  def pipeline(video_in, model_name):
59
  tmp_dir = tempfile.mkdtemp()
60
  try:
61
- if not video_in: return "❌ Source vide", None, None
 
62
 
63
  # A. Extraction Audio
64
- yield "⏳ Extraction de l'audio...", None, None
65
  full_wav = os.path.join(tmp_dir, "full.wav")
66
  subprocess.run(f"ffmpeg -y -i {shlex.quote(video_in)} -vn -ac 1 -ar 16000 {full_wav}", shell=True, check=True)
67
 
68
- # B. Segmentation Temporelle (10s)
69
  segment_pattern = os.path.join(tmp_dir, "seg_%03d.wav")
70
  subprocess.run(f"ffmpeg -i {full_wav} -f segment -segment_time 10 -c copy {segment_pattern}", shell=True, check=True)
71
  audio_segments = sorted(glob.glob(os.path.join(tmp_dir, "seg_*.wav")))
72
 
73
- # C. Chargement et Calcul de la Précision Temporelle
74
- model_gen = load_model(model_name)
75
- model = None
76
- for update in model_gen:
77
- if isinstance(update, str): yield update, None, None
78
- else: model = update
79
 
80
- # Calcul dynamique du stride (facteur de conversion frames -> secondes)
81
  stride = 0.02
82
  if hasattr(model, 'preprocessor') and hasattr(model.preprocessor, 'featurizer'):
83
  hop = model.preprocessor.featurizer.hop_length
84
  sr = model.preprocessor.featurizer.sample_rate
85
  stride = hop / sr
86
 
87
- # D. Transcription par segments
88
  all_words_ts = []
89
  for idx, seg_path in enumerate(audio_segments):
90
  base_time = idx * 10.0
91
- yield f"⏳ IA : Transcription segment {idx+1}/{len(audio_segments)}...", None, None
92
 
93
  hyp = model.transcribe([seg_path], return_hypotheses=True)[0]
94
  offsets = getattr(hyp, 'word_offsets', None)
@@ -99,12 +96,11 @@ def pipeline(video_in, model_name):
99
  start_t = base_time + (offsets[i] * stride)
100
  all_words_ts.append({"word": word, "start": start_t, "end": start_t + 0.45})
101
  else:
102
- # Fallback : Répartition linéaire si les offsets manquent
103
  gap = 10.0 / max(len(words), 1)
104
  for i, w in enumerate(words):
105
  all_words_ts.append({"word": w, "start": base_time + (i * gap), "end": base_time + ((i+1) * gap)})
106
 
107
- # E. Génération du fichier SRT
108
  srt_path = os.path.join(tmp_dir, "final.srt")
109
  words_per_line = 6
110
  with open(srt_path, "w", encoding="utf-8") as f:
@@ -114,11 +110,10 @@ def pipeline(video_in, model_name):
114
  f.write(f"{format_srt_time(chunk[0]['start'])} --> {format_srt_time(chunk[-1]['end'])}\n")
115
  f.write(" ".join([c['word'] for c in chunk]) + "\n\n")
116
 
117
- # F. Encodage Vidéo avec Incrustation (Burn-in)
118
- yield "⏳ Rendu vidéo final ...", None, srt_path
119
  out_path = os.path.abspath(f"robotsmali_final_{int(time.time())}.mp4")
120
 
121
- # Protection des chemins pour FFmpeg (indispensable pour Windows/Linux)
122
  safe_srt = srt_path.replace("\\", "/").replace(":", "\\:")
123
  cmd_ffmpeg = (
124
  f"ffmpeg -y -i {shlex.quote(video_in)} "
@@ -127,28 +122,27 @@ def pipeline(video_in, model_name):
127
  )
128
  subprocess.run(cmd_ffmpeg, shell=True, check=True)
129
 
130
- yield "✅ Transcription et Incrustation Terminées !", out_path, srt_path
131
 
132
  except Exception as e:
133
  traceback.print_exc()
134
- yield f"❌ Erreur : {str(e)}", None, None
135
 
136
- # 6. INTERFACE UTILISATEUR GRADIO
137
- with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}") as demo:
138
- gr.HTML("<h1 style='text-align:center; color:#EAB308;'>🤖 ROBOTSMALI TRANSCRIPTION PRO</h1>")
139
 
140
  with gr.Row():
141
- with gr.Column(scale=1):
142
- v_in = gr.Video(label="Vidéo Source (Upload ou Webcam)", sources=["upload", "webcam"])
143
- m_sel = gr.Dropdown(choices=list(MODELS.keys()), value="Soloba V3 (CTC)", label="Choisir le Modèle IA")
144
- btn_run = gr.Button("🚀 GÉNÉRER LES SOUS-TITRES", variant="primary")
145
 
146
- with gr.Column(scale=1):
147
- status = gr.Markdown("### État\nPrêt à l'emploi.")
148
- v_out = gr.Video(label="Vidéo Finale Incrustée")
149
- f_srt = gr.File(label="Fichier Sous-titres (.SRT)")
150
 
151
- btn_run.click(pipeline, [v_in, m_sel], [status, v_out, f_srt])
152
 
153
  if __name__ == "__main__":
154
- demo.launch(debug=True, show_error=True)
 
8
  # 1. CONFIGURATION MATÉRIEL
9
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
+ # 2. DICTIONNAIRE DES MODÈLES
12
  MODELS = {
13
  "Soloba V3 (CTC)": ("RobotsMali/soloba-ctc-0.6b-v3", "ctc"),
14
  "Soloba V1.5 (TDT)": ("RobotsMali/soloba-tdt-0.6b-v1.5", "rnnt"),
 
21
  _cache = {}
22
 
23
  def clear_memory():
24
+ """Libère la VRAM et la RAM."""
25
  _cache.clear()
26
  gc.collect()
27
  if torch.cuda.is_available():
28
  torch.cuda.empty_cache()
29
 
30
+ def get_model(name):
31
+ """Charge le modèle et le retourne directement."""
32
+ if name in _cache:
33
+ return _cache[name]
34
 
 
35
  clear_memory()
 
36
  repo, mode = MODELS[name]
37
  folder = snapshot_download(repo, local_dir_use_symlinks=False)
38
  nemo_file = next((os.path.join(folder, f) for f in os.listdir(folder) if f.endswith(".nemo")), None)
 
44
 
45
  model.to(DEVICE).eval()
46
  if DEVICE == "cuda":
47
+ model.half()
48
 
49
  _cache[name] = model
50
  return model
 
57
  def pipeline(video_in, model_name):
58
  tmp_dir = tempfile.mkdtemp()
59
  try:
60
+ if not video_in:
61
+ return "❌ Source vide", None
62
 
63
  # A. Extraction Audio
64
+ yield "⏳ Extraction de l'audio...", None
65
  full_wav = os.path.join(tmp_dir, "full.wav")
66
  subprocess.run(f"ffmpeg -y -i {shlex.quote(video_in)} -vn -ac 1 -ar 16000 {full_wav}", shell=True, check=True)
67
 
68
+ # B. Segmentation (10s)
69
  segment_pattern = os.path.join(tmp_dir, "seg_%03d.wav")
70
  subprocess.run(f"ffmpeg -i {full_wav} -f segment -segment_time 10 -c copy {segment_pattern}", shell=True, check=True)
71
  audio_segments = sorted(glob.glob(os.path.join(tmp_dir, "seg_*.wav")))
72
 
73
+ # C. Chargement du modèle
74
+ yield f"⏳ Chargement du modèle {model_name}...", None
75
+ model = get_model(model_name)
 
 
 
76
 
77
+ # Facteur de temps dynamique
78
  stride = 0.02
79
  if hasattr(model, 'preprocessor') and hasattr(model.preprocessor, 'featurizer'):
80
  hop = model.preprocessor.featurizer.hop_length
81
  sr = model.preprocessor.featurizer.sample_rate
82
  stride = hop / sr
83
 
84
+ # D. Transcription
85
  all_words_ts = []
86
  for idx, seg_path in enumerate(audio_segments):
87
  base_time = idx * 10.0
88
+ yield f"⏳ IA : Transcription {idx+1}/{len(audio_segments)}...", None
89
 
90
  hyp = model.transcribe([seg_path], return_hypotheses=True)[0]
91
  offsets = getattr(hyp, 'word_offsets', None)
 
96
  start_t = base_time + (offsets[i] * stride)
97
  all_words_ts.append({"word": word, "start": start_t, "end": start_t + 0.45})
98
  else:
 
99
  gap = 10.0 / max(len(words), 1)
100
  for i, w in enumerate(words):
101
  all_words_ts.append({"word": w, "start": base_time + (i * gap), "end": base_time + ((i+1) * gap)})
102
 
103
+ # E. Génération SRT (Fichier temporaire interne)
104
  srt_path = os.path.join(tmp_dir, "final.srt")
105
  words_per_line = 6
106
  with open(srt_path, "w", encoding="utf-8") as f:
 
110
  f.write(f"{format_srt_time(chunk[0]['start'])} --> {format_srt_time(chunk[-1]['end'])}\n")
111
  f.write(" ".join([c['word'] for c in chunk]) + "\n\n")
112
 
113
+ # F. Encodage Vidéo
114
+ yield "⏳ Rendu vidéo final...", None
115
  out_path = os.path.abspath(f"robotsmali_final_{int(time.time())}.mp4")
116
 
 
117
  safe_srt = srt_path.replace("\\", "/").replace(":", "\\:")
118
  cmd_ffmpeg = (
119
  f"ffmpeg -y -i {shlex.quote(video_in)} "
 
122
  )
123
  subprocess.run(cmd_ffmpeg, shell=True, check=True)
124
 
125
+ yield "✅ Terminé !", out_path
126
 
127
  except Exception as e:
128
  traceback.print_exc()
129
+ yield f"❌ Erreur : {str(e)}", None
130
 
131
+ # 6. INTERFACE
132
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
133
+ gr.HTML("<h1 style='text-align:center; color:#EAB308;'>🤖 ROBOTSMALI TRANSCRIPTION</h1>")
134
 
135
  with gr.Row():
136
+ with gr.Column():
137
+ v_in = gr.Video(label="Source")
138
+ m_sel = gr.Dropdown(choices=list(MODELS.keys()), value="Soloba V3 (CTC)", label="Modèle IA")
139
+ btn_run = gr.Button("🚀 GÉNÉRER", variant="primary")
140
 
141
+ with gr.Column():
142
+ status = gr.Markdown("### État\nPrêt.")
143
+ v_out = gr.Video(label="Vidéo Finale")
 
144
 
145
+ btn_run.click(pipeline, [v_in, m_sel], [status, v_out])
146
 
147
  if __name__ == "__main__":
148
+ demo.launch(debug=True)