Merge b122988be0a4c47d81a731dead738a0a75233d37 into 13055fa56994e75a7152c176047c56c62bbeede4

This commit is contained in:
Karasukaigan 2025-05-09 03:25:23 +08:00 committed by GitHub
commit 1e3c1e04ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 42 additions and 24 deletions

View File

@ -287,8 +287,9 @@ class TTS_Config:
configs: dict = self._load_configs(self.configs_path)
assert isinstance(configs, dict)
version = configs.get("version", "v2").lower()
assert version in ["v1", "v2", "v3", "v4"]
version = "v2"
if "custom" in configs and configs["custom"]["version"].lower() in ["v1", "v2", "v3", "v4"]:
version = configs["custom"]["version"].lower()
self.default_configs[version] = configs.get(version, self.default_configs[version])
self.configs: dict = configs.get("custom", deepcopy(self.default_configs[version]))

View File

@ -91,6 +91,8 @@ infer_ttswebui = os.environ.get("infer_ttswebui", 9872)
infer_ttswebui = int(infer_ttswebui)
is_share = os.environ.get("is_share", "False")
is_share = eval(is_share)
local_mode = os.environ.get("local_mode", "False")
local_mode = eval(local_mode)
if "_CUDA_VISIBLE_DEVICES" in os.environ:
os.environ["CUDA_VISIBLE_DEVICES"] = os.environ["_CUDA_VISIBLE_DEVICES"]
is_half = eval(os.environ.get("is_half", "True")) and torch.cuda.is_available()
@ -1273,7 +1275,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI") as app:
if __name__ == "__main__":
app.queue().launch( # concurrency_count=511, max_size=1022
server_name="0.0.0.0",
server_name="127.0.0.1" if local_mode else "0.0.0.0",
inbrowser=True,
share=is_share,
server_port=infer_ttswebui,

40
api.py
View File

@ -176,9 +176,9 @@ import subprocess
class DefaultRefer:
def __init__(self, path, text, language):
self.path = args.default_refer_path
self.text = args.default_refer_text
self.language = args.default_refer_language
self.path = path
self.text = text
self.language = language
def is_ready(self) -> bool:
return is_full(self.path, self.text, self.language)
@ -294,11 +294,13 @@ from process_ckpt import get_sovits_version_from_path_fast, load_sovits_new
def get_sovits_weights(sovits_path):
path_sovits_v3 = "GPT_SoVITS/pretrained_models/s2Gv3.pth"
is_exist_s2gv3 = os.path.exists(path_sovits_v3)
path_sovits_v4 = "GPT_SoVITS/pretrained_models/gsv-v4-pretrained/s2Gv4.pth"
version, model_version, if_lora_v3 = get_sovits_version_from_path_fast(sovits_path)
if if_lora_v3 == True and is_exist_s2gv3 == False:
if if_lora_v3 == True and not os.path.exists(path_sovits_v3):
logger.info("SoVITS V3 底模缺失,无法加载相应 LoRA 权重")
if model_version == "v4" and not os.path.exists(path_sovits_v4):
logger.info("SoVITS V4 底模缺失,无法加载相应 LoRA 权重")
dict_s2 = load_sovits_new(sovits_path)
hps = dict_s2["config"]
@ -310,12 +312,13 @@ def get_sovits_weights(sovits_path):
hps.model.version = "v1"
else:
hps.model.version = "v2"
if model_version == "v3":
hps.model.version = "v3"
if model_version == "v4":
hps.model.version = "v4"
model_params_dict = vars(hps.model)
if model_version != "v3":
if model_version != "v3" and model_version != "v4":
vq_model = SynthesizerTrn(
hps.data.filter_length // 2 + 1,
hps.train.segment_size // hps.data.hop_length,
@ -342,10 +345,13 @@ def get_sovits_weights(sovits_path):
else:
vq_model = vq_model.to(device)
vq_model.eval()
if if_lora_v3 == False:
if if_lora_v3 == False or model_version != "v4":
vq_model.load_state_dict(dict_s2["weight"], strict=False)
else:
vq_model.load_state_dict(load_sovits_new(path_sovits_v3)["weight"], strict=False)
if model_version == "v4":
vq_model.load_state_dict(load_sovits_new(path_sovits_v4)["weight"], strict=False)
else:
vq_model.load_state_dict(load_sovits_new(path_sovits_v3)["weight"], strict=False)
lora_rank = dict_s2["lora_rank"]
lora_config = LoraConfig(
target_modules=["to_k", "to_q", "to_v", "to_out.0"],
@ -394,13 +400,11 @@ def change_gpt_sovits_weights(gpt_path, sovits_path):
try:
gpt = get_gpt_weights(gpt_path)
sovits = get_sovits_weights(sovits_path)
speaker_list["default"] = Speaker(name="default", gpt=gpt, sovits=sovits)
return JSONResponse({"code": 0, "message": "Success"}, status_code=200)
except Exception as e:
return JSONResponse({"code": 400, "message": str(e)}, status_code=400)
speaker_list["default"] = Speaker(name="default", gpt=gpt, sovits=sovits)
return JSONResponse({"code": 0, "message": "Success"}, status_code=200)
def get_bert_feature(text, word2ph):
with torch.no_grad():
inputs = tokenizer(text, return_tensors="pt")
@ -759,7 +763,7 @@ def get_tts_wav(
prompt_semantic = codes[0, 0]
prompt = prompt_semantic.unsqueeze(0).to(device)
if version != "v3":
if version != "v3" and version != "v4":
refers = []
if inp_refs:
for path in inp_refs:
@ -811,7 +815,7 @@ def get_tts_wav(
pred_semantic = pred_semantic[:, -idx:].unsqueeze(0)
t3 = ttime()
if version != "v3":
if version != "v3" and version != "v4":
audio = (
vq_model.decode(pred_semantic, torch.LongTensor(phones2).to(device).unsqueeze(0), refers, speed=speed)
.detach()
@ -880,7 +884,7 @@ def get_tts_wav(
audio_opt = np.concatenate(audio_opt, 0)
t4 = ttime()
sr = hps.data.sampling_rate if version != "v3" else 24000
sr = hps.data.sampling_rate if version != "v3" and version != "v4" else 24000
if if_sr and sr == 24000:
audio_opt = torch.from_numpy(audio_opt).float().to(device)
audio_opt, sr = audio_sr(audio_opt.unsqueeze(0), sr)
@ -901,7 +905,7 @@ def get_tts_wav(
if not stream_mode == "normal":
if media_type == "wav":
sr = 48000 if if_sr else 24000
sr = hps.data.sampling_rate if version != "v3" else sr
sr = hps.data.sampling_rate if version != "v3" and version != "v4" else sr
audio_bytes = pack_wav(audio_bytes, sr)
yield audio_bytes.getvalue()

View File

@ -30,6 +30,9 @@ webui_port_subfix = 9871
api_port = 9880
# 设置为True可启用本地模式该模式只允许本机访问避免出现潜在安全问题。默认为False。
local_mode = False
if infer_device == "cuda":
gpu_name = torch.cuda.get_device_name(0)
if (

View File

@ -298,6 +298,7 @@ if __name__ == "__main__":
parser.add_argument("--json_key_text", default="text", help="the text key name in json, Default: text")
parser.add_argument("--json_key_path", default="wav_path", help="the path key name in json, Default: wav_path")
parser.add_argument("--g_batch", default=10, help="max number g_batch wav to display, Default: 10")
parser.add_argument("--local_mode", action="store_true", help="enable local mode (bind to 127.0.0.1)")
args = parser.parse_args()
@ -407,7 +408,7 @@ if __name__ == "__main__":
)
demo.launch(
server_name="0.0.0.0",
server_name="127.0.0.1" if args.local_mode else "0.0.0.0",
inbrowser=True,
# quiet=True,
share=eval(args.is_share),

View File

@ -32,6 +32,7 @@ device = sys.argv[1]
is_half = eval(sys.argv[2])
webui_port_uvr5 = int(sys.argv[3])
is_share = eval(sys.argv[4])
local_mode = sys.argv[5].lower() == 'true' if len(sys.argv) > 5 else False
def html_left(text, label="p"):
@ -220,7 +221,7 @@ with gr.Blocks(title="UVR5 WebUI") as app:
api_name="uvr_convert",
)
app.queue().launch( # concurrency_count=511, max_size=1022
server_name="0.0.0.0",
server_name="127.0.0.1" if local_mode else "0.0.0.0",
inbrowser=True,
share=is_share,
server_port=webui_port_uvr5,

View File

@ -74,6 +74,7 @@ from config import (
webui_port_main,
webui_port_subfix,
webui_port_uvr5,
local_mode,
)
from tools import my_utils
from tools.i18n.i18n import I18nAuto, scan_language_list
@ -388,6 +389,8 @@ def change_label(path_list):
webui_port_subfix,
is_share,
)
if local_mode:
cmd += " --local_mode"
yield (
process_info(process_name_subfix, "opened"),
{"__type__": "update", "visible": False},
@ -412,6 +415,8 @@ def change_uvr5():
global p_uvr5
if p_uvr5 is None:
cmd = '"%s" tools/uvr5/webui.py "%s" %s %s %s' % (python_exec, infer_device, is_half, webui_port_uvr5, is_share)
if local_mode:
cmd += " True"
yield (
process_info(process_name_uvr5, "opened"),
{"__type__": "update", "visible": False},
@ -450,6 +455,7 @@ def change_tts_inference(bert_path, cnhubert_base_path, gpu_number, gpt_path, so
os.environ["is_half"] = str(is_half)
os.environ["infer_ttswebui"] = str(webui_port_infer_tts)
os.environ["is_share"] = str(is_share)
os.environ["local_mode"] = str(local_mode)
yield (
process_info(process_name_tts, "opened"),
{"__type__": "update", "visible": False},
@ -1955,7 +1961,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI") as app:
gr.Markdown(value=i18n("施工中,请静候佳音"))
app.queue().launch( # concurrency_count=511, max_size=1022
server_name="0.0.0.0",
server_name="127.0.0.1" if local_mode else "0.0.0.0",
inbrowser=True,
share=is_share,
server_port=webui_port_main,