binhcoder1709 commited on
Commit ·
8dc0593
1
Parent(s): 82c11aa
feat: main.py
Browse files- apps/gradio_main.py +117 -25
- apps/gradio_xpu.py +21 -3
apps/gradio_main.py
CHANGED
|
@@ -27,11 +27,14 @@ from vieneu_utils.phonemize_text import phonemize_to_chunks, normalize_to_chunks
|
|
| 27 |
# PuncNormalizer = sea_g2p.Normalizer luôn bật punc_norm=True.
|
| 28 |
from vieneu_utils.phonemize_text import PuncNormalizer as Normalizer
|
| 29 |
import gc
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
@spaces.GPU
|
| 33 |
-
def _zerogpu_warmup():
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# PDF text extraction
|
| 37 |
try:
|
|
@@ -276,13 +279,70 @@ def get_model_status_message() -> str:
|
|
| 276 |
|
| 277 |
def restore_ui_state():
|
| 278 |
"""Update UI components based on persistence"""
|
| 279 |
-
global model_loaded
|
| 280 |
msg = get_model_status_message()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
return (
|
| 282 |
-
msg,
|
| 283 |
-
gr.update(interactive=
|
| 284 |
-
gr.update(interactive=
|
| 285 |
-
gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
)
|
| 287 |
|
| 288 |
def should_use_lmdeploy(backbone_choice: str, device_choice: str) -> bool:
|
|
@@ -2382,11 +2442,7 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS", head=head_html) as demo
|
|
| 2382 |
names = list_user_voices(tts) if model_loaded else []
|
| 2383 |
return gr.update(choices=names, value=names[0] if names else None)
|
| 2384 |
|
| 2385 |
-
def _after_model_load():
|
| 2386 |
-
return gr.update(interactive=bool(model_loaded)), _user_voice_choices()
|
| 2387 |
-
|
| 2388 |
btn_load.click(lambda: gr.update(interactive=False), outputs=btn_generate_clone)
|
| 2389 |
-
load_event.then(_after_model_load, outputs=[btn_generate_clone, user_voice_dd])
|
| 2390 |
|
| 2391 |
# --- PDF Upload Event Handlers ---
|
| 2392 |
def on_pdf_upload(pdf_file):
|
|
@@ -2437,16 +2493,33 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS", head=head_html) as demo
|
|
| 2437 |
btn_generate_conv.click(lambda: gr.update(interactive=True), outputs=btn_stop)
|
| 2438 |
conv_gen_event.then(lambda: gr.update(interactive=False), outputs=btn_stop)
|
| 2439 |
|
| 2440 |
-
# --- Auto-adjust Temperature on Tab Switch ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2441 |
conv_tab.select(
|
| 2442 |
-
fn=
|
| 2443 |
-
inputs=backbone_select,
|
| 2444 |
-
outputs=temperature_slider
|
| 2445 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2446 |
single_tab.select(
|
| 2447 |
-
fn=
|
| 2448 |
-
inputs=backbone_select,
|
| 2449 |
-
outputs=temperature_slider
|
| 2450 |
)
|
| 2451 |
tab_custom.select(
|
| 2452 |
fn=lambda bb: gr.update(value=0.8 if "v3" in (bb or "").lower() else default_temp),
|
|
@@ -2572,7 +2645,7 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS", head=head_html) as demo
|
|
| 2572 |
clone_gen_event.then(fn=on_audio_generated, inputs=[audio_output], outputs=[download_btn])
|
| 2573 |
|
| 2574 |
# --- SRT → speech ---
|
| 2575 |
-
def _srt_voices():
|
| 2576 |
"""The loaded model's presets for the SRT tab, fetched when the tab
|
| 2577 |
opens so the tab needs no plumbing into the model-load handler."""
|
| 2578 |
if not model_loaded or tts is None:
|
|
@@ -2581,13 +2654,15 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS", head=head_html) as demo
|
|
| 2581 |
voices = tts.list_preset_voices()
|
| 2582 |
except Exception:
|
| 2583 |
voices = []
|
|
|
|
| 2584 |
default_v = getattr(tts, "_default_voice", None)
|
| 2585 |
values = [v[1] if isinstance(v, tuple) else v for v in voices]
|
| 2586 |
if default_v not in values and values:
|
| 2587 |
default_v = values[0]
|
| 2588 |
-
|
|
|
|
| 2589 |
|
| 2590 |
-
srt_tab.select(_srt_voices, outputs=[srt_voice, btn_generate_srt])
|
| 2591 |
|
| 2592 |
def _srt_run(srt_path, voice_choice, keep_timing, fmt, use_batch_flag, batch_size):
|
| 2593 |
_STOP_EVENT.clear()
|
|
@@ -2612,11 +2687,28 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS", head=head_html) as demo
|
|
| 2612 |
outputs=[download_btn]
|
| 2613 |
)
|
| 2614 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2615 |
# Persistence: Restore UI state on load
|
| 2616 |
demo.load(
|
| 2617 |
fn=restore_ui_state,
|
| 2618 |
-
outputs=[
|
| 2619 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2620 |
|
| 2621 |
def main():
|
| 2622 |
# Cho phép override từ biến môi trường (hữu ích cho Docker)
|
|
|
|
| 27 |
# PuncNormalizer = sea_g2p.Normalizer luôn bật punc_norm=True.
|
| 28 |
from vieneu_utils.phonemize_text import PuncNormalizer as Normalizer
|
| 29 |
import gc
|
| 30 |
+
try:
|
| 31 |
+
import spaces
|
| 32 |
|
| 33 |
+
@spaces.GPU
|
| 34 |
+
def _zerogpu_warmup():
|
| 35 |
+
pass
|
| 36 |
+
except ImportError:
|
| 37 |
+
spaces = None
|
| 38 |
|
| 39 |
# PDF text extraction
|
| 40 |
try:
|
|
|
|
| 279 |
|
| 280 |
def restore_ui_state():
|
| 281 |
"""Update UI components based on persistence"""
|
| 282 |
+
global model_loaded, tts, current_backbone, PRESET_VOICES_CACHE, CONV_VOICES_CACHE
|
| 283 |
msg = get_model_status_message()
|
| 284 |
+
|
| 285 |
+
if not model_loaded or tts is None:
|
| 286 |
+
slot_no_updates = [gr.update(choices=[])] * MAX_SPEAKERS
|
| 287 |
+
default_bb = list(BACKBONE_CONFIGS.keys())[0] if BACKBONE_CONFIGS else ""
|
| 288 |
+
return (
|
| 289 |
+
msg,
|
| 290 |
+
gr.update(interactive=False), # btn_generate
|
| 291 |
+
gr.update(interactive=False), # btn_generate_conv
|
| 292 |
+
gr.update(interactive=False), # btn_stop
|
| 293 |
+
gr.update(choices=[], value=None, interactive=False), # voice_select
|
| 294 |
+
gr.update(visible=_supports_cloning(default_bb)), # tab_custom
|
| 295 |
+
gr.update(visible=False), # conv_tab
|
| 296 |
+
gr.update(choices=[], value=None), # srt_voice
|
| 297 |
+
gr.update(interactive=False), # btn_generate_srt
|
| 298 |
+
*slot_no_updates # speaker_voice_dds
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Populate voice caches if not already cached
|
| 302 |
+
if not PRESET_VOICES_CACHE:
|
| 303 |
+
try:
|
| 304 |
+
voices = tts.list_preset_voices()
|
| 305 |
+
except Exception:
|
| 306 |
+
voices = []
|
| 307 |
+
_sort_voices(tts, voices)
|
| 308 |
+
PRESET_VOICES_CACHE = voices
|
| 309 |
+
|
| 310 |
+
def _check_podcast(v_id):
|
| 311 |
+
val = tts._preset_voices.get(v_id, {}).get("podcast", True)
|
| 312 |
+
if isinstance(val, str):
|
| 313 |
+
return val.strip().lower() == "true"
|
| 314 |
+
return bool(val)
|
| 315 |
+
|
| 316 |
+
CONV_VOICES_CACHE = [v for v in voices if _check_podcast(v[1] if isinstance(v, tuple) else v)]
|
| 317 |
+
|
| 318 |
+
default_v = getattr(tts, "_default_voice", None)
|
| 319 |
+
values = [v[1] if isinstance(v, tuple) else v for v in PRESET_VOICES_CACHE]
|
| 320 |
+
if not default_v and values:
|
| 321 |
+
default_v = values[0]
|
| 322 |
+
|
| 323 |
+
voice_update = gr.update(choices=PRESET_VOICES_CACHE, value=default_v, interactive=True)
|
| 324 |
+
tab_custom_update = gr.update(visible=_supports_cloning(current_backbone or (list(BACKBONE_CONFIGS.keys())[0] if BACKBONE_CONFIGS else "")))
|
| 325 |
+
|
| 326 |
+
is_v2 = (current_backbone == "VieNeu-TTS-v2 (GPU)" or current_backbone == "VieNeu-TTS-v2 (CPU)")
|
| 327 |
+
is_v3_conv = "v3" in (current_backbone or "").lower()
|
| 328 |
+
conv_tab_update = gr.update(visible=is_v2 or is_v3_conv)
|
| 329 |
+
|
| 330 |
+
slot_updates = [gr.update(choices=CONV_VOICES_CACHE)] * MAX_SPEAKERS
|
| 331 |
+
|
| 332 |
+
srt_voice_update = gr.update(choices=PRESET_VOICES_CACHE, value=default_v)
|
| 333 |
+
btn_srt_update = gr.update(interactive=bool(values and hasattr(tts, "infer_batch")))
|
| 334 |
+
|
| 335 |
return (
|
| 336 |
+
msg,
|
| 337 |
+
gr.update(interactive=True), # btn_generate
|
| 338 |
+
gr.update(interactive=True), # btn_generate_conv
|
| 339 |
+
gr.update(interactive=False), # btn_stop
|
| 340 |
+
voice_update, # voice_select
|
| 341 |
+
tab_custom_update, # tab_custom
|
| 342 |
+
conv_tab_update, # conv_tab
|
| 343 |
+
srt_voice_update, # srt_voice
|
| 344 |
+
btn_srt_update, # btn_generate_srt
|
| 345 |
+
*slot_updates, # speaker_voice_dds
|
| 346 |
)
|
| 347 |
|
| 348 |
def should_use_lmdeploy(backbone_choice: str, device_choice: str) -> bool:
|
|
|
|
| 2442 |
names = list_user_voices(tts) if model_loaded else []
|
| 2443 |
return gr.update(choices=names, value=names[0] if names else None)
|
| 2444 |
|
|
|
|
|
|
|
|
|
|
| 2445 |
btn_load.click(lambda: gr.update(interactive=False), outputs=btn_generate_clone)
|
|
|
|
| 2446 |
|
| 2447 |
# --- PDF Upload Event Handlers ---
|
| 2448 |
def on_pdf_upload(pdf_file):
|
|
|
|
| 2493 |
btn_generate_conv.click(lambda: gr.update(interactive=True), outputs=btn_stop)
|
| 2494 |
conv_gen_event.then(lambda: gr.update(interactive=False), outputs=btn_stop)
|
| 2495 |
|
| 2496 |
+
# --- Auto-adjust Temperature and ensure voices on Tab Switch ---
|
| 2497 |
+
def _on_conv_tab_select(bb):
|
| 2498 |
+
temp_val = 0.8 if "v3" in (bb or "").lower() else 1.0
|
| 2499 |
+
slots = [gr.update(choices=CONV_VOICES_CACHE)] * MAX_SPEAKERS if (model_loaded and CONV_VOICES_CACHE) else [gr.update()] * MAX_SPEAKERS
|
| 2500 |
+
return gr.update(value=temp_val), *slots
|
| 2501 |
+
|
| 2502 |
conv_tab.select(
|
| 2503 |
+
fn=_on_conv_tab_select,
|
| 2504 |
+
inputs=[backbone_select],
|
| 2505 |
+
outputs=[temperature_slider, *speaker_voice_dds]
|
| 2506 |
)
|
| 2507 |
+
|
| 2508 |
+
def _on_single_tab_select(bb, current_voice):
|
| 2509 |
+
temp_val = 0.8 if "v3" in (bb or "").lower() else default_temp
|
| 2510 |
+
if model_loaded and PRESET_VOICES_CACHE:
|
| 2511 |
+
default_v = getattr(tts, "_default_voice", None)
|
| 2512 |
+
values = [v[1] if isinstance(v, tuple) else v for v in PRESET_VOICES_CACHE]
|
| 2513 |
+
if not default_v and values:
|
| 2514 |
+
default_v = values[0]
|
| 2515 |
+
val = current_voice if (current_voice and current_voice in values) else default_v
|
| 2516 |
+
return gr.update(value=temp_val), gr.update(choices=PRESET_VOICES_CACHE, value=val, interactive=True)
|
| 2517 |
+
return gr.update(value=temp_val), gr.update()
|
| 2518 |
+
|
| 2519 |
single_tab.select(
|
| 2520 |
+
fn=_on_single_tab_select,
|
| 2521 |
+
inputs=[backbone_select, voice_select],
|
| 2522 |
+
outputs=[temperature_slider, voice_select]
|
| 2523 |
)
|
| 2524 |
tab_custom.select(
|
| 2525 |
fn=lambda bb: gr.update(value=0.8 if "v3" in (bb or "").lower() else default_temp),
|
|
|
|
| 2645 |
clone_gen_event.then(fn=on_audio_generated, inputs=[audio_output], outputs=[download_btn])
|
| 2646 |
|
| 2647 |
# --- SRT → speech ---
|
| 2648 |
+
def _srt_voices(current_choice=None):
|
| 2649 |
"""The loaded model's presets for the SRT tab, fetched when the tab
|
| 2650 |
opens so the tab needs no plumbing into the model-load handler."""
|
| 2651 |
if not model_loaded or tts is None:
|
|
|
|
| 2654 |
voices = tts.list_preset_voices()
|
| 2655 |
except Exception:
|
| 2656 |
voices = []
|
| 2657 |
+
_sort_voices(tts, voices)
|
| 2658 |
default_v = getattr(tts, "_default_voice", None)
|
| 2659 |
values = [v[1] if isinstance(v, tuple) else v for v in voices]
|
| 2660 |
if default_v not in values and values:
|
| 2661 |
default_v = values[0]
|
| 2662 |
+
val = current_choice if (current_choice and current_choice in values) else default_v
|
| 2663 |
+
return gr.update(choices=voices, value=val), gr.update(interactive=bool(values) and hasattr(tts, "infer_batch"))
|
| 2664 |
|
| 2665 |
+
srt_tab.select(_srt_voices, inputs=[srt_voice], outputs=[srt_voice, btn_generate_srt])
|
| 2666 |
|
| 2667 |
def _srt_run(srt_path, voice_choice, keep_timing, fmt, use_batch_flag, batch_size):
|
| 2668 |
_STOP_EVENT.clear()
|
|
|
|
| 2687 |
outputs=[download_btn]
|
| 2688 |
)
|
| 2689 |
|
| 2690 |
+
def _after_model_load():
|
| 2691 |
+
srt_v, srt_btn = _srt_voices()
|
| 2692 |
+
return gr.update(interactive=bool(model_loaded)), _user_voice_choices(), srt_v, srt_btn
|
| 2693 |
+
|
| 2694 |
+
load_event.then(_after_model_load, outputs=[btn_generate_clone, user_voice_dd, srt_voice, btn_generate_srt])
|
| 2695 |
+
|
| 2696 |
# Persistence: Restore UI state on load
|
| 2697 |
demo.load(
|
| 2698 |
fn=restore_ui_state,
|
| 2699 |
+
outputs=[
|
| 2700 |
+
model_status,
|
| 2701 |
+
btn_generate,
|
| 2702 |
+
btn_generate_conv,
|
| 2703 |
+
btn_stop,
|
| 2704 |
+
voice_select,
|
| 2705 |
+
tab_custom,
|
| 2706 |
+
conv_tab,
|
| 2707 |
+
srt_voice,
|
| 2708 |
+
btn_generate_srt,
|
| 2709 |
+
*speaker_voice_dds
|
| 2710 |
+
]
|
| 2711 |
+
).then(_after_model_load, outputs=[btn_generate_clone, user_voice_dd, srt_voice, btn_generate_srt])
|
| 2712 |
|
| 2713 |
def main():
|
| 2714 |
# Cho phép override từ biến môi trường (hữu ích cho Docker)
|
apps/gradio_xpu.py
CHANGED
|
@@ -111,12 +111,30 @@ def get_model_status_message() -> str:
|
|
| 111 |
|
| 112 |
def restore_ui_state():
|
| 113 |
"""Update UI components based on persistence"""
|
| 114 |
-
global model_loaded
|
| 115 |
msg = get_model_status_message()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return (
|
| 117 |
msg,
|
| 118 |
gr.update(interactive=model_loaded), # btn_generate
|
| 119 |
-
gr.update(interactive=False)
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
def load_model(backbone_choice: str, codec_choice: str, device_choice: str,
|
|
@@ -826,7 +844,7 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS (XPU)", head=head_html) a
|
|
| 826 |
|
| 827 |
demo.load(
|
| 828 |
fn=restore_ui_state,
|
| 829 |
-
outputs=[model_status, btn_generate, btn_stop]
|
| 830 |
)
|
| 831 |
|
| 832 |
def main():
|
|
|
|
| 111 |
|
| 112 |
def restore_ui_state():
|
| 113 |
"""Update UI components based on persistence"""
|
| 114 |
+
global model_loaded, tts
|
| 115 |
msg = get_model_status_message()
|
| 116 |
+
if not model_loaded or tts is None:
|
| 117 |
+
voice_update = gr.update(choices=[], value=None, interactive=False)
|
| 118 |
+
else:
|
| 119 |
+
try:
|
| 120 |
+
voices = tts.list_preset_voices()
|
| 121 |
+
except Exception:
|
| 122 |
+
voices = []
|
| 123 |
+
default_v = getattr(tts, "_default_voice", None)
|
| 124 |
+
is_tuple = (len(voices) > 0 and isinstance(voices[0], tuple))
|
| 125 |
+
voice_values = [v[1] for v in voices] if is_tuple else voices
|
| 126 |
+
if not default_v and voice_values:
|
| 127 |
+
default_v = voice_values[0]
|
| 128 |
+
if is_tuple:
|
| 129 |
+
voices.sort(key=lambda x: str(x[0]))
|
| 130 |
+
else:
|
| 131 |
+
voices.sort()
|
| 132 |
+
voice_update = gr.update(choices=voices, value=default_v, interactive=True)
|
| 133 |
return (
|
| 134 |
msg,
|
| 135 |
gr.update(interactive=model_loaded), # btn_generate
|
| 136 |
+
gr.update(interactive=False), # btn_stop
|
| 137 |
+
voice_update # voice_select
|
| 138 |
)
|
| 139 |
|
| 140 |
def load_model(backbone_choice: str, codec_choice: str, device_choice: str,
|
|
|
|
| 844 |
|
| 845 |
demo.load(
|
| 846 |
fn=restore_ui_state,
|
| 847 |
+
outputs=[model_status, btn_generate, btn_stop, voice_select]
|
| 848 |
)
|
| 849 |
|
| 850 |
def main():
|