Woziii commited on
Commit
9883968
·
verified ·
1 Parent(s): d7f693b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -91
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 torch
7
- import json
8
 
9
  # Initialisation du modèle TTS
10
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
11
 
12
- # Dossier de base pour les projets
13
- base_output_folder = "projects"
14
- os.makedirs(base_output_folder, exist_ok=True)
15
 
16
- # Fonction pour créer ou charger un projet
17
- def create_or_load_project(project_name, speaker, agree):
18
  if not agree:
19
- raise gr.Error(" Veuillez accepter les conditions d'utilisation.")
20
 
21
- project_folder = os.path.join(base_output_folder, project_name)
22
  os.makedirs(project_folder, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- project_data = {
25
- "name": project_name,
26
- "speaker": speaker,
27
- "sections": []
28
- }
29
-
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(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
- raise gr.Error(f"❌ Section '{section_name}' non trouvée.")
70
-
71
- # Interface Gradio
72
- with gr.Blocks() as demo:
73
- gr.Markdown("# 🎙️ Projet Margaux TTS")
 
 
 
 
 
 
 
 
74
 
75
- # État du projet
76
- project_state = gr.State(value={})
77
 
78
- # Étape 1: Création ou chargement du projet
79
- with gr.Tab("🆕 Création/Chargement du Projet"):
80
- project_name = gr.Textbox(label="📝 Nom du projet")
81
- speaker = gr.Dropdown(label="🔊 Voix", choices=["Margaux"], value="Margaux")
82
- agree = gr.Checkbox(label="✅ J'accepte les conditions d'utilisation")
83
- create_btn = gr.Button("🚀 Créer/Charger le Projet")
84
 
85
- # Étape 2: Gestion des sections
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
- # Fonctions de callback
95
- def load_project(project_name, speaker, agree):
96
- project_data = create_or_load_project(project_name, speaker, agree)
97
- sections = [section["name"] for section in project_data["sections"]]
98
- return project_data, gr.List.update(value=sections)
99
 
100
- def add_section(project_data, section_name, section_text):
101
- updated_project = update_section(project_data, section_name, section_text)
102
- sections = [section["name"] for section in updated_project["sections"]]
103
- return updated_project, gr.List.update(value=sections)
 
 
 
 
104
 
105
- def generate_audio(project_data, section_name):
106
- updated_project, audio_path = generate_section_audio(project_data, section_name)
107
- return updated_project, audio_path
108
 
109
- # Connexion des événements
110
- create_btn.click(load_project, inputs=[project_name, speaker, agree], outputs=[project_state, sections_list])
111
- add_section_btn.click(add_section, inputs=[project_state, section_name, section_text], outputs=[project_state, sections_list])
112
- generate_section_btn.click(generate_audio, inputs=[project_state, section_name], outputs=[project_state, audio_output])
113
-
114
- # Lancement de l'interface
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)