Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,165 +3,134 @@ import os
|
|
| 3 |
import time
|
| 4 |
import gradio as gr
|
| 5 |
from TTS.api import TTS
|
|
|
|
| 6 |
|
| 7 |
# Initialisation du modèle TTS
|
| 8 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
if not agree:
|
| 15 |
-
raise gr.Error("
|
| 16 |
|
| 17 |
-
project_folder = os.path.join(
|
| 18 |
os.makedirs(project_folder, exist_ok=True)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
tts.tts_to_file(
|
| 30 |
text=text,
|
| 31 |
file_path=output_path,
|
| 32 |
-
speaker_wav=
|
| 33 |
language="fr"
|
| 34 |
)
|
| 35 |
-
|
| 36 |
return output_path
|
| 37 |
|
| 38 |
-
|
| 39 |
-
.
|
| 40 |
-
|
| 41 |
-
background-color: #f7f9fc;
|
| 42 |
-
}
|
| 43 |
-
.gr-form {
|
| 44 |
-
background-color: white;
|
| 45 |
-
border-radius: 15px;
|
| 46 |
-
padding: 30px;
|
| 47 |
-
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
|
| 48 |
-
}
|
| 49 |
-
.gr-button {
|
| 50 |
-
background-color: #4a90e2;
|
| 51 |
-
border: none;
|
| 52 |
-
color: white;
|
| 53 |
-
font-weight: bold;
|
| 54 |
-
transition: all 0.3s ease;
|
| 55 |
-
}
|
| 56 |
-
.gr-button:hover {
|
| 57 |
-
background-color: #3a7bc8;
|
| 58 |
-
transform: translateY(-2px);
|
| 59 |
-
}
|
| 60 |
-
.gr-input, .gr-dropdown {
|
| 61 |
-
border: 1px solid #e0e0e0;
|
| 62 |
-
border-radius: 8px;
|
| 63 |
-
padding: 10px;
|
| 64 |
-
}
|
| 65 |
-
.gr-checkbox {
|
| 66 |
-
margin-top: 10px;
|
| 67 |
-
}
|
| 68 |
-
.gr-form > div {
|
| 69 |
-
margin-bottom: 20px;
|
| 70 |
-
}
|
| 71 |
-
"""
|
| 72 |
-
|
| 73 |
-
title = "🎙️ Synthèse Vocale XTTS"
|
| 74 |
-
description = """
|
| 75 |
-
<h3 style='text-align: center; margin-bottom: 1em;'>Bienvenue sur notre outil de synthèse vocale XTTS ! 🌟</h3>
|
| 76 |
-
<p style='text-align: center;'>Générez une voix naturelle à partir de votre texte en français. Choisissez une voix, entrez votre texte, et écoutez le résultat ! 🎤</p>
|
| 77 |
-
"""
|
| 78 |
-
article = """
|
| 79 |
-
<div style='margin: 20px auto; text-align: center; padding: 10px; background-color: #e8f0fe; border-radius: 10px;'>
|
| 80 |
-
<p>En utilisant cette démo, vous acceptez les <a href='https://coqui.ai/cpml' target='_blank' style='color: #4a90e2; text-decoration: none;'>conditions d'utilisation du modèle Coqui Public</a> 📜</p>
|
| 81 |
-
</div>
|
| 82 |
-
"""
|
| 83 |
-
|
| 84 |
-
available_speakers = list(set([f.split('_')[0] for f in os.listdir("examples") if f.endswith(".wav")]))
|
| 85 |
-
|
| 86 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 87 |
-
gr.Markdown(f"<h1 style='text-align: center; color: #4a90e2;'>{title}</h1>")
|
| 88 |
-
gr.Markdown(description)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
)
|
| 97 |
-
speaker = gr.Dropdown(
|
| 98 |
-
label="🎤 Voix",
|
| 99 |
-
choices=available_speakers,
|
| 100 |
-
value=available_speakers[0] if available_speakers else None
|
| 101 |
-
)
|
| 102 |
-
agree = gr.Checkbox(
|
| 103 |
-
label="✅ J'accepte les conditions d'utilisation",
|
| 104 |
-
value=False
|
| 105 |
-
)
|
| 106 |
-
create_project_btn = gr.Button("🔨 Créer le projet", variant="primary")
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return folder_path
|
| 113 |
-
|
| 114 |
-
create_project_btn.click(
|
| 115 |
-
on_create_project,
|
| 116 |
-
inputs=[project_name, speaker, agree],
|
| 117 |
-
outputs=[project_folder_output]
|
| 118 |
-
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
lines=5
|
| 127 |
-
)
|
| 128 |
-
section_name_textbox = gr.Textbox(
|
| 129 |
-
label=f"📝 Nom de la section {len(section_textboxes) + 1}",
|
| 130 |
-
placeholder="Entrez le nom du fichier pour cette section",
|
| 131 |
-
lines=1
|
| 132 |
-
)
|
| 133 |
-
section_textboxes.append((section_textbox, section_name_textbox))
|
| 134 |
-
return section_textbox, section_name_textbox
|
| 135 |
-
|
| 136 |
-
add_section_btn = gr.Button("➕ Ajouter une section")
|
| 137 |
-
|
| 138 |
-
def remove_section():
|
| 139 |
-
if section_textboxes:
|
| 140 |
-
removed_section = section_textboxes.pop()
|
| 141 |
-
return removed_section[0], removed_section[1]
|
| 142 |
-
|
| 143 |
-
remove_section_btn = gr.Button("➖ Supprimer une section")
|
| 144 |
-
|
| 145 |
-
add_section_btn.click(add_section)
|
| 146 |
-
remove_section_btn.click(remove_section)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
return
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
import gradio as gr
|
| 5 |
from TTS.api import TTS
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
# Initialisation du modèle TTS
|
| 9 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 10 |
|
| 11 |
+
# Dossier de base pour les projets
|
| 12 |
+
base_output_folder = "projects"
|
| 13 |
+
os.makedirs(base_output_folder, exist_ok=True)
|
| 14 |
|
| 15 |
+
# Fonction pour créer ou charger un projet
|
| 16 |
+
def create_or_load_project(project_name, speaker, agree):
|
| 17 |
if not agree:
|
| 18 |
+
raise gr.Error("❗ Veuillez accepter les conditions d'utilisation.")
|
| 19 |
|
| 20 |
+
project_folder = os.path.join(base_output_folder, project_name)
|
| 21 |
os.makedirs(project_folder, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
project_data = {
|
| 24 |
+
"name": project_name,
|
| 25 |
+
"speaker": speaker,
|
| 26 |
+
"sections": []
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# Vérifier si le projet existe déjà
|
| 30 |
+
project_file = os.path.join(project_folder, "project_data.json")
|
| 31 |
+
if os.path.exists(project_file):
|
| 32 |
+
with open(project_file, "r") as f:
|
| 33 |
+
project_data = json.load(f)
|
| 34 |
+
else:
|
| 35 |
+
with open(project_file, "w") as f:
|
| 36 |
+
json.dump(project_data, f)
|
| 37 |
+
|
| 38 |
+
return project_data
|
| 39 |
|
| 40 |
+
# Fonction pour ajouter ou mettre à jour une section
|
| 41 |
+
def update_section(project_data, section_name, section_text):
|
| 42 |
+
for section in project_data["sections"]:
|
| 43 |
+
if section["name"] == section_name:
|
| 44 |
+
section["text"] = section_text
|
| 45 |
+
return project_data
|
| 46 |
+
|
| 47 |
+
project_data["sections"].append({
|
| 48 |
+
"name": section_name,
|
| 49 |
+
"text": section_text,
|
| 50 |
+
"audio_path": None
|
| 51 |
+
})
|
| 52 |
+
return project_data
|
| 53 |
|
| 54 |
+
# Fonction pour générer l'audio d'une section
|
| 55 |
+
def generate_section_audio(section_name, text, speaker):
|
| 56 |
+
output_path = os.path.join(base_output_folder, f"{section_name}.wav")
|
| 57 |
+
|
| 58 |
tts.tts_to_file(
|
| 59 |
text=text,
|
| 60 |
file_path=output_path,
|
| 61 |
+
speaker_wav=[os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")],
|
| 62 |
language="fr"
|
| 63 |
)
|
| 64 |
+
|
| 65 |
return output_path
|
| 66 |
|
| 67 |
+
# Interface Gradio
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# 🎙️ Projet Margaux TTS")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Étape 1: Création ou chargement du projet
|
| 72 |
+
with gr.Tab("🔧 Création du Projet"):
|
| 73 |
+
project_name = gr.Textbox(label="📝 Nom du projet")
|
| 74 |
+
speaker = gr.Dropdown(label="🔊 Voix", choices=["Margaux"], value="Margaux")
|
| 75 |
+
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
| 76 |
+
create_btn = gr.Button("🚀 Créer/Charger le Projet")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# Étape 2: Gestion des sections
|
| 79 |
+
with gr.Tab("📋 Gestion des Sections"):
|
| 80 |
+
project_info = gr.JSON(label="📁 Informations du projet", visible=False)
|
| 81 |
+
sections_list = gr.List(label="📜 Sections du projet")
|
| 82 |
+
|
| 83 |
+
# Entrées pour ajouter une nouvelle section
|
| 84 |
+
section_name = gr.Textbox(label="📝 Nom de la section")
|
| 85 |
+
section_text = gr.Textbox(label="✍️ Texte de la section", lines=5)
|
| 86 |
+
|
| 87 |
+
add_section_btn = gr.Button("➕ Ajouter/Mettre à jour la section")
|
| 88 |
|
| 89 |
+
# Section pour générer l'audio de chaque section individuellement
|
| 90 |
+
audio_outputs_display = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Fonctions de callback
|
| 93 |
+
def load_project(project_name, speaker, agree):
|
| 94 |
+
project_data = create_or_load_project(project_name, speaker, agree)
|
| 95 |
+
sections = [section["name"] for section in project_data["sections"]]
|
| 96 |
+
|
| 97 |
+
return project_data, sections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
def add_section(project_data, section_name, section_text):
|
| 100 |
+
updated_project = update_section(project_data, section_name, section_text)
|
| 101 |
+
sections = [section["name"] for section in updated_project["sections"]]
|
| 102 |
+
|
| 103 |
+
return updated_project, sections
|
| 104 |
+
|
| 105 |
+
def generate_audio(section_name, text):
|
| 106 |
+
audio_path = generate_section_audio(section_name, text, speaker.value)
|
| 107 |
+
|
| 108 |
+
return audio_path
|
| 109 |
+
|
| 110 |
+
# Connexion des événements
|
| 111 |
+
create_btn.click(load_project, inputs=[project_name, speaker, agree], outputs=[project_info, sections_list])
|
| 112 |
+
add_section_btn.click(add_section, inputs=[project_info, section_name, section_text], outputs=[project_info, sections_list])
|
| 113 |
+
|
| 114 |
+
# Dynamique : Créer des boutons de génération audio pour chaque section
|
| 115 |
+
def update_audio_buttons(sections):
|
| 116 |
+
global audio_outputs_display
|
| 117 |
+
|
| 118 |
+
audio_outputs_display.clear()
|
| 119 |
+
|
| 120 |
+
for sec in sections:
|
| 121 |
+
audio_output_btn = gr.Button(f"🎤 Générer l'audio pour {sec}")
|
| 122 |
+
audio_output_display = gr.Audio(label=f"🔊 Audio de {sec}", type="filepath", visible=False)
|
| 123 |
+
|
| 124 |
+
def generate_for_section(sec=sec):
|
| 125 |
+
text_for_section = next((s['text'] for s in project_info['sections'] if s['name'] == sec), "")
|
| 126 |
+
return generate_audio(sec, text_for_section)
|
| 127 |
+
|
| 128 |
+
audio_output_btn.click(generate_for_section)
|
| 129 |
+
audio_outputs_display.append(audio_output_display)
|
| 130 |
+
|
| 131 |
+
# Ajouter les boutons et les affichages audio à l'interface
|
| 132 |
+
demo.add(audio_output_btn)
|
| 133 |
+
demo.add(audio_output_display)
|
| 134 |
+
|
| 135 |
+
# Lancement de l'interface
|
| 136 |
+
demo.launch()
|