Woziii commited on
Commit
6bca271
·
verified ·
1 Parent(s): c6e52e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -89
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import subprocess
2
- import os
3
  import gradio as gr
4
  from TTS.api import TTS
 
 
 
5
  # Initialisation du modèle TTS
6
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
7
 
 
8
  output_folder = "output_audio"
9
  os.makedirs(output_folder, exist_ok=True)
10
 
@@ -12,25 +15,24 @@ def create_project(project_name, speaker, agree):
12
  if not agree:
13
  raise gr.Error("Veuillez accepter les conditions d'utilisation.")
14
 
15
- project_folder = os.path.join(output_folder, project_name.strip())
16
  os.makedirs(project_folder, exist_ok=True)
17
  return project_folder
18
 
19
- def generate_audio_section(text, section_name, project_folder, speaker):
20
- file_name = f"{section_name.strip()}.wav"
21
- output_path = os.path.join(project_folder, file_name)
22
-
23
  speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
24
  if not speaker_wav_paths:
25
  raise gr.Error(f"Aucun fichier audio trouvé pour le speaker : {speaker}")
26
 
 
 
27
  tts.tts_to_file(
28
- text=text,
29
  file_path=output_path,
30
  speaker_wav=speaker_wav_paths,
31
  language="fr"
32
  )
33
-
34
  return output_path
35
 
36
  custom_css = """
@@ -71,104 +73,66 @@ custom_css = """
71
  title = "🎙️ Synthèse Vocale XTTS"
72
  description = """
73
  <h3 style='text-align: center; margin-bottom: 1em;'>Bienvenue sur notre outil de synthèse vocale XTTS !</h3>
74
- <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>
75
- """
76
- article = """
77
- <div style='margin: 20px auto; text-align: center; padding: 10px; background-color: #e8f0fe; border-radius: 10px;'>
78
- <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>
79
- </div>
80
  """
81
 
82
  available_speakers = list(set([f.split('_')[0] for f in os.listdir("examples") if f.endswith(".wav")]))
83
 
84
  with gr.Blocks(css=custom_css) as demo:
 
 
85
  gr.Markdown(f"<h1 style='text-align: center; color: #4a90e2;'>{title}</h1>")
86
  gr.Markdown(description)
87
 
88
  with gr.Row():
89
- with gr.Column(scale=1):
90
- project_name = gr.Textbox(
91
- label="Nom du projet",
92
- placeholder="Entrez le nom du projet",
93
- lines=1
94
- )
95
- speaker = gr.Dropdown(
96
- label="Voix",
97
- choices=available_speakers,
98
- value=available_speakers[0] if available_speakers else None
99
- )
100
- agree = gr.Checkbox(
101
- label="J'accepte les conditions d'utilisation",
102
- value=False
103
- )
104
- create_project_btn = gr.Button("Créer le projet", variant="primary")
105
 
106
- project_folder_output = gr.State()
107
-
108
- def on_create_project(name, spkr, agr):
109
- folder_path = create_project(name, spkr, agr)
110
- return folder_path
111
 
112
- create_project_btn.click(
113
- on_create_project,
114
- inputs=[project_name, speaker, agree],
115
- outputs=[project_folder_output]
116
- )
117
-
118
- section_textboxes = []
119
 
120
  def add_section():
121
- section_textbox = gr.Textbox(
122
- label=f"Texte de la section {len(section_textboxes) + 1}",
123
- placeholder="Entrez le texte pour cette section",
124
- lines=5
125
- )
126
- section_name_textbox = gr.Textbox(
127
- label=f"Nom de la section {len(section_textboxes) + 1}",
128
- placeholder="Entrez le nom du fichier pour cette section",
129
- lines=1
130
- )
131
- section_textboxes.append((section_textbox, section_name_textbox))
132
- return section_textbox, section_name_textbox
133
-
134
- add_section_btn = gr.Button("+ Ajouter une section")
135
 
136
- def remove_section():
137
- if section_textboxes:
138
- removed_section = section_textboxes.pop()
139
- return removed_section[0], removed_section[1]
140
 
141
- remove_section_btn = gr.Button("- Supprimer une section")
 
 
 
142
 
143
- add_section_btn.click(add_section)
144
 
145
- # Outputs for the sections
146
- added_sections_output = []
147
-
148
- def update_sections_output():
149
- return [section[0] for section in section_textboxes], [section[1] for section in section_textboxes]
150
-
151
- add_section_btn.click(update_sections_output)
 
 
 
 
 
 
 
 
 
 
 
152
 
153
- remove_section_btn.click(update_sections_output)
 
 
 
154
 
155
- generate_audio_btn = gr.Button("Générer l'audio")
156
-
157
- def generate_all_sections(proj_folder):
158
- audio_outputs = []
159
- for text_box, name_box in section_textboxes:
160
- text = text_box.value
161
- name = name_box.value
162
- audio_path = generate_audio_section(text, name.strip(), proj_folder.value, speaker.value)
163
- audio_outputs.append(audio_path)
164
- return audio_outputs
165
-
166
- audio_outputs_display = [gr.Audio(label=f"Audio Section {i+1}") for i in range(len(section_textboxes))]
167
-
168
- generate_audio_btn.click(
169
- generate_all_sections,
170
- inputs=[project_folder_output],
171
- outputs=audio_outputs_display
172
- )
173
-
174
  demo.launch(debug=True)
 
1
  import subprocess
 
2
  import gradio as gr
3
  from TTS.api import TTS
4
+ import os
5
+ import time
6
+
7
  # Initialisation du modèle TTS
8
  tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
9
 
10
+ # Dossier pour les fichiers audio générés
11
  output_folder = "output_audio"
12
  os.makedirs(output_folder, exist_ok=True)
13
 
 
15
  if not agree:
16
  raise gr.Error("Veuillez accepter les conditions d'utilisation.")
17
 
18
+ project_folder = os.path.join(output_folder, project_name)
19
  os.makedirs(project_folder, exist_ok=True)
20
  return project_folder
21
 
22
+ def generate_audio(prompt, speaker, project_folder, section_name):
 
 
 
23
  speaker_wav_paths = [os.path.join("examples", f) for f in os.listdir("examples") if f.startswith(speaker) and f.endswith(".wav")]
24
  if not speaker_wav_paths:
25
  raise gr.Error(f"Aucun fichier audio trouvé pour le speaker : {speaker}")
26
 
27
+ output_path = os.path.join(project_folder, f"{section_name}.wav")
28
+
29
  tts.tts_to_file(
30
+ text=prompt,
31
  file_path=output_path,
32
  speaker_wav=speaker_wav_paths,
33
  language="fr"
34
  )
35
+
36
  return output_path
37
 
38
  custom_css = """
 
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. Créez un projet et ajoutez des sections pour votre script.</p>
 
 
 
 
 
77
  """
78
 
79
  available_speakers = list(set([f.split('_')[0] for f in os.listdir("examples") if f.endswith(".wav")]))
80
 
81
  with gr.Blocks(css=custom_css) as demo:
82
+
83
+ # Étape 1 : Création du Projet
84
  gr.Markdown(f"<h1 style='text-align: center; color: #4a90e2;'>{title}</h1>")
85
  gr.Markdown(description)
86
 
87
  with gr.Row():
88
+ project_name = gr.Textbox(label="Nom du projet", placeholder="Entrez le nom du projet")
89
+ speaker = gr.Dropdown(label="Voix", choices=available_speakers, value=available_speakers[0] if available_speakers else None)
90
+ agree = gr.Checkbox(label="J'accepte les conditions d'utilisation", value=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ create_project_btn = gr.Button("Créer le Projet", variant="primary")
 
 
 
 
93
 
94
+ # Étape 2 : Gestion des Sections
95
+ sections = []
 
 
 
 
 
96
 
97
  def add_section():
98
+ section_name = f"Section {len(sections) + 1}"
99
+ sections.append({"name": section_name, "text": ""})
100
+ return sections
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ def remove_section(index):
103
+ if index < len(sections):
104
+ sections.pop(index)
105
+ return sections
106
 
107
+ def update_section_text(index, text):
108
+ if index < len(sections):
109
+ sections[index]["text"] = text
110
+ return sections
111
 
112
+ section_outputs = []
113
 
114
+ with gr.Column():
115
+ section_list = gr.State(value=sections)
116
+
117
+ def render_sections(sections):
118
+ return [gr.Row([
119
+ gr.Textbox(label=f"Texte pour {section['name']}", value=section['text'], lines=5).change(update_section_text, inputs=[sections.index(section)], outputs=None),
120
+ gr.Textbox(label="Nom du fichier", value=section['name'], interactive=False),
121
+ gr.Button("Régénérer", variant="secondary").click(generate_audio, inputs=[section['text'], speaker, project_folder, section['name']], outputs=[gr.Audio(label=f"Audio {section['name']}")])
122
+ ]) for section in sections]
123
+
124
+ with gr.Row():
125
+ add_section_btn = gr.Button("+ Ajouter une section")
126
+ remove_section_btn = gr.Button("- Supprimer la dernière section")
127
+
128
+ add_section_btn.click(add_section, outputs=section_list)
129
+ remove_section_btn.click(remove_section, inputs=[len(sections)-1], outputs=section_list)
130
+
131
+ section_output_area = gr.Column()
132
 
133
+ # Dynamically render sections
134
+ with section_output_area:
135
+ for section in render_sections(sections):
136
+ section_output_area.append(section)
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  demo.launch(debug=True)