Upload myinfer.py
Browse files- myinfer.py +79 -0
myinfer.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os,sys,pdb,torch
|
2 |
+
f0up_key=sys.argv[1]
|
3 |
+
input_path=sys.argv[2]
|
4 |
+
index_path=sys.argv[3]
|
5 |
+
npy_path=sys.argv[4]
|
6 |
+
opt_path=sys.argv[5]
|
7 |
+
model_path=sys.argv[6]
|
8 |
+
print(sys.argv)
|
9 |
+
sys.argv=['myinfer.py']
|
10 |
+
now_dir=os.getcwd()
|
11 |
+
sys.path.append(now_dir)
|
12 |
+
from vc_infer_pipeline import VC
|
13 |
+
from infer_pack.models import SynthesizerTrnMs256NSFsid, SynthesizerTrnMs256NSFsid_nono
|
14 |
+
from my_utils import load_audio
|
15 |
+
from fairseq import checkpoint_utils
|
16 |
+
from scipy.io import wavfile
|
17 |
+
|
18 |
+
|
19 |
+
# f0up_key=0
|
20 |
+
# input_path=r"E:\codes\py39\RVC-beta\todo-songs\1111.wav"
|
21 |
+
# index_path=r"E:\codes\py39\logs\mi-test\added_IVF677_Flat_nprobe_7.index"
|
22 |
+
# npy_path =r"E:\codes\py39\logs\mi-test\total_fea.npy"
|
23 |
+
# opt_path ="test.wav"
|
24 |
+
# model_path="mi-test.pth"
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
hubert_model=None
|
29 |
+
is_half=False
|
30 |
+
device="cuda"
|
31 |
+
def load_hubert():
|
32 |
+
global hubert_model
|
33 |
+
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)
|
34 |
+
hubert_model = models[0]
|
35 |
+
hubert_model = hubert_model.to(device)
|
36 |
+
if(is_half):hubert_model = hubert_model.half()
|
37 |
+
else:hubert_model = hubert_model.float()
|
38 |
+
hubert_model.eval()
|
39 |
+
|
40 |
+
|
41 |
+
def vc_single(sid,input_audio,f0_up_key,f0_file,f0_method,file_index,file_big_npy,index_rate):#spk_item, input_audio0, vc_transform0,f0_file,f0method0
|
42 |
+
global tgt_sr,net_g,vc,hubert_model
|
43 |
+
if input_audio is None:return "You need to upload an audio", None
|
44 |
+
f0_up_key = int(f0_up_key)
|
45 |
+
audio=load_audio(input_audio,16000)
|
46 |
+
times = [0, 0, 0]
|
47 |
+
if(hubert_model==None):load_hubert()
|
48 |
+
if_f0 = cpt.get("f0", 1)
|
49 |
+
audio_opt=vc.pipeline(hubert_model,net_g,sid,audio,times,f0_up_key,f0_method,file_index,file_big_npy,index_rate,if_f0,f0_file=f0_file)
|
50 |
+
print(times)
|
51 |
+
return audio_opt
|
52 |
+
|
53 |
+
|
54 |
+
def get_vc(sid):
|
55 |
+
global n_spk,tgt_sr,net_g,vc,cpt
|
56 |
+
person = "weights/%s" % (sid)
|
57 |
+
print("loading %s"%person)
|
58 |
+
cpt = torch.load(person, map_location="cpu")
|
59 |
+
tgt_sr = cpt["config"][-1]
|
60 |
+
cpt["config"][-3]=cpt["weight"]["emb_g.weight"].shape[0]#n_spk
|
61 |
+
if_f0=cpt.get("f0",1)
|
62 |
+
if(if_f0==1):
|
63 |
+
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
|
64 |
+
else:
|
65 |
+
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
66 |
+
del net_g.enc_q
|
67 |
+
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净,真奇葩
|
68 |
+
net_g.eval().to(device)
|
69 |
+
if (is_half):net_g = net_g.half()
|
70 |
+
else:net_g = net_g.float()
|
71 |
+
vc = VC(tgt_sr, device, is_half)
|
72 |
+
n_spk=cpt["config"][-3]
|
73 |
+
# return {"visible": True,"maximum": n_spk, "__type__": "update"}
|
74 |
+
|
75 |
+
|
76 |
+
get_vc(model_path)
|
77 |
+
wav_opt=vc_single(0,input_path,f0up_key,None,"harvest",index_path,npy_path,0.6)
|
78 |
+
wavfile.write(opt_path, tgt_sr, wav_opt)
|
79 |
+
|