Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -84,6 +84,10 @@ MODEL_CFG = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_lay
|
|
84 |
_cached_local_paths = {}
|
85 |
loaded_models = {} # хранит объекты моделей в памяти (по имени выбора)
|
86 |
|
|
|
|
|
|
|
|
|
87 |
# ----------------- Вспомогательные функции HF -----------------
|
88 |
def hf_download_file(repo_id: str, filename: str, token: str = None):
|
89 |
try:
|
@@ -143,6 +147,22 @@ print("Loading vocoder (CPU) ...")
|
|
143 |
vocoder = load_vocoder()
|
144 |
print("Vocoder loaded.")
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
# ----------------- Основная функция синтеза (GPU-aware) -----------------
|
147 |
# Декорируем synthesize, чтобы при вызове Space выделял GPU (если доступно).
|
148 |
# duration — сколько секунд просим GPU (адаптируйте под ваш инференс).
|
@@ -168,7 +188,7 @@ def synthesize(
|
|
168 |
"""
|
169 |
if not ref_audio:
|
170 |
gr.Warning("Please provide reference audio.")
|
171 |
-
return None, None, ref_text
|
172 |
|
173 |
if seed is None or seed < 0 or seed > 2**31 - 1:
|
174 |
seed = np.random.randint(0, 2**31 - 1)
|
@@ -176,7 +196,7 @@ def synthesize(
|
|
176 |
|
177 |
if not gen_text or not gen_text.strip():
|
178 |
gr.Warning("Please enter text to generate.")
|
179 |
-
return None, None, ref_text
|
180 |
|
181 |
# ASR если нужно
|
182 |
if not ref_text or not ref_text.strip():
|
@@ -195,18 +215,18 @@ def synthesize(
|
|
195 |
gr.Info(f"ASR transcription: {ref_text}")
|
196 |
except Exception as e:
|
197 |
gr.Warning(f"ASR failed: {e}")
|
198 |
-
return None, None, ref_text
|
199 |
|
200 |
-
# Акцентирование
|
201 |
-
processed_ref_text =
|
202 |
-
processed_gen_text =
|
203 |
|
204 |
# Ленивая загрузка модели (в CPU)
|
205 |
try:
|
206 |
model = load_model_if_needed(model_choice)
|
207 |
except Exception as e:
|
208 |
gr.Warning(f"Failed to download/load model {model_choice}: {e}")
|
209 |
-
return None, None,
|
210 |
|
211 |
# Определяем устройство (в ZeroGPU внутри декоратора должен быть доступен CUDA)
|
212 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -230,7 +250,7 @@ def synthesize(
|
|
230 |
|
231 |
# Препроцессинг рефа (оно ожидает путь/файл)
|
232 |
try:
|
233 |
-
ref_audio_proc,
|
234 |
ref_audio,
|
235 |
processed_ref_text,
|
236 |
show_info=gr.Info
|
@@ -238,13 +258,13 @@ def synthesize(
|
|
238 |
except Exception as e:
|
239 |
gr.Warning(f"Preprocess failed: {e}")
|
240 |
traceback.print_exc()
|
241 |
-
return None, None,
|
242 |
|
243 |
# Инференс (предполагается, что infer_process корректно работает и на GPU)
|
244 |
try:
|
245 |
final_wave, final_sample_rate, combined_spectrogram = infer_process(
|
246 |
ref_audio_proc,
|
247 |
-
|
248 |
processed_gen_text,
|
249 |
model,
|
250 |
vocoder,
|
@@ -257,7 +277,7 @@ def synthesize(
|
|
257 |
except Exception as e:
|
258 |
gr.Warning(f"Infer failed: {e}")
|
259 |
traceback.print_exc()
|
260 |
-
return None, None,
|
261 |
|
262 |
# Удаление тишин (на CPU)
|
263 |
if remove_silence:
|
@@ -280,7 +300,7 @@ def synthesize(
|
|
280 |
print("Save spectrogram failed:", e)
|
281 |
spectrogram_path = None
|
282 |
|
283 |
-
return (final_sample_rate, final_wave), spectrogram_path,
|
284 |
|
285 |
finally:
|
286 |
# Переносим всё обратно на CPU и очищаем GPU память
|
@@ -301,6 +321,18 @@ def synthesize(
|
|
301 |
with gr.Blocks(title="ESpeech-TTS") as app:
|
302 |
gr.Markdown("# ESpeech-TTS")
|
303 |
gr.Markdown("See more on https://huggingface.co/ESpeech")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
model_choice = gr.Dropdown(
|
306 |
choices=list(MODEL_REPOS.keys()),
|
@@ -312,9 +344,29 @@ with gr.Blocks(title="ESpeech-TTS") as app:
|
|
312 |
with gr.Row():
|
313 |
with gr.Column():
|
314 |
ref_audio_input = gr.Audio(label="Reference Audio", type="filepath")
|
315 |
-
ref_text_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
with gr.Column():
|
317 |
-
gen_text_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
|
319 |
with gr.Row():
|
320 |
with gr.Column():
|
@@ -331,6 +383,37 @@ with gr.Blocks(title="ESpeech-TTS") as app:
|
|
331 |
audio_output = gr.Audio(label="Generated Audio", type="numpy")
|
332 |
spectrogram_output = gr.Image(label="Spectrogram", type="filepath")
|
333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
generate_btn.click(
|
335 |
synthesize,
|
336 |
inputs=[
|
@@ -344,7 +427,7 @@ with gr.Blocks(title="ESpeech-TTS") as app:
|
|
344 |
nfe_slider,
|
345 |
speed_slider,
|
346 |
],
|
347 |
-
outputs=[audio_output, spectrogram_output,
|
348 |
)
|
349 |
|
350 |
if __name__ == "__main__":
|
|
|
84 |
_cached_local_paths = {}
|
85 |
loaded_models = {} # хранит объекты моделей в памяти (по имени выбора)
|
86 |
|
87 |
+
# Пример текста для демонстрации
|
88 |
+
EXAMPLE_TEXT = "Экспериментальный центр напоминает вам о том, что кубы не умеют разговаривать. В случае, если грузовой куб все же заговорит, центр настоятельно рекомендует вам игнорировать его советы."
|
89 |
+
EXAMPLE_REF_AUDIO = "ref/example.mp3"
|
90 |
+
|
91 |
# ----------------- Вспомогательные функции HF -----------------
|
92 |
def hf_download_file(repo_id: str, filename: str, token: str = None):
|
93 |
try:
|
|
|
147 |
vocoder = load_vocoder()
|
148 |
print("Vocoder loaded.")
|
149 |
|
150 |
+
# ----------------- Функция для обработки текста с учетом "+" -----------------
|
151 |
+
def process_text_with_accent(text, accentizer):
|
152 |
+
"""
|
153 |
+
Обрабатывает текст через RUAccent, если в нем нет символа '+'.
|
154 |
+
Если есть '+' - пользователь сам проставил ударения, не трогаем.
|
155 |
+
"""
|
156 |
+
if not text or not text.strip():
|
157 |
+
return text
|
158 |
+
|
159 |
+
if '+' in text:
|
160 |
+
# Пользователь сам проставил ударения
|
161 |
+
return text
|
162 |
+
else:
|
163 |
+
# Прогоняем через RUAccent
|
164 |
+
return accentizer.process_all(text)
|
165 |
+
|
166 |
# ----------------- Основная функция синтеза (GPU-aware) -----------------
|
167 |
# Декорируем synthesize, чтобы при вызове Space выделял GPU (если доступно).
|
168 |
# duration — сколько секунд просим GPU (адаптируйте под ваш инференс).
|
|
|
188 |
"""
|
189 |
if not ref_audio:
|
190 |
gr.Warning("Please provide reference audio.")
|
191 |
+
return None, None, ref_text, gen_text
|
192 |
|
193 |
if seed is None or seed < 0 or seed > 2**31 - 1:
|
194 |
seed = np.random.randint(0, 2**31 - 1)
|
|
|
196 |
|
197 |
if not gen_text or not gen_text.strip():
|
198 |
gr.Warning("Please enter text to generate.")
|
199 |
+
return None, None, ref_text, gen_text
|
200 |
|
201 |
# ASR если нужно
|
202 |
if not ref_text or not ref_text.strip():
|
|
|
215 |
gr.Info(f"ASR transcription: {ref_text}")
|
216 |
except Exception as e:
|
217 |
gr.Warning(f"ASR failed: {e}")
|
218 |
+
return None, None, ref_text, gen_text
|
219 |
|
220 |
+
# Акцентирование с учетом наличия символа "+"
|
221 |
+
processed_ref_text = process_text_with_accent(ref_text, accentizer)
|
222 |
+
processed_gen_text = process_text_with_accent(gen_text, accentizer)
|
223 |
|
224 |
# Ленивая загрузка модели (в CPU)
|
225 |
try:
|
226 |
model = load_model_if_needed(model_choice)
|
227 |
except Exception as e:
|
228 |
gr.Warning(f"Failed to download/load model {model_choice}: {e}")
|
229 |
+
return None, None, processed_ref_text, processed_gen_text
|
230 |
|
231 |
# Определяем устройство (в ZeroGPU внутри декоратора должен быть доступен CUDA)
|
232 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
250 |
|
251 |
# Препроцессинг рефа (оно ожидает путь/файл)
|
252 |
try:
|
253 |
+
ref_audio_proc, processed_ref_text_final = preprocess_ref_audio_text(
|
254 |
ref_audio,
|
255 |
processed_ref_text,
|
256 |
show_info=gr.Info
|
|
|
258 |
except Exception as e:
|
259 |
gr.Warning(f"Preprocess failed: {e}")
|
260 |
traceback.print_exc()
|
261 |
+
return None, None, processed_ref_text, processed_gen_text
|
262 |
|
263 |
# Инференс (предполагается, что infer_process корректно работает и на GPU)
|
264 |
try:
|
265 |
final_wave, final_sample_rate, combined_spectrogram = infer_process(
|
266 |
ref_audio_proc,
|
267 |
+
processed_ref_text_final,
|
268 |
processed_gen_text,
|
269 |
model,
|
270 |
vocoder,
|
|
|
277 |
except Exception as e:
|
278 |
gr.Warning(f"Infer failed: {e}")
|
279 |
traceback.print_exc()
|
280 |
+
return None, None, processed_ref_text, processed_gen_text
|
281 |
|
282 |
# Удаление тишин (на CPU)
|
283 |
if remove_silence:
|
|
|
300 |
print("Save spectrogram failed:", e)
|
301 |
spectrogram_path = None
|
302 |
|
303 |
+
return (final_sample_rate, final_wave), spectrogram_path, processed_ref_text_final, processed_gen_text
|
304 |
|
305 |
finally:
|
306 |
# Переносим всё обратно на CPU и очищаем GPU память
|
|
|
321 |
with gr.Blocks(title="ESpeech-TTS") as app:
|
322 |
gr.Markdown("# ESpeech-TTS")
|
323 |
gr.Markdown("See more on https://huggingface.co/ESpeech")
|
324 |
+
gr.Markdown("💡 **Tip:** Add '+' symbol in text to mark custom stress (e.g., 'прив+ет'). Text with '+' won't be processed by RUAccent.")
|
325 |
+
|
326 |
+
# Описание моделей на русском языке
|
327 |
+
gr.Markdown("""
|
328 |
+
## 📋 Описание моделей:
|
329 |
+
|
330 |
+
- **ESpeech-TTS-1 [RL] V1** - Первая версия модели с RL
|
331 |
+
- **ESpeech-TTS-1 [RL] V2** - Вторая версия модели с RL
|
332 |
+
- **ESpeech-TTS-1 PODCASTER [SFT]** - Модель обученная только на подкастах, лучше генерирует спонтанную речь
|
333 |
+
- **ESpeech-TTS-1 [SFT] 95K** - чекпоинт с 95000 шагов (на нем основана RL V1)
|
334 |
+
- **ESpeech-TTS-1 [SFT] 265K** - чекпоинт с 265000 шагов (на нем основана RL V2)
|
335 |
+
""")
|
336 |
|
337 |
model_choice = gr.Dropdown(
|
338 |
choices=list(MODEL_REPOS.keys()),
|
|
|
344 |
with gr.Row():
|
345 |
with gr.Column():
|
346 |
ref_audio_input = gr.Audio(label="Reference Audio", type="filepath")
|
347 |
+
ref_text_input = gr.Textbox(
|
348 |
+
label="Reference Text",
|
349 |
+
lines=2,
|
350 |
+
placeholder="leave empty → ASR will transcribe"
|
351 |
+
)
|
352 |
+
ref_text_output = gr.Textbox(
|
353 |
+
label="Processed Reference Text (with accents)",
|
354 |
+
lines=2,
|
355 |
+
interactive=False
|
356 |
+
)
|
357 |
with gr.Column():
|
358 |
+
gen_text_input = gr.Textbox(
|
359 |
+
label="Text to Generate",
|
360 |
+
lines=5,
|
361 |
+
max_lines=20,
|
362 |
+
placeholder="Enter text to synthesize..."
|
363 |
+
)
|
364 |
+
gen_text_output = gr.Textbox(
|
365 |
+
label="Processed Text to Generate (with accents)",
|
366 |
+
lines=5,
|
367 |
+
max_lines=20,
|
368 |
+
interactive=False
|
369 |
+
)
|
370 |
|
371 |
with gr.Row():
|
372 |
with gr.Column():
|
|
|
383 |
audio_output = gr.Audio(label="Generated Audio", type="numpy")
|
384 |
spectrogram_output = gr.Image(label="Spectrogram", type="filepath")
|
385 |
|
386 |
+
# Примеры
|
387 |
+
gr.Markdown("## 🎯 Example")
|
388 |
+
gr.Examples(
|
389 |
+
examples=[
|
390 |
+
[
|
391 |
+
EXAMPLE_REF_AUDIO, # ref_audio
|
392 |
+
"", # ref_text (empty for ASR)
|
393 |
+
EXAMPLE_TEXT, # gen_text
|
394 |
+
False, # remove_silence
|
395 |
+
42, # seed
|
396 |
+
0.15, # cross_fade
|
397 |
+
48, # nfe_step
|
398 |
+
1.0, # speed
|
399 |
+
]
|
400 |
+
],
|
401 |
+
inputs=[
|
402 |
+
ref_audio_input,
|
403 |
+
ref_text_input,
|
404 |
+
gen_text_input,
|
405 |
+
remove_silence,
|
406 |
+
seed_input,
|
407 |
+
cross_fade_slider,
|
408 |
+
nfe_slider,
|
409 |
+
speed_slider,
|
410 |
+
],
|
411 |
+
outputs=[audio_output, spectrogram_output, ref_text_output, gen_text_output],
|
412 |
+
fn=lambda *args: synthesize(model_choice.value, *args),
|
413 |
+
cache_examples=True,
|
414 |
+
run_on_click=True,
|
415 |
+
)
|
416 |
+
|
417 |
generate_btn.click(
|
418 |
synthesize,
|
419 |
inputs=[
|
|
|
427 |
nfe_slider,
|
428 |
speed_slider,
|
429 |
],
|
430 |
+
outputs=[audio_output, spectrogram_output, ref_text_output, gen_text_output]
|
431 |
)
|
432 |
|
433 |
if __name__ == "__main__":
|