File size: 2,217 Bytes
9580089
 
 
99fa17d
 
 
 
 
 
9580089
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3b8617
9580089
 
 
 
 
f3b8617
9580089
 
f3b8617
9580089
 
 
 
51df8b3
 
9580089
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import torch
from pathlib import Path
import os
import sys

# 添加项目根目录到 Python 路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.deploy.voice_clone import VoiceCloneSystem

# 创建临时目录
TEMP_DIR = Path("temp")
TEMP_DIR.mkdir(exist_ok=True)

# 初始化系统
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 = str(TEMP_DIR / "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="参考音频",
            sources=["microphone", "upload"],
            type="filepath"
        )
    ],
    outputs=gr.Audio(label="生成的语音"),
    title="语音克隆系统",
    description="录制或上传一段参考音频,输入文本,系统会生成具有相同声音特征的语音。支持中文和英文!",
    article="""
    ## 使用说明
    1. 点击录音按钮录制语音,或上传一段音频文件(5-10秒最佳)
    2. 输入要转换的文本(支持中文和英文)
    3. 点击提交,等待系统生成语音
    
    ## 注意事项
    - 请在安静的环境下录音
    - 说话时保持适当距离和音量
    - 首次运行时需要下载模型,可能需要等待一段时间
    """,
    examples=[
        ["你好,这是一段测试文本。", None],
        ["Hello, this is a test message.", None],
    ]
)

# 启动应用
if __name__ == "__main__":
    demo.launch()