Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,7 +30,6 @@ except Exception as e:
|
|
| 30 |
def encode_reference(self, path): return None
|
| 31 |
def infer(self, text, ref, ref_text):
|
| 32 |
import numpy as np
|
| 33 |
-
# Giả lập độ trễ để test tính năng đo thời gian
|
| 34 |
time.sleep(1.5)
|
| 35 |
return np.random.uniform(-0.5, 0.5, 24000*3)
|
| 36 |
tts = MockTTS()
|
|
@@ -65,23 +64,24 @@ def load_reference_info(voice_choice):
|
|
| 65 |
return None, ""
|
| 66 |
|
| 67 |
@spaces.GPU(duration=120)
|
| 68 |
-
def synthesize_speech(text, voice_choice, custom_audio, custom_text,
|
| 69 |
try:
|
| 70 |
if not text or text.strip() == "":
|
| 71 |
return None, "⚠️ Vui lòng nhập văn bản cần tổng hợp!"
|
| 72 |
|
| 73 |
-
# --- LOGIC CHECK LIMIT 250 ---
|
| 74 |
if len(text) > 250:
|
| 75 |
return None, f"❌ Văn bản quá dài ({len(text)}/250 ký tự)! Vui lòng cắt ngắn lại để đảm bảo chất lượng."
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
if custom_audio is None or not custom_text:
|
| 80 |
return None, "⚠️ Vui lòng tải lên Audio và nhập nội dung Audio đó."
|
| 81 |
ref_audio_path = custom_audio
|
| 82 |
ref_text_raw = custom_text
|
| 83 |
print("🎨 Mode: Custom Voice")
|
| 84 |
-
else:
|
| 85 |
if voice_choice not in VOICE_SAMPLES:
|
| 86 |
return None, "⚠️ Vui lòng chọn một giọng mẫu."
|
| 87 |
ref_audio_path = VOICE_SAMPLES[voice_choice]["audio"]
|
|
@@ -94,7 +94,6 @@ def synthesize_speech(text, voice_choice, custom_audio, custom_text, use_custom_
|
|
| 94 |
ref_text_raw = f.read()
|
| 95 |
print(f"🎤 Mode: Preset Voice ({voice_choice})")
|
| 96 |
|
| 97 |
-
# Inference & Đo thời gian
|
| 98 |
print(f"📝 Text: {text[:50]}...")
|
| 99 |
|
| 100 |
start_time = time.time()
|
|
@@ -105,7 +104,6 @@ def synthesize_speech(text, voice_choice, custom_audio, custom_text, use_custom_
|
|
| 105 |
end_time = time.time()
|
| 106 |
process_time = end_time - start_time
|
| 107 |
|
| 108 |
-
# Save
|
| 109 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 110 |
sf.write(tmp_file.name, wav, 24000)
|
| 111 |
output_path = tmp_file.name
|
|
@@ -162,7 +160,6 @@ css = """
|
|
| 162 |
transition: color 0.2s;
|
| 163 |
}
|
| 164 |
.link-group a:hover { color: #38bdf8; text-shadow: 0 0 5px rgba(56, 189, 248, 0.5); }
|
| 165 |
-
|
| 166 |
.status-box { font-weight: bold; text-align: center; border: none; background: transparent; }
|
| 167 |
"""
|
| 168 |
|
|
@@ -180,7 +177,6 @@ EXAMPLES_LIST = [
|
|
| 180 |
with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS Studio") as demo:
|
| 181 |
|
| 182 |
with gr.Column(elem_classes="container"):
|
| 183 |
-
# Header
|
| 184 |
gr.HTML("""
|
| 185 |
<div class="header-box">
|
| 186 |
<div class="header-title">🎙️ VieNeu-TTS Studio</div>
|
|
@@ -208,20 +204,17 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS Studio") as demo:
|
|
| 208 |
show_label=False
|
| 209 |
)
|
| 210 |
|
| 211 |
-
# Counter
|
| 212 |
with gr.Row():
|
| 213 |
char_count = gr.HTML("<div style='text-align: right; color: #64748B; font-size: 0.8rem;'>0 / 250 ký tự</div>")
|
| 214 |
|
| 215 |
gr.Markdown("### 🗣️ Chọn giọng đọc")
|
| 216 |
|
| 217 |
-
# SỬA LỖI: Dùng Radio thay vì Tabs để tránh lỗi internal Gradio
|
| 218 |
voice_mode = gr.Radio(
|
| 219 |
choices=["👤 Giọng có sẵn (Preset)", "🎙️ Giọng tùy chỉnh (Custom)"],
|
| 220 |
value="👤 Giọng có sẵn (Preset)",
|
| 221 |
label="Chế độ"
|
| 222 |
)
|
| 223 |
|
| 224 |
-
# Preset controls
|
| 225 |
with gr.Group(visible=True) as preset_group:
|
| 226 |
voice_select = gr.Dropdown(
|
| 227 |
choices=list(VOICE_SAMPLES.keys()),
|
|
@@ -233,13 +226,11 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS Studio") as demo:
|
|
| 233 |
ref_audio_preview = gr.Audio(label="Audio mẫu", interactive=False, type="filepath")
|
| 234 |
ref_text_preview = gr.Markdown("...")
|
| 235 |
|
| 236 |
-
# Custom controls
|
| 237 |
with gr.Group(visible=False) as custom_group:
|
| 238 |
gr.Markdown("Tải lên giọng của bạn (Zero-shot Cloning)")
|
| 239 |
custom_audio = gr.Audio(label="File ghi âm (.wav)", type="filepath")
|
| 240 |
custom_text = gr.Textbox(label="Nội dung ghi âm", placeholder="Nhập chính xác lời thoại...")
|
| 241 |
|
| 242 |
-
use_custom_voice = gr.Checkbox(value=False, visible=False)
|
| 243 |
btn_generate = gr.Button("🎵 Tổng hợp giọng nói", variant="primary", size="lg")
|
| 244 |
|
| 245 |
# --- RIGHT: OUTPUT ---
|
|
@@ -278,30 +269,24 @@ with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS Studio") as demo:
|
|
| 278 |
voice_select.change(update_ref_preview, voice_select, [ref_audio_preview, ref_text_preview])
|
| 279 |
demo.load(update_ref_preview, voice_select, [ref_audio_preview, ref_text_preview])
|
| 280 |
|
| 281 |
-
# Voice mode switching
|
| 282 |
def toggle_voice_mode(mode):
|
| 283 |
is_custom = (mode == "🎙️ Giọng tùy chỉnh (Custom)")
|
| 284 |
-
return (
|
| 285 |
-
gr.update(visible=not is_custom), # preset_group
|
| 286 |
-
gr.update(visible=is_custom), # custom_group
|
| 287 |
-
is_custom # use_custom_voice
|
| 288 |
-
)
|
| 289 |
|
| 290 |
voice_mode.change(
|
| 291 |
fn=toggle_voice_mode,
|
| 292 |
inputs=[voice_mode],
|
| 293 |
-
outputs=[preset_group, custom_group
|
| 294 |
)
|
| 295 |
|
| 296 |
btn_generate.click(
|
| 297 |
fn=synthesize_speech,
|
| 298 |
-
inputs=[text_input, voice_select, custom_audio, custom_text,
|
| 299 |
outputs=[audio_output, status_output]
|
| 300 |
)
|
| 301 |
|
| 302 |
if __name__ == "__main__":
|
| 303 |
demo.queue().launch(
|
| 304 |
server_name="0.0.0.0",
|
| 305 |
-
server_port=7860
|
| 306 |
-
share=True # QUAN TRỌNG: Thêm share=True cho HF Spaces
|
| 307 |
)
|
|
|
|
| 30 |
def encode_reference(self, path): return None
|
| 31 |
def infer(self, text, ref, ref_text):
|
| 32 |
import numpy as np
|
|
|
|
| 33 |
time.sleep(1.5)
|
| 34 |
return np.random.uniform(-0.5, 0.5, 24000*3)
|
| 35 |
tts = MockTTS()
|
|
|
|
| 64 |
return None, ""
|
| 65 |
|
| 66 |
@spaces.GPU(duration=120)
|
| 67 |
+
def synthesize_speech(text, voice_choice, custom_audio, custom_text, voice_mode):
|
| 68 |
try:
|
| 69 |
if not text or text.strip() == "":
|
| 70 |
return None, "⚠️ Vui lòng nhập văn bản cần tổng hợp!"
|
| 71 |
|
|
|
|
| 72 |
if len(text) > 250:
|
| 73 |
return None, f"❌ Văn bản quá dài ({len(text)}/250 ký tự)! Vui lòng cắt ngắn lại để đảm bảo chất lượng."
|
| 74 |
|
| 75 |
+
# Xác định mode dựa vào voice_mode string
|
| 76 |
+
use_custom = (voice_mode == "🎙️ Giọng tùy chỉnh (Custom)")
|
| 77 |
+
|
| 78 |
+
if use_custom:
|
| 79 |
if custom_audio is None or not custom_text:
|
| 80 |
return None, "⚠️ Vui lòng tải lên Audio và nhập nội dung Audio đó."
|
| 81 |
ref_audio_path = custom_audio
|
| 82 |
ref_text_raw = custom_text
|
| 83 |
print("🎨 Mode: Custom Voice")
|
| 84 |
+
else:
|
| 85 |
if voice_choice not in VOICE_SAMPLES:
|
| 86 |
return None, "⚠️ Vui lòng chọn một giọng mẫu."
|
| 87 |
ref_audio_path = VOICE_SAMPLES[voice_choice]["audio"]
|
|
|
|
| 94 |
ref_text_raw = f.read()
|
| 95 |
print(f"🎤 Mode: Preset Voice ({voice_choice})")
|
| 96 |
|
|
|
|
| 97 |
print(f"📝 Text: {text[:50]}...")
|
| 98 |
|
| 99 |
start_time = time.time()
|
|
|
|
| 104 |
end_time = time.time()
|
| 105 |
process_time = end_time - start_time
|
| 106 |
|
|
|
|
| 107 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 108 |
sf.write(tmp_file.name, wav, 24000)
|
| 109 |
output_path = tmp_file.name
|
|
|
|
| 160 |
transition: color 0.2s;
|
| 161 |
}
|
| 162 |
.link-group a:hover { color: #38bdf8; text-shadow: 0 0 5px rgba(56, 189, 248, 0.5); }
|
|
|
|
| 163 |
.status-box { font-weight: bold; text-align: center; border: none; background: transparent; }
|
| 164 |
"""
|
| 165 |
|
|
|
|
| 177 |
with gr.Blocks(theme=theme, css=css, title="VieNeu-TTS Studio") as demo:
|
| 178 |
|
| 179 |
with gr.Column(elem_classes="container"):
|
|
|
|
| 180 |
gr.HTML("""
|
| 181 |
<div class="header-box">
|
| 182 |
<div class="header-title">🎙️ VieNeu-TTS Studio</div>
|
|
|
|
| 204 |
show_label=False
|
| 205 |
)
|
| 206 |
|
|
|
|
| 207 |
with gr.Row():
|
| 208 |
char_count = gr.HTML("<div style='text-align: right; color: #64748B; font-size: 0.8rem;'>0 / 250 ký tự</div>")
|
| 209 |
|
| 210 |
gr.Markdown("### 🗣️ Chọn giọng đọc")
|
| 211 |
|
|
|
|
| 212 |
voice_mode = gr.Radio(
|
| 213 |
choices=["👤 Giọng có sẵn (Preset)", "🎙️ Giọng tùy chỉnh (Custom)"],
|
| 214 |
value="👤 Giọng có sẵn (Preset)",
|
| 215 |
label="Chế độ"
|
| 216 |
)
|
| 217 |
|
|
|
|
| 218 |
with gr.Group(visible=True) as preset_group:
|
| 219 |
voice_select = gr.Dropdown(
|
| 220 |
choices=list(VOICE_SAMPLES.keys()),
|
|
|
|
| 226 |
ref_audio_preview = gr.Audio(label="Audio mẫu", interactive=False, type="filepath")
|
| 227 |
ref_text_preview = gr.Markdown("...")
|
| 228 |
|
|
|
|
| 229 |
with gr.Group(visible=False) as custom_group:
|
| 230 |
gr.Markdown("Tải lên giọng của bạn (Zero-shot Cloning)")
|
| 231 |
custom_audio = gr.Audio(label="File ghi âm (.wav)", type="filepath")
|
| 232 |
custom_text = gr.Textbox(label="Nội dung ghi âm", placeholder="Nhập chính xác lời thoại...")
|
| 233 |
|
|
|
|
| 234 |
btn_generate = gr.Button("🎵 Tổng hợp giọng nói", variant="primary", size="lg")
|
| 235 |
|
| 236 |
# --- RIGHT: OUTPUT ---
|
|
|
|
| 269 |
voice_select.change(update_ref_preview, voice_select, [ref_audio_preview, ref_text_preview])
|
| 270 |
demo.load(update_ref_preview, voice_select, [ref_audio_preview, ref_text_preview])
|
| 271 |
|
|
|
|
| 272 |
def toggle_voice_mode(mode):
|
| 273 |
is_custom = (mode == "🎙️ Giọng tùy chỉnh (Custom)")
|
| 274 |
+
return gr.update(visible=not is_custom), gr.update(visible=is_custom)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
voice_mode.change(
|
| 277 |
fn=toggle_voice_mode,
|
| 278 |
inputs=[voice_mode],
|
| 279 |
+
outputs=[preset_group, custom_group]
|
| 280 |
)
|
| 281 |
|
| 282 |
btn_generate.click(
|
| 283 |
fn=synthesize_speech,
|
| 284 |
+
inputs=[text_input, voice_select, custom_audio, custom_text, voice_mode],
|
| 285 |
outputs=[audio_output, status_output]
|
| 286 |
)
|
| 287 |
|
| 288 |
if __name__ == "__main__":
|
| 289 |
demo.queue().launch(
|
| 290 |
server_name="0.0.0.0",
|
| 291 |
+
server_port=7860
|
|
|
|
| 292 |
)
|