Spaces:
Running
Running
File size: 4,840 Bytes
a5c86e8 9b2107c fb0fda6 11501d1 060eb01 fb0fda6 11501d1 fb0fda6 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 729b788 11501d1 9b2107c 11501d1 0df1e09 |
1 2 3 4 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 |
import subprocess
import gradio as gr
from TTS.api import TTS
import os
import time
import torch
# 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:
# Création du sous-dossier pour le projet
project_path = os.path.join(output_folder, project_name)
os.makedirs(project_path, exist_ok=True)
# Définir le chemin de sortie pour cette section
file_name = f"{section_name}.wav"
output_path = os.path.join(project_path, file_name)
# Vérifier la disponibilité des fichiers audio pour le speaker
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}")
# Génération de l'audio
tts.tts_to_file(
text=text,
file_path=output_path,
speaker_wav=speaker_wav_paths,
language="fr"
)
return output_path # Retourne le chemin de l'audio généré
except Exception as e:
return str(e) # Retourne l'erreur pour gestion dans l'interface
# Fonction pour gérer l'état d'avancement de l'interface
def update_step(step):
return {"visible": step == 1}, {"visible": step == 2}, {"visible": step == 3}
# Interface Gradio
with gr.Blocks() as demo:
step = gr.State(value=1) # État pour suivre l'étape active
sections = gr.State(value=[]) # Liste dynamique des sections
# Étape 1 : Création du Projet
with gr.Row(visible=True) as step1:
gr.Markdown("### 🛠️ Étape 1 : Création du Projet")
project_name = gr.Textbox(label="Nom du Projet", placeholder="Exemple : Capsule_Video_PLU")
speaker = gr.Dropdown(label="Voix 🎙️", choices=["Margaux"], value="Margaux") # Liste de voix
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
next_btn_1 = gr.Button("Suivant ➡️")
# Étape 2 : Gestion des Sections
with gr.Row(visible=False) as step2:
gr.Markdown("### ✍️ Étape 2 : Ajoutez vos Sections")
sections_list = gr.Column() # Conteneur pour les sections
add_section_btn = gr.Button("+ Ajouter une Section ➕")
remove_section_btn = gr.Button("- Supprimer la dernière Section ➖")
prev_btn_2 = gr.Button("⬅️ Précédent")
next_btn_2 = gr.Button("Suivant ➡️")
# Étape 3 : Génération des Audios et Sauvegarde
with gr.Row(visible=False) as step3:
gr.Markdown("### 🎧 Étape 3 : Génération et Sauvegarde")
generate_btn = gr.Button("Générer les Audios ▶️")
results_output = gr.Column() # Conteneur pour les audios générés
prev_btn_3 = gr.Button("⬅️ Précédent")
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
# Actions des Boutons
def create_project(project_name, speaker, agree):
if not agree:
return gr.Error("❗ Veuillez accepter les conditions d'utilisation.")
if not project_name:
return gr.Error("❗ Le nom du projet est obligatoire.")
os.makedirs(os.path.join(output_folder, project_name), exist_ok=True)
return f"✅ Projet '{project_name}' créé avec succès !", 2
next_btn_1.click(create_project, inputs=[project_name, speaker, agree], outputs=[None, step])
step.change(update_step, inputs=step, outputs=[step1, step2, step3])
def add_section(sections):
section = {"name": f"Section_{len(sections) + 1}", "text": ""}
sections.append(section)
return sections
add_section_btn.click(add_section, inputs=[sections], outputs=[sections_list])
def remove_section(sections):
if sections:
sections.pop()
return sections
remove_section_btn.click(remove_section, inputs=[sections], outputs=[sections_list])
def generate_audios(project_name, sections, speaker):
results = process_project(project_name, sections, speaker)
return results
next_btn_2.click(lambda: 3, inputs=None, outputs=step)
prev_btn_2.click(lambda: 1, inputs=None, outputs=step)
step.change(update_step, inputs=step, outputs=[step1, step2, step3])
prev_btn_3.click(lambda: 2, inputs=None, outputs=step)
generate_btn.click(generate_audios, inputs=[project_name, sections, speaker], outputs=[results_output])
# Lancement de l'interface
demo.launch(debug=True) |