Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,134 +3,146 @@ import os
|
|
3 |
import time
|
4 |
import gradio as gr
|
5 |
from TTS.api import TTS
|
6 |
-
import json
|
7 |
|
8 |
-
# Initialisation du modèle TTS
|
9 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
os.makedirs(base_output_folder, exist_ok=True)
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
if not agree:
|
18 |
-
raise gr.Error("❗ Veuillez accepter les conditions d'utilisation.")
|
19 |
-
|
20 |
-
project_folder = os.path.join(base_output_folder, project_name)
|
21 |
-
os.makedirs(project_folder, exist_ok=True)
|
22 |
-
|
23 |
-
project_data = {
|
24 |
-
"name": project_name,
|
25 |
-
"speaker": speaker,
|
26 |
-
"sections": []
|
27 |
-
}
|
28 |
-
|
29 |
-
# Vérifier si le projet existe déjà
|
30 |
-
project_file = os.path.join(project_folder, "project_data.json")
|
31 |
-
if os.path.exists(project_file):
|
32 |
-
with open(project_file, "r") as f:
|
33 |
-
project_data = json.load(f)
|
34 |
-
else:
|
35 |
-
with open(project_file, "w") as f:
|
36 |
-
json.dump(project_data, f)
|
37 |
-
|
38 |
-
return project_data
|
39 |
-
|
40 |
-
# Fonction pour ajouter ou mettre à jour une section
|
41 |
-
def update_section(project_data, section_name, section_text):
|
42 |
-
for section in project_data["sections"]:
|
43 |
-
if section["name"] == section_name:
|
44 |
-
section["text"] = section_text
|
45 |
-
return project_data
|
46 |
-
|
47 |
-
project_data["sections"].append({
|
48 |
-
"name": section_name,
|
49 |
-
"text": section_text,
|
50 |
-
"audio_path": None
|
51 |
-
})
|
52 |
-
return project_data
|
53 |
-
|
54 |
-
# Fonction pour générer l'audio d'une section
|
55 |
-
def generate_section_audio(section_name, text, speaker):
|
56 |
-
output_path = os.path.join(base_output_folder, f"{section_name}.wav")
|
57 |
-
|
58 |
-
tts.tts_to_file(
|
59 |
-
text=text,
|
60 |
-
file_path=output_path,
|
61 |
-
speaker_wav=[os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")],
|
62 |
-
language="fr"
|
63 |
-
)
|
64 |
-
|
65 |
-
return output_path
|
66 |
|
67 |
# Interface Gradio
|
68 |
with gr.Blocks() as demo:
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
# Étape
|
79 |
-
with gr.
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
return
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
add_section_btn.click(add_section, inputs=[
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
return generate_audio(sec, text_for_section)
|
127 |
-
|
128 |
-
audio_output_btn.click(generate_for_section)
|
129 |
-
audio_outputs_display.append(audio_output_display)
|
130 |
-
|
131 |
-
# Ajouter les boutons et les affichages audio à l'interface
|
132 |
-
demo.add(audio_output_btn)
|
133 |
-
demo.add(audio_output_display)
|
134 |
|
135 |
# Lancement de l'interface
|
136 |
-
demo.launch()
|
|
|
3 |
import time
|
4 |
import gradio as gr
|
5 |
from TTS.api import TTS
|
|
|
6 |
|
|
|
|
|
7 |
|
8 |
+
# Initialisation du modèle TTS avec GPU désactivé
|
9 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
|
|
10 |
|
11 |
+
# Répertoire de sortie pour tous les fichiers audio
|
12 |
+
output_folder = "output_audio"
|
13 |
+
os.makedirs(output_folder, exist_ok=True)
|
14 |
+
|
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 |
+
# Génération de l'audio
|
32 |
+
tts.tts_to_file(
|
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) # Retourne l'erreur pour gestion dans l'interface
|
42 |
+
|
43 |
+
# Fonction pour traiter un projet complet
|
44 |
+
def process_project(project_name, sections, speaker):
|
45 |
+
results = []
|
46 |
+
for section in sections:
|
47 |
+
section_name, text = section["name"], section["text"]
|
48 |
+
result = generate_section_audio(project_name, section_name, text, speaker)
|
49 |
+
results.append({"section": section_name, "result": result})
|
50 |
+
return results
|
51 |
+
|
52 |
+
# Fonction de validation des conditions d'utilisation
|
53 |
+
def validate_conditions(agree):
|
54 |
if not agree:
|
55 |
+
raise gr.Error("❗ Veuillez accepter les conditions d'utilisation pour continuer.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Interface Gradio
|
58 |
with gr.Blocks() as demo:
|
59 |
+
# Titre principal
|
60 |
+
gr.Markdown("# 🎙️ Synthèse Vocale Margaux")
|
61 |
+
|
62 |
+
# Introduction et explication globale
|
63 |
+
gr.Markdown("""
|
64 |
+
## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée
|
65 |
+
Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio.
|
66 |
+
**Étapes principales :**
|
67 |
+
1. 🛠️ **Créer un projet** : Définissez le nom du projet et choisissez la voix.
|
68 |
+
2. ✍️ **Ajouter des sections** : Divisez votre texte en parties claires, chacune avec un nom unique.
|
69 |
+
3. 🎧 **Générer les audios** : Chaque section est transformée en fichier audio individuel.
|
70 |
+
4. 🔄 **Écoutez et ajustez** : Régénérez les audios si nécessaire, indépendamment des autres.
|
71 |
+
5. 📁 **Sauvegardez le projet** : Finalisez et récupérez les fichiers validés.
|
72 |
+
""")
|
73 |
+
|
74 |
+
# Étape 1 : Création du Projet
|
75 |
+
with gr.Box():
|
76 |
+
gr.Markdown("### 🛠️ Étape 1 : Création du Projet")
|
77 |
+
gr.Markdown("**📂 Définissez les informations générales pour votre projet.**")
|
78 |
+
gr.Markdown("Le nom du projet servira à organiser vos fichiers dans un dossier dédié.")
|
79 |
+
project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU")
|
80 |
+
speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux") # Liste de voix
|
81 |
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
82 |
+
create_project_btn = gr.Button("Créer le Projet 🚀")
|
83 |
+
|
84 |
+
# Étape 2 : Gestion des Sections
|
85 |
+
with gr.Box():
|
86 |
+
gr.Markdown("### ✍️ Étape 2 : Ajoutez vos Sections")
|
87 |
+
gr.Markdown("""
|
88 |
+
**📝 Divisez votre script en plusieurs sections pour une meilleure qualité.**
|
89 |
+
Chaque section doit avoir :
|
90 |
+
- Un **nom unique** 🏷️ qui servira à nommer le fichier audio.
|
91 |
+
- Un **texte clair et concis** ✏️.
|
92 |
+
""")
|
93 |
+
sections = gr.State([]) # Liste des sections dynamiques
|
94 |
+
sections_list = gr.Column() # Conteneur pour les sections ajoutées dynamiquement
|
95 |
+
add_section_btn = gr.Button("+ Ajouter une Section ➕")
|
96 |
+
remove_section_btn = gr.Button("- Supprimer la dernière Section ➖")
|
97 |
|
98 |
+
# Étape 3 : Validation des Sections et Génération des Audios
|
99 |
+
with gr.Box():
|
100 |
+
gr.Markdown("### 🎧 Étape 3 : Génération des Audios")
|
101 |
+
gr.Markdown("""
|
102 |
+
**🎶 Générez un fichier audio pour chaque section.**
|
103 |
+
- 🔄 Régénérez l’audio d’une section indépendamment si nécessaire.
|
104 |
+
- ⚠️ En cas d’erreur, seuls les audios de sections valides seront disponibles.
|
105 |
+
""")
|
106 |
+
generate_btn = gr.Button("Générer les Audios ▶️")
|
107 |
+
results_output = gr.Column() # Conteneur pour les audios générés
|
108 |
+
|
109 |
+
# Étape 4 : Sauvegarde Finale
|
110 |
+
with gr.Box():
|
111 |
+
gr.Markdown("### 📁 Étape 4 : Sauvegarde Finale")
|
112 |
+
gr.Markdown("""
|
113 |
+
**💾 Une fois satisfait des résultats :**
|
114 |
+
- Cliquez sur **Sauvegarder** pour conserver uniquement les fichiers validés.
|
115 |
+
- Les fichiers seront organisés dans un dossier au nom de votre projet.
|
116 |
+
""")
|
117 |
+
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
|
118 |
+
|
119 |
+
# Actions des Boutons
|
120 |
+
def create_project(project_name, speaker, agree):
|
121 |
+
validate_conditions(agree)
|
122 |
+
os.makedirs(os.path.join(output_folder, project_name), exist_ok=True)
|
123 |
+
return f"✅ Projet '{project_name}' créé avec succès !"
|
124 |
+
|
125 |
+
create_project_btn.click(create_project, inputs=[project_name, speaker, agree], outputs=[])
|
126 |
+
|
127 |
+
def add_section(sections):
|
128 |
+
section = {"name": f"Section_{len(sections) + 1}", "text": ""}
|
129 |
+
sections.append(section)
|
130 |
+
return sections
|
131 |
+
|
132 |
+
add_section_btn.click(add_section, inputs=[sections], outputs=[sections_list])
|
133 |
+
|
134 |
+
def remove_section(sections):
|
135 |
+
if sections:
|
136 |
+
sections.pop()
|
137 |
+
return sections
|
138 |
+
|
139 |
+
remove_section_btn.click(remove_section, inputs=[sections], outputs=[sections_list])
|
140 |
+
|
141 |
+
def generate_audios(project_name, sections, speaker):
|
142 |
+
results = process_project(project_name, sections, speaker)
|
143 |
+
return results
|
144 |
+
|
145 |
+
generate_btn.click(generate_audios, inputs=[project_name, sections, speaker], outputs=[results_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
# Lancement de l'interface
|
148 |
+
demo.launch(debug=True)
|