Skip to content

Commit eb1a88c

Browse files
Format code (#522)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 4de5d0d commit eb1a88c

File tree

1 file changed

+78
-42
lines changed

1 file changed

+78
-42
lines changed

infer_batch_rvc.py

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
'''
1+
"""
22
v1
33
runtime\python.exe myinfer-v2-0528.py 0 "E:\codes\py39\RVC-beta\todo-songs" "E:\codes\py39\logs\mi-test\added_IVF677_Flat_nprobe_7.index" harvest "E:\codes\py39\RVC-beta\output" "E:\codes\py39\test-20230416b\weights\mi-test.pth" 0.66 cuda:0 True 3 0 1 0.33
44
v2
55
runtime\python.exe myinfer-v2-0528.py 0 "E:\codes\py39\RVC-beta\todo-songs" "E:\codes\py39\test-20230416b\logs\mi-test-v2\aadded_IVF677_Flat_nprobe_1_v2.index" harvest "E:\codes\py39\RVC-beta\output_v2" "E:\codes\py39\test-20230416b\weights\mi-test-v2.pth" 0.66 cuda:0 True 3 0 1 0.33
6-
'''
7-
import os,sys,pdb,torch
6+
"""
7+
import os, sys, pdb, torch
8+
89
now_dir = os.getcwd()
910
sys.path.append(now_dir)
1011
import argparse
@@ -13,8 +14,10 @@
1314
import torch
1415
import tqdm as tq
1516
from multiprocessing import cpu_count
17+
18+
1619
class Config:
17-
def __init__(self,device,is_half):
20+
def __init__(self, device, is_half):
1821
self.device = device
1922
self.is_half = is_half
2023
self.n_cpu = 0
@@ -90,22 +93,23 @@ def device_config(self) -> tuple:
9093

9194
return x_pad, x_query, x_center, x_max
9295

93-
f0up_key=sys.argv[1]
94-
input_path=sys.argv[2]
95-
index_path=sys.argv[3]
96-
f0method=sys.argv[4]#harvest or pm
97-
opt_path=sys.argv[5]
98-
model_path=sys.argv[6]
99-
index_rate=float(sys.argv[7])
100-
device=sys.argv[8]
101-
is_half=bool(sys.argv[9])
102-
filter_radius=int(sys.argv[10])
103-
resample_sr=int(sys.argv[11])
104-
rms_mix_rate=float(sys.argv[12])
105-
protect=float(sys.argv[13])
96+
97+
f0up_key = sys.argv[1]
98+
input_path = sys.argv[2]
99+
index_path = sys.argv[3]
100+
f0method = sys.argv[4] # harvest or pm
101+
opt_path = sys.argv[5]
102+
model_path = sys.argv[6]
103+
index_rate = float(sys.argv[7])
104+
device = sys.argv[8]
105+
is_half = bool(sys.argv[9])
106+
filter_radius = int(sys.argv[10])
107+
resample_sr = int(sys.argv[11])
108+
rms_mix_rate = float(sys.argv[12])
109+
protect = float(sys.argv[13])
106110
print(sys.argv)
107-
config=Config(device,is_half)
108-
now_dir=os.getcwd()
111+
config = Config(device, is_half)
112+
now_dir = os.getcwd()
109113
sys.path.append(now_dir)
110114
from vc_infer_pipeline import VC
111115
from infer_pack.models import (
@@ -118,64 +122,96 @@ def device_config(self) -> tuple:
118122
from fairseq import checkpoint_utils
119123
from scipy.io import wavfile
120124

121-
hubert_model=None
125+
hubert_model = None
126+
127+
122128
def load_hubert():
123129
global hubert_model
124-
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(["hubert_base.pt"],suffix="",)
130+
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task(
131+
["hubert_base.pt"],
132+
suffix="",
133+
)
125134
hubert_model = models[0]
126135
hubert_model = hubert_model.to(device)
127-
if(is_half):hubert_model = hubert_model.half()
128-
else:hubert_model = hubert_model.float()
136+
if is_half:
137+
hubert_model = hubert_model.half()
138+
else:
139+
hubert_model = hubert_model.float()
129140
hubert_model.eval()
130141

131-
def vc_single(sid,input_audio,f0_up_key,f0_file,f0_method,file_index,index_rate):
132-
global tgt_sr,net_g,vc,hubert_model,version
133-
if input_audio is None:return "You need to upload an audio", None
142+
143+
def vc_single(sid, input_audio, f0_up_key, f0_file, f0_method, file_index, index_rate):
144+
global tgt_sr, net_g, vc, hubert_model, version
145+
if input_audio is None:
146+
return "You need to upload an audio", None
134147
f0_up_key = int(f0_up_key)
135-
audio=load_audio(input_audio,16000)
148+
audio = load_audio(input_audio, 16000)
136149
times = [0, 0, 0]
137-
if(hubert_model==None):load_hubert()
150+
if hubert_model == None:
151+
load_hubert()
138152
if_f0 = cpt.get("f0", 1)
139153
# 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)
140-
audio_opt=vc.pipeline(hubert_model,net_g,sid,audio,input_audio,times,f0_up_key,f0_method,file_index,index_rate,if_f0,filter_radius,tgt_sr,resample_sr,rms_mix_rate,version,protect,f0_file=f0_file)
154+
audio_opt = vc.pipeline(
155+
hubert_model,
156+
net_g,
157+
sid,
158+
audio,
159+
input_audio,
160+
times,
161+
f0_up_key,
162+
f0_method,
163+
file_index,
164+
index_rate,
165+
if_f0,
166+
filter_radius,
167+
tgt_sr,
168+
resample_sr,
169+
rms_mix_rate,
170+
version,
171+
protect,
172+
f0_file=f0_file,
173+
)
141174
print(times)
142175
return audio_opt
143176

144177

145178
def get_vc(model_path):
146-
global n_spk,tgt_sr,net_g,vc,cpt,device,is_half,version
147-
print("loading pth %s"%model_path)
179+
global n_spk, tgt_sr, net_g, vc, cpt, device, is_half, version
180+
print("loading pth %s" % model_path)
148181
cpt = torch.load(model_path, map_location="cpu")
149182
tgt_sr = cpt["config"][-1]
150-
cpt["config"][-3]=cpt["weight"]["emb_g.weight"].shape[0]#n_spk
151-
if_f0=cpt.get("f0",1)
183+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
184+
if_f0 = cpt.get("f0", 1)
152185
version = cpt.get("version", "v1")
153186
if version == "v1":
154187
if if_f0 == 1:
155188
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
156189
else:
157190
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
158191
elif version == "v2":
159-
if if_f0 == 1:#
192+
if if_f0 == 1: #
160193
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=is_half)
161194
else:
162195
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
163196
del net_g.enc_q
164197
print(net_g.load_state_dict(cpt["weight"], strict=False)) # 不加这一行清不干净,真奇葩
165198
net_g.eval().to(device)
166-
if (is_half):net_g = net_g.half()
167-
else:net_g = net_g.float()
199+
if is_half:
200+
net_g = net_g.half()
201+
else:
202+
net_g = net_g.float()
168203
vc = VC(tgt_sr, config)
169-
n_spk=cpt["config"][-3]
204+
n_spk = cpt["config"][-3]
170205
# return {"visible": True,"maximum": n_spk, "__type__": "update"}
171206

172207

173208
get_vc(model_path)
174209
audios = os.listdir(input_path)
175210
for file in tq.tqdm(audios):
176-
if file.endswith('.wav'):
177-
file_path = input_path + '/' + file
178-
wav_opt=vc_single(0,file_path,f0up_key,None,f0method,index_path,index_rate)
179-
out_path = opt_path + '/' + file
211+
if file.endswith(".wav"):
212+
file_path = input_path + "/" + file
213+
wav_opt = vc_single(
214+
0, file_path, f0up_key, None, f0method, index_path, index_rate
215+
)
216+
out_path = opt_path + "/" + file
180217
wavfile.write(out_path, tgt_sr, wav_opt)
181-

0 commit comments

Comments
 (0)