Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,132 +15,184 @@ os.makedirs(output_folder, exist_ok=True)
|
|
15 |
# Fonction pour générer un fichier audio à partir d'une section
|
16 |
def generate_section_audio(project_name, section_name, text, speaker):
|
17 |
try:
|
18 |
-
# Création du sous-dossier pour le projet
|
19 |
project_path = os.path.join(output_folder, project_name)
|
20 |
os.makedirs(project_path, exist_ok=True)
|
21 |
-
|
22 |
-
# Définir le chemin de sortie pour cette section
|
23 |
file_name = f"{section_name}.wav"
|
24 |
output_path = os.path.join(project_path, file_name)
|
25 |
-
|
26 |
-
# Vérifier la disponibilité des fichiers audio pour le speaker
|
27 |
speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
|
28 |
if not speaker_wav_paths:
|
29 |
raise ValueError(f"Aucun fichier audio trouvé pour le speaker : {speaker}")
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
text=text,
|
34 |
-
file_path=output_path,
|
35 |
-
speaker_wav=speaker_wav_paths,
|
36 |
-
language="fr"
|
37 |
-
)
|
38 |
-
|
39 |
-
return output_path # Retourne le chemin de l'audio généré
|
40 |
except Exception as e:
|
41 |
-
return str(e)
|
42 |
-
|
43 |
-
# Fonction pour gérer l'état d'avancement de l'interface
|
44 |
-
def update_step(step):
|
45 |
-
return {"visible": step == 1}, {"visible": step == 2}, {"visible": step == 3}
|
46 |
|
47 |
# Interface Gradio
|
48 |
with gr.Blocks() as demo:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
sections = gr.State(value=[])
|
53 |
-
|
54 |
-
# Introduction générale
|
55 |
-
gr.Markdown("""
|
56 |
-
# 🎙️ Synthèse Vocale Margaux
|
57 |
-
## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée
|
58 |
-
Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio.
|
59 |
-
**Étapes principales :**
|
60 |
-
1. 🛠️ **Créer un projet** : Définissez le nom du projet et choisissez la voix.
|
61 |
-
2. ✍️ **Ajouter des sections** : Divisez votre texte en parties claires, chacune avec un nom unique.
|
62 |
-
3. 🎧 **Générer les audios** : Chaque section est transformée en fichier audio individuel.
|
63 |
-
4. 📁 **Sauvegardez le projet** : Finalisez et récupérez les fichiers validés.
|
64 |
-
""")
|
65 |
|
66 |
# Étape 1 : Création du Projet
|
67 |
-
with gr.
|
68 |
-
gr.Markdown("
|
69 |
-
gr.Markdown("
|
70 |
-
project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU")
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
# Étape 2 : Gestion des Sections
|
77 |
-
with gr.
|
78 |
-
gr.Markdown("
|
79 |
gr.Markdown("""
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
- Un **texte clair et concis** ✏️.
|
84 |
""")
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
gr.Markdown("""
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
""")
|
|
|
99 |
generate_btn = gr.Button("Générer les Audios ▶️")
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
|
103 |
|
104 |
-
#
|
105 |
-
def
|
106 |
-
|
107 |
-
return "❗ Veuillez accepter les conditions d'utilisation.", 1
|
108 |
-
if not project_name:
|
109 |
-
return "❗ Le nom du projet est obligatoire.", 1
|
110 |
-
os.makedirs(os.path.join(output_folder, project_name), exist_ok=True)
|
111 |
-
return f"✅ Projet '{project_name}' créé avec succès !", 2
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
def add_section(sections):
|
121 |
-
|
122 |
-
sections.append(section)
|
123 |
return sections
|
124 |
|
125 |
-
add_section_btn.click(add_section, inputs=[sections], outputs=[sections_list])
|
126 |
-
|
127 |
def remove_section(sections):
|
128 |
if sections:
|
129 |
sections.pop()
|
|
|
130 |
return sections
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Fonction pour générer un fichier audio à partir d'une section
|
16 |
def generate_section_audio(project_name, section_name, text, speaker):
|
17 |
try:
|
|
|
18 |
project_path = os.path.join(output_folder, project_name)
|
19 |
os.makedirs(project_path, exist_ok=True)
|
|
|
|
|
20 |
file_name = f"{section_name}.wav"
|
21 |
output_path = os.path.join(project_path, file_name)
|
22 |
+
|
|
|
23 |
speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
|
24 |
if not speaker_wav_paths:
|
25 |
raise ValueError(f"Aucun fichier audio trouvé pour le speaker : {speaker}")
|
26 |
+
|
27 |
+
tts.tts_to_file(text=text, file_path=output_path, speaker_wav=speaker_wav_paths, language="fr")
|
28 |
+
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
except Exception as e:
|
30 |
+
return str(e)
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Interface Gradio
|
33 |
with gr.Blocks() as demo:
|
34 |
+
current_step = gr.State(1)
|
35 |
+
project_info = gr.State({})
|
36 |
+
sections = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Étape 1 : Création du Projet
|
39 |
+
with gr.Group() as step1:
|
40 |
+
gr.Markdown("## 🛠️ Étape 1 : Création du Projet")
|
41 |
+
gr.Markdown("**Définissez les informations générales pour votre projet de synthèse vocale.**")
|
42 |
+
project_name = gr.Textbox(label="Nom du Projet 📂", placeholder="Exemple : Capsule_Video_PLU")
|
43 |
+
gr.Markdown("Le nom du projet sera utilisé pour organiser les fichiers dans un dossier dédié.")
|
44 |
+
speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux")
|
45 |
+
gr.Markdown("Choisissez la voix souhaitée pour générer vos fichiers audio. Plus de voix seront ajoutées prochainement.")
|
46 |
+
agree = gr.Checkbox(label="✅ J’accepte les conditions d’utilisation")
|
47 |
+
gr.Markdown("Vous devez accepter les conditions d’utilisation pour continuer.")
|
48 |
+
next_btn_1 = gr.Button("Créer le Projet ➡️")
|
49 |
|
50 |
# Étape 2 : Gestion des Sections
|
51 |
+
with gr.Group(visible=False) as step2:
|
52 |
+
gr.Markdown("## ✍️ Étape 2 : Ajoutez vos Sections")
|
53 |
gr.Markdown("""
|
54 |
+
**Divisez votre texte en plusieurs sections pour une meilleure qualité audio.**
|
55 |
+
Chaque section correspond à une partie de votre script. Par exemple, une introduction, une explication, ou une conclusion.
|
56 |
+
Donnez un nom unique à chaque section et saisissez le texte correspondant.
|
|
|
57 |
""")
|
58 |
+
|
59 |
+
sections_list = gr.List(
|
60 |
+
[gr.Group([
|
61 |
+
gr.Textbox(label="Nom de la Section 🏷️", placeholder="Exemple : Introduction"),
|
62 |
+
gr.TextArea(label="Texte de la Section ✏️", placeholder="Écrivez ici le texte à convertir en voix off.")
|
63 |
+
])],
|
64 |
+
label="Sections"
|
65 |
+
)
|
66 |
+
|
67 |
+
add_section_btn = gr.Button("Ajouter une Section ➕")
|
68 |
+
remove_section_btn = gr.Button("Supprimer une Section ➖")
|
69 |
+
next_btn_2 = gr.Button("Valider les Sections ➡️")
|
70 |
+
prev_btn_2 = gr.Button("⬅️ Retour")
|
71 |
+
|
72 |
+
# Étape 3 : Validation des Sections
|
73 |
+
with gr.Group(visible=False) as step3:
|
74 |
+
gr.Markdown("## 🎧 Étape 3 : Validation des Sections")
|
75 |
gr.Markdown("""
|
76 |
+
**Vérifiez vos sections avant de lancer la génération des audios.**
|
77 |
+
Assurez-vous que chaque section a un nom unique et que les textes sont complets.
|
78 |
+
Cliquez sur Générer les Audios pour commencer.
|
79 |
""")
|
80 |
+
|
81 |
generate_btn = gr.Button("Générer les Audios ▶️")
|
82 |
+
prev_btn_3 = gr.Button("⬅️ Retour")
|
83 |
+
|
84 |
+
# Étape 4 : Génération des Audios
|
85 |
+
with gr.Group(visible=False) as step4:
|
86 |
+
gr.Markdown("## 🎧 Étape 4 : Génération des Audios")
|
87 |
+
audio_outputs = []
|
88 |
+
|
89 |
+
def render_audio_outputs(sections):
|
90 |
+
return [gr.Audio(label=f"Audio de {section['name']}") for section in sections]
|
91 |
+
|
92 |
+
audio_outputs.extend(render_audio_outputs(sections))
|
93 |
+
|
94 |
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
|
95 |
|
96 |
+
# Fonctions de navigation et de traitement
|
97 |
+
def next_step(step):
|
98 |
+
return step + 1
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
def prev_step(step):
|
101 |
+
return max(1, step - 1)
|
102 |
+
|
103 |
+
def update_visibility(step):
|
104 |
+
return {
|
105 |
+
step1: gr.update(visible=step == 1),
|
106 |
+
step2: gr.update(visible=step == 2),
|
107 |
+
step3: gr.update(visible=step == 3),
|
108 |
+
step4: gr.update(visible=step == 4)
|
109 |
+
}
|
110 |
+
|
111 |
+
def create_project(name, voice, terms):
|
112 |
+
if not name or not terms:
|
113 |
+
return "Veuillez remplir tous les champs et accepter les conditions.", None
|
114 |
+
return f"Projet '{name}' créé avec succès !", {"name": name, "voice": voice}, 2
|
115 |
|
116 |
def add_section(sections):
|
117 |
+
sections.append({"name": f"Section {len(sections) + 1}", "text": ""})
|
|
|
118 |
return sections
|
119 |
|
|
|
|
|
120 |
def remove_section(sections):
|
121 |
if sections:
|
122 |
sections.pop()
|
123 |
+
return sections
|
124 |
return sections
|
125 |
|
126 |
+
def generate_audios(project_info, sections):
|
127 |
+
outputs = []
|
128 |
+
for section in sections:
|
129 |
+
audio_path = generate_section_audio(
|
130 |
+
project_info["name"],
|
131 |
+
section["name"],
|
132 |
+
section["text"],
|
133 |
+
project_info["voice"]
|
134 |
+
)
|
135 |
+
outputs.append(audio_path)
|
136 |
+
|
137 |
+
return outputs
|
138 |
+
|
139 |
+
# Événements
|
140 |
+
next_btn_1.click(
|
141 |
+
create_project,
|
142 |
+
inputs=[project_name, speaker, agree],
|
143 |
+
outputs=[None, project_info, current_step]
|
144 |
+
).then(
|
145 |
+
update_visibility,
|
146 |
+
inputs=[current_step],
|
147 |
+
outputs=[step1, step2, step3]
|
148 |
+
)
|
149 |
|
150 |
+
add_section_btn.click(
|
151 |
+
add_section,
|
152 |
+
inputs=[sections],
|
153 |
+
outputs=[sections_list]
|
154 |
+
)
|
155 |
+
|
156 |
+
remove_section_btn.click(
|
157 |
+
remove_section,
|
158 |
+
inputs=[sections],
|
159 |
+
outputs=[sections_list]
|
160 |
+
)
|
161 |
+
|
162 |
+
next_btn_2.click(
|
163 |
+
lambda x: x + 1,
|
164 |
+
inputs=[current_step],
|
165 |
+
outputs=[current_step]
|
166 |
+
).then(
|
167 |
+
update_visibility,
|
168 |
+
inputs=[current_step],
|
169 |
+
outputs=[step1, step2, step3]
|
170 |
+
)
|
171 |
+
|
172 |
+
prev_btn_2.click(
|
173 |
+
prev_step,
|
174 |
+
inputs=[current_step],
|
175 |
+
outputs=[current_step]
|
176 |
+
).then(
|
177 |
+
update_visibility,
|
178 |
+
inputs=[current_step],
|
179 |
+
outputs=[step1, step2, step3]
|
180 |
+
)
|
181 |
+
|
182 |
+
generate_btn.click(
|
183 |
+
generate_audios,
|
184 |
+
inputs=[project_info, sections],
|
185 |
+
outputs=audio_outputs
|
186 |
+
)
|
187 |
+
|
188 |
+
prev_btn_3.click(
|
189 |
+
prev_step,
|
190 |
+
inputs=[current_step],
|
191 |
+
outputs=[current_step]
|
192 |
+
).then(
|
193 |
+
update_visibility,
|
194 |
+
inputs=[current_step],
|
195 |
+
outputs=[step1, step2, step3]
|
196 |
+
)
|
197 |
+
|
198 |
+
demo.launch()
|