So-VITS-SVC / app.py
None1145's picture
add
56e5a69
raw
history blame
5.05 kB
model = None
sid = ""
import io
import gradio as gr
import librosa
import numpy as np
import soundfile
from inference.infer_tool import Svc
import os
def list_files_tree(directory, indent=""):
items = os.listdir(directory)
for i, item in enumerate(items):
prefix = "└── " if i == len(items) - 1 else "├── "
print(indent + prefix + item)
item_path = os.path.join(directory, item)
if os.path.isdir(item_path):
next_indent = indent + (" " if i == len(items) - 1 else "│ ")
list_files_tree(item_path, next_indent)
from huggingface_hub import snapshot_download
print("Models...")
models_id = """None1145/So-VITS-SVC-Vulpisfoglia"""
for model_id in models_id.split("\n"):
if model_id in ["", " "]:
break
print(f"{model_id}...")
snapshot_download(repo_id=model_id, local_dir=f"./Models/{model_id}")
print(f"{model_id}!!!")
print("Models!!!")
list_files_tree("./")
import re
models_info = {}
models_folder_path = "./Models/None1145"
folder_names = [name for name in os.listdir(models_folder_path) if os.path.isdir(os.path.join(models_folder_path, name))]
for folder_name in folder_names:
speaker = folder_name[12:]
pattern = re.compile(r"G_(\d+)\.pth$")
max_value = -1
max_file = None
models_path = f"{models_folder_path}/{folder_name}/Models"
config_path = f"{models_folder_path}/{folder_name}/Configs"
for filename in os.listdir(models_path):
match = pattern.search(filename)
if match:
value = int(match.group(1))
if value > max_value:
max_value = value
max_file = filename
models_info[speaker] = {}
models_info[speaker]["model"] = f"{models_path}/{max_file}"
models_info[speaker]["config"] = f"{config_path}/config.json"
if os.path.exists(f"{models_path}/feature_and_index.pkl"):
models_info[speaker]["cluster"] = f"{models_path}/feature_and_index.pkl"
elif os.path.exists(f"{models_path}/kmeans_10000.pt"):
models_info[speaker]["cluster"] = f"{models_path}/kmeans_10000.pt"
else:
models_info[speaker]["cluster"] = ""
speakers = list(models_info.keys())
def load(speaker):
global sid
global model
sid = speaker
model = Svc(models_info[speaker]["model"], models_info[speaker]["config"], cluster_model_path=models_info[speaker]["cluster"])
return "加载成功"
load(speakers[0])
def vc_fn(input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise_scale):
global sid
if input_audio is None:
return "You need to upload an audio", None
sampling_rate, audio = input_audio
# print(audio.shape,sampling_rate)
duration = audio.shape[0] / sampling_rate
# if duration > 90:
# return "请上传小于90s的音频,需要转换长音频请本地进行转换", None
audio = (audio / np.iinfo(audio.dtype).max).astype(np.float32)
if len(audio.shape) > 1:
audio = librosa.to_mono(audio.transpose(1, 0))
if sampling_rate != 16000:
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
print(audio.shape)
out_wav_path = "temp.wav"
soundfile.write(out_wav_path, audio, 16000, format="wav")
print( cluster_ratio, auto_f0, noise_scale)
_audio = model.slice_inference(out_wav_path, sid, vc_transform, slice_db, cluster_ratio, auto_f0, noise_scale)
return "Success", (44100, _audio)
app = gr.Blocks()
with app:
with gr.Tabs():
with gr.TabItem("Model"):
speaker = gr.Dropdown(label="讲话人", choices=speakers, value=speakers[0])
model_submit = gr.Button("加载模型", variant="primary")
model_output1 = gr.Textbox(label="Output Message")
model_submit.click(load, [speaker], [model_output1])
with gr.TabItem("Basic"):
# sid = gr.Dropdown(label="音色", choices=speakers, value=speakers[0])
vc_input3 = gr.Audio(label="上传音频")
vc_transform = gr.Number(label="变调(整数,可以正负,半音数量,升高八度就是12)", value=0)
cluster_ratio = gr.Number(label="聚类模型混合比例,0-1之间,默认为0不启用聚类,能提升音色相似度,但会导致咬字下降(如果使用建议0.5左右)", value=0)
auto_f0 = gr.Checkbox(label="自动f0预测,配合聚类模型f0预测效果更好,会导致变调功能失效(仅限转换语音,歌声不要勾选此项会究极跑调)", value=False)
slice_db = gr.Number(label="切片阈值", value=-40)
noise_scale = gr.Number(label="noise_scale 建议不要动,会影响音质,玄学参数", value=0.4)
vc_submit = gr.Button("转换", variant="primary")
vc_output1 = gr.Textbox(label="Output Message")
vc_output2 = gr.Audio(label="Output Audio")
vc_submit.click(vc_fn, [vc_input3, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale], [vc_output1, vc_output2])
app.launch()