Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,167 @@
|
|
1 |
import subprocess
|
2 |
-
import gradio as gr
|
3 |
-
from TTS.api import TTS
|
4 |
import os
|
5 |
import time
|
6 |
-
import
|
7 |
-
import
|
8 |
|
9 |
# Initialisation du modèle TTS
|
10 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
os.makedirs(base_output_folder, exist_ok=True)
|
15 |
|
16 |
-
|
17 |
-
def create_or_load_project(project_name, speaker, agree):
|
18 |
if not agree:
|
19 |
-
raise gr.Error("
|
20 |
|
21 |
-
project_folder = os.path.join(
|
22 |
os.makedirs(project_folder, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
if section["name"] == section_name:
|
44 |
-
section["text"] = section_text
|
45 |
-
return project_data
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return project_data
|
53 |
|
54 |
-
|
55 |
-
def generate_section_audio(project_data, section_name):
|
56 |
-
project_folder = os.path.join(base_output_folder, project_data["name"])
|
57 |
-
for section in project_data["sections"]:
|
58 |
-
if section["name"] == section_name:
|
59 |
-
output_path = os.path.join(project_folder, f"{section_name}.wav")
|
60 |
-
tts.tts_to_file(
|
61 |
-
text=section["text"],
|
62 |
-
file_path=output_path,
|
63 |
-
speaker_wav=[os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(project_data["speaker"]) and f.endswith(".wav")],
|
64 |
-
language="fr"
|
65 |
-
)
|
66 |
-
section["audio_path"] = output_path
|
67 |
-
return project_data, output_path
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
project_state = gr.State(value={})
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
|
83 |
-
create_btn = gr.Button("🚀 Créer/Charger le Projet")
|
84 |
|
85 |
-
|
86 |
-
with gr.Tab("📋 Gestion des Sections"):
|
87 |
-
sections_list = gr.List(label="📜 Sections du projet")
|
88 |
-
section_name = gr.Textbox(label="📝 Nom de la section")
|
89 |
-
section_text = gr.Textbox(label="✍️ Texte de la section", lines=5)
|
90 |
-
add_section_btn = gr.Button("➕ Ajouter/Mettre à jour la section")
|
91 |
-
generate_section_btn = gr.Button("🎤 Générer l'audio de la section")
|
92 |
-
audio_output = gr.Audio(label="🔊 Audio généré")
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
return project_data, gr.List.update(value=sections)
|
99 |
|
100 |
-
def
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
updated_project, audio_path = generate_section_audio(project_data, section_name)
|
107 |
-
return updated_project, audio_path
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
demo.launch()
|
|
|
1 |
import subprocess
|
|
|
|
|
2 |
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 |
+
output_folder = "output_audio"
|
11 |
+
os.makedirs(output_folder, exist_ok=True)
|
|
|
12 |
|
13 |
+
def create_project(project_name, speaker, agree):
|
|
|
14 |
if not agree:
|
15 |
+
raise gr.Error("🚨 Veuillez accepter les conditions d'utilisation.")
|
16 |
|
17 |
+
project_folder = os.path.join(output_folder, project_name.strip())
|
18 |
os.makedirs(project_folder, exist_ok=True)
|
19 |
+
return project_folder
|
20 |
+
|
21 |
+
def generate_audio_section(text, section_name, project_folder, speaker):
|
22 |
+
file_name = f"{section_name.strip()}.wav"
|
23 |
+
output_path = os.path.join(project_folder, file_name)
|
24 |
+
|
25 |
+
speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
|
26 |
+
if not speaker_wav_paths:
|
27 |
+
raise gr.Error(f"🚨 Aucun fichier audio trouvé pour le speaker : {speaker}")
|
28 |
+
|
29 |
+
tts.tts_to_file(
|
30 |
+
text=text,
|
31 |
+
file_path=output_path,
|
32 |
+
speaker_wav=speaker_wav_paths,
|
33 |
+
language="fr"
|
34 |
+
)
|
35 |
+
|
36 |
+
return output_path
|
37 |
+
|
38 |
+
custom_css = """
|
39 |
+
.gradio-container {
|
40 |
+
font-family: 'Roboto', sans-serif;
|
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 |
+
with gr.Row():
|
91 |
+
with gr.Column(scale=1):
|
92 |
+
project_name = gr.Textbox(
|
93 |
+
label="🗂️ Nom du projet",
|
94 |
+
placeholder="Entrez le nom du projet",
|
95 |
+
lines=1
|
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 |
+
project_folder_output = gr.State()
|
109 |
|
110 |
+
def on_create_project(name, spkr, agr):
|
111 |
+
folder_path = create_project(name, spkr, agr)
|
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 |
+
section_textboxes = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
+
def add_section():
|
123 |
+
section_textbox = gr.Textbox(
|
124 |
+
label=f"✏️ Texte de la section {len(section_textboxes) + 1}",
|
125 |
+
placeholder="Entrez le texte pour cette section",
|
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 |
+
generate_audio_btn = gr.Button("🎶 Générer l'audio")
|
|
|
149 |
|
150 |
+
def generate_all_sections(sections_data, proj_folder):
|
151 |
+
audio_outputs = []
|
152 |
+
for text_box, name_box in sections_data:
|
153 |
+
text = text_box.value
|
154 |
+
name = name_box.value
|
155 |
+
audio_path = generate_audio_section(text, name.strip(), proj_folder.value, speaker.value)
|
156 |
+
audio_outputs.append(audio_path)
|
157 |
+
return audio_outputs
|
158 |
|
159 |
+
audio_outputs_display = [gr.Audio(label=f"🔊 Audio Section {i+1}") for i in range(len(section_textboxes))]
|
|
|
|
|
160 |
|
161 |
+
generate_audio_btn.click(
|
162 |
+
generate_all_sections,
|
163 |
+
inputs=[section_textboxes, project_folder_output],
|
164 |
+
outputs=audio_outputs_display
|
165 |
+
)
|
166 |
+
|
167 |
+
demo.launch(debug=True)
|