tts / app.py
clown145's picture
Update app.py
284e7f7 verified
raw
history blame contribute delete
941 Bytes
import gradio as gr
from melo.api import TTS
import torch
# 设定设备
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
# 加载模型
model = TTS(language='ZH', device=device)
speaker_ids = model.hps.data.spk2id
def text_to_speech(text, speed):
# 从模型生成音频
output_path = 'output.wav'
model.tts_to_file(text, speaker_ids['ZH'], output_path, speed=speed)
return output_path
# 创建 Gradio 界面
iface = gr.Interface(
fn=text_to_speech,
inputs=[
gr.Textbox(label="输入中文文本", value="我最近在学习机器学习,希望能够在未来的人工智能领域有所建树。"),
gr.Slider(minimum=0.5, maximum=1.5, step=0.1, value=1.0, label="语速")
],
outputs=gr.Audio(label="生成的语音"),
title="中文文本转语音",
description="输入一段中文文本,然后点击“Submit”按钮来生成语音。"
)
# 启动应用
iface.launch()