Spaces:
Paused
Paused
Update rvc/lib/utils.py
Browse files- rvc/lib/utils.py +66 -64
rvc/lib/utils.py
CHANGED
|
@@ -1,64 +1,66 @@
|
|
| 1 |
-
import os, sys
|
| 2 |
-
import ffmpeg
|
| 3 |
-
import numpy as np
|
| 4 |
-
import re
|
| 5 |
-
import unicodedata
|
| 6 |
-
from fairseq import checkpoint_utils
|
| 7 |
-
|
| 8 |
-
import logging
|
| 9 |
-
|
| 10 |
-
logging.getLogger("fairseq").setLevel(logging.WARNING)
|
| 11 |
-
|
| 12 |
-
now_dir = os.getcwd()
|
| 13 |
-
sys.path.append(now_dir)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def load_audio(file, sampling_rate):
|
| 17 |
-
try:
|
| 18 |
-
file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
| 19 |
-
out, _ = (
|
| 20 |
-
ffmpeg.input(file, threads=0)
|
| 21 |
-
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sampling_rate)
|
| 22 |
-
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
| 23 |
-
)
|
| 24 |
-
except Exception as error:
|
| 25 |
-
raise RuntimeError(f"Failed to load audio: {error}")
|
| 26 |
-
|
| 27 |
-
return np.frombuffer(out, np.float32).flatten()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def format_title(title):
|
| 31 |
-
formatted_title = (
|
| 32 |
-
unicodedata.normalize("NFKD", title).encode("ascii", "ignore").decode("utf-8")
|
| 33 |
-
)
|
| 34 |
-
formatted_title = re.sub(r"[\u2500-\u257F]+", "", formatted_title)
|
| 35 |
-
formatted_title = re.sub(r"[^\w\s.-]", "", formatted_title)
|
| 36 |
-
formatted_title = re.sub(r"\s+", "_", formatted_title)
|
| 37 |
-
return formatted_title
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, sys
|
| 2 |
+
import ffmpeg
|
| 3 |
+
import numpy as np
|
| 4 |
+
import re
|
| 5 |
+
import unicodedata
|
| 6 |
+
from fairseq import checkpoint_utils
|
| 7 |
+
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
logging.getLogger("fairseq").setLevel(logging.WARNING)
|
| 11 |
+
|
| 12 |
+
now_dir = os.getcwd()
|
| 13 |
+
sys.path.append(now_dir)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def load_audio(file, sampling_rate):
|
| 17 |
+
try:
|
| 18 |
+
file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
|
| 19 |
+
out, _ = (
|
| 20 |
+
ffmpeg.input(file, threads=0)
|
| 21 |
+
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sampling_rate)
|
| 22 |
+
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
| 23 |
+
)
|
| 24 |
+
except Exception as error:
|
| 25 |
+
raise RuntimeError(f"Failed to load audio: {error}")
|
| 26 |
+
|
| 27 |
+
return np.frombuffer(out, np.float32).flatten()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def format_title(title):
|
| 31 |
+
formatted_title = (
|
| 32 |
+
unicodedata.normalize("NFKD", title).encode("ascii", "ignore").decode("utf-8")
|
| 33 |
+
)
|
| 34 |
+
formatted_title = re.sub(r"[\u2500-\u257F]+", "", formatted_title)
|
| 35 |
+
formatted_title = re.sub(r"[^\w\s.-]", "", formatted_title)
|
| 36 |
+
formatted_title = re.sub(r"\s+", "_", formatted_title)
|
| 37 |
+
return formatted_title
|
| 38 |
+
|
| 39 |
+
import spaces
|
| 40 |
+
|
| 41 |
+
@spaces.GPU
|
| 42 |
+
def load_embedding(embedder_model, custom_embedder=None):
|
| 43 |
+
embedder_root = os.path.join(now_dir, "rvc", "embedders")
|
| 44 |
+
embedding_list = {
|
| 45 |
+
"contentvec": os.path.join(embedder_root, "contentvec_base.pt"),
|
| 46 |
+
"hubert": os.path.join(embedder_root, "hubert_base.pt"),
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
if embedder_model == "custom":
|
| 50 |
+
model_path = custom_embedder
|
| 51 |
+
if not custom_embedder and os.path.exists(custom_embedder):
|
| 52 |
+
print("Custom embedder not found. Using the default embedder.")
|
| 53 |
+
model_path = embedding_list["hubert"]
|
| 54 |
+
else:
|
| 55 |
+
model_path = embedding_list[embedder_model]
|
| 56 |
+
if not os.path.exists(model_path):
|
| 57 |
+
print("Custom embedder not found. Using the default embedder.")
|
| 58 |
+
model_path = embedding_list["hubert"]
|
| 59 |
+
|
| 60 |
+
models = checkpoint_utils.load_model_ensemble_and_task(
|
| 61 |
+
[model_path],
|
| 62 |
+
suffix="",
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
print(f"Embedding model {embedder_model} loaded successfully.")
|
| 66 |
+
return models
|