做项目拿到了GPT-sovits的网页版代码,项目要求改成接口后面开发人员拿到能直接调用于是修改
webui.py
if __name__ =="__main__":
# 指定要删除的空文件夹路径
folder_path = 'logs/1abc'
# 删除空文件夹
try:
shutil.rmtree(folder_path)
print(f"Successfully deleted the folder and its contents: {folder_path}")
except OSError as e:
print(f"Error: {e.strerror}")
slice_generator = open_slice("/root/autodl-tmp/GPTSoVits/reference.wav","output/slicer_opt","-34","4000","300","10","500",0.9,0.25,4)
for output in slice_generator:
print(output)
asr_generator = open_asr("/root/autodl-tmp/GPTSoVits/output/slicer_opt", "output/asr_opt", "达摩 ASR (中文)", "large", "zh")#请注意这一步一定要有显卡
for output in asr_generator:
print(output)
open1abc_generator = open1abc("/root/autodl-tmp/GPTSoVits/output/asr_opt/slicer_opt.list","/root/autodl-tmp/GPTSoVits/output/slicer_opt","1abc","0-1","0-1","0-1","GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large","GPT_SoVITS/pretrained_models/chinese-hubert-base","GPT_SoVITS/pretrained_models/s2G488k.pth")
for output in open1abc_generator:
print(output)
open1Ba_generator = open1Ba(6,8,"1abc",0.4,True,True,4,"0-1","GPT_SoVITS/pretrained_models/s2G488k.pth","GPT_SoVITS/pretrained_models/s2D488k.pth")
for output in open1Ba_generator:
print(output)
open1Bb_generator = open1Bb(1,15,"1abc",True,True,True,5,"0-1","GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt")
for output in open1Bb_generator:
print(output)
也就是直接调用生成函数,把with部分注释掉,这样可以直接不经过网页版运行py文件。这里要注意明白每一个传入参数的意义,特别是路径部分。
推理部分代码如下
if __name__ == "__main__":
# input_text = "李白字太白,号青莲居士。是中国唐代最伟大的诗人之一,被誉为“诗仙”。"
GPT_weights = "GPT_weights/1abc-e15.ckpt"
Sovits_weights = "SoVITS_weights/1abc_e8_s152.pth"
change = change_tts_inference(True,"GPT_SoVITS/pretrained_models/chinese-roberta-wwm-ext-large","GPT_SoVITS/pretrained_models/chinese-hubert-base","0",GPT_weights,Sovits_weights)
for output in change:
print(output)
这一步主要是传入两个训练好的权重
另外修改GPT_SoVITS/inference_webui.py
同样注释掉with并加入如下代码
synthesis_result = get_tts_wav(
"/root/autodl-tmp/GPTSoVits/clipped_audio.wav",
None,
i18n("中文"),
"李白字太白,号青莲居士。是中国唐代最伟大的诗人之一,被誉为“诗仙”。",
i18n("中文")
)
# 提取最后一个音频数据和采样率
for last_sampling_rate, last_audio_data in synthesis_result:
pass # 迭代生成器,获取最后一个生成的结果
# 确保音频数据和采样率都存在
if last_sampling_rate is not None and last_audio_data is not None:
output_path = "/root/autodl-tmp/GPTSoVits" # 确保这个路径存在
os.makedirs(output_path, exist_ok=True)
output_wav_path = os.path.join(output_path, "output.wav")
sf.write(output_wav_path, last_audio_data, last_sampling_rate)
result = "Audio saved to " + output_wav_path
print(result)
else:
print("No audio data generated.")