Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,148 +3,113 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
from TTS.api import TTS
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 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 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
return output_path, "✅ Audio généré avec succès"
|
| 117 |
-
except Exception as e:
|
| 118 |
-
return None, f"⚠️ Erreur: {str(e)}"
|
| 119 |
-
|
| 120 |
-
# Événements
|
| 121 |
-
create_btn.click(
|
| 122 |
-
fn=update_project_info,
|
| 123 |
-
inputs=[project_name, terms],
|
| 124 |
-
outputs=[status_message]
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
add_section.click(
|
| 128 |
-
fn=add_new_section,
|
| 129 |
-
inputs=[section_template],
|
| 130 |
-
outputs=[section_template]
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
remove_section.click(
|
| 134 |
-
fn=remove_last_section,
|
| 135 |
-
inputs=[section_template],
|
| 136 |
-
outputs=[section_template]
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
generate_all.click(
|
| 140 |
-
fn=generate_audio,
|
| 141 |
-
inputs=[project_name, speaker],
|
| 142 |
-
outputs=[audio_output, status]
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
return demo
|
| 146 |
-
|
| 147 |
-
if __name__ == "__main__":
|
| 148 |
-
interface = TTSInterface()
|
| 149 |
-
demo = interface.create_interface()
|
| 150 |
-
demo.launch(debug=True)
|
|
|
|
| 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 |
+
project_path = os.path.join(output_folder, project_name)
|
| 17 |
+
os.makedirs(project_path, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
file_name = f"{section_name}.wav"
|
| 20 |
+
output_path = os.path.join(project_path, file_name)
|
| 21 |
+
|
| 22 |
+
speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
|
| 23 |
+
if not speaker_wav_paths:
|
| 24 |
+
raise ValueError(f"Aucun fichier audio trouvé pour le speaker : {speaker}")
|
| 25 |
+
|
| 26 |
+
tts.tts_to_file(
|
| 27 |
+
text=text,
|
| 28 |
+
file_path=output_path,
|
| 29 |
+
speaker_wav=speaker_wav_paths,
|
| 30 |
+
language="fr"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return output_path
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return str(e)
|
| 36 |
+
|
| 37 |
+
# Fonction pour générer les audios de toutes les sections
|
| 38 |
+
def generate_all_audios(project_name, sections, speaker):
|
| 39 |
+
results = []
|
| 40 |
+
for section in sections:
|
| 41 |
+
name = section["name"]
|
| 42 |
+
text = section["text"]
|
| 43 |
+
audio_path = generate_section_audio(project_name, name, text, speaker)
|
| 44 |
+
results.append(audio_path)
|
| 45 |
+
return results
|
| 46 |
+
|
| 47 |
+
# Interface Gradio
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("""
|
| 50 |
+
# 🎙️ Synthèse Vocale Margaux
|
| 51 |
+
## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée
|
| 52 |
+
Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio.
|
| 53 |
+
""")
|
| 54 |
+
|
| 55 |
+
# Étape 1 : Création du Projet
|
| 56 |
+
with gr.Box():
|
| 57 |
+
gr.Markdown("### 🛠️ Étape 1 : Création du Projet")
|
| 58 |
+
|
| 59 |
+
project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU")
|
| 60 |
+
speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux")
|
| 61 |
+
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
| 62 |
+
|
| 63 |
+
next_step_btn = gr.Button("Suivant ➡️")
|
| 64 |
+
|
| 65 |
+
# Étape 2 : Ajout des Sections
|
| 66 |
+
with gr.Box():
|
| 67 |
+
gr.Markdown("### ✍️ Étape 2 : Ajoutez vos Sections")
|
| 68 |
+
|
| 69 |
+
sections = gr.State(value=[{"name": "Section_1", "text": ""}])
|
| 70 |
+
|
| 71 |
+
section_inputs = gr.Column() # Conteneur pour afficher les sections ajoutées
|
| 72 |
+
|
| 73 |
+
def update_section_list(sections):
|
| 74 |
+
return [gr.Row([
|
| 75 |
+
gr.Textbox(label=f"Nom : {s['name']}", value=s["name"], interactive=False),
|
| 76 |
+
gr.Textbox(label="Texte", value=s["text"], lines=2)
|
| 77 |
+
]) for s in sections]
|
| 78 |
+
|
| 79 |
+
add_section_btn = gr.Button("+ Ajouter une Section ➕")
|
| 80 |
+
remove_section_btn = gr.Button("- Supprimer la dernière Section ➖")
|
| 81 |
+
|
| 82 |
+
add_section_btn.click(lambda sections: (sections + [{"name": f"Section_{len(sections) + 1}", "text": ""}], update_section_list(sections)), inputs=sections, outputs=[sections, section_inputs])
|
| 83 |
+
|
| 84 |
+
remove_section_btn.click(lambda sections: (sections[:-1], update_section_list(sections[:-1])) if len(sections) > 1 else (sections, update_section_list(sections)), inputs=sections, outputs=[sections, section_inputs])
|
| 85 |
+
|
| 86 |
+
# Étape 3 : Génération des Audios
|
| 87 |
+
with gr.Box():
|
| 88 |
+
gr.Markdown("### 🎧 Étape 3 : Génération des Audios")
|
| 89 |
+
|
| 90 |
+
generate_btn = gr.Button("Générer les Audios ▶️")
|
| 91 |
+
|
| 92 |
+
results_output = gr.Column() # Conteneur pour afficher les audios générés
|
| 93 |
+
|
| 94 |
+
def generate_audios_and_display(project_name, sections, speaker):
|
| 95 |
+
results = generate_all_audios(project_name, sections, speaker)
|
| 96 |
+
return [gr.Audio(label=f"Audio : {s['name']}", value=path) for s, path in zip(sections, results)]
|
| 97 |
+
|
| 98 |
+
generate_btn.click(generate_audios_and_display,
|
| 99 |
+
inputs=[project_name, sections, speaker],
|
| 100 |
+
outputs=[results_output])
|
| 101 |
+
|
| 102 |
+
# Sauvegarde du Projet
|
| 103 |
+
with gr.Box():
|
| 104 |
+
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
|
| 105 |
+
|
| 106 |
+
def save_project(project_name):
|
| 107 |
+
project_path = os.path.join(output_folder, project_name)
|
| 108 |
+
if not os.path.exists(project_path):
|
| 109 |
+
return "⚠️ Aucun audio généré à sauvegarder."
|
| 110 |
+
return f"🎉 Projet '{project_name}' sauvegardé dans le dossier '{project_path}'."
|
| 111 |
+
|
| 112 |
+
save_project_btn.click(save_project, inputs=[project_name], outputs=[results_output])
|
| 113 |
+
|
| 114 |
+
# Lancement de l'interface
|
| 115 |
+
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|