Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,149 @@
|
|
1 |
import subprocess
|
|
|
2 |
import gradio as gr
|
3 |
from TTS.api import TTS
|
4 |
-
import os
|
5 |
-
import time
|
6 |
|
7 |
-
# Initialisation du modèle TTS
|
8 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
9 |
|
10 |
-
#
|
11 |
output_folder = "output_audio"
|
12 |
os.makedirs(output_folder, exist_ok=True)
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
.
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
.
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
.
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
.gr-input, .gr-dropdown {
|
61 |
-
border: 1px solid #e0e0e0;
|
62 |
-
border-radius: 8px;
|
63 |
-
padding: 10px;
|
64 |
-
}
|
65 |
-
.gr-checkbox {
|
66 |
-
margin-top: 10px;
|
67 |
-
}
|
68 |
-
.gr-form > div {
|
69 |
-
margin-bottom: 20px;
|
70 |
-
}
|
71 |
-
"""
|
72 |
-
|
73 |
-
title = "🎙️ Synthèse Vocale XTTS"
|
74 |
-
description = """
|
75 |
-
<h3 style='text-align: center; margin-bottom: 1em;'>Bienvenue sur notre outil de synthèse vocale XTTS !</h3>
|
76 |
-
<p style='text-align: center;'>Générez une voix naturelle à partir de votre texte en français. Créez un projet et ajoutez des sections pour votre script.</p>
|
77 |
-
"""
|
78 |
-
|
79 |
-
available_speakers = list(set([f.split('_')[0] for f in os.listdir("examples") if f.endswith(".wav")]))
|
80 |
-
|
81 |
-
with gr.Blocks(css=custom_css) as demo:
|
82 |
-
|
83 |
# Étape 1 : Création du Projet
|
84 |
-
gr.
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
# Étape 2 : Gestion des Sections
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
def add_section(sections):
|
98 |
-
|
99 |
-
sections.append(
|
100 |
return sections
|
101 |
-
|
|
|
|
|
102 |
def remove_section(sections):
|
103 |
if sections:
|
104 |
sections.pop()
|
105 |
return sections
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
# Dynamically render sections
|
134 |
-
with section_output_area:
|
135 |
-
for section in render_sections(sections):
|
136 |
-
section_output_area.append(section)
|
137 |
-
|
138 |
-
demo.launch(debug=True)
|
|
|
1 |
import subprocess
|
2 |
+
import os
|
3 |
import gradio as gr
|
4 |
from TTS.api import TTS
|
|
|
|
|
5 |
|
6 |
+
# Initialisation du modèle TTS avec GPU désactivé
|
7 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
8 |
|
9 |
+
# Répertoire de sortie pour tous les fichiers audio
|
10 |
output_folder = "output_audio"
|
11 |
os.makedirs(output_folder, exist_ok=True)
|
12 |
|
13 |
+
# Fonction pour générer un fichier audio à partir d'une section
|
14 |
+
def generate_section_audio(project_name, section_name, text, speaker):
|
15 |
+
try:
|
16 |
+
# Création du sous-dossier pour le projet
|
17 |
+
project_path = os.path.join(output_folder, project_name)
|
18 |
+
os.makedirs(project_path, exist_ok=True)
|
19 |
+
|
20 |
+
# Définir le chemin de sortie pour cette section
|
21 |
+
file_name = f"{section_name}.wav"
|
22 |
+
output_path = os.path.join(project_path, file_name)
|
23 |
+
|
24 |
+
# Vérifier la disponibilité des fichiers audio pour le speaker
|
25 |
+
speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
|
26 |
+
if not speaker_wav_paths:
|
27 |
+
raise ValueError(f"Aucun fichier audio trouvé pour le speaker : {speaker}")
|
28 |
+
|
29 |
+
# Génération de l'audio
|
30 |
+
tts.tts_to_file(
|
31 |
+
text=text,
|
32 |
+
file_path=output_path,
|
33 |
+
speaker_wav=speaker_wav_paths,
|
34 |
+
language="fr"
|
35 |
+
)
|
36 |
+
|
37 |
+
return output_path # Retourne le chemin de l'audio généré
|
38 |
+
except Exception as e:
|
39 |
+
return str(e) # Retourne l'erreur pour gestion dans l'interface
|
40 |
+
|
41 |
+
# Interface Gradio
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
# État global pour gérer les étapes et les sections
|
44 |
+
step = gr.State(value=1)
|
45 |
+
sections = gr.State(value=[]) # Liste dynamique des sections ajoutées
|
46 |
+
|
47 |
+
# Introduction et explication globale
|
48 |
+
gr.Markdown("""
|
49 |
+
# 🎙️ Synthèse Vocale Margaux
|
50 |
+
## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée
|
51 |
+
Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio.
|
52 |
+
**Étapes principales :**
|
53 |
+
1. 🛠️ **Créer un projet** : Définissez le nom du projet et choisissez la voix.
|
54 |
+
2. ✍️ **Ajouter des sections** : Divisez votre texte en parties claires, chacune avec un nom unique.
|
55 |
+
3. 🎧 **Générer les audios** : Chaque section est transformée en fichier audio individuel.
|
56 |
+
4. 📁 **Sauvegardez le projet** : Finalisez et récupérez les fichiers validés.
|
57 |
+
""")
|
58 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
# Étape 1 : Création du Projet
|
60 |
+
with gr.Row(visible=True) as step1:
|
61 |
+
gr.Markdown("### 🛠️ Étape 1 : Création du Projet")
|
62 |
+
gr.Markdown("**📂 Définissez les informations générales pour votre projet.**")
|
63 |
+
project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU")
|
64 |
+
speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux") # Liste de voix
|
65 |
+
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
66 |
+
project_message = gr.Markdown(value="", visible=False) # Composant pour afficher les messages
|
67 |
+
next_btn_1 = gr.Button("Suivant ➡️")
|
68 |
+
|
|
|
69 |
# Étape 2 : Gestion des Sections
|
70 |
+
with gr.Row(visible=False) as step2:
|
71 |
+
gr.Markdown("### ✍️ Étape 2 : Ajoutez vos Sections")
|
72 |
+
gr.Markdown("""
|
73 |
+
**📝 Divisez votre script en plusieurs sections pour une meilleure qualité.**
|
74 |
+
Chaque section doit avoir :
|
75 |
+
- Un **nom unique** 🏷️ qui servira à nommer le fichier audio.
|
76 |
+
- Un **texte clair et concis** ✏️.
|
77 |
+
""")
|
78 |
+
sections_list = gr.Column() # Conteneur dynamique pour afficher les sections ajoutées
|
79 |
+
add_section_btn = gr.Button("+ Ajouter une Section ➕")
|
80 |
+
remove_section_btn = gr.Button("- Supprimer la dernière Section ➖")
|
81 |
+
prev_btn_2 = gr.Button("⬅️ Précédent")
|
82 |
+
next_btn_2 = gr.Button("Suivant ➡️")
|
83 |
+
|
84 |
+
# Étape 3 : Génération des Audios et Gestion des Sections
|
85 |
+
with gr.Row(visible=False) as step3:
|
86 |
+
gr.Markdown("### 🎧 Étape 3 : Génération et Gestion des Audios")
|
87 |
+
gr.Markdown("""
|
88 |
+
**🎶 Générez un fichier audio pour chaque section.**
|
89 |
+
- 🔄 Régénérez l’audio d’une section indépendamment si nécessaire.
|
90 |
+
- ⚠️ En cas d’erreur, seuls les audios de sections valides seront disponibles.
|
91 |
+
""")
|
92 |
+
generate_btn = gr.Button("Générer les Audios ▶️")
|
93 |
+
results_output = gr.Column() # Conteneur pour afficher les lecteurs audio et les boutons de régénération
|
94 |
+
prev_btn_3 = gr.Button("⬅️ Précédent")
|
95 |
+
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
|
96 |
+
|
97 |
+
# Actions des Boutons
|
98 |
+
def create_project(project_name, speaker, agree):
|
99 |
+
if not agree:
|
100 |
+
return "❗ Veuillez accepter les conditions d'utilisation.", 1
|
101 |
+
if not project_name:
|
102 |
+
return "❗ Le nom du projet est obligatoire.", 1
|
103 |
+
os.makedirs(os.path.join(output_folder, project_name), exist_ok=True)
|
104 |
+
return f"✅ Projet '{project_name}' créé avec succès !", 2
|
105 |
+
|
106 |
+
next_btn_1.click(
|
107 |
+
create_project,
|
108 |
+
inputs=[project_name, speaker, agree],
|
109 |
+
outputs=[project_message, step]
|
110 |
+
)
|
111 |
+
|
112 |
def add_section(sections):
|
113 |
+
section = {"name": f"Section_{len(sections) + 1}", "text": ""}
|
114 |
+
sections.append(section)
|
115 |
return sections
|
116 |
+
|
117 |
+
add_section_btn.click(add_section, inputs=[sections], outputs=[sections_list])
|
118 |
+
|
119 |
def remove_section(sections):
|
120 |
if sections:
|
121 |
sections.pop()
|
122 |
return sections
|
123 |
+
|
124 |
+
remove_section_btn.click(remove_section, inputs=[sections], outputs=[sections_list])
|
125 |
+
|
126 |
+
def generate_audios(project_name, sections, speaker):
|
127 |
+
results = []
|
128 |
+
for section in sections:
|
129 |
+
name = section["name"]
|
130 |
+
text = section["text"]
|
131 |
+
audio_path = generate_section_audio(project_name, name, text, speaker)
|
132 |
+
results.append(audio_path)
|
133 |
+
return results
|
134 |
+
|
135 |
+
generate_btn.click(
|
136 |
+
generate_audios,
|
137 |
+
inputs=[project_name, sections, speaker],
|
138 |
+
outputs=[results_output]
|
139 |
+
)
|
140 |
+
|
141 |
+
next_btn_2.click(lambda: 3, inputs=None, outputs=step)
|
142 |
+
prev_btn_2.click(lambda: 1, inputs=None, outputs=step)
|
143 |
+
step.change(lambda x: {"visible": x == 2}, inputs=step, outputs=[step1, step2])
|
144 |
+
|
145 |
+
prev_btn_3.click(lambda: 2, inputs=None, outputs=step)
|
146 |
+
save_project_btn.click(lambda: "🎉 Projet sauvegardé avec succès !", outputs=[project_message])
|
147 |
+
|
148 |
+
# Lancement de l'interface
|
149 |
+
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|