Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,22 +31,13 @@ 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
|
| 38 |
-
corrected_text = re.sub(r"\s+", " ", corrected_text) # Suppression des doubles espaces
|
| 39 |
-
corrected_text = re.sub(r"\b([lLdDmMcCjJnNsStT]) ['’] (\w)", r"\1'\2", corrected_text) # Correction des apostrophes
|
| 40 |
-
corrected_text = re.sub(r"(?<=\w) ([?!:;])", r"\1", corrected_text) # Suppression de l'espace avant !, ?, : et ;
|
| 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):
|
|
@@ -63,6 +54,7 @@ def transcribe_audio(audio_path):
|
|
| 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 = []
|
|
@@ -122,11 +114,29 @@ def save_segments(table_data):
|
|
| 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 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
zip_folder_name = f"{zip_name}_dataset"
|
| 132 |
zip_path = os.path.join(TEMP_DIR, f"{zip_folder_name}.zip")
|
|
@@ -139,15 +149,6 @@ def generate_zip(metadata_state, audio_path, zip_name):
|
|
| 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 |
|
|
@@ -158,7 +159,7 @@ 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",
|
| 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,8 +167,8 @@ with gr.Blocks() as demo:
|
|
| 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
|
| 170 |
-
save_button.click(save_segments, inputs=table, outputs=[metadata_state
|
| 171 |
generate_button.click(generate_zip, inputs=[metadata_state, audio_input, zip_name], outputs=zip_file)
|
| 172 |
|
| 173 |
demo.queue().launch()
|
|
|
|
| 31 |
|
| 32 |
def init_metadata_state():
|
| 33 |
return []
|
| 34 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# -------------------------------------------------
|
| 36 |
# 2. Transcription de l'audio avec Whisper (Timestamps de fin + Marge de Sécurité)
|
| 37 |
# -------------------------------------------------
|
| 38 |
+
def correct_typography(text):
|
| 39 |
+
text = re.sub(r"\b([lLdDmMcCjJnNsStT]) ['’] (\w)", r"\1'\2", text) # Corrige les espaces autour des apostrophes
|
| 40 |
+
return text
|
| 41 |
|
| 42 |
@spaces.GPU(duration=120)
|
| 43 |
def transcribe_audio(audio_path):
|
|
|
|
| 54 |
return "Erreur : Aucun timestamp détecté.", None, [], ""
|
| 55 |
|
| 56 |
raw_transcription = " ".join([w["text"] for w in words])
|
| 57 |
+
raw_transcription = correct_typography(raw_transcription)
|
| 58 |
|
| 59 |
MARGIN = 0.06 # 60ms
|
| 60 |
word_timestamps = []
|
|
|
|
| 114 |
# 4. Génération du fichier ZIP
|
| 115 |
# -------------------------------------------------
|
| 116 |
def generate_zip(metadata_state, audio_path, zip_name):
|
| 117 |
+
if isinstance(metadata_state, tuple):
|
| 118 |
+
metadata_state = metadata_state[0]
|
| 119 |
+
|
| 120 |
if metadata_state is None or metadata_state.empty:
|
| 121 |
+
print("[LOG ERROR] Aucun segment valide trouvé pour la génération du ZIP.")
|
| 122 |
return None
|
| 123 |
|
| 124 |
+
zip_folder_name = f"{zip_name}_dataset"
|
| 125 |
+
zip_path = os.path.join(TEMP_DIR, f"{zip_folder_name}.zip")
|
| 126 |
+
metadata_csv_path = os.path.join(TEMP_DIR, "metadata.csv")
|
| 127 |
+
|
| 128 |
+
metadata_state["ID"] = [f"{zip_name}_seg_{i+1:02d}" for i in range(len(metadata_state))]
|
| 129 |
+
metadata_state["Texte"] = metadata_state["Texte"].apply(correct_typography)
|
| 130 |
+
metadata_state["Commentaires"] = ""
|
| 131 |
+
metadata_state.to_csv(metadata_csv_path, sep="|", index=False)
|
| 132 |
+
|
| 133 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 134 |
+
zf.write(metadata_csv_path, "metadata.csv")
|
| 135 |
+
|
| 136 |
+
print("[LOG] Fichier ZIP généré avec succès.")
|
| 137 |
+
return zip_path
|
| 138 |
+
if metadata_state is None or metadata_state.empty:
|
| 139 |
+
return None
|
| 140 |
|
| 141 |
zip_folder_name = f"{zip_name}_dataset"
|
| 142 |
zip_path = os.path.join(TEMP_DIR, f"{zip_folder_name}.zip")
|
|
|
|
| 149 |
|
| 150 |
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 151 |
zf.write(metadata_csv_path, "metadata.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
return zip_path
|
| 154 |
|
|
|
|
| 159 |
gr.Markdown("# Application de Découpe Audio")
|
| 160 |
metadata_state = gr.State(init_metadata_state())
|
| 161 |
audio_input = gr.Audio(type="filepath", label="Fichier audio")
|
| 162 |
+
zip_name = gr.Textbox(label="Nom du fichier ZIP", interactive=True)
|
| 163 |
raw_transcription = gr.Textbox(label="Transcription", interactive=True)
|
| 164 |
transcription_timestamps = gr.Textbox(label="Transcription avec Timestamps", interactive=True)
|
| 165 |
table = gr.Dataframe(headers=["Texte", "Début (s)", "Fin (s)"], datatype=["str", "str", "str"], row_count=(1, "dynamic"))
|
|
|
|
| 167 |
generate_button = gr.Button("Générer ZIP")
|
| 168 |
zip_file = gr.File(label="Télécharger le ZIP")
|
| 169 |
|
| 170 |
+
audio_input.change(transcribe_audio, inputs=audio_input, outputs=[raw_transcription, transcription_timestamps, audio_input])
|
| 171 |
+
save_button.click(save_segments, inputs=table, outputs=[metadata_state])
|
| 172 |
generate_button.click(generate_zip, inputs=[metadata_state, audio_input, zip_name], outputs=zip_file)
|
| 173 |
|
| 174 |
demo.queue().launch()
|