|
|
""" |
|
|
Genie TTS Hugging Face Spaces Deployment - Main Application |
|
|
重构后的主应用文件,仅包含Gradio界面定义和应用启动逻辑 |
|
|
|
|
|
模块化重构后的架构: |
|
|
- installer.py: 依赖管理 |
|
|
- config.py: 配置常量 |
|
|
- tts_engine.py: TTS核心功能 |
|
|
- ui_utils.py: UI辅助函数 |
|
|
- app.py: 主应用界面(当前文件) |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import logging |
|
|
import warnings |
|
|
from tts_engine import tts_interface |
|
|
from ui_utils import clear_all, load_example, create_tts_wrapper, create_system_status_display |
|
|
from config import APP_TITLE, APP_DESCRIPTION, EXAMPLE_TEXTS, UI_CONFIG, UI_TEXT, GRADIO_THEME |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", category=FutureWarning) |
|
|
warnings.filterwarnings("ignore", category=UserWarning) |
|
|
|
|
|
|
|
|
tts_wrapper = create_tts_wrapper(tts_interface) |
|
|
get_system_status = create_system_status_display(tts_interface) |
|
|
|
|
|
|
|
|
def create_interface(): |
|
|
"""创建Gradio界面""" |
|
|
|
|
|
|
|
|
with gr.Blocks( |
|
|
title=APP_TITLE, |
|
|
theme=gr.themes.Soft(), |
|
|
css=""" |
|
|
.gradio-container { |
|
|
max-width: 1200px !important; |
|
|
} |
|
|
.status-success { |
|
|
color: #28a745 !important; |
|
|
} |
|
|
.status-error { |
|
|
color: #dc3545 !important; |
|
|
} |
|
|
""" |
|
|
) as demo: |
|
|
gr.Markdown(f""" |
|
|
# {APP_TITLE} |
|
|
|
|
|
{APP_DESCRIPTION} |
|
|
|
|
|
<div style="background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); padding: 1rem; border-radius: 10px; color: white; margin: 1rem 0;"> |
|
|
<strong>🌟 功能特点</strong><br> |
|
|
✅ CPU 优化推理,无需 GPU<br> |
|
|
✅ 基于 GPT-SoVITS V2 技术<br> |
|
|
✅ 支持长文本自动分句<br> |
|
|
✅ 实时音频流输出 |
|
|
</div> |
|
|
|
|
|
**📖 使用说明:** 选择角色模型 → 输入日语文本 → 点击合成按钮 → 获得高质量语音 |
|
|
""") |
|
|
|
|
|
|
|
|
system_status = get_system_status() |
|
|
if "🔴" in system_status: |
|
|
status_color = "#ff4444" |
|
|
status_text = "服务不可用" |
|
|
details = ("Hugging Face Spaces环境限制导致PyAudio依赖安装失败。<br>" |
|
|
"💡 <strong>解决方案:</strong> 请在本地环境运行此应用以获得完整功能。") |
|
|
else: |
|
|
status_color = "#44ff44" |
|
|
status_text = "服务正常" |
|
|
details = "Genie TTS引擎已成功加载,可以正常使用。" |
|
|
|
|
|
gr.Markdown(f""" |
|
|
<div style="background: {status_color}20; border-left: 4px solid {status_color}; padding: 1rem; margin: 1rem 0; border-radius: 0 8px 8px 0;"> |
|
|
<strong>{system_status}</strong><br> |
|
|
<small>{details}</small> |
|
|
</div> |
|
|
""") |
|
|
|
|
|
with gr.Tab("🎵 语音合成") as tts_tab: |
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
|
|
|
with gr.Group(): |
|
|
gr.Markdown("### 👤 角色设置") |
|
|
character_dropdown = gr.Dropdown( |
|
|
choices=tts_interface.available_characters, |
|
|
value=tts_interface.available_characters[0], |
|
|
label=UI_TEXT["character_label"], |
|
|
info="当前可用的预训练角色模型", |
|
|
interactive=True |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Group(): |
|
|
gr.Markdown("### 📝 文本输入") |
|
|
text_input = gr.Textbox( |
|
|
lines=5, |
|
|
label=UI_TEXT["text_label"], |
|
|
placeholder=UI_TEXT["text_placeholder"], |
|
|
info="💡 支持日语文本,建议输入完整的句子以获得更好的效果", |
|
|
show_copy_button=True |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
submit_btn = gr.Button( |
|
|
UI_TEXT["submit_button"], |
|
|
variant="primary", |
|
|
size="lg", |
|
|
scale=2 |
|
|
) |
|
|
clear_btn = gr.Button( |
|
|
UI_TEXT["clear_button"], |
|
|
variant="secondary", |
|
|
scale=1 |
|
|
) |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
|
|
|
with gr.Group(): |
|
|
gr.Markdown("### 🔊 音频输出") |
|
|
audio_output = gr.Audio( |
|
|
label=UI_TEXT["audio_label"], |
|
|
type="filepath", |
|
|
interactive=False, |
|
|
show_download_button=True |
|
|
) |
|
|
|
|
|
|
|
|
status_output = gr.Textbox( |
|
|
label=UI_TEXT["status_label"], |
|
|
interactive=False, |
|
|
show_copy_button=False |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Tab("📚 示例与教程") as examples_tab: |
|
|
gr.Markdown("### 🎯 快速示例") |
|
|
gr.Markdown("点击下面的示例可以快速体验不同类型的文本合成效果:") |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
gr.Markdown("**🌅 问候语**") |
|
|
gr.Examples( |
|
|
examples=EXAMPLE_TEXTS[:3], |
|
|
inputs=[text_input, character_dropdown], |
|
|
outputs=[text_input, character_dropdown, status_output], |
|
|
fn=load_example, |
|
|
run_on_click=True |
|
|
) |
|
|
|
|
|
with gr.Column(): |
|
|
gr.Markdown("**💭 情感表达**") |
|
|
gr.Examples( |
|
|
examples=[ |
|
|
["どうしようかな……やっぱりやりたいかも……!", "misono_mika"], |
|
|
["うーん、これは難しいですね", "misono_mika"], |
|
|
["わあ、すごいですね!", "misono_mika"] |
|
|
], |
|
|
inputs=[text_input, character_dropdown], |
|
|
outputs=[text_input, character_dropdown, status_output], |
|
|
fn=load_example, |
|
|
run_on_click=True |
|
|
) |
|
|
|
|
|
with gr.Column(): |
|
|
gr.Markdown("**🎭 日常对话**") |
|
|
gr.Examples( |
|
|
examples=[ |
|
|
["ありがとうございます", "misono_mika"], |
|
|
["さようなら、また明日", "misono_mika"], |
|
|
["お先に失礼します", "misono_mika"] |
|
|
], |
|
|
inputs=[text_input, character_dropdown], |
|
|
outputs=[text_input, character_dropdown, status_output], |
|
|
fn=load_example, |
|
|
run_on_click=True |
|
|
) |
|
|
|
|
|
gr.Markdown(""" |
|
|
### 📋 使用技巧 |
|
|
|
|
|
1. **文本长度**: 建议单次输入文本长度在 100 字以内,过长的文本会自动分句处理 |
|
|
2. **标点符号**: 适当使用标点符号(。!?)可以改善语音的自然度 |
|
|
3. **特殊符号**: 支持省略号(……)和感叹号(!)等情感表达 |
|
|
4. **处理时间**: 首次加载角色需要下载模型(约30秒),后续合成较快(5-10秒) |
|
|
|
|
|
### 🔧 技术说明 |
|
|
|
|
|
- **模型架构**: 基于 Transformer 的端到端语音合成 |
|
|
- **采样率**: 32kHz,支持高质量音频输出 |
|
|
- **推理方式**: CPU 优化的 ONNX 模型,适合云端部署 |
|
|
- **内存占用**: 约 500MB RAM,支持并发处理 |
|
|
""") |
|
|
|
|
|
|
|
|
with gr.Tab("ℹ️ 关于项目") as about_tab: |
|
|
gr.Markdown(""" |
|
|
### 🔍 项目信息 |
|
|
|
|
|
**Genie TTS** 是基于 GPT-SoVITS V2 架构的轻量级语音合成引擎,专门为 CPU 推理优化。 |
|
|
|
|
|
#### 📊 技术规格 |
|
|
|
|
|
| 项目 | 规格 | |
|
|
|------|------| |
|
|
| **基础模型** | GPT-SoVITS V2 | |
|
|
| **推理框架** | ONNX Runtime | |
|
|
| **支持语言** | 日语 (Japanese) | |
|
|
| **音频格式** | WAV, 32kHz | |
|
|
| **推理设备** | CPU (无需 GPU) | |
|
|
| **模型大小** | ~200MB | |
|
|
| **内存需求** | ~500MB RAM | |
|
|
|
|
|
#### 🔗 相关链接 |
|
|
|
|
|
- 🏠 [项目主页](https://github.com/High-Logic/Genie) |
|
|
- 🤗 [Hugging Face 模型](https://huggingface.co/High-Logic/Genie) |
|
|
- 📖 [GPT-SoVITS 官方](https://github.com/RVC-Boss/GPT-SoVITS) |
|
|
- 💬 [问题反馈](https://github.com/High-Logic/Genie/issues) |
|
|
|
|
|
#### 🙏 致谢 |
|
|
|
|
|
感谢以下项目和开发者: |
|
|
- [High-Logic](https://github.com/High-Logic) 团队开发的 Genie TTS |
|
|
- [RVC-Boss](https://github.com/RVC-Boss) 团队的 GPT-SoVITS 项目 |
|
|
- Hugging Face 提供的模型托管和 Spaces 平台 |
|
|
|
|
|
#### ⚖️ 免责声明 |
|
|
|
|
|
本应用仅用于演示和研究目的。请合理使用,生成的语音内容责任由使用者承担。 |
|
|
""") |
|
|
|
|
|
|
|
|
submit_btn.click( |
|
|
fn=tts_wrapper, |
|
|
inputs=[text_input, character_dropdown], |
|
|
outputs=[audio_output, status_output], |
|
|
show_progress="full", |
|
|
queue=True |
|
|
) |
|
|
|
|
|
clear_btn.click( |
|
|
fn=clear_all, |
|
|
outputs=[text_input, audio_output, status_output] |
|
|
) |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo = create_interface() |
|
|
|
|
|
|
|
|
launch_config = UI_CONFIG.copy() |
|
|
try: |
|
|
|
|
|
import gradio as gr |
|
|
if hasattr(gr.Blocks, 'launch') and 'theme' in gr.Blocks.launch.__code__.co_varnames: |
|
|
launch_config['theme'] = GRADIO_THEME |
|
|
except: |
|
|
|
|
|
pass |
|
|
|
|
|
demo.launch(**launch_config) |