Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
-
# Version: Corrected After Test 3 (V2 -
|
2 |
-
# Description: Cette version
|
3 |
-
#
|
4 |
-
#
|
5 |
|
6 |
import os
|
7 |
import shutil
|
@@ -14,11 +14,12 @@ from pydub import AudioSegment
|
|
14 |
from transformers import pipeline
|
15 |
|
16 |
# -------------------------------------------------
|
17 |
-
# Configuration
|
18 |
# -------------------------------------------------
|
19 |
MODEL_NAME = "openai/whisper-large-v3"
|
20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
|
|
|
22 |
pipe = pipeline(
|
23 |
task="automatic-speech-recognition",
|
24 |
model=MODEL_NAME,
|
@@ -26,12 +27,16 @@ pipe = pipeline(
|
|
26 |
model_kwargs={"low_cpu_mem_usage": True},
|
27 |
)
|
28 |
|
|
|
29 |
TEMP_DIR = "./temp_audio"
|
30 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
31 |
|
32 |
def init_metadata_state():
|
33 |
return []
|
34 |
|
|
|
|
|
|
|
35 |
def transcribe_audio(audio_path):
|
36 |
if not audio_path:
|
37 |
print("[LOG] Aucun fichier audio fourni.")
|
@@ -54,6 +59,9 @@ def transcribe_audio(audio_path):
|
|
54 |
print(f"[LOG DETAIL] Timestamps associés : {word_timestamps}")
|
55 |
return raw_transcription, [], audio_path, word_timestamps, transcription_with_timestamps
|
56 |
|
|
|
|
|
|
|
57 |
def preprocess_segments(table_data, word_timestamps):
|
58 |
print("[LOG] Début du prétraitement des segments...")
|
59 |
formatted_data = []
|
@@ -82,6 +90,9 @@ def preprocess_segments(table_data, word_timestamps):
|
|
82 |
|
83 |
return formatted_data
|
84 |
|
|
|
|
|
|
|
85 |
def validate_segments(audio_path, table_data, metadata_state, word_timestamps):
|
86 |
print("[LOG] Début de la validation des segments...")
|
87 |
if not audio_path or not word_timestamps:
|
@@ -125,7 +136,35 @@ def validate_segments(audio_path, table_data, metadata_state, word_timestamps):
|
|
125 |
return segment_paths, updated_metadata
|
126 |
|
127 |
# -------------------------------------------------
|
128 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
# -------------------------------------------------
|
130 |
with gr.Blocks() as demo:
|
131 |
gr.Markdown("# Application de Découpe Audio")
|
|
|
1 |
+
# Version: Corrected After Test 3 (V2.2.0 - Structured, Commented, and ZIP Generation Restored)
|
2 |
+
# Description: Cette version structure le script selon l'ordre des étapes du processus.
|
3 |
+
# La génération du fichier ZIP a été réintégrée après avoir été omise dans la version précédente.
|
4 |
+
# Chaque section est commentée pour assurer une meilleure lisibilité et une logique claire.
|
5 |
|
6 |
import os
|
7 |
import shutil
|
|
|
14 |
from transformers import pipeline
|
15 |
|
16 |
# -------------------------------------------------
|
17 |
+
# 1. Configuration et Initialisation
|
18 |
# -------------------------------------------------
|
19 |
MODEL_NAME = "openai/whisper-large-v3"
|
20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
|
22 |
+
# Initialisation du modèle Whisper
|
23 |
pipe = pipeline(
|
24 |
task="automatic-speech-recognition",
|
25 |
model=MODEL_NAME,
|
|
|
27 |
model_kwargs={"low_cpu_mem_usage": True},
|
28 |
)
|
29 |
|
30 |
+
# Création du répertoire temporaire pour stocker les extraits audio
|
31 |
TEMP_DIR = "./temp_audio"
|
32 |
os.makedirs(TEMP_DIR, exist_ok=True)
|
33 |
|
34 |
def init_metadata_state():
|
35 |
return []
|
36 |
|
37 |
+
# -------------------------------------------------
|
38 |
+
# 2. Transcription de l'audio avec Whisper
|
39 |
+
# -------------------------------------------------
|
40 |
def transcribe_audio(audio_path):
|
41 |
if not audio_path:
|
42 |
print("[LOG] Aucun fichier audio fourni.")
|
|
|
59 |
print(f"[LOG DETAIL] Timestamps associés : {word_timestamps}")
|
60 |
return raw_transcription, [], audio_path, word_timestamps, transcription_with_timestamps
|
61 |
|
62 |
+
# -------------------------------------------------
|
63 |
+
# 3. Prétraitement des segments : Associer les timestamps aux phrases sélectionnées
|
64 |
+
# -------------------------------------------------
|
65 |
def preprocess_segments(table_data, word_timestamps):
|
66 |
print("[LOG] Début du prétraitement des segments...")
|
67 |
formatted_data = []
|
|
|
90 |
|
91 |
return formatted_data
|
92 |
|
93 |
+
# -------------------------------------------------
|
94 |
+
# 4. Validation et découpage des extraits audio
|
95 |
+
# -------------------------------------------------
|
96 |
def validate_segments(audio_path, table_data, metadata_state, word_timestamps):
|
97 |
print("[LOG] Début de la validation des segments...")
|
98 |
if not audio_path or not word_timestamps:
|
|
|
136 |
return segment_paths, updated_metadata
|
137 |
|
138 |
# -------------------------------------------------
|
139 |
+
# 5. Génération du fichier ZIP
|
140 |
+
# -------------------------------------------------
|
141 |
+
def generate_zip(metadata_state):
|
142 |
+
if not metadata_state:
|
143 |
+
print("[LOG ERROR] Aucun segment valide trouvé pour la génération du ZIP.")
|
144 |
+
return None
|
145 |
+
|
146 |
+
zip_path = os.path.join(TEMP_DIR, "dataset.zip")
|
147 |
+
if os.path.exists(zip_path):
|
148 |
+
os.remove(zip_path)
|
149 |
+
|
150 |
+
metadata_csv_path = os.path.join(TEMP_DIR, "metadata.csv")
|
151 |
+
with open(metadata_csv_path, "w", encoding="utf-8") as f:
|
152 |
+
f.write("audio_file|text|speaker_name|API\n")
|
153 |
+
for seg in metadata_state:
|
154 |
+
f.write(f"{seg['audio_file']}|{seg['text']}|projectname|/API_PHONETIC/\n")
|
155 |
+
|
156 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
157 |
+
zf.write(metadata_csv_path, "metadata.csv")
|
158 |
+
for seg in metadata_state:
|
159 |
+
file_path = os.path.join(TEMP_DIR, seg["audio_file"])
|
160 |
+
if os.path.exists(file_path):
|
161 |
+
zf.write(file_path, seg["audio_file"])
|
162 |
+
|
163 |
+
print("[LOG] Fichier ZIP généré avec succès.")
|
164 |
+
return zip_path
|
165 |
+
|
166 |
+
# -------------------------------------------------
|
167 |
+
# 6. Interface utilisateur Gradio
|
168 |
# -------------------------------------------------
|
169 |
with gr.Blocks() as demo:
|
170 |
gr.Markdown("# Application de Découpe Audio")
|