Spaces:
Running on Zero
Running on Zero
Update src/rvc.py
Browse files9 unmodified lines10111213141591 unmodified lines1071081091101111121131141151161171181191201211229 unmodified lines)from my_utils import load_audiofrom vc_infer_pipeline import VCBASE_DIR = Path(__file__).resolve().parent.parent91 unmodified linesreturn x_pad, x_query, x_center, x_maxdef load_hubert(device, is_half, model_path):from fairseq import checkpoint_utilsimport fairseqtorch.serialization.add_safe_globals([fairseq.data.dictionary.Dictionary])models, saved_cfg, task = checkpoint_utils.load_model_ensemble_and_task([model_path], suffix='', )hubert = models[0]hubert = hubert.to(device)if is_half:hubert = hubert.half()else:hubert = hubert.float()9 unmodified lines10111213141516171891 unmodified lines1101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631649 unmodified lines)from my_utils import load_audiofrom vc_infer_pipeline import VCimport osimport torch.nn as nnfrom transformers import HubertConfig, HubertModelBASE_DIR = Path(__file__).resolve().parent.parent91 unmodified linesreturn x_pad, x_query, x_center, x_maxclass HubertModelWithFinalProj(HubertModel):def __init__(self, config):super().__init__(config)self.final_proj = nn.Linear(config.hidden_size, config.classifier_proj_size)class FairseqHubertWrapper(nn.Module):def __init__(self, model_path_or_name="r3gm/hubert_base"):super().__init__()try:self.model = HubertModelWithFinalProj.from_pretrained(model_path_or_name)except Exception:self.model = HubertModel.from_pretrained(model_path_or_name)def extract_features(self, source, padding_mask=None, output_layer=12, **kwargs):param_dtype = next(self.model.parameters()).dtypeif source.dtype != param_dtype:source = source.to(param_dtype)if source.dim() == 1:source = source.unsqueeze(0)with torch.no_grad():outputs = self.model(source, output_hidden_states=True)# 9 for v1, 12 for v2/ContentVecif output_layer is None or output_layer >= len(outputs.hidden_states):hidden_state = outputs.hidden_states[-1]else:hidden_state = outputs.hidden_states[output_layer]return (hidden_state, None)def final_proj(self, x):if hasattr(self.model, "final_proj"):return self.model.final_proj(x)return xdef load_hubert(device, is_half, model_path=None):if model_path and (os.path.exists(model_path) or os.path.isdir(model_path)):target_path = model_pathelse:target_path = "r3gm/hubert_base"hubert = FairseqHubertWrapper(target_path)hubert = hubert.to(device)if is_half and torch.device(device).type != "cpu":hubert = hubert.half()else:hubert = hubert.float()
src/rvc.py+49-7