| | 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 |
| |
|
| | |
| | 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() |