Spaces:
Runtime error
Runtime error
Download app.py from nguyenduchuyiu/tts: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/spaces/nguyenduchuyiu/tts/resolve/ed34a6eddc754b5b5a00a268279d59c1665a14f6/app.py
- Command line
-
hf download hf://spaces/nguyenduchuyiu/tts@ed34a6eddc754b5b5a00a268279d59c1665a14f6/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nguyenduchuyiu/tts/resolve/ed34a6eddc754b5b5a00a268279d59c1665a14f6/app.py
2.82 kB
| import os | |
| import sys | |
| import gradio as gr | |
| import logging | |
| # Thêm thư mục src vào path để import vieneu | |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "src"))) | |
| from vieneu import Vieneu | |
| # Cấu hình logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger("VieNeu-App") | |
| # Khởi tạo mô hình (Mặc định chạy CPU) | |
| logger.info("Đang khởi tạo mô hình VieNeu-TTS...") | |
| tts = Vieneu() | |
| # Lấy danh sách giọng nói | |
| voices = tts.list_preset_voices() | |
| voice_map = {desc: vid for desc, vid in voices} | |
| voice_names = list(voice_map.keys()) | |
| def generate_audio(text, voice_name): | |
| if not text.strip(): | |
| return None | |
| logger.info(f"Đang tạo giọng nói cho văn bản: {text[:50]}...") | |
| voice_id = voice_map.get(voice_name) | |
| voice_data = tts.get_preset_voice(voice_id) | |
| # Tạo âm thanh | |
| audio = tts.infer(text=text, voice=voice_data) | |
| # Lưu file tạm | |
| output_path = "output.wav" | |
| tts.save(audio, output_path) | |
| return output_path | |
| # CSS để làm chữ to và giao diện sạch sẽ | |
| custom_css = """ | |
| .big-text { font-size: 24px !important; } | |
| .instruction { font-size: 18px !important; margin-bottom: 10px; } | |
| button { font-size: 20px !important; min-height: 60px !important; } | |
| textarea { font-size: 20px !important; } | |
| .gradio-container { max-width: 800px !important; margin: auto !important; } | |
| """ | |
| with gr.Blocks(css=custom_css, title="VieNeu-TTS Siêu Đơn Giản") as demo: | |
| gr.Markdown("# 🦜 VieNeu-TTS - Chuyển văn bản thành giọng nói", elem_classes=["big-text"]) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### 1. Nhập chữ vào đây:", elem_classes=["instruction"]) | |
| input_text = gr.Textbox( | |
| label="", | |
| placeholder="Ví dụ: Chào bạn, hôm nay bạn thấy thế nào?", | |
| lines=5 | |
| ) | |
| gr.Markdown("### 2. Chọn giọng nói:", elem_classes=["instruction"]) | |
| voice_select = gr.Dropdown( | |
| choices=voice_names, | |
| value=voice_names[0] if voice_names else None, | |
| label="" | |
| ) | |
| generate_btn = gr.Button("🚀 NHẤN VÀO ĐÂY ĐỂ TẠO GIỌNG NÓI", variant="primary") | |
| with gr.Column(): | |
| gr.Markdown("### 3. Nghe và tải xuống:", elem_classes=["instruction"]) | |
| output_audio = gr.Audio( | |
| label="", | |
| type="filepath", | |
| interactive=False | |
| ) | |
| generate_btn.click( | |
| fn=generate_audio, | |
| inputs=[input_text, voice_select], | |
| outputs=output_audio | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) | |