import gradio as gr import torch from pathlib import Path from .voice_clone import VoiceCloneSystem # 初始化系统 system = VoiceCloneSystem(device="cpu" if not torch.cuda.is_available() else "cuda") def clone_voice(text: str, reference_audio) -> str: """ 克隆语音的 Gradio 接口函数 Args: text: 要转换的文本 reference_audio: 参考音频文件路径 Returns: 生成的音频文件路径 """ try: # 生成语音 speech = system.clone_voice(text, [reference_audio]) # 保存音频 output_path = "temp/output.wav" system.save_audio(speech, output_path) return output_path except Exception as e: raise gr.Error(str(e)) # 创建 Gradio 界面 demo = gr.Interface( fn=clone_voice, inputs=[ gr.Textbox( label="输入文本", placeholder="请输入要转换的文本...", lines=3 ), gr.Audio( label="参考音频", type="filepath" ) ], outputs=gr.Audio(label="生成的语音"), title="语音克隆系统", description="上传一段参考音频,输入文本,系统会生成具有相同声音特征的语音。", examples=[ ["你好,这是一段测试文本。", "examples/reference.wav"], ], cache_examples=False ) # 启动服务 if __name__ == "__main__": demo.launch()