Spaces:
Sleeping
Sleeping
File size: 5,020 Bytes
a5c86e8 163771f 9883968 6bca271 549ef14 f31c7bc edf2a53 549ef14 f31c7bc 549ef14 163771f 549ef14 623884e 549ef14 623884e 549ef14 623884e 549ef14 623884e 45e716a 794ce29 549ef14 45e716a 549ef14 ad0fea3 549ef14 ad0fea3 623884e 45e716a 549ef14 794ce29 623884e 794ce29 549ef14 794ce29 623884e 794ce29 623884e 549ef14 794ce29 549ef14 794ce29 |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 |
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:
# 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é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
Créez des voix off naturelles à partir de textes. Divisez votre contenu en plusieurs sections pour une meilleure qualité audio.
""")
with gr.Box():
gr.Markdown("### 🛠️ Configuration 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")
with gr.Box():
gr.Markdown("### ✍️ Gestion des Sections")
sections = gr.State(value=[]) # État pour stocker les sections
def update_section_list(sections):
return [gr.Textbox(label=f"Nom : {s['name']}", value=s["text"], lines=2) for s in sections]
sections_list = gr.Column() # Conteneur pour afficher les sections ajoutées
add_section_btn = gr.Button("+ Ajouter une Section ➕")
def add_section(sections):
section_number = len(sections) + 1
section = {"name": f"Section_{section_number}", "text": ""}
sections.append(section)
return sections, update_section_list(sections)
add_section_btn.click(add_section, inputs=[sections], outputs=[sections, sections_list])
remove_section_btn = gr.Button("- Supprimer la dernière Section ➖")
def remove_section(sections):
if sections:
sections.pop()
return sections, update_section_list(sections)
remove_section_btn.click(remove_section, inputs=[sections], outputs=[sections, sections_list])
with gr.Box():
gr.Markdown("### 🎧 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]
)
with gr.Box():
gr.Markdown("### 📁 Sauvegarde du Projet")
save_project_btn = gr.Button("Sauvegarder le Projet ✅")
save_message = gr.Markdown("")
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=[save_message])
# Lancement de l'interface
demo.launch(debug=True)
|