audio_mod / app.py
lxcxjxhx's picture
Update app.py
248e10e verified
import gradio as gr
from TTS.api import TTS
import numpy as np
import os
# 设置TTS_HOME并创建tos_agreed文件以绕过许可提示
tts_home = os.path.expanduser("~/.tts")
os.makedirs(tts_home, exist_ok=True)
tos_agreed_file = os.path.join(tts_home, "tos_agreed")
if not os.path.exists(tos_agreed_file):
with open(tos_agreed_file, "w") as f:
f.write("I agree to the terms of the non-commercial CPML: https://coqui.ai/cpml")
# 初始化TTS模型,使用支持中文的XTTS模型
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", progress_bar=False, gpu=False)
def text_to_speech(text):
try:
# 确保输入文本是字符串
if not isinstance(text, str):
return None, "请输入有效的中文文本"
# 生成语音,指定语言为zh-cn
wav = tts.tts(text=text, language="zh-cn")
# 保存为wav文件
output_path = "output.wav"
tts.save_wav(wav, output_path)
return output_path, "语音生成成功"
except Exception as e:
return None, f"生成失败,可能由于文本内容不合适或模型限制: {str(e)}"
# Gradio界面
with gr.Blocks() as demo:
gr.Markdown("# 中文语音生成器 _
gr.Markdown("输入中文文本,生成对应的语音。")
with gr.Row():
text_input = gr.Textbox(label="输入中文文本", placeholder="请输入要转换的中文文本...")
submit_button = gr.Button("生成语音")
with gr.Row():
audio_output = gr.Audio(label="生成的语音")
status_output = gr.Textbox(label="状态")
submit_button.click(
fn=text_to_speech,
inputs=text_input,
outputs=[audio_output, status_output]
)
# 启动Gradio应用
demo.launch()