import subprocess import os import gradio as gr from TTS.api import TTS class TTSInterface: def __init__(self): # Initialisation du modèle TTS self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False) self.output_folder = "output_audio" os.makedirs(self.output_folder, exist_ok=True) def create_interface(self): with gr.Blocks(theme=gr.themes.Soft()) as demo: # Variables d'état state = gr.State({ "project_name": "", "sections": [] }) # En-tête 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. **Étapes principales :** 1. 🛠️ **Créer un projet** : Définissez le nom du projet et choisissez la voix. 2. ✍️ **Ajouter des sections** : Divisez votre texte en parties claires. 3. 🎧 **Générer les audios** : Chaque section est transformée en fichier audio. 4. 📁 **Sauvegardez le projet** : Finalisez et récupérez les fichiers. """) # Tabs pour les étapes with gr.Tabs() as tabs: # Étape 1: Configuration du projet with gr.Tab("🛠️ Création du Projet"): with gr.Column(): gr.Markdown("### Configuration initiale") project_name = gr.Textbox( label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU", scale=1 ) speaker = gr.Dropdown( choices=["Margaux"], value="Margaux", label="Voix 🎙️", scale=1 ) terms = gr.Checkbox( label="✅ J'accepte les conditions d'utilisation" ) create_btn = gr.Button("Créer le Projet ➡️", variant="primary") # Étape 2: Gestion des sections with gr.Tab("✍️ Sections"): with gr.Column(): gr.Markdown("### Gestion des sections") # Container pour les sections dynamiques sections_container = gr.Column() with gr.Row(): add_section = gr.Button("+ Ajouter une Section ➕") remove_section = gr.Button("- Supprimer la dernière Section ➖") # Template pour une section def create_section_template(index): with gr.Group(): with gr.Box(): gr.Markdown(f"#### Section {index}") name = gr.Textbox( label="Nom de la section", value=f"Section_{index}" ) text = gr.TextArea( label="Texte à synthétiser", placeholder="Entrez votre texte ici..." ) return [name, text] # Étape 3: Génération des audios with gr.Tab("🎧 Génération"): with gr.Column(): gr.Markdown("### Génération des audios") generate_all = gr.Button("Générer tous les audios ▶️", variant="primary") audio_outputs = gr.Column() save_project = gr.Button("Sauvegarder le Projet ✅") # Fonctions de callback def update_project_info(name, terms): if not terms: return gr.Error("Veuillez accepter les conditions d'utilisation") if not name.strip(): return gr.Error("Le nom du projet est requis") return gr.Info(f"Projet '{name}' créé avec succès!") def add_new_section(state): current_sections = state["sections"] new_section = { "name": f"Section_{len(current_sections) + 1}", "text": "" } current_sections.append(new_section) return {"sections": current_sections} def remove_last_section(state): current_sections = state["sections"] if current_sections: current_sections.pop() return {"sections": current_sections} def generate_audio(state, section_data): try: # Logique de génération audio ici output_path = os.path.join( self.output_folder, state["project_name"], f"{section_data['name']}.wav" ) # Simulation de génération (à remplacer par votre logique TTS) self.tts.tts_to_file( text=section_data["text"], file_path=output_path, language="fr" ) return gr.Audio(output_path) except Exception as e: return gr.Error(str(e)) # Événements create_btn.click( update_project_info, inputs=[project_name, terms], outputs=[gr.Info] ) add_section.click( add_new_section, inputs=[state], outputs=[state] ) remove_section.click( remove_last_section, inputs=[state], outputs=[state] ) generate_all.click( lambda x: [generate_audio(x, section) for section in x["sections"]], inputs=[state], outputs=[audio_outputs] ) return demo if __name__ == "__main__": interface = TTSInterface() demo = interface.create_interface() demo.launch(debug=True)