Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
import librosa
|
4 |
+
import gradio as gr
|
5 |
+
from scipy.io.wavfile import write
|
6 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
7 |
+
import utils
|
8 |
+
from models import SynthesizerTrn
|
9 |
+
from mel_processing import mel_spectrogram_torch
|
10 |
+
from speaker_encoder.voice_encoder import SpeakerEncoder
|
11 |
+
import logging
|
12 |
+
from transformers import Wav2Vec2FeatureExtractor, HubertModel
|
13 |
+
|
14 |
+
# 设置日志级别
|
15 |
+
logging.getLogger('numba').setLevel(logging.WARNING)
|
16 |
+
|
17 |
+
# 模型配置
|
18 |
+
MODEL_CONFIG = {
|
19 |
+
"freevc": {
|
20 |
+
"repo_id": "guetLzy/Chinese-FreeVC-Model", # FreeVC模型仓库
|
21 |
+
"files": ["G_17000.pth", "G_35000.pth"] # 需要下载的模型文件
|
22 |
+
},
|
23 |
+
"hubert": {
|
24 |
+
"repo_id": "guetLzy/chinese-hubert-large-fariseq-ckpt", # 中文HuBERT官方仓库
|
25 |
+
}
|
26 |
+
}
|
27 |
+
|
28 |
+
# 设备设置
|
29 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
30 |
+
|
31 |
+
# 可用的模型选项
|
32 |
+
MODEL_OPTIONS = {
|
33 |
+
"Model_17000": "model/G_17000.pth",
|
34 |
+
"Model_35000": "model/G_35000.pth", # 新增 G_35000.pth 选项
|
35 |
+
}
|
36 |
+
|
37 |
+
def download_models():
|
38 |
+
"""下载所有需要的模型文件"""
|
39 |
+
os.makedirs("model", exist_ok=True)
|
40 |
+
os.makedirs("hubert/chinese-hubert-large-fairseq-ckpt", exist_ok=True)
|
41 |
+
|
42 |
+
# 下载FreeVC模型
|
43 |
+
freevc_paths = {}
|
44 |
+
for model_name, model_path in MODEL_OPTIONS.items():
|
45 |
+
path = hf_hub_download(
|
46 |
+
repo_id=MODEL_CONFIG["freevc"]["repo_id"],
|
47 |
+
filename=os.path.basename(model_path),
|
48 |
+
local_dir="model",
|
49 |
+
resume_download=True
|
50 |
+
)
|
51 |
+
freevc_paths[model_name] = path
|
52 |
+
|
53 |
+
# 下载整个HuBERT仓库
|
54 |
+
hubert_dir = "hubert/chinese-hubert-large-fairseq-ckpt"
|
55 |
+
snapshot_download(
|
56 |
+
repo_id=MODEL_CONFIG["hubert"]["repo_id"],
|
57 |
+
local_dir=hubert_dir,
|
58 |
+
repo_type="model",
|
59 |
+
resume_download=True
|
60 |
+
)
|
61 |
+
hubert_paths = {"snapshot": hubert_dir} # 返回整个目录路径
|
62 |
+
|
63 |
+
return {
|
64 |
+
"freevc": freevc_paths,
|
65 |
+
"hubert": hubert_paths
|
66 |
+
}
|
67 |
+
|
68 |
+
def load_hubert(hubert_dir):
|
69 |
+
"""加载HuBERT模型(使用fairseq格式的检查点)"""
|
70 |
+
print("正在加载 HuBERT 模型...")
|
71 |
+
|
72 |
+
# 加载标准HuBERT模型
|
73 |
+
model = HubertModel.from_pretrained(hubert_dir)
|
74 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(hubert_dir)
|
75 |
+
|
76 |
+
return model.to(device).float().eval(), feature_extractor
|
77 |
+
|
78 |
+
def load_freevc(model_path):
|
79 |
+
"""加载FreeVC模型(使用本地配置文件)"""
|
80 |
+
print(f"正在从 {model_path} 加载 FreeVC 模型...")
|
81 |
+
hps = utils.get_hparams_from_file("configs/freevc.json") # 本地配置文件
|
82 |
+
|
83 |
+
net_g = SynthesizerTrn(
|
84 |
+
hps.data.filter_length // 2 + 1,
|
85 |
+
hps.train.segment_size // hps.data.hop_length,
|
86 |
+
**hps.model
|
87 |
+
).to(device)
|
88 |
+
|
89 |
+
utils.load_checkpoint(model_path, net_g, None, True)
|
90 |
+
net_g.eval()
|
91 |
+
|
92 |
+
# 加载本地说话人编码器
|
93 |
+
smodel = SpeakerEncoder("speaker_encoder/ckpt/pretrained_bak_5805000.pt") if hps.model.use_spk else None
|
94 |
+
return net_g, smodel, hps
|
95 |
+
|
96 |
+
# 预加载模型
|
97 |
+
print("正在下载模型...")
|
98 |
+
model_paths = download_models()
|
99 |
+
print(model_paths)
|
100 |
+
print("正在初始化 HuBERT...")
|
101 |
+
hubert_dir = "hubert/chinese-hubert-large-fairseq-ckpt"
|
102 |
+
hubert_model, hubert_feature_extractor = load_hubert(hubert_dir)
|
103 |
+
|
104 |
+
def voice_conversion(src_audio, tgt_audio, output_name, model_selection):
|
105 |
+
"""执行语音转换"""
|
106 |
+
try:
|
107 |
+
# 加载选中的FreeVC模型
|
108 |
+
freevc_model, speaker_model, hps = load_freevc(MODEL_OPTIONS[model_selection])
|
109 |
+
|
110 |
+
with torch.no_grad():
|
111 |
+
# 处理目标音频
|
112 |
+
wav_tgt, _ = librosa.load(tgt_audio, sr=hps.data.sampling_rate)
|
113 |
+
wav_tgt, _ = librosa.effects.trim(wav_tgt, top_db=20)
|
114 |
+
|
115 |
+
if hps.model.use_spk:
|
116 |
+
g_tgt = speaker_model.embed_utterance(wav_tgt)
|
117 |
+
g_tgt = torch.from_numpy(g_tgt).unsqueeze(0).to(device)
|
118 |
+
else:
|
119 |
+
wav_tgt = torch.from_numpy(wav_tgt).unsqueeze(0).to(device)
|
120 |
+
mel_tgt = mel_spectrogram_torch(
|
121 |
+
wav_tgt,
|
122 |
+
hps.data.filter_length,
|
123 |
+
hps.data.n_mel_channels,
|
124 |
+
hps.data.sampling_rate,
|
125 |
+
hps.data.hop_length,
|
126 |
+
hps.data.win_length,
|
127 |
+
hps.data.mel_fmin,
|
128 |
+
hps.data.mel_fmax
|
129 |
+
)
|
130 |
+
|
131 |
+
# 处理源音频(HuBERT需要16kHz)
|
132 |
+
wav_src, _ = librosa.load(src_audio, sr=16000)
|
133 |
+
inputs = hubert_feature_extractor(
|
134 |
+
wav_src,
|
135 |
+
return_tensors="pt",
|
136 |
+
sampling_rate=16000
|
137 |
+
).input_values.to(device)
|
138 |
+
|
139 |
+
c = hubert_model(inputs.float()).last_hidden_state.transpose(1, 2)
|
140 |
+
|
141 |
+
# 执行转换
|
142 |
+
audio = freevc_model.infer(c, g=g_tgt) if hps.model.use_spk else freevc_model.infer(c, mel=mel_tgt)
|
143 |
+
|
144 |
+
# 保存结果
|
145 |
+
os.makedirs("output", exist_ok=True)
|
146 |
+
output_path = f"output/{output_name}.wav"
|
147 |
+
write(output_path, hps.data.sampling_rate, audio[0][0].data.cpu().float().numpy())
|
148 |
+
|
149 |
+
return output_path
|
150 |
+
|
151 |
+
except Exception as e:
|
152 |
+
print(f"转换错误: {str(e)}")
|
153 |
+
return None
|
154 |
+
|
155 |
+
# Gradio界面
|
156 |
+
with gr.Blocks(title="Chinese-FreeVC 语音转换") as app:
|
157 |
+
gr.Markdown("## Chinese-FreeVC 语音转换系统")
|
158 |
+
|
159 |
+
with gr.Row():
|
160 |
+
with gr.Column():
|
161 |
+
src_input = gr.Audio(label="源语音", type="filepath")
|
162 |
+
tgt_input = gr.Audio(label="目标音色", type="filepath")
|
163 |
+
model_dropdown = gr.Dropdown(
|
164 |
+
choices=list(MODEL_OPTIONS.keys()),
|
165 |
+
label="选择模型",
|
166 |
+
value="Model_17000"
|
167 |
+
)
|
168 |
+
output_name = gr.Textbox(label="输出文件名", value="converted")
|
169 |
+
convert_btn = gr.Button("开始转换", variant="primary")
|
170 |
+
|
171 |
+
with gr.Column():
|
172 |
+
output_audio = gr.Audio(label="转换结果", interactive=False)
|
173 |
+
status = gr.Textbox(label="状态")
|
174 |
+
|
175 |
+
convert_btn.click(
|
176 |
+
fn=voice_conversion,
|
177 |
+
inputs=[src_input, tgt_input, output_name, model_dropdown],
|
178 |
+
outputs=[output_audio],
|
179 |
+
api_name="convert"
|
180 |
+
)
|
181 |
+
|
182 |
+
if __name__ == "__main__":
|
183 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|