Woziii commited on
Commit
72cecc5
·
verified ·
1 Parent(s): 6b565a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -21
app.py CHANGED
@@ -1,3 +1,9 @@
 
 
 
 
 
 
1
  import os
2
  import shutil
3
  import zipfile
@@ -60,7 +66,6 @@ def transcribe_audio(audio_path):
60
  def preprocess_segments(table_data, word_timestamps):
61
  print("[LOG] Début du prétraitement des segments...")
62
  formatted_data = []
63
-
64
  for i, row in enumerate(table_data):
65
  if not row or len(row) < 1 or not row[0].strip():
66
  print(f"[LOG WARNING] Ignoré : ligne vide à l'index {i}.")
@@ -68,12 +73,14 @@ def preprocess_segments(table_data, word_timestamps):
68
 
69
  text = row[0].strip()
70
  segment_id = f"seg_{i+1:02d}"
 
 
71
 
72
- start_time = next((start for word, start in word_timestamps if word in text.split()), None)
73
- end_time = next((start for word, start in word_timestamps if word in text.split()), None)
74
-
75
- if start_time is not None and end_time is not None:
76
- end_time += 0.5 # Ajout d'un délai de sécurité pour ne pas couper trop court
77
 
78
  formatted_data.append([text, start_time, end_time, segment_id])
79
  print(f"[LOG] Segment ajouté : {text} | Début: {start_time}, Fin: {end_time}, ID: {segment_id}")
@@ -83,26 +90,24 @@ def preprocess_segments(table_data, word_timestamps):
83
  # -------------------------------------------------
84
  # 4. Validation et découpage des extraits audio
85
  # -------------------------------------------------
86
- def validate_segments(audio_path, table_data, metadata_state):
87
  print("[LOG] Début de la validation des segments...")
88
-
89
- if not audio_path:
90
- print("[LOG ERROR] Erreur : Aucun fichier audio fourni !")
91
  return [], metadata_state
92
 
93
  if os.path.exists(TEMP_DIR):
94
  shutil.rmtree(TEMP_DIR)
95
  os.makedirs(TEMP_DIR, exist_ok=True)
96
-
97
  original_audio = AudioSegment.from_file(audio_path)
98
  segment_paths = []
99
  updated_metadata = []
100
-
101
  for row in table_data:
102
  if len(row) < 4:
103
- print("[LOG ERROR] Données invalides pour un segment.")
104
  continue
105
-
106
  text, start_time, end_time, segment_id = row
107
 
108
  if start_time is None or end_time is None:
@@ -113,7 +118,7 @@ def validate_segments(audio_path, table_data, metadata_state):
113
  if start_ms < 0 or end_ms <= start_ms:
114
  print(f"[LOG ERROR] Problème de découpage : {text} | {start_time}s - {end_time}s")
115
  continue
116
-
117
  segment_filename = f"{Path(audio_path).stem}_{segment_id}.wav"
118
  segment_path = os.path.join(TEMP_DIR, segment_filename)
119
 
@@ -131,7 +136,6 @@ def validate_segments(audio_path, table_data, metadata_state):
131
  print(f"[LOG] Extrait généré : {segment_filename}")
132
 
133
  return segment_paths, updated_metadata
134
-
135
  # -------------------------------------------------
136
  # 5. Génération du fichier ZIP
137
  # -------------------------------------------------
@@ -141,7 +145,17 @@ def generate_zip(metadata_state):
141
  return None
142
 
143
  zip_path = os.path.join(TEMP_DIR, "dataset.zip")
 
 
 
 
 
 
 
 
 
144
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
 
145
  for seg in metadata_state:
146
  file_path = os.path.join(TEMP_DIR, seg["audio_file"])
147
  if os.path.exists(file_path):
@@ -149,24 +163,27 @@ def generate_zip(metadata_state):
149
 
150
  print("[LOG] Fichier ZIP généré avec succès.")
151
  return zip_path
152
-
153
  # -------------------------------------------------
154
  # 6. Interface utilisateur Gradio
155
  # -------------------------------------------------
156
  with gr.Blocks() as demo:
157
  gr.Markdown("# Application de Découpe Audio")
158
  metadata_state = gr.State(init_metadata_state())
159
-
 
160
  audio_input = gr.Audio(type="filepath", label="Fichier audio")
161
  raw_transcription = gr.Textbox(label="Transcription", interactive=False)
162
  transcription_timestamps = gr.Textbox(label="Transcription avec Timestamps", interactive=False)
163
  table = gr.Dataframe(headers=["Texte"], datatype=["str"], row_count=(1, "dynamic"), col_count=1)
 
164
  validate_button = gr.Button("Valider")
165
  generate_button = gr.Button("Générer ZIP")
166
  zip_file = gr.File(label="Télécharger le ZIP")
 
167
 
168
- audio_input.change(transcribe_audio, inputs=audio_input, outputs=[raw_transcription, table, audio_input, transcription_timestamps])
169
- validate_button.click(validate_segments, inputs=[audio_input, table, metadata_state], outputs=[table, metadata_state])
 
170
  generate_button.click(generate_zip, inputs=metadata_state, outputs=zip_file)
171
 
172
- demo.queue().launch()
 
1
+ # Version: Corrected After Test 4 (V2.4.1 - Fixed Validation & Timestamps Button Restored)
2
+ # Description: Cette version corrige la validation des segments modifiés manuellement.
3
+ # Réintégration du bouton "Générer les timestamps".
4
+ # Correction du bug "too many values to unpack".
5
+ # La génération du fichier ZIP fonctionne correctement après validation.
6
+
7
  import os
8
  import shutil
9
  import zipfile
 
66
  def preprocess_segments(table_data, word_timestamps):
67
  print("[LOG] Début du prétraitement des segments...")
68
  formatted_data = []
 
69
  for i, row in enumerate(table_data):
70
  if not row or len(row) < 1 or not row[0].strip():
71
  print(f"[LOG WARNING] Ignoré : ligne vide à l'index {i}.")
 
73
 
74
  text = row[0].strip()
75
  segment_id = f"seg_{i+1:02d}"
76
+ start_time = None
77
+ end_time = None
78
 
79
+ for j, (word, start) in enumerate(word_timestamps):
80
+ if word in text.split():
81
+ if start_time is None:
82
+ start_time = start
83
+ end_time = word_timestamps[j+1][1] - 0.01 if j+1 < len(word_timestamps) else start + 0.5
84
 
85
  formatted_data.append([text, start_time, end_time, segment_id])
86
  print(f"[LOG] Segment ajouté : {text} | Début: {start_time}, Fin: {end_time}, ID: {segment_id}")
 
90
  # -------------------------------------------------
91
  # 4. Validation et découpage des extraits audio
92
  # -------------------------------------------------
93
+ def validate_segments(audio_path, table_data, metadata_state, word_timestamps):
94
  print("[LOG] Début de la validation des segments...")
95
+ if not audio_path or not word_timestamps:
96
+ print("[LOG ERROR] Erreur : Aucun timestamp valide trouvé !")
 
97
  return [], metadata_state
98
 
99
  if os.path.exists(TEMP_DIR):
100
  shutil.rmtree(TEMP_DIR)
101
  os.makedirs(TEMP_DIR, exist_ok=True)
102
+
103
  original_audio = AudioSegment.from_file(audio_path)
104
  segment_paths = []
105
  updated_metadata = []
106
+
107
  for row in table_data:
108
  if len(row) < 4:
109
+ print(f"[LOG ERROR] Données incorrectes pour la validation : {row}")
110
  continue
 
111
  text, start_time, end_time, segment_id = row
112
 
113
  if start_time is None or end_time is None:
 
118
  if start_ms < 0 or end_ms <= start_ms:
119
  print(f"[LOG ERROR] Problème de découpage : {text} | {start_time}s - {end_time}s")
120
  continue
121
+
122
  segment_filename = f"{Path(audio_path).stem}_{segment_id}.wav"
123
  segment_path = os.path.join(TEMP_DIR, segment_filename)
124
 
 
136
  print(f"[LOG] Extrait généré : {segment_filename}")
137
 
138
  return segment_paths, updated_metadata
 
139
  # -------------------------------------------------
140
  # 5. Génération du fichier ZIP
141
  # -------------------------------------------------
 
145
  return None
146
 
147
  zip_path = os.path.join(TEMP_DIR, "dataset.zip")
148
+ if os.path.exists(zip_path):
149
+ os.remove(zip_path)
150
+
151
+ metadata_csv_path = os.path.join(TEMP_DIR, "metadata.csv")
152
+ with open(metadata_csv_path, "w", encoding="utf-8") as f:
153
+ f.write("audio_file|text|speaker_name|API\n")
154
+ for seg in metadata_state:
155
+ f.write(f"{seg['audio_file']}|{seg['text']}|projectname|/API_PHONETIC/\n")
156
+
157
  with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
158
+ zf.write(metadata_csv_path, "metadata.csv")
159
  for seg in metadata_state:
160
  file_path = os.path.join(TEMP_DIR, seg["audio_file"])
161
  if os.path.exists(file_path):
 
163
 
164
  print("[LOG] Fichier ZIP généré avec succès.")
165
  return zip_path
 
166
  # -------------------------------------------------
167
  # 6. Interface utilisateur Gradio
168
  # -------------------------------------------------
169
  with gr.Blocks() as demo:
170
  gr.Markdown("# Application de Découpe Audio")
171
  metadata_state = gr.State(init_metadata_state())
172
+ extracted_segments = gr.State([])
173
+
174
  audio_input = gr.Audio(type="filepath", label="Fichier audio")
175
  raw_transcription = gr.Textbox(label="Transcription", interactive=False)
176
  transcription_timestamps = gr.Textbox(label="Transcription avec Timestamps", interactive=False)
177
  table = gr.Dataframe(headers=["Texte"], datatype=["str"], row_count=(1, "dynamic"), col_count=1)
178
+ generate_timestamps_button = gr.Button("Générer les timestamps")
179
  validate_button = gr.Button("Valider")
180
  generate_button = gr.Button("Générer ZIP")
181
  zip_file = gr.File(label="Télécharger le ZIP")
182
+ word_timestamps = gr.State()
183
 
184
+ audio_input.change(transcribe_audio, inputs=audio_input, outputs=[raw_transcription, table, audio_input, word_timestamps, transcription_timestamps])
185
+ generate_timestamps_button.click(preprocess_segments, inputs=[table, word_timestamps], outputs=table)
186
+ validate_button.click(validate_segments, inputs=[audio_input, table, metadata_state, word_timestamps], outputs=[extracted_segments, metadata_state])
187
  generate_button.click(generate_zip, inputs=metadata_state, outputs=zip_file)
188
 
189
+ demo.queue().launch()