Woziii commited on
Commit
dbbf7fc
·
verified ·
1 Parent(s): f86f536

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -54
app.py CHANGED
@@ -31,7 +31,7 @@ os.makedirs(TEMP_DIR, exist_ok=True)
31
 
32
  def init_metadata_state():
33
  return []
34
-
35
  # Fonction de correction typographique complète avec détection préalable
36
  def correct_typography(text):
37
  corrected_text = text
@@ -41,56 +41,58 @@ def correct_typography(text):
41
  corrected_text = re.sub(r"([?!:;])(?=\w)", r"\1 ", corrected_text) # Ajout d'un espace après !, ?, : et ; si nécessaire
42
  corrected_text = re.sub(r"(?<!\d) (\.)", r"\1", corrected_text) # Suppression de l'espace avant un point
43
  corrected_text = re.sub(r"(\.) (?=\w)", r". \2", corrected_text) # Ajout d'un espace après un point si nécessaire
 
 
44
  return corrected_text.strip() if corrected_text != text else text
45
-
46
  # -------------------------------------------------
47
- # 2. Transcription de l'audio avec Whisper
48
  # -------------------------------------------------
 
49
  @spaces.GPU(duration=120)
50
  def transcribe_audio(audio_path):
51
- import os
52
- if not audio_path:
53
- return "Aucun fichier audio fourni", None, [], "", ""
54
-
55
- file_name = os.path.basename(audio_path).rsplit('.', 1)[0] # Extraire le nom sans extension
56
- result = pipe(audio_path, return_timestamps="word")
57
- words = result.get("chunks", [])
58
-
59
- if not words:
60
- return "Erreur : Aucun timestamp détecté.", None, [], "", file_name
61
-
62
- raw_transcription = " ".join([w["text"] for w in words])
63
- word_timestamps = [(w["text"], w["timestamp"][0], w["timestamp"][1]) for w in words]
64
- transcription_with_timestamps = " ".join([f"{w[0]}[{w[1]:.2f}-{w[2]:.2f}]" for w in word_timestamps])
65
-
66
- return raw_transcription, word_timestamps, transcription_with_timestamps, audio_path, file_name
67
  if not audio_path:
 
68
  return "Aucun fichier audio fourni", None, [], ""
69
 
 
70
  result = pipe(audio_path, return_timestamps="word")
71
  words = result.get("chunks", [])
72
 
73
  if not words:
 
74
  return "Erreur : Aucun timestamp détecté.", None, [], ""
75
 
76
  raw_transcription = " ".join([w["text"] for w in words])
77
- word_timestamps = [(w["text"], w["timestamp"][0], w["timestamp"][1]) for w in words]
78
- transcription_with_timestamps = " ".join([f"{w[0]}[{w[1]:.2f}-{w[2]:.2f}]" for w in word_timestamps])
79
 
80
- return raw_transcription, word_timestamps, transcription_with_timestamps, audio_path
 
 
 
 
 
 
 
 
 
81
 
 
 
 
 
 
 
82
 
83
  # -------------------------------------------------
84
- # 3. Enregistrement des segments définis par l'utilisateur
85
  # -------------------------------------------------
86
- def save_segments(table_data, zip_name):
87
  print("[LOG] Enregistrement des segments définis par l'utilisateur...")
88
  formatted_data = []
89
  confirmation_message = "**📌 Segments enregistrés :**\n"
90
 
91
  for i, row in table_data.iterrows():
92
  text, start_time, end_time = row["Texte"], row["Début (s)"], row["Fin (s)"]
93
- segment_id = f"{zip_name}_seg_{i+1:02d}"
94
 
95
  try:
96
  start_time = str(start_time).replace(",", ".")
@@ -114,39 +116,17 @@ def save_segments(table_data, zip_name):
114
  print(f"[LOG ERROR] Erreur de conversion des timestamps : {e}")
115
  return pd.DataFrame(), "❌ **Erreur** : Vérifiez que les valeurs sont bien des nombres valides."
116
 
117
- return pd.DataFrame(formatted_data, columns=["Texte", "Début (s)", "Fin (s)", "ID"]), confirmation_message, zip_name
118
- formatted_data = []
119
- for i, row in table_data.iterrows():
120
- formatted_data.append([row["Texte"], float(row["Début (s)"]), float(row["Fin (s)"])] )
121
- return pd.DataFrame(formatted_data, columns=["Texte", "Début (s)", "Fin (s)"]), zip_name
122
 
123
  # -------------------------------------------------
124
- # 4. Génération du fichier ZIP avec correction typographique
125
  # -------------------------------------------------
126
  def generate_zip(metadata_state, audio_path, zip_name):
127
- if isinstance(metadata_state, tuple):
128
- metadata_state = metadata_state[0]
129
-
130
  if metadata_state is None or metadata_state.empty:
131
- print("[LOG ERROR] Aucun segment valide trouvé pour la génération du ZIP.")
132
  return None
133
 
134
- zip_folder_name = f"{zip_name}_dataset"
135
- zip_path = os.path.join(TEMP_DIR, f"{zip_folder_name}.zip")
136
- metadata_csv_path = os.path.join(TEMP_DIR, "metadata.csv")
137
-
138
- metadata_state["ID"] = [f"{zip_name}_seg_{i+1:02d}" for i in range(len(metadata_state))]
139
- metadata_state["Texte"] = metadata_state["Texte"].apply(correct_typography)
140
- metadata_state["Commentaires"] = ""
141
- metadata_state.to_csv(metadata_csv_path, sep="|", index=False)
142
-
143
- with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
144
- zf.write(metadata_csv_path, "metadata.csv")
145
-
146
- print("[LOG] Fichier ZIP généré avec succès.")
147
- return zip_path
148
- if metadata_state is None or metadata_state.empty:
149
- return None
150
 
151
  zip_folder_name = f"{zip_name}_dataset"
152
  zip_path = os.path.join(TEMP_DIR, f"{zip_folder_name}.zip")
@@ -159,6 +139,15 @@ def generate_zip(metadata_state, audio_path, zip_name):
159
 
160
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
161
  zf.write(metadata_csv_path, "metadata.csv")
 
 
 
 
 
 
 
 
 
162
 
163
  return zip_path
164
 
@@ -168,8 +157,8 @@ def generate_zip(metadata_state, audio_path, zip_name):
168
  with gr.Blocks() as demo:
169
  gr.Markdown("# Application de Découpe Audio")
170
  metadata_state = gr.State(init_metadata_state())
171
- zip_name = gr.audio_input = gr.Audio(type="filepath", label="Fichier audio")
172
- zip_name = gr.Textbox(label="Nom du fichier ZIP", interactive=True)
173
  raw_transcription = gr.Textbox(label="Transcription", interactive=True)
174
  transcription_timestamps = gr.Textbox(label="Transcription avec Timestamps", interactive=True)
175
  table = gr.Dataframe(headers=["Texte", "Début (s)", "Fin (s)"], datatype=["str", "str", "str"], row_count=(1, "dynamic"))
@@ -177,7 +166,7 @@ with gr.Blocks() as demo:
177
  generate_button = gr.Button("Générer ZIP")
178
  zip_file = gr.File(label="Télécharger le ZIP")
179
 
180
- audio_input.change(transcribe_audio, inputs=audio_input, outputs=[raw_transcription, word_timestamps, transcription_timestamps, audio_input, zip_name])
181
  save_button.click(save_segments, inputs=table, outputs=[metadata_state, zip_name])
182
  generate_button.click(generate_zip, inputs=[metadata_state, audio_input, zip_name], outputs=zip_file)
183
 
 
31
 
32
  def init_metadata_state():
33
  return []
34
+
35
  # Fonction de correction typographique complète avec détection préalable
36
  def correct_typography(text):
37
  corrected_text = text
 
41
  corrected_text = re.sub(r"([?!:;])(?=\w)", r"\1 ", corrected_text) # Ajout d'un espace après !, ?, : et ; si nécessaire
42
  corrected_text = re.sub(r"(?<!\d) (\.)", r"\1", corrected_text) # Suppression de l'espace avant un point
43
  corrected_text = re.sub(r"(\.) (?=\w)", r". \2", corrected_text) # Ajout d'un espace après un point si nécessaire
44
+
45
+ # Appliquer la correction uniquement si le texte a changé
46
  return corrected_text.strip() if corrected_text != text else text
 
47
  # -------------------------------------------------
48
+ # 2. Transcription de l'audio avec Whisper (Timestamps de fin + Marge de Sécurité)
49
  # -------------------------------------------------
50
+
51
  @spaces.GPU(duration=120)
52
  def transcribe_audio(audio_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  if not audio_path:
54
+ print("[LOG] Aucun fichier audio fourni.")
55
  return "Aucun fichier audio fourni", None, [], ""
56
 
57
+ print(f"[LOG] Début de la transcription de {audio_path}...")
58
  result = pipe(audio_path, return_timestamps="word")
59
  words = result.get("chunks", [])
60
 
61
  if not words:
62
+ print("[LOG ERROR] Erreur : Aucun timestamp détecté.")
63
  return "Erreur : Aucun timestamp détecté.", None, [], ""
64
 
65
  raw_transcription = " ".join([w["text"] for w in words])
 
 
66
 
67
+ MARGIN = 0.06 # 60ms
68
+ word_timestamps = []
69
+
70
+ for i, w in enumerate(words):
71
+ start_time = w["timestamp"][0]
72
+ end_time = w["timestamp"][1] if w["timestamp"][1] is not None else start_time + 0.5
73
+
74
+ if i < len(words) - 1:
75
+ next_start_time = words[i + 1]["timestamp"][0]
76
+ end_time = min(end_time + MARGIN, next_start_time - 0.01)
77
 
78
+ word_timestamps.append((w["text"], start_time, end_time))
79
+
80
+ transcription_with_timestamps = " ".join([f"{w[0]}[{w[1]:.2f}-{w[2]:.2f}]" for w in word_timestamps])
81
+
82
+ print(f"[LOG] Transcription brute corrigée : {raw_transcription}")
83
+ return raw_transcription, word_timestamps, transcription_with_timestamps, audio_path
84
 
85
  # -------------------------------------------------
86
+ # 3. Enregistrement des segments définis par l'utilisateur (Affichage sur Interface)
87
  # -------------------------------------------------
88
+ def save_segments(table_data):
89
  print("[LOG] Enregistrement des segments définis par l'utilisateur...")
90
  formatted_data = []
91
  confirmation_message = "**📌 Segments enregistrés :**\n"
92
 
93
  for i, row in table_data.iterrows():
94
  text, start_time, end_time = row["Texte"], row["Début (s)"], row["Fin (s)"]
95
+ segment_id = f"seg_{i+1:02d}"
96
 
97
  try:
98
  start_time = str(start_time).replace(",", ".")
 
116
  print(f"[LOG ERROR] Erreur de conversion des timestamps : {e}")
117
  return pd.DataFrame(), "❌ **Erreur** : Vérifiez que les valeurs sont bien des nombres valides."
118
 
119
+ return pd.DataFrame(formatted_data, columns=["Texte", "Début (s)", "Fin (s)", "ID"]), confirmation_message
 
 
 
 
120
 
121
  # -------------------------------------------------
122
+ # 4. Génération du fichier ZIP
123
  # -------------------------------------------------
124
  def generate_zip(metadata_state, audio_path, zip_name):
 
 
 
125
  if metadata_state is None or metadata_state.empty:
 
126
  return None
127
 
128
+ if not zip_name.strip():
129
+ zip_name = "processed_audio"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  zip_folder_name = f"{zip_name}_dataset"
132
  zip_path = os.path.join(TEMP_DIR, f"{zip_folder_name}.zip")
 
139
 
140
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
141
  zf.write(metadata_csv_path, "metadata.csv")
142
+ original_audio = AudioSegment.from_file(audio_path)
143
+
144
+ for i, row in metadata_state.iterrows():
145
+ start_ms, end_ms = int(row["Début (s)"] * 1000), int(row["Fin (s)"] * 1000)
146
+ segment_audio = original_audio[start_ms:end_ms]
147
+ segment_filename = f"{zip_name}_seg_{i+1:02d}.wav"
148
+ segment_path = os.path.join(TEMP_DIR, segment_filename)
149
+ segment_audio.export(segment_path, format="wav")
150
+ zf.write(segment_path, segment_filename)
151
 
152
  return zip_path
153
 
 
157
  with gr.Blocks() as demo:
158
  gr.Markdown("# Application de Découpe Audio")
159
  metadata_state = gr.State(init_metadata_state())
160
+ audio_input = gr.Audio(type="filepath", label="Fichier audio")
161
+ zip_name = gr.Textbox(label="Nom du fichier ZIP", placeholder="Nom personnalisé", interactive=True)
162
  raw_transcription = gr.Textbox(label="Transcription", interactive=True)
163
  transcription_timestamps = gr.Textbox(label="Transcription avec Timestamps", interactive=True)
164
  table = gr.Dataframe(headers=["Texte", "Début (s)", "Fin (s)"], datatype=["str", "str", "str"], row_count=(1, "dynamic"))
 
166
  generate_button = gr.Button("Générer ZIP")
167
  zip_file = gr.File(label="Télécharger le ZIP")
168
 
169
+ audio_input.change(transcribe_audio, inputs=audio_input, outputs=[raw_transcription, transcription_timestamps, audio_input, zip_name])
170
  save_button.click(save_segments, inputs=table, outputs=[metadata_state, zip_name])
171
  generate_button.click(generate_zip, inputs=[metadata_state, audio_input, zip_name], outputs=zip_file)
172