Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,168 +1,137 @@
|
|
| 1 |
import subprocess
|
| 2 |
import os
|
| 3 |
-
import time
|
| 4 |
import gradio as gr
|
| 5 |
from TTS.api import TTS
|
| 6 |
|
| 7 |
-
# Initialisation du modèle TTS
|
| 8 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 9 |
|
|
|
|
| 10 |
output_folder = "output_audio"
|
| 11 |
os.makedirs(output_folder, exist_ok=True)
|
| 12 |
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
label="🎤 Voix",
|
| 99 |
-
choices=available_speakers,
|
| 100 |
-
value=available_speakers[0] if available_speakers else None
|
| 101 |
-
)
|
| 102 |
-
agree = gr.Checkbox(
|
| 103 |
-
label="✅ J'accepte les conditions d'utilisation",
|
| 104 |
-
value=False
|
| 105 |
-
)
|
| 106 |
-
create_project_btn = gr.Button("🔨 Créer le projet", variant="primary")
|
| 107 |
-
|
| 108 |
-
project_folder_output = gr.State()
|
| 109 |
-
|
| 110 |
-
def on_create_project(name, spkr, agr):
|
| 111 |
-
folder_path = create_project(name, spkr, agr)
|
| 112 |
-
return folder_path
|
| 113 |
-
|
| 114 |
-
create_project_btn.click(
|
| 115 |
-
on_create_project,
|
| 116 |
inputs=[project_name, speaker, agree],
|
| 117 |
-
outputs=[
|
| 118 |
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
remove_section_btn.click(remove_section)
|
| 148 |
-
|
| 149 |
-
generate_audio_btn = gr.Button("🎶 Générer l'audio")
|
| 150 |
-
|
| 151 |
-
audio_outputs_display = []
|
| 152 |
-
|
| 153 |
-
def generate_all_sections(sections_data, proj_folder):
|
| 154 |
-
audio_outputs = []
|
| 155 |
-
for text_box, name_box in sections_data:
|
| 156 |
-
text = text_box.value
|
| 157 |
-
name = name_box.value
|
| 158 |
-
audio_path = generate_audio_section(text, name.strip(), proj_folder.value, speaker.value)
|
| 159 |
-
audio_outputs.append(audio_path)
|
| 160 |
-
return audio_outputs
|
| 161 |
-
|
| 162 |
-
generate_audio_btn.click(
|
| 163 |
-
generate_all_sections,
|
| 164 |
-
inputs=[section_textboxes, project_folder_output],
|
| 165 |
-
outputs=audio_outputs_display
|
| 166 |
)
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Fonction pour gérer la visibilité des étapes
|
| 42 |
+
def update_steps(current_step, target_step):
|
| 43 |
+
return {"visible": target_step == 1}, {"visible": target_step == 2}, {"visible": target_step == 3}
|
| 44 |
+
|
| 45 |
+
# Interface Gradio
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
# État global pour gérer les sections
|
| 48 |
+
sections = gr.State(value=[])
|
| 49 |
+
|
| 50 |
+
# Introduction générale
|
| 51 |
+
gr.Markdown("""
|
| 52 |
+
# 🎙️ Synthèse Vocale Margaux
|
| 53 |
+
## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée
|
| 54 |
+
Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio.
|
| 55 |
+
**Étapes principales :**
|
| 56 |
+
1. 🛠️ **Créer un projet** : Définissez le nom du projet et choisissez la voix.
|
| 57 |
+
2. ✍️ **Ajouter des sections** : Divisez votre texte en parties claires, chacune avec un nom unique.
|
| 58 |
+
3. 🎧 **Générer les audios** : Chaque section est transformée en fichier audio individuel.
|
| 59 |
+
4. 📁 **Sauvegardez le projet** : Finalisez et récupérez les fichiers validés.
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
# Étape 1 : Création du Projet
|
| 63 |
+
with gr.Row(visible=True) as step1:
|
| 64 |
+
gr.Markdown("### 🛠️ Étape 1 : Création du Projet")
|
| 65 |
+
project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU")
|
| 66 |
+
speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux") # Liste de voix
|
| 67 |
+
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
| 68 |
+
project_message = gr.Markdown(value="", visible=False) # Composant pour afficher les messages
|
| 69 |
+
next_btn_1 = gr.Button("Suivant ➡️")
|
| 70 |
+
|
| 71 |
+
# Étape 2 : Gestion des Sections
|
| 72 |
+
with gr.Row(visible=False) as step2:
|
| 73 |
+
gr.Markdown("### ✍️ Étape 2 : Ajoutez vos Sections")
|
| 74 |
+
sections_list = gr.Column() # Conteneur pour afficher les sections ajoutées
|
| 75 |
+
add_section_btn = gr.Button("+ Ajouter une Section ➕")
|
| 76 |
+
remove_section_btn = gr.Button("- Supprimer la dernière Section ➖")
|
| 77 |
+
prev_btn_2 = gr.Button("⬅️ Précédent")
|
| 78 |
+
next_btn_2 = gr.Button("Suivant ➡️")
|
| 79 |
+
|
| 80 |
+
# Étape 3 : Génération des Audios
|
| 81 |
+
with gr.Row(visible=False) as step3:
|
| 82 |
+
gr.Markdown("### 🎧 Étape 3 : Génération des Audios")
|
| 83 |
+
results_output = gr.Column() # Conteneur pour afficher les résultats
|
| 84 |
+
generate_btn = gr.Button("Générer les Audios ▶️")
|
| 85 |
+
prev_btn_3 = gr.Button("⬅️ Précédent")
|
| 86 |
+
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
|
| 87 |
+
|
| 88 |
+
# Actions des Boutons
|
| 89 |
+
def create_project(project_name, speaker, agree):
|
| 90 |
+
if not agree:
|
| 91 |
+
return "❗ Veuillez accepter les conditions d'utilisation.", False, False
|
| 92 |
+
if not project_name:
|
| 93 |
+
return "❗ Le nom du projet est obligatoire.", False, False
|
| 94 |
+
os.makedirs(os.path.join(output_folder, project_name), exist_ok=True)
|
| 95 |
+
return f"✅ Projet '{project_name}' créé avec succès !", True, False
|
| 96 |
+
|
| 97 |
+
next_btn_1.click(
|
| 98 |
+
create_project,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
inputs=[project_name, speaker, agree],
|
| 100 |
+
outputs=[project_message, step1, step2],
|
| 101 |
)
|
| 102 |
|
| 103 |
+
def add_section(sections):
|
| 104 |
+
section = {"name": f"Section_{len(sections) + 1}", "text": ""}
|
| 105 |
+
sections.append(section)
|
| 106 |
+
return sections
|
| 107 |
+
|
| 108 |
+
add_section_btn.click(add_section, inputs=[sections], outputs=[sections_list])
|
| 109 |
+
|
| 110 |
+
def remove_section(sections):
|
| 111 |
+
if sections:
|
| 112 |
+
sections.pop()
|
| 113 |
+
return sections
|
| 114 |
+
|
| 115 |
+
remove_section_btn.click(remove_section, inputs=[sections], outputs=[sections_list])
|
| 116 |
+
|
| 117 |
+
def generate_audios(project_name, sections, speaker):
|
| 118 |
+
results = []
|
| 119 |
+
for section in sections:
|
| 120 |
+
name = section["name"]
|
| 121 |
+
text = section["text"]
|
| 122 |
+
audio_path = generate_section_audio(project_name, name, text, speaker)
|
| 123 |
+
results.append(audio_path)
|
| 124 |
+
return results
|
| 125 |
+
|
| 126 |
+
generate_btn.click(
|
| 127 |
+
generate_audios,
|
| 128 |
+
inputs=[project_name, sections, speaker],
|
| 129 |
+
outputs=[results_output],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
+
prev_btn_2.click(lambda: (True, False), inputs=None, outputs=[step1, step2])
|
| 133 |
+
next_btn_2.click(lambda: (False, True), inputs=None, outputs=[step2, step3])
|
| 134 |
+
prev_btn_3.click(lambda: (True, False), inputs=None, outputs=[step2, step3])
|
| 135 |
+
|
| 136 |
+
# Lancement de l'interface
|
| 137 |
+
demo.launch(debug=True)
|