Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForTextToSpeech, AutoTokenizer
|
| 3 |
+
import soundfile as sf
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# 加载模型和Tokenizer(自动下载SoulX模型,首次构建会慢一点)
|
| 8 |
+
model_name = "Soul-AILab/SoulX-Podcast-1.7B"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForTextToSpeech.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
torch_dtype=torch.float16, # 适配GPU,无GPU会自动切换CPU
|
| 13 |
+
device_map="auto" # 自动分配运行设备
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# 语音生成函数(对接Gradio界面)
|
| 17 |
+
def generate_speech(text):
|
| 18 |
+
if not text.strip():
|
| 19 |
+
return None, "错误:请输入有效文本!"
|
| 20 |
+
|
| 21 |
+
# 文本编码(模型要求的格式)
|
| 22 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 23 |
+
|
| 24 |
+
# 生成音频(核心逻辑)
|
| 25 |
+
with torch.no_grad(): # 关闭梯度计算,节省内存
|
| 26 |
+
audio_output = model.generate(**inputs)
|
| 27 |
+
|
| 28 |
+
# 保存音频文件(临时存储,Gradio会自动读取)
|
| 29 |
+
output_path = "output.wav"
|
| 30 |
+
sf.write(output_path, audio_output[0].cpu().numpy(), samplerate=24000)
|
| 31 |
+
|
| 32 |
+
return output_path, "语音生成成功!"
|
| 33 |
+
|
| 34 |
+
# 构建Gradio界面(可视化操作面板)
|
| 35 |
+
with gr.Blocks(title="SoulX-Podcast-1.7B 中英双语TTS") as demo:
|
| 36 |
+
gr.Markdown("# 🎤 SoulX-Podcast-1.7B 文本转语音")
|
| 37 |
+
gr.Markdown("支持中英双语输入,生成自然流畅的语音(采样率24000Hz)")
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
# 文本输入框
|
| 41 |
+
text_input = gr.Textbox(
|
| 42 |
+
label="输入文本",
|
| 43 |
+
placeholder="请输入要转换的文本(建议≤500字),支持中英双语...",
|
| 44 |
+
lines=5
|
| 45 |
+
)
|
| 46 |
+
# 音频输出框
|
| 47 |
+
audio_output = gr.Audio(label="生成的语音", type="filepath")
|
| 48 |
+
|
| 49 |
+
# 状态提示框
|
| 50 |
+
status_text = gr.Textbox(label="状态", interactive=False)
|
| 51 |
+
|
| 52 |
+
# 生成按钮
|
| 53 |
+
generate_btn = gr.Button("🚀 开始生成", variant="primary")
|
| 54 |
+
|
| 55 |
+
# 绑定按钮事件:点击后触发生成函数
|
| 56 |
+
generate_btn.click(
|
| 57 |
+
fn=generate_speech,
|
| 58 |
+
inputs=text_input,
|
| 59 |
+
outputs=[audio_output, status_text]
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# 启动应用(Hugging Face Space会自动运行)
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|