Spaces:
Runtime error
Runtime error
Delete webUI.py
Browse files
webUI.py
DELETED
@@ -1,311 +0,0 @@
|
|
1 |
-
import io
|
2 |
-
import os
|
3 |
-
|
4 |
-
# os.system("wget -P cvec/ https://huggingface.co/spaces/innnky/nanami/resolve/main/checkpoint_best_legacy_500.pt")
|
5 |
-
import gradio as gr
|
6 |
-
import gradio.processing_utils as gr_pu
|
7 |
-
import librosa
|
8 |
-
import numpy as np
|
9 |
-
import soundfile
|
10 |
-
from inference.infer_tool import Svc
|
11 |
-
import logging
|
12 |
-
import re
|
13 |
-
import json
|
14 |
-
|
15 |
-
import subprocess
|
16 |
-
import edge_tts
|
17 |
-
import asyncio
|
18 |
-
from scipy.io import wavfile
|
19 |
-
import librosa
|
20 |
-
import torch
|
21 |
-
import time
|
22 |
-
import traceback
|
23 |
-
from itertools import chain
|
24 |
-
from utils import mix_model
|
25 |
-
|
26 |
-
logging.getLogger('numba').setLevel(logging.WARNING)
|
27 |
-
logging.getLogger('markdown_it').setLevel(logging.WARNING)
|
28 |
-
logging.getLogger('urllib3').setLevel(logging.WARNING)
|
29 |
-
logging.getLogger('matplotlib').setLevel(logging.WARNING)
|
30 |
-
logging.getLogger('multipart').setLevel(logging.WARNING)
|
31 |
-
|
32 |
-
model = None
|
33 |
-
spk = None
|
34 |
-
debug = False
|
35 |
-
|
36 |
-
cuda = {}
|
37 |
-
if torch.cuda.is_available():
|
38 |
-
for i in range(torch.cuda.device_count()):
|
39 |
-
device_name = torch.cuda.get_device_properties(i).name
|
40 |
-
cuda[f"CUDA:{i} {device_name}"] = f"cuda:{i}"
|
41 |
-
|
42 |
-
def upload_mix_append_file(files,sfiles):
|
43 |
-
try:
|
44 |
-
if(sfiles == None):
|
45 |
-
file_paths = [file.name for file in files]
|
46 |
-
else:
|
47 |
-
file_paths = [file.name for file in chain(files,sfiles)]
|
48 |
-
p = {file:100 for file in file_paths}
|
49 |
-
return file_paths,mix_model_output1.update(value=json.dumps(p,indent=2))
|
50 |
-
except Exception as e:
|
51 |
-
if debug: traceback.print_exc()
|
52 |
-
raise gr.Error(e)
|
53 |
-
|
54 |
-
def mix_submit_click(js,mode):
|
55 |
-
try:
|
56 |
-
assert js.lstrip()!=""
|
57 |
-
modes = {"凸组合":0, "线性组合":1}
|
58 |
-
mode = modes[mode]
|
59 |
-
data = json.loads(js)
|
60 |
-
data = list(data.items())
|
61 |
-
model_path,mix_rate = zip(*data)
|
62 |
-
path = mix_model(model_path,mix_rate,mode)
|
63 |
-
return f"成功,文件被保存在了{path}"
|
64 |
-
except Exception as e:
|
65 |
-
if debug: traceback.print_exc()
|
66 |
-
raise gr.Error(e)
|
67 |
-
|
68 |
-
def updata_mix_info(files):
|
69 |
-
try:
|
70 |
-
if files == None : return mix_model_output1.update(value="")
|
71 |
-
p = {file.name:100 for file in files}
|
72 |
-
return mix_model_output1.update(value=json.dumps(p,indent=2))
|
73 |
-
except Exception as e:
|
74 |
-
if debug: traceback.print_exc()
|
75 |
-
raise gr.Error(e)
|
76 |
-
|
77 |
-
def modelAnalysis(model_path,config_path,cluster_model_path,device,enhance):
|
78 |
-
global model
|
79 |
-
try:
|
80 |
-
device = cuda[device] if "CUDA" in device else device
|
81 |
-
model = Svc(model_path.name, config_path.name, device=device if device!="Auto" else None, cluster_model_path = cluster_model_path.name if cluster_model_path != None else "",nsf_hifigan_enhance=enhance)
|
82 |
-
spks = list(model.spk2id.keys())
|
83 |
-
device_name = torch.cuda.get_device_properties(model.dev).name if "cuda" in str(model.dev) else str(model.dev)
|
84 |
-
msg = f"成功加载模型到设备{device_name}上\n"
|
85 |
-
if cluster_model_path is None:
|
86 |
-
msg += "未加载聚类模型\n"
|
87 |
-
else:
|
88 |
-
msg += f"聚类模型{cluster_model_path.name}加载成功\n"
|
89 |
-
msg += "当前模型的可用音色:\n"
|
90 |
-
for i in spks:
|
91 |
-
msg += i + " "
|
92 |
-
return sid.update(choices = spks,value=spks[0]), msg
|
93 |
-
except Exception as e:
|
94 |
-
if debug: traceback.print_exc()
|
95 |
-
raise gr.Error(e)
|
96 |
-
|
97 |
-
|
98 |
-
def modelUnload():
|
99 |
-
global model
|
100 |
-
if model is None:
|
101 |
-
return sid.update(choices = [],value=""),"没有模型需要卸载!"
|
102 |
-
else:
|
103 |
-
model.unload_model()
|
104 |
-
model = None
|
105 |
-
torch.cuda.empty_cache()
|
106 |
-
return sid.update(choices = [],value=""),"模型卸载完毕!"
|
107 |
-
|
108 |
-
|
109 |
-
def vc_fn(sid, input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,F0_mean_pooling,enhancer_adaptive_key,cr_threshold):
|
110 |
-
global model
|
111 |
-
try:
|
112 |
-
if input_audio is None:
|
113 |
-
raise gr.Error("你需要上传音频")
|
114 |
-
if model is None:
|
115 |
-
raise gr.Error("你需要指定模型")
|
116 |
-
sampling_rate, audio = input_audio
|
117 |
-
# print(audio.shape,sampling_rate)
|
118 |
-
audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
|
119 |
-
if len(audio.shape) > 1:
|
120 |
-
audio = librosa.to_mono(audio.transpose(1, 0))
|
121 |
-
temp_path = "temp.wav"
|
122 |
-
soundfile.write(temp_path, audio, sampling_rate, format="wav")
|
123 |
-
_audio = model.slice_inference(temp_path, sid, vc_transform, slice_db, cluster_ratio, auto_f0, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,F0_mean_pooling,enhancer_adaptive_key,cr_threshold)
|
124 |
-
model.clear_empty()
|
125 |
-
os.remove(temp_path)
|
126 |
-
#构建保存文件的路径,并保存到results文件夹内
|
127 |
-
try:
|
128 |
-
timestamp = str(int(time.time()))
|
129 |
-
filename = sid + "_" + timestamp + ".wav"
|
130 |
-
output_file = os.path.join("./results", filename)
|
131 |
-
soundfile.write(output_file, _audio, model.target_sample, format="wav")
|
132 |
-
return f"推理成功,音频文件保存为results/{filename}", (model.target_sample, _audio)
|
133 |
-
except Exception as e:
|
134 |
-
if debug: traceback.print_exc()
|
135 |
-
return f"文件保存失败,请手动保存", (model.target_sample, _audio)
|
136 |
-
except Exception as e:
|
137 |
-
if debug: traceback.print_exc()
|
138 |
-
raise gr.Error(e)
|
139 |
-
|
140 |
-
|
141 |
-
def tts_func(_text,_rate,_voice):
|
142 |
-
#使用edge-tts把文字转成音频
|
143 |
-
# voice = "zh-CN-XiaoyiNeural"#女性,较高音
|
144 |
-
# voice = "zh-CN-YunxiNeural"#男性
|
145 |
-
voice = "zh-CN-YunxiNeural"#男性
|
146 |
-
if ( _voice == "女" ) : voice = "zh-CN-XiaoyiNeural"
|
147 |
-
output_file = _text[0:10]+".wav"
|
148 |
-
# communicate = edge_tts.Communicate(_text, voice)
|
149 |
-
# await communicate.save(output_file)
|
150 |
-
if _rate>=0:
|
151 |
-
ratestr="+{:.0%}".format(_rate)
|
152 |
-
elif _rate<0:
|
153 |
-
ratestr="{:.0%}".format(_rate)#减号自带
|
154 |
-
|
155 |
-
p=subprocess.Popen("edge-tts "+
|
156 |
-
" --text "+_text+
|
157 |
-
" --write-media "+output_file+
|
158 |
-
" --voice "+voice+
|
159 |
-
" --rate="+ratestr
|
160 |
-
,shell=True,
|
161 |
-
stdout=subprocess.PIPE,
|
162 |
-
stdin=subprocess.PIPE)
|
163 |
-
p.wait()
|
164 |
-
return output_file
|
165 |
-
|
166 |
-
def text_clear(text):
|
167 |
-
return re.sub(r"[\n\,\(\) ]", "", text)
|
168 |
-
|
169 |
-
def vc_fn2(sid, input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,text2tts,tts_rate,tts_voice,F0_mean_pooling,enhancer_adaptive_key,cr_threshold):
|
170 |
-
#使用edge-tts把文字转成音频
|
171 |
-
text2tts=text_clear(text2tts)
|
172 |
-
output_file=tts_func(text2tts,tts_rate,tts_voice)
|
173 |
-
|
174 |
-
#调整采样率
|
175 |
-
sr2=44100
|
176 |
-
wav, sr = librosa.load(output_file)
|
177 |
-
wav2 = librosa.resample(wav, orig_sr=sr, target_sr=sr2)
|
178 |
-
save_path2= text2tts[0:10]+"_44k"+".wav"
|
179 |
-
wavfile.write(save_path2,sr2,
|
180 |
-
(wav2 * np.iinfo(np.int16).max).astype(np.int16)
|
181 |
-
)
|
182 |
-
|
183 |
-
#读取音频
|
184 |
-
sample_rate, data=gr_pu.audio_from_file(save_path2)
|
185 |
-
vc_input=(sample_rate, data)
|
186 |
-
|
187 |
-
a,b=vc_fn(sid, vc_input, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,F0_mean_pooling,enhancer_adaptive_key,cr_threshold)
|
188 |
-
os.remove(output_file)
|
189 |
-
os.remove(save_path2)
|
190 |
-
return a,b
|
191 |
-
|
192 |
-
def debug_change():
|
193 |
-
global debug
|
194 |
-
debug = debug_button.value
|
195 |
-
|
196 |
-
with gr.Blocks(
|
197 |
-
theme=gr.themes.Base(
|
198 |
-
primary_hue = gr.themes.colors.green,
|
199 |
-
font=["Source Sans Pro", "Arial", "sans-serif"],
|
200 |
-
font_mono=['JetBrains mono', "Consolas", 'Courier New']
|
201 |
-
),
|
202 |
-
) as app:
|
203 |
-
with gr.Tabs():
|
204 |
-
with gr.TabItem("推理"):
|
205 |
-
gr.Markdown(value="""
|
206 |
-
So-vits-svc 4.0 推理 webui
|
207 |
-
""")
|
208 |
-
with gr.Row(variant="panel"):
|
209 |
-
with gr.Column():
|
210 |
-
gr.Markdown(value="""
|
211 |
-
<font size=2> 模型设置</font>
|
212 |
-
""")
|
213 |
-
model_path = gr.File(label="选择模型文件")
|
214 |
-
config_path = gr.File(label="选择配置文件")
|
215 |
-
cluster_model_path = gr.File(label="选择聚类模型文件(没有可以不选)")
|
216 |
-
device = gr.Dropdown(label="推理设备,默认为自动选择CPU和GPU", choices=["Auto",*cuda.keys(),"CPU"], value="Auto")
|
217 |
-
enhance = gr.Checkbox(label="是否使用NSF_HIFIGAN增强,该选项对部分训练集少的模型有一定的音质增强效果,但是对训练好的模型有反面效果,默认关闭", value=False)
|
218 |
-
with gr.Column():
|
219 |
-
gr.Markdown(value="""
|
220 |
-
<font size=3>左侧文件全部选择完毕后(全部文件模块显示download),点击“加载模型”进行解析:</font>
|
221 |
-
""")
|
222 |
-
model_load_button = gr.Button(value="加载模型", variant="primary")
|
223 |
-
model_unload_button = gr.Button(value="卸载模型", variant="primary")
|
224 |
-
sid = gr.Dropdown(label="音色(说话人)")
|
225 |
-
sid_output = gr.Textbox(label="Output Message")
|
226 |
-
|
227 |
-
|
228 |
-
with gr.Row(variant="panel"):
|
229 |
-
with gr.Column():
|
230 |
-
gr.Markdown(value="""
|
231 |
-
<font size=2> 推理设置</font>
|
232 |
-
""")
|
233 |
-
auto_f0 = gr.Checkbox(label="自动f0预测,配合聚类模型f0预测效果更好,会导致变调功能失效(仅限转换语音,歌声勾选此项会究极跑调)", value=False)
|
234 |
-
F0_mean_pooling = gr.Checkbox(label="是否对F0使用均值滤波器(池化),对部分哑音有改善。注意,启动该选项会导致推理速度下降,默认关闭", value=False)
|
235 |
-
vc_transform = gr.Number(label="变调(整数,可以正负,半音数量,升高八度就是12)", value=0)
|
236 |
-
cluster_ratio = gr.Number(label="聚类模型混合比例,0-1之间,0即不启用聚类。使用聚类模型能提升音色相似度,��会导致咬字下降(如果使用建议0.5左右)", value=0)
|
237 |
-
slice_db = gr.Number(label="切片阈值", value=-40)
|
238 |
-
noise_scale = gr.Number(label="noise_scale 建议不要动,会影响音质,玄学参数", value=0.4)
|
239 |
-
with gr.Column():
|
240 |
-
pad_seconds = gr.Number(label="推理音频pad秒数,由于未知原因开头结尾会有异响,pad一小段静音段后就不会出现", value=0.5)
|
241 |
-
cl_num = gr.Number(label="音频自动切片,0为不切片,单位为秒(s)", value=0)
|
242 |
-
lg_num = gr.Number(label="两端音频切片的交叉淡入长度,如果自动切片后出现人声不连贯可调整该数值,如果连贯建议采用默认值0,注意,该设置会影响推理速度,单位为秒/s", value=0)
|
243 |
-
lgr_num = gr.Number(label="自动音频切片后,需要舍弃每段切片的头尾。该参数设置交叉长度保留的比例,范围0-1,左开右闭", value=0.75)
|
244 |
-
enhancer_adaptive_key = gr.Number(label="使增强器适应更高的音域(单位为半音数)|默认为0", value=0)
|
245 |
-
cr_threshold = gr.Number(label="F0过滤阈值,只有启动f0_mean_pooling时有效. 数值范围从0-1. 降低该值可减少跑调概率,但会增加哑音", value=0.05)
|
246 |
-
with gr.Tabs():
|
247 |
-
with gr.TabItem("音频转音频"):
|
248 |
-
vc_input3 = gr.Audio(label="选择音频")
|
249 |
-
vc_submit = gr.Button("音频转换", variant="primary")
|
250 |
-
with gr.TabItem("文字转音频"):
|
251 |
-
text2tts=gr.Textbox(label="在此输入要转译的文字。注意,使用该功能建议打开F0预测,不然会很怪")
|
252 |
-
tts_rate = gr.Number(label="tts语速", value=0)
|
253 |
-
tts_voice = gr.Radio(label="性别",choices=["男","女"], value="男")
|
254 |
-
vc_submit2 = gr.Button("文字转换", variant="primary")
|
255 |
-
with gr.Row():
|
256 |
-
with gr.Column():
|
257 |
-
vc_output1 = gr.Textbox(label="Output Message")
|
258 |
-
with gr.Column():
|
259 |
-
vc_output2 = gr.Audio(label="Output Audio", interactive=False)
|
260 |
-
|
261 |
-
with gr.TabItem("小工具/实验室特性"):
|
262 |
-
gr.Markdown(value="""
|
263 |
-
<font size=2> So-vits-svc 4.0 小工具/实验室特性</font>
|
264 |
-
""")
|
265 |
-
with gr.Tabs():
|
266 |
-
with gr.TabItem("静态声线融合"):
|
267 |
-
gr.Markdown(value="""
|
268 |
-
<font size=2> 介绍:该功能可以将多个声音模型合成为一个声音模型(多个模型参数的凸组合或线性组合),从而制造出现实中不存在的声线
|
269 |
-
注意:
|
270 |
-
1.该功能仅支持单说话人的模型
|
271 |
-
2.如果强行使用多说话人模型,需要保证多个模型的说话人数量相同,这样可以混合同一个SpaekerID下的声音
|
272 |
-
3.保证所有待混合模型的config.json中的model字段是相同的
|
273 |
-
4.输出的混合模型可以使用待合成模型的任意一个config.json,但聚类模型将不能使用
|
274 |
-
5.批量上传模型的时候最好把模型放到一个文件夹选中后一起上传
|
275 |
-
6.混合比例调整建议大小在0-100之间,也可以调为其他数字,但在线性组合模式下会出现未知的效果
|
276 |
-
7.混合完毕后,文件将会保存在项目根目录中,文件名为output.pth
|
277 |
-
8.凸组合模式会将混合比例执行Softmax使混合比例相加为1,而线性组合模式不会
|
278 |
-
</font>
|
279 |
-
""")
|
280 |
-
mix_model_path = gr.Files(label="选择需要混合模型文件")
|
281 |
-
mix_model_upload_button = gr.UploadButton("选择/追加需要混合模型文件", file_count="multiple", variant="primary")
|
282 |
-
mix_model_output1 = gr.Textbox(
|
283 |
-
label="混合比例调整,单位/%",
|
284 |
-
interactive = True
|
285 |
-
)
|
286 |
-
mix_mode = gr.Radio(choices=["凸组合", "线性组合"], label="融合模式",value="凸组合",interactive = True)
|
287 |
-
mix_submit = gr.Button("声线融合启动", variant="primary")
|
288 |
-
mix_model_output2 = gr.Textbox(
|
289 |
-
label="Output Message"
|
290 |
-
)
|
291 |
-
mix_model_path.change(updata_mix_info,[mix_model_path],[mix_model_output1])
|
292 |
-
mix_model_upload_button.upload(upload_mix_append_file, [mix_model_upload_button,mix_model_path], [mix_model_path,mix_model_output1])
|
293 |
-
mix_submit.click(mix_submit_click, [mix_model_output1,mix_mode], [mix_model_output2])
|
294 |
-
|
295 |
-
|
296 |
-
with gr.Tabs():
|
297 |
-
with gr.Row(variant="panel"):
|
298 |
-
with gr.Column():
|
299 |
-
gr.Markdown(value="""
|
300 |
-
<font size=2> WebUI设置</font>
|
301 |
-
""")
|
302 |
-
debug_button = gr.Checkbox(label="Debug模式,如果向社区反馈BUG需要打开,打开后控制台可以显示具体错误提示", value=debug)
|
303 |
-
vc_submit.click(vc_fn, [sid, vc_input3, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,F0_mean_pooling,enhancer_adaptive_key,cr_threshold], [vc_output1, vc_output2])
|
304 |
-
vc_submit2.click(vc_fn2, [sid, vc_input3, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,text2tts,tts_rate,tts_voice,F0_mean_pooling,enhancer_adaptive_key,cr_threshold], [vc_output1, vc_output2])
|
305 |
-
debug_button.change(debug_change,[],[])
|
306 |
-
model_load_button.click(modelAnalysis,[model_path,config_path,cluster_model_path,device,enhance],[sid,sid_output])
|
307 |
-
model_unload_button.click(modelUnload,[],[sid,sid_output])
|
308 |
-
app.launch()
|
309 |
-
|
310 |
-
|
311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|