Spaces:
Sleeping
Sleeping
File size: 5,076 Bytes
a5c86e8 fb0fda6 9883968 2eba633 fb0fda6 edf2a53 060eb01 fb0fda6 2eba633 edf2a53 2eba633 edf2a53 2eba633 edf2a53 2eba633 edf2a53 2eba633 9883968 2eba633 9883968 2eba633 9883968 2eba633 9883968 2eba633 9883968 2eba633 edf2a53 2eba633 edf2a53 2eba633 edf2a53 2eba633 edf2a53 2eba633 9883968 2eba633 |
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 129 130 131 132 133 134 135 136 137 |
import subprocess
import os
import time
import gradio as gr
from TTS.api import TTS
import json
# Initialisation du modèle TTS
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
# Dossier de base pour les projets
base_output_folder = "projects"
os.makedirs(base_output_folder, exist_ok=True)
# Fonction pour créer ou charger un projet
def create_or_load_project(project_name, speaker, agree):
if not agree:
raise gr.Error("❗ Veuillez accepter les conditions d'utilisation.")
project_folder = os.path.join(base_output_folder, project_name)
os.makedirs(project_folder, exist_ok=True)
project_data = {
"name": project_name,
"speaker": speaker,
"sections": []
}
# Vérifier si le projet existe déjà
project_file = os.path.join(project_folder, "project_data.json")
if os.path.exists(project_file):
with open(project_file, "r") as f:
project_data = json.load(f)
else:
with open(project_file, "w") as f:
json.dump(project_data, f)
return project_data
# Fonction pour ajouter ou mettre à jour une section
def update_section(project_data, section_name, section_text):
for section in project_data["sections"]:
if section["name"] == section_name:
section["text"] = section_text
return project_data
project_data["sections"].append({
"name": section_name,
"text": section_text,
"audio_path": None
})
return project_data
# Fonction pour générer l'audio d'une section
def generate_section_audio(section_name, text, speaker):
output_path = os.path.join(base_output_folder, f"{section_name}.wav")
tts.tts_to_file(
text=text,
file_path=output_path,
speaker_wav=[os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")],
language="fr"
)
return output_path
# Interface Gradio
with gr.Blocks() as demo:
gr.Markdown("# 🎙️ Projet Margaux TTS")
# Étape 1: Création ou chargement du projet
with gr.Tab("🔧 Création du Projet"):
project_name = gr.Textbox(label="📝 Nom du projet")
speaker = gr.Dropdown(label="🔊 Voix", choices=["Margaux"], value="Margaux")
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
create_btn = gr.Button("🚀 Créer/Charger le Projet")
# Étape 2: Gestion des sections
with gr.Tab("📋 Gestion des Sections"):
project_info = gr.JSON(label="📁 Informations du projet", visible=False)
sections_list = gr.List(label="📜 Sections du projet")
# Entrées pour ajouter une nouvelle section
section_name = gr.Textbox(label="📝 Nom de la section")
section_text = gr.Textbox(label="✍️ Texte de la section", lines=5)
add_section_btn = gr.Button("➕ Ajouter/Mettre à jour la section")
# Section pour générer l'audio de chaque section individuellement
audio_outputs_display = []
# Fonctions de callback
def load_project(project_name, speaker, agree):
project_data = create_or_load_project(project_name, speaker, agree)
sections = [section["name"] for section in project_data["sections"]]
return project_data, sections
def add_section(project_data, section_name, section_text):
updated_project = update_section(project_data, section_name, section_text)
sections = [section["name"] for section in updated_project["sections"]]
return updated_project, sections
def generate_audio(section_name, text):
audio_path = generate_section_audio(section_name, text, speaker.value)
return audio_path
# Connexion des événements
create_btn.click(load_project, inputs=[project_name, speaker, agree], outputs=[project_info, sections_list])
add_section_btn.click(add_section, inputs=[project_info, section_name, section_text], outputs=[project_info, sections_list])
# Dynamique : Créer des boutons de génération audio pour chaque section
def update_audio_buttons(sections):
global audio_outputs_display
audio_outputs_display.clear()
for sec in sections:
audio_output_btn = gr.Button(f"🎤 Générer l'audio pour {sec}")
audio_output_display = gr.Audio(label=f"🔊 Audio de {sec}", type="filepath", visible=False)
def generate_for_section(sec=sec):
text_for_section = next((s['text'] for s in project_info['sections'] if s['name'] == sec), "")
return generate_audio(sec, text_for_section)
audio_output_btn.click(generate_for_section)
audio_outputs_display.append(audio_output_display)
# Ajouter les boutons et les affichages audio à l'interface
demo.add(audio_output_btn)
demo.add(audio_output_display)
# Lancement de l'interface
demo.launch()
|