Spaces:
Running on Zero
Running on Zero
Update src/rvc.py
Browse files- src/rvc.py +49 -7
src/rvc.py
CHANGED
|
@@ -10,6 +10,9 @@ from infer_pack.models import (
|
|
| 10 |
)
|
| 11 |
from my_utils import load_audio
|
| 12 |
from vc_infer_pipeline import VC
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
BASE_DIR = Path(__file__).resolve().parent.parent
|
| 15 |
|
|
@@ -107,16 +110,55 @@ class Config:
|
|
| 107 |
return x_pad, x_query, x_center, x_max
|
| 108 |
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
hubert = hubert.to(device)
|
| 118 |
|
| 119 |
-
if is_half:
|
| 120 |
hubert = hubert.half()
|
| 121 |
else:
|
| 122 |
hubert = hubert.float()
|
|
|
|
| 10 |
)
|
| 11 |
from my_utils import load_audio
|
| 12 |
from vc_infer_pipeline import VC
|
| 13 |
+
import os
|
| 14 |
+
import torch.nn as nn
|
| 15 |
+
from transformers import HubertConfig, HubertModel
|
| 16 |
|
| 17 |
BASE_DIR = Path(__file__).resolve().parent.parent
|
| 18 |
|
|
|
|
| 110 |
return x_pad, x_query, x_center, x_max
|
| 111 |
|
| 112 |
|
| 113 |
+
class HubertModelWithFinalProj(HubertModel):
|
| 114 |
+
def __init__(self, config):
|
| 115 |
+
super().__init__(config)
|
| 116 |
+
self.final_proj = nn.Linear(config.hidden_size, config.classifier_proj_size)
|
| 117 |
|
| 118 |
+
|
| 119 |
+
class FairseqHubertWrapper(nn.Module):
|
| 120 |
+
def __init__(self, model_path_or_name="r3gm/hubert_base"):
|
| 121 |
+
super().__init__()
|
| 122 |
+
try:
|
| 123 |
+
self.model = HubertModelWithFinalProj.from_pretrained(model_path_or_name)
|
| 124 |
+
except Exception:
|
| 125 |
+
self.model = HubertModel.from_pretrained(model_path_or_name)
|
| 126 |
+
|
| 127 |
+
def extract_features(self, source, padding_mask=None, output_layer=12, **kwargs):
|
| 128 |
+
param_dtype = next(self.model.parameters()).dtype
|
| 129 |
+
if source.dtype != param_dtype:
|
| 130 |
+
source = source.to(param_dtype)
|
| 131 |
+
|
| 132 |
+
if source.dim() == 1:
|
| 133 |
+
source = source.unsqueeze(0)
|
| 134 |
+
|
| 135 |
+
with torch.no_grad():
|
| 136 |
+
outputs = self.model(source, output_hidden_states=True)
|
| 137 |
+
|
| 138 |
+
# 9 for v1, 12 for v2/ContentVec
|
| 139 |
+
if output_layer is None or output_layer >= len(outputs.hidden_states):
|
| 140 |
+
hidden_state = outputs.hidden_states[-1]
|
| 141 |
+
else:
|
| 142 |
+
hidden_state = outputs.hidden_states[output_layer]
|
| 143 |
+
|
| 144 |
+
return (hidden_state, None)
|
| 145 |
+
|
| 146 |
+
def final_proj(self, x):
|
| 147 |
+
if hasattr(self.model, "final_proj"):
|
| 148 |
+
return self.model.final_proj(x)
|
| 149 |
+
return x
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def load_hubert(device, is_half, model_path=None):
|
| 153 |
+
if model_path and (os.path.exists(model_path) or os.path.isdir(model_path)):
|
| 154 |
+
target_path = model_path
|
| 155 |
+
else:
|
| 156 |
+
target_path = "r3gm/hubert_base"
|
| 157 |
+
|
| 158 |
+
hubert = FairseqHubertWrapper(target_path)
|
| 159 |
hubert = hubert.to(device)
|
| 160 |
|
| 161 |
+
if is_half and torch.device(device).type != "cpu":
|
| 162 |
hubert = hubert.half()
|
| 163 |
else:
|
| 164 |
hubert = hubert.float()
|