Spaces:
Sleeping
Sleeping
| import subprocess | |
| import os | |
| import gradio as gr | |
| from TTS.api import TTS | |
| # Initialisation du modèle TTS avec GPU désactivé | |
| tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False) | |
| # Répertoire de sortie pour tous les fichiers audio | |
| output_folder = "output_audio" | |
| os.makedirs(output_folder, exist_ok=True) | |
| # Fonction pour générer un fichier audio à partir d'une section | |
| def generate_section_audio(project_name, section_name, text, speaker): | |
| try: | |
| project_path = os.path.join(output_folder, project_name) | |
| os.makedirs(project_path, exist_ok=True) | |
| file_name = f"{section_name}.wav" | |
| output_path = os.path.join(project_path, file_name) | |
| speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")] | |
| if not speaker_wav_paths: | |
| raise ValueError(f"Aucun fichier audio trouvé pour le speaker : {speaker}") | |
| tts.tts_to_file( | |
| text=text, | |
| file_path=output_path, | |
| speaker_wav=speaker_wav_paths, | |
| language="fr" | |
| ) | |
| return output_path | |
| except Exception as e: | |
| return str(e) | |
| # Fonction pour générer les audios de toutes les sections | |
| def generate_all_audios(project_name, sections, speaker): | |
| results = [] | |
| for section in sections: | |
| name = section["name"] | |
| text = section["text"] | |
| audio_path = generate_section_audio(project_name, name, text, speaker) | |
| results.append(audio_path) | |
| return results | |
| # Interface Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown(""" | |
| # 🎙️ Synthèse Vocale Margaux | |
| ## 👋 Bienvenue sur Margaux - Votre outil de synthèse vocale avancée | |
| Margaux vous permet de générer des voix off naturelles à partir de textes, structurées par sections pour une meilleure qualité audio. | |
| """) | |
| # Étape 1 : Création du Projet | |
| with gr.Row(): | |
| project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU") | |
| speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux") | |
| agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation") | |
| next_step_btn = gr.Button("Suivant ➡️") | |
| # Étape 2 : Ajout des Sections | |
| sections = gr.State(value=[{"name": "Section_1", "text": ""}]) | |
| section_inputs = gr.Column() # Conteneur pour afficher les sections ajoutées | |
| def update_section_list(sections): | |
| return [gr.Row([ | |
| gr.Textbox(label=f"Nom : {s['name']}", value=s["name"], interactive=False), | |
| gr.Textbox(label="Texte", value=s["text"], lines=2) | |
| ]) for s in sections] | |
| add_section_btn = gr.Button("+ Ajouter une Section ➕") | |
| remove_section_btn = gr.Button("- Supprimer la dernière Section ➖") | |
| def add_section(sections): | |
| new_section = {"name": f"Section_{len(sections) + 1}", "text": ""} | |
| sections.append(new_section) | |
| return sections, update_section_list(sections) | |
| def remove_section(sections): | |
| if len(sections) > 1: # Ne pas supprimer si c'est la seule section | |
| sections.pop() | |
| return sections, update_section_list(sections) | |
| add_section_btn.click(add_section, inputs=sections, outputs=[sections, section_inputs]) | |
| remove_section_btn.click(remove_section, inputs=sections, outputs=[sections, section_inputs]) | |
| # Étape 3 : Génération des Audios | |
| generate_btn = gr.Button("Générer les Audios ▶️") | |
| results_output = gr.Column() # Conteneur pour afficher les audios générés | |
| def generate_audios_and_display(project_name, sections, speaker): | |
| results = generate_all_audios(project_name, sections, speaker) | |
| return [gr.Audio(label=f"Audio : {s['name']}", value=path) for s, path in zip(sections, results)] | |
| generate_btn.click(generate_audios_and_display, | |
| inputs=[project_name, sections, speaker], | |
| outputs=[results_output]) | |
| # Sauvegarde du Projet | |
| save_project_btn = gr.Button("Sauvegarder le Projet ✅") | |
| def save_project(project_name): | |
| project_path = os.path.join(output_folder, project_name) | |
| if not os.path.exists(project_path): | |
| return "⚠️ Aucun audio généré à sauvegarder." | |
| return f"🎉 Projet '{project_name}' sauvegardé dans le dossier '{project_path}'." | |
| save_project_btn.click(save_project, inputs=[project_name], outputs=[results_output]) | |
| # Lancement de l'interface | |
| demo.launch(debug=True) | |