Merge pull request #103 from luoye2333/4.0

Added the TTS function
This commit is contained in:
YuriHead 2023-03-31 12:52:16 +08:00 committed by GitHub
commit c38cf7d91a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 71 additions and 15 deletions

View File

@ -3,12 +3,18 @@ import os
# os.system("wget -P cvec/ https://huggingface.co/spaces/innnky/nanami/resolve/main/checkpoint_best_legacy_500.pt") # os.system("wget -P cvec/ https://huggingface.co/spaces/innnky/nanami/resolve/main/checkpoint_best_legacy_500.pt")
import gradio as gr import gradio as gr
import gradio.processing_utils as gr_pu
import librosa import librosa
import numpy as np import numpy as np
import soundfile import soundfile
from inference.infer_tool import Svc from inference.infer_tool import Svc
import logging import logging
import torch
import subprocess
import edge_tts
import asyncio
from scipy.io import wavfile
import librosa
logging.getLogger('numba').setLevel(logging.WARNING) logging.getLogger('numba').setLevel(logging.WARNING)
logging.getLogger('markdown_it').setLevel(logging.WARNING) logging.getLogger('markdown_it').setLevel(logging.WARNING)
@ -18,10 +24,6 @@ logging.getLogger('multipart').setLevel(logging.WARNING)
model = None model = None
spk = None spk = None
cuda = []
if torch.cuda.is_available():
for i in range(torch.cuda.device_count()):
cuda.append("cuda:{}".format(i))
def vc_fn(sid, input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num): def vc_fn(sid, input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num):
global model global model
@ -36,13 +38,56 @@ def vc_fn(sid, input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise
if len(audio.shape) > 1: if len(audio.shape) > 1:
audio = librosa.to_mono(audio.transpose(1, 0)) audio = librosa.to_mono(audio.transpose(1, 0))
temp_path = "temp.wav" temp_path = "temp.wav"
soundfile.write(temp_path, audio, sampling_rate, format="wav") soundfile.write(temp_path, audio, model.target_sample, format="wav")
_audio = model.slice_inference(temp_path, sid, vc_transform, slice_db, cluster_ratio, auto_f0, noise_scale,pad_seconds,cl_num,lg_num,lgr_num) _audio = model.slice_inference(temp_path, sid, vc_transform, slice_db, cluster_ratio, auto_f0, noise_scale,pad_seconds,cl_num,lg_num,lgr_num)
model.clear_empty() model.clear_empty()
os.remove(temp_path) os.remove(temp_path)
return "Success", (model.target_sample, _audio) return "Success", (model.target_sample, _audio)
except Exception as e: except Exception as e:
return "异常信息:"+str(e)+"\n请排障后重试",None return "异常信息:"+str(e)+"\n请排障后重试",None
def tts_func(_text,_rate):
#使用edge-tts把文字转成音频
# voice = "zh-CN-XiaoyiNeural"#女性,较高音
voice = "zh-CN-YunxiNeural"#男性
output_file = _text[0:10]+".wav"
# communicate = edge_tts.Communicate(_text, voice)
# await communicate.save(output_file)
if _rate>=0:
ratestr="+{:.0%}".format(_rate)
elif _rate<0:
ratestr="{:.0%}".format(_rate)#减号自带
p=subprocess.Popen(["edge-tts",
"--text",_text,
"--write-media",output_file,
"--voice",voice,
"--rate="+ratestr]
,shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
p.wait()
return output_file
def vc_fn2(sid, input_audio, vc_transform, auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,text2tts,tts_rate):
#使用edge-tts把文字转成音频
output_file=tts_func(text2tts,tts_rate)
#调整采样率
sr2=44100
wav, sr = librosa.load(output_file)
wav2 = librosa.resample(wav, orig_sr=sr, target_sr=sr2)
save_path2= text2tts[0:10]+"_44k"+".wav"
wavfile.write(save_path2,sr2,
(wav2 * np.iinfo(np.int16).max).astype(np.int16)
)
#读取音频
sample_rate, data=gr_pu.audio_from_file(save_path2)
vc_input=(sample_rate, data)
a,b=vc_fn(sid, vc_input, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num)
return a,b
app = gr.Blocks() app = gr.Blocks()
with app: with app:
@ -64,13 +109,17 @@ with app:
<font size=3>下面是聚类模型文件选择没有可以不填</font> <font size=3>下面是聚类模型文件选择没有可以不填</font>
""") """)
cluster_model_path = gr.File(label="聚类模型文件") cluster_model_path = gr.File(label="聚类模型文件")
device = gr.Dropdown(label="推理设备,默认为自动选择cpu和gpu",choices=["Auto",*cuda,"cpu"],value="Auto") device = gr.Dropdown(label="推理设备,留白则为自动选择cpu和gpu",choices=[None,"cuda","cpu"],value=None)
gr.Markdown(value=""" gr.Markdown(value="""
<font size=3>全部上传完毕后(全部文件模块显示download),点击模型解析进行解析</font> <font size=3>全部上传完毕后(全部文件模块显示download),点击模型解析进行解析</font>
""") """)
model_analysis_button = gr.Button(value="模型解析") model_analysis_button = gr.Button(value="模型解析")
sid = gr.Dropdown(label="音色(说话人)") sid = gr.Dropdown(label="音色(说话人)")
sid_output = gr.Textbox(label="Output Message") sid_output = gr.Textbox(label="Output Message")
text2tts=gr.Textbox(label="在此输入要转译的文字")
tts_rate = gr.Number(label="tts语速", value=0)
vc_input3 = gr.Audio(label="上传音频") vc_input3 = gr.Audio(label="上传音频")
vc_transform = gr.Number(label="变调整数可以正负半音数量升高八度就是12", value=0) vc_transform = gr.Number(label="变调整数可以正负半音数量升高八度就是12", value=0)
cluster_ratio = gr.Number(label="聚类模型混合比例0-1之间默认为0不启用聚类能提升音色相似度但会导致咬字下降如果使用建议0.5左右)", value=0) cluster_ratio = gr.Number(label="聚类模型混合比例0-1之间默认为0不启用聚类能提升音色相似度但会导致咬字下降如果使用建议0.5左右)", value=0)
@ -81,19 +130,26 @@ with app:
pad_seconds = gr.Number(label="推理音频pad秒数由于未知原因开头结尾会有异响pad一小段静音段后就不会出现", value=0.5) pad_seconds = gr.Number(label="推理音频pad秒数由于未知原因开头结尾会有异响pad一小段静音段后就不会出现", value=0.5)
lg_num = gr.Number(label="两端音频切片的交叉淡入长度如果自动切片后出现人声不连贯可调整该数值如果连贯建议采用默认值0注意该设置会影响推理速度单位为秒/s", value=0) lg_num = gr.Number(label="两端音频切片的交叉淡入长度如果自动切片后出现人声不连贯可调整该数值如果连贯建议采用默认值0注意该设置会影响推理速度单位为秒/s", value=0)
lgr_num = gr.Number(label="自动音频切片后需要舍弃每段切片的头尾。该参数设置交叉长度保留的比例范围0-1,左开右闭", value=0.75,interactive=True) lgr_num = gr.Number(label="自动音频切片后需要舍弃每段切片的头尾。该参数设置交叉长度保留的比例范围0-1,左开右闭", value=0.75,interactive=True)
vc_submit = gr.Button("转换", variant="primary") vc_submit = gr.Button("音频直接转换", variant="primary")
vc_submit2 = gr.Button("文字转音频+转换", variant="primary")
vc_output1 = gr.Textbox(label="Output Message") vc_output1 = gr.Textbox(label="Output Message")
vc_output2 = gr.Audio(label="Output Audio") vc_output2 = gr.Audio(label="Output Audio")
def modelAnalysis(model_path,config_path,cluster_model_path,device): def modelAnalysis(model_path,config_path,cluster_model_path,device):
try: global model
global model debug=False
model = Svc(model_path.name, config_path.name,device=device if device!="Auto" else None,cluster_model_path= cluster_model_path.name if cluster_model_path!=None else "") if debug:
model = Svc(model_path.name, config_path.name,device=device if device!="" else None,cluster_model_path= cluster_model_path.name if cluster_model_path!=None else "")
spks = list(model.spk2id.keys()) spks = list(model.spk2id.keys())
device_name = torch.cuda.get_device_properties(model.dev).name if "cuda" in str(model.dev) else str(model.dev) return sid.update(choices = spks,value=spks[0]),"ok"
return sid.update(choices = spks,value=spks[0]),"ok,模型被加载到了设备{}之上".format(device_name) else:
except Exception as e: try:
return "","异常信息:"+str(e)+"\n请排障后重试" model = Svc(model_path.name, config_path.name,device=device if device!="" else None,cluster_model_path= cluster_model_path.name if cluster_model_path!=None else "")
spks = list(model.spk2id.keys())
return sid.update(choices = spks,value=spks[0]),"ok"
except Exception as e:
return "","异常信息:"+str(e)+"\n请排障后重试"
vc_submit.click(vc_fn, [sid, vc_input3, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num], [vc_output1, vc_output2]) vc_submit.click(vc_fn, [sid, vc_input3, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num], [vc_output1, vc_output2])
vc_submit2.click(vc_fn2, [sid, vc_input3, vc_transform,auto_f0,cluster_ratio, slice_db, noise_scale,pad_seconds,cl_num,lg_num,lgr_num,text2tts,tts_rate], [vc_output1, vc_output2])
model_analysis_button.click(modelAnalysis,[model_path,config_path,cluster_model_path,device],[sid,sid_output]) model_analysis_button.click(modelAnalysis,[model_path,config_path,cluster_model_path,device],[sid,sid_output])
app.launch() app.launch()