Spaces:
Running
Running
Delete src/rvc.py
Browse files- src/rvc.py +0 -151
src/rvc.py
DELETED
@@ -1,151 +0,0 @@
|
|
1 |
-
from multiprocessing import cpu_count
|
2 |
-
from pathlib import Path
|
3 |
-
|
4 |
-
import torch
|
5 |
-
from fairseq import checkpoint_utils
|
6 |
-
from scipy.io import wavfile
|
7 |
-
|
8 |
-
from infer_pack.models import (
|
9 |
-
SynthesizerTrnMs256NSFsid,
|
10 |
-
SynthesizerTrnMs256NSFsid_nono,
|
11 |
-
SynthesizerTrnMs768NSFsid,
|
12 |
-
SynthesizerTrnMs768NSFsid_nono,
|
13 |
-
)
|
14 |
-
from my_utils import load_audio
|
15 |
-
from vc_infer_pipeline import VC
|
16 |
-
|
17 |
-
BASE_DIR = Path(__file__).resolve().parent.parent
|
18 |
-
|
19 |
-
|
20 |
-
class Config:
|
21 |
-
def __init__(self, device, is_half):
|
22 |
-
self.device = device
|
23 |
-
self.is_half = is_half
|
24 |
-
self.n_cpu = 0
|
25 |
-
self.gpu_name = None
|
26 |
-
self.gpu_mem = None
|
27 |
-
self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
|
28 |
-
|
29 |
-
def device_config(self) -> tuple:
|
30 |
-
if torch.cuda.is_available():
|
31 |
-
i_device = int(self.device.split(":")[-1])
|
32 |
-
self.gpu_name = torch.cuda.get_device_name(i_device)
|
33 |
-
if (
|
34 |
-
("16" in self.gpu_name and "V100" not in self.gpu_name.upper())
|
35 |
-
or "P40" in self.gpu_name.upper()
|
36 |
-
or "1060" in self.gpu_name
|
37 |
-
or "1070" in self.gpu_name
|
38 |
-
or "1080" in self.gpu_name
|
39 |
-
):
|
40 |
-
print("16 series/10 series P40 forced single precision")
|
41 |
-
self.is_half = False
|
42 |
-
for config_file in ["32k.json", "40k.json", "48k.json"]:
|
43 |
-
with open(BASE_DIR / "src" / "configs" / config_file, "r") as f:
|
44 |
-
strr = f.read().replace("true", "false")
|
45 |
-
with open(BASE_DIR / "src" / "configs" / config_file, "w") as f:
|
46 |
-
f.write(strr)
|
47 |
-
with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "r") as f:
|
48 |
-
strr = f.read().replace("3.7", "3.0")
|
49 |
-
with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "w") as f:
|
50 |
-
f.write(strr)
|
51 |
-
else:
|
52 |
-
self.gpu_name = None
|
53 |
-
self.gpu_mem = int(
|
54 |
-
torch.cuda.get_device_properties(i_device).total_memory
|
55 |
-
/ 1024
|
56 |
-
/ 1024
|
57 |
-
/ 1024
|
58 |
-
+ 0.4
|
59 |
-
)
|
60 |
-
if self.gpu_mem <= 4:
|
61 |
-
with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "r") as f:
|
62 |
-
strr = f.read().replace("3.7", "3.0")
|
63 |
-
with open(BASE_DIR / "src" / "trainset_preprocess_pipeline_print.py", "w") as f:
|
64 |
-
f.write(strr)
|
65 |
-
elif torch.backends.mps.is_available():
|
66 |
-
print("No supported N-card found, use MPS for inference")
|
67 |
-
self.device = "mps"
|
68 |
-
else:
|
69 |
-
print("No supported N-card found, use CPU for inference")
|
70 |
-
self.device = "cpu"
|
71 |
-
self.is_half = True
|
72 |
-
|
73 |
-
if self.n_cpu == 0:
|
74 |
-
self.n_cpu = cpu_count()
|
75 |
-
|
76 |
-
if self.is_half:
|
77 |
-
# 6G memory config
|
78 |
-
x_pad = 3
|
79 |
-
x_query = 10
|
80 |
-
x_center = 60
|
81 |
-
x_max = 65
|
82 |
-
else:
|
83 |
-
# 5G memory config
|
84 |
-
x_pad = 1
|
85 |
-
x_query = 6
|
86 |
-
x_center = 38
|
87 |
-
x_max = 41
|
88 |
-
|
89 |
-
if self.gpu_mem != None and self.gpu_mem <= 4:
|
90 |
-
x_pad = 1
|
91 |
-
x_query = 5
|
92 |
-
x_center = 30
|
93 |
-
x_max = 32
|
94 |
-
|
95 |
-
return x_pad, x_query, x_center, x_max
|
96 |
-
|
97 |
-
|
98 |
-
def load_hubert(device, is_half, model_path):
|
99 |
-
models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task([model_path], suffix='', )
|
100 |
-
hubert = models[0]
|
101 |
-
hubert = hubert.to(device)
|
102 |
-
|
103 |
-
if is_half:
|
104 |
-
hubert = hubert.half()
|
105 |
-
else:
|
106 |
-
hubert = hubert.float()
|
107 |
-
|
108 |
-
hubert.eval()
|
109 |
-
return hubert
|
110 |
-
|
111 |
-
|
112 |
-
def get_vc(device, is_half, config, model_path):
|
113 |
-
cpt = torch.load(model_path, map_location='cpu')
|
114 |
-
if "config" not in cpt or "weight" not in cpt:
|
115 |
-
raise ValueError(f'Incorrect format for {model_path}. Use a voice model trained using RVC v2 instead.')
|
116 |
-
|
117 |
-
tgt_sr = cpt["config"][-1]
|
118 |
-
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0]
|
119 |
-
if_f0 = cpt.get("f0", 1)
|
120 |
-
version = cpt.get("version", "v1")
|
121 |
-
|
122 |
-
if version == "v1":
|
123 |
-
if if_f0 == 1:
|
124 |
-
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=is_half)
|
125 |
-
else:
|
126 |
-
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
127 |
-
elif version == "v2":
|
128 |
-
if if_f0 == 1:
|
129 |
-
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=is_half)
|
130 |
-
else:
|
131 |
-
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
132 |
-
|
133 |
-
del net_g.enc_q
|
134 |
-
print(net_g.load_state_dict(cpt["weight"], strict=False))
|
135 |
-
net_g.eval().to(device)
|
136 |
-
|
137 |
-
if is_half:
|
138 |
-
net_g = net_g.half()
|
139 |
-
else:
|
140 |
-
net_g = net_g.float()
|
141 |
-
|
142 |
-
vc = VC(tgt_sr, config)
|
143 |
-
return cpt, version, net_g, tgt_sr, vc
|
144 |
-
|
145 |
-
|
146 |
-
def rvc_infer(index_path, index_rate, input_path, output_path, pitch_change, f0_method, cpt, version, net_g, filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model):
|
147 |
-
audio = load_audio(input_path, 16000)
|
148 |
-
times = [0, 0, 0]
|
149 |
-
if_f0 = cpt.get('f0', 1)
|
150 |
-
audio_opt = vc.pipeline(hubert_model, net_g, 0, audio, input_path, times, pitch_change, f0_method, index_path, index_rate, if_f0, filter_radius, tgt_sr, 0, rms_mix_rate, version, protect, crepe_hop_length)
|
151 |
-
wavfile.write(output_path, tgt_sr, audio_opt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|