Clean up previous raw files
Browse files- Zero-CPU/infer.py +0 -306
Zero-CPU/infer.py
DELETED
|
@@ -1,306 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
-
import commons
|
| 4 |
-
import utils
|
| 5 |
-
from models import SynthesizerTrn
|
| 6 |
-
from models_jp_extra import SynthesizerTrn as SynthesizerTrnJPExtra
|
| 7 |
-
from text import cleaned_text_to_sequence, get_bert
|
| 8 |
-
from text.cleaner import clean_text
|
| 9 |
-
from text.symbols import symbols
|
| 10 |
-
from common.log import logger
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
class InvalidToneError(ValueError):
|
| 14 |
-
pass
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def get_net_g(model_path: str, version: str, device: str, hps):
|
| 18 |
-
if version.endswith("JP-Extra"):
|
| 19 |
-
#logger.info("Using JP-Extra model")
|
| 20 |
-
net_g = SynthesizerTrnJPExtra(
|
| 21 |
-
len(symbols),
|
| 22 |
-
hps.data.filter_length // 2 + 1,
|
| 23 |
-
hps.train.segment_size // hps.data.hop_length,
|
| 24 |
-
n_speakers=hps.data.n_speakers,
|
| 25 |
-
**hps.model,
|
| 26 |
-
).to(device)
|
| 27 |
-
else:
|
| 28 |
-
#logger.info("Using normal model")
|
| 29 |
-
net_g = SynthesizerTrn(
|
| 30 |
-
len(symbols),
|
| 31 |
-
hps.data.filter_length // 2 + 1,
|
| 32 |
-
hps.train.segment_size // hps.data.hop_length,
|
| 33 |
-
n_speakers=hps.data.n_speakers,
|
| 34 |
-
**hps.model,
|
| 35 |
-
).to(device)
|
| 36 |
-
net_g.state_dict()
|
| 37 |
-
_ = net_g.eval()
|
| 38 |
-
if model_path.endswith(".pth") or model_path.endswith(".pt"):
|
| 39 |
-
_ = utils.load_checkpoint(model_path, net_g, None, skip_optimizer=True)
|
| 40 |
-
elif model_path.endswith(".safetensors"):
|
| 41 |
-
_ = utils.load_safetensors(model_path, net_g, True)
|
| 42 |
-
else:
|
| 43 |
-
raise ValueError(f"Unknown model format: {model_path}")
|
| 44 |
-
return net_g
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def get_text(
|
| 48 |
-
text,
|
| 49 |
-
language_str,
|
| 50 |
-
hps,
|
| 51 |
-
device,
|
| 52 |
-
assist_text=None,
|
| 53 |
-
assist_text_weight=0.7,
|
| 54 |
-
given_tone=None,
|
| 55 |
-
):
|
| 56 |
-
use_jp_extra = hps.version.endswith("JP-Extra")
|
| 57 |
-
norm_text, phone, tone, word2ph = clean_text(text, language_str, use_jp_extra)
|
| 58 |
-
if given_tone is not None:
|
| 59 |
-
if len(given_tone) != len(phone):
|
| 60 |
-
raise InvalidToneError(
|
| 61 |
-
f"Length of given_tone ({len(given_tone)}) != length of phone ({len(phone)})"
|
| 62 |
-
)
|
| 63 |
-
tone = given_tone
|
| 64 |
-
phone, tone, language = cleaned_text_to_sequence(phone, tone, language_str)
|
| 65 |
-
|
| 66 |
-
if hps.data.add_blank:
|
| 67 |
-
phone = commons.intersperse(phone, 0)
|
| 68 |
-
tone = commons.intersperse(tone, 0)
|
| 69 |
-
language = commons.intersperse(language, 0)
|
| 70 |
-
for i in range(len(word2ph)):
|
| 71 |
-
word2ph[i] = word2ph[i] * 2
|
| 72 |
-
word2ph[0] += 1
|
| 73 |
-
bert_ori = get_bert(
|
| 74 |
-
norm_text, word2ph, language_str, device, assist_text, assist_text_weight
|
| 75 |
-
)
|
| 76 |
-
del word2ph
|
| 77 |
-
assert bert_ori.shape[-1] == len(phone), phone
|
| 78 |
-
|
| 79 |
-
if language_str == "ZH":
|
| 80 |
-
bert = bert_ori
|
| 81 |
-
ja_bert = torch.zeros(1024, len(phone))
|
| 82 |
-
en_bert = torch.zeros(1024, len(phone))
|
| 83 |
-
elif language_str == "JP":
|
| 84 |
-
bert = torch.zeros(1024, len(phone))
|
| 85 |
-
ja_bert = bert_ori
|
| 86 |
-
en_bert = torch.zeros(1024, len(phone))
|
| 87 |
-
elif language_str == "EN":
|
| 88 |
-
bert = torch.zeros(1024, len(phone))
|
| 89 |
-
ja_bert = torch.zeros(1024, len(phone))
|
| 90 |
-
en_bert = bert_ori
|
| 91 |
-
else:
|
| 92 |
-
raise ValueError("language_str should be ZH, JP or EN")
|
| 93 |
-
|
| 94 |
-
assert bert.shape[-1] == len(
|
| 95 |
-
phone
|
| 96 |
-
), f"Bert seq len {bert.shape[-1]} != {len(phone)}"
|
| 97 |
-
|
| 98 |
-
phone = torch.LongTensor(phone)
|
| 99 |
-
tone = torch.LongTensor(tone)
|
| 100 |
-
language = torch.LongTensor(language)
|
| 101 |
-
return bert, ja_bert, en_bert, phone, tone, language
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
def infer(
|
| 105 |
-
text,
|
| 106 |
-
style_vec,
|
| 107 |
-
sdp_ratio,
|
| 108 |
-
noise_scale,
|
| 109 |
-
noise_scale_w,
|
| 110 |
-
length_scale,
|
| 111 |
-
sid: int, # In the original Bert-VITS2, its speaker_name: str, but here it's id
|
| 112 |
-
language,
|
| 113 |
-
hps,
|
| 114 |
-
net_g,
|
| 115 |
-
device,
|
| 116 |
-
skip_start=False,
|
| 117 |
-
skip_end=False,
|
| 118 |
-
assist_text=None,
|
| 119 |
-
assist_text_weight=0.7,
|
| 120 |
-
given_tone=None,
|
| 121 |
-
):
|
| 122 |
-
is_jp_extra = hps.version.endswith("JP-Extra")
|
| 123 |
-
bert, ja_bert, en_bert, phones, tones, lang_ids = get_text(
|
| 124 |
-
text,
|
| 125 |
-
language,
|
| 126 |
-
hps,
|
| 127 |
-
device,
|
| 128 |
-
assist_text=assist_text,
|
| 129 |
-
assist_text_weight=assist_text_weight,
|
| 130 |
-
given_tone=given_tone,
|
| 131 |
-
)
|
| 132 |
-
if skip_start:
|
| 133 |
-
phones = phones[3:]
|
| 134 |
-
tones = tones[3:]
|
| 135 |
-
lang_ids = lang_ids[3:]
|
| 136 |
-
bert = bert[:, 3:]
|
| 137 |
-
ja_bert = ja_bert[:, 3:]
|
| 138 |
-
en_bert = en_bert[:, 3:]
|
| 139 |
-
if skip_end:
|
| 140 |
-
phones = phones[:-2]
|
| 141 |
-
tones = tones[:-2]
|
| 142 |
-
lang_ids = lang_ids[:-2]
|
| 143 |
-
bert = bert[:, :-2]
|
| 144 |
-
ja_bert = ja_bert[:, :-2]
|
| 145 |
-
en_bert = en_bert[:, :-2]
|
| 146 |
-
with torch.no_grad():
|
| 147 |
-
x_tst = phones.to(device).unsqueeze(0)
|
| 148 |
-
tones = tones.to(device).unsqueeze(0)
|
| 149 |
-
lang_ids = lang_ids.to(device).unsqueeze(0)
|
| 150 |
-
bert = bert.to(device).unsqueeze(0)
|
| 151 |
-
ja_bert = ja_bert.to(device).unsqueeze(0)
|
| 152 |
-
en_bert = en_bert.to(device).unsqueeze(0)
|
| 153 |
-
x_tst_lengths = torch.LongTensor([phones.size(0)]).to(device)
|
| 154 |
-
style_vec = torch.from_numpy(style_vec).to(device).unsqueeze(0)
|
| 155 |
-
del phones
|
| 156 |
-
sid_tensor = torch.LongTensor([sid]).to(device)
|
| 157 |
-
if is_jp_extra:
|
| 158 |
-
output = net_g.infer(
|
| 159 |
-
x_tst,
|
| 160 |
-
x_tst_lengths,
|
| 161 |
-
sid_tensor,
|
| 162 |
-
tones,
|
| 163 |
-
lang_ids,
|
| 164 |
-
ja_bert,
|
| 165 |
-
style_vec=style_vec,
|
| 166 |
-
sdp_ratio=sdp_ratio,
|
| 167 |
-
noise_scale=noise_scale,
|
| 168 |
-
noise_scale_w=noise_scale_w,
|
| 169 |
-
length_scale=length_scale,
|
| 170 |
-
)
|
| 171 |
-
else:
|
| 172 |
-
output = net_g.infer(
|
| 173 |
-
x_tst,
|
| 174 |
-
x_tst_lengths,
|
| 175 |
-
sid_tensor,
|
| 176 |
-
tones,
|
| 177 |
-
lang_ids,
|
| 178 |
-
bert,
|
| 179 |
-
ja_bert,
|
| 180 |
-
en_bert,
|
| 181 |
-
style_vec=style_vec,
|
| 182 |
-
sdp_ratio=sdp_ratio,
|
| 183 |
-
noise_scale=noise_scale,
|
| 184 |
-
noise_scale_w=noise_scale_w,
|
| 185 |
-
length_scale=length_scale,
|
| 186 |
-
)
|
| 187 |
-
audio = output[0][0, 0].data.cpu().float().numpy()
|
| 188 |
-
del (
|
| 189 |
-
x_tst,
|
| 190 |
-
tones,
|
| 191 |
-
lang_ids,
|
| 192 |
-
bert,
|
| 193 |
-
x_tst_lengths,
|
| 194 |
-
sid_tensor,
|
| 195 |
-
ja_bert,
|
| 196 |
-
en_bert,
|
| 197 |
-
style_vec,
|
| 198 |
-
) # , emo
|
| 199 |
-
if torch.cuda.is_available():
|
| 200 |
-
torch.cuda.empty_cache()
|
| 201 |
-
return audio
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
def infer_multilang(
|
| 205 |
-
text,
|
| 206 |
-
style_vec,
|
| 207 |
-
sdp_ratio,
|
| 208 |
-
noise_scale,
|
| 209 |
-
noise_scale_w,
|
| 210 |
-
length_scale,
|
| 211 |
-
sid,
|
| 212 |
-
language,
|
| 213 |
-
hps,
|
| 214 |
-
net_g,
|
| 215 |
-
device,
|
| 216 |
-
skip_start=False,
|
| 217 |
-
skip_end=False,
|
| 218 |
-
):
|
| 219 |
-
bert, ja_bert, en_bert, phones, tones, lang_ids = [], [], [], [], [], []
|
| 220 |
-
# emo = get_emo_(reference_audio, emotion, sid)
|
| 221 |
-
# if isinstance(reference_audio, np.ndarray):
|
| 222 |
-
# emo = get_clap_audio_feature(reference_audio, device)
|
| 223 |
-
# else:
|
| 224 |
-
# emo = get_clap_text_feature(emotion, device)
|
| 225 |
-
# emo = torch.squeeze(emo, dim=1)
|
| 226 |
-
for idx, (txt, lang) in enumerate(zip(text, language)):
|
| 227 |
-
_skip_start = (idx != 0) or (skip_start and idx == 0)
|
| 228 |
-
_skip_end = (idx != len(language) - 1) or skip_end
|
| 229 |
-
(
|
| 230 |
-
temp_bert,
|
| 231 |
-
temp_ja_bert,
|
| 232 |
-
temp_en_bert,
|
| 233 |
-
temp_phones,
|
| 234 |
-
temp_tones,
|
| 235 |
-
temp_lang_ids,
|
| 236 |
-
) = get_text(txt, lang, hps, device)
|
| 237 |
-
if _skip_start:
|
| 238 |
-
temp_bert = temp_bert[:, 3:]
|
| 239 |
-
temp_ja_bert = temp_ja_bert[:, 3:]
|
| 240 |
-
temp_en_bert = temp_en_bert[:, 3:]
|
| 241 |
-
temp_phones = temp_phones[3:]
|
| 242 |
-
temp_tones = temp_tones[3:]
|
| 243 |
-
temp_lang_ids = temp_lang_ids[3:]
|
| 244 |
-
if _skip_end:
|
| 245 |
-
temp_bert = temp_bert[:, :-2]
|
| 246 |
-
temp_ja_bert = temp_ja_bert[:, :-2]
|
| 247 |
-
temp_en_bert = temp_en_bert[:, :-2]
|
| 248 |
-
temp_phones = temp_phones[:-2]
|
| 249 |
-
temp_tones = temp_tones[:-2]
|
| 250 |
-
temp_lang_ids = temp_lang_ids[:-2]
|
| 251 |
-
bert.append(temp_bert)
|
| 252 |
-
ja_bert.append(temp_ja_bert)
|
| 253 |
-
en_bert.append(temp_en_bert)
|
| 254 |
-
phones.append(temp_phones)
|
| 255 |
-
tones.append(temp_tones)
|
| 256 |
-
lang_ids.append(temp_lang_ids)
|
| 257 |
-
bert = torch.concatenate(bert, dim=1)
|
| 258 |
-
ja_bert = torch.concatenate(ja_bert, dim=1)
|
| 259 |
-
en_bert = torch.concatenate(en_bert, dim=1)
|
| 260 |
-
phones = torch.concatenate(phones, dim=0)
|
| 261 |
-
tones = torch.concatenate(tones, dim=0)
|
| 262 |
-
lang_ids = torch.concatenate(lang_ids, dim=0)
|
| 263 |
-
with torch.no_grad():
|
| 264 |
-
x_tst = phones.to(device).unsqueeze(0)
|
| 265 |
-
tones = tones.to(device).unsqueeze(0)
|
| 266 |
-
lang_ids = lang_ids.to(device).unsqueeze(0)
|
| 267 |
-
bert = bert.to(device).unsqueeze(0)
|
| 268 |
-
ja_bert = ja_bert.to(device).unsqueeze(0)
|
| 269 |
-
en_bert = en_bert.to(device).unsqueeze(0)
|
| 270 |
-
# emo = emo.to(device).unsqueeze(0)
|
| 271 |
-
x_tst_lengths = torch.LongTensor([phones.size(0)]).to(device)
|
| 272 |
-
del phones
|
| 273 |
-
speakers = torch.LongTensor([hps.data.spk2id[sid]]).to(device)
|
| 274 |
-
audio = (
|
| 275 |
-
net_g.infer(
|
| 276 |
-
x_tst,
|
| 277 |
-
x_tst_lengths,
|
| 278 |
-
speakers,
|
| 279 |
-
tones,
|
| 280 |
-
lang_ids,
|
| 281 |
-
bert,
|
| 282 |
-
ja_bert,
|
| 283 |
-
en_bert,
|
| 284 |
-
style_vec=style_vec,
|
| 285 |
-
sdp_ratio=sdp_ratio,
|
| 286 |
-
noise_scale=noise_scale,
|
| 287 |
-
noise_scale_w=noise_scale_w,
|
| 288 |
-
length_scale=length_scale,
|
| 289 |
-
)[0][0, 0]
|
| 290 |
-
.data.cpu()
|
| 291 |
-
.float()
|
| 292 |
-
.numpy()
|
| 293 |
-
)
|
| 294 |
-
del (
|
| 295 |
-
x_tst,
|
| 296 |
-
tones,
|
| 297 |
-
lang_ids,
|
| 298 |
-
bert,
|
| 299 |
-
x_tst_lengths,
|
| 300 |
-
speakers,
|
| 301 |
-
ja_bert,
|
| 302 |
-
en_bert,
|
| 303 |
-
) # , emo
|
| 304 |
-
if torch.cuda.is_available():
|
| 305 |
-
torch.cuda.empty_cache()
|
| 306 |
-
return audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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