| import gradio as gr |
| from TTS.api import TTS |
| import numpy as np |
| import os |
|
|
| |
| 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 = 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, "请输入有效的中文文本" |
| |
| |
| wav = tts.tts(text=text, language="zh-cn") |
| |
| |
| output_path = "output.wav" |
| tts.save_wav(wav, output_path) |
| |
| return output_path, "语音生成成功" |
| except Exception as e: |
| return None, f"生成失败,可能由于文本内容不合适或模型限制: {str(e)}" |
|
|
| |
| 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() |