Spaces:
Sleeping
Sleeping
File size: 5,860 Bytes
9a85c9a b49028b 9a85c9a b49028b d532b1b b49028b 88a47ac d532b1b 9a85c9a db45ded 9a85c9a ff8b33b 9a85c9a db45ded 9a85c9a db45ded 9a85c9a db45ded 9a85c9a bacb9d9 9a85c9a 714116a db45ded 9a85c9a db45ded 9a85c9a db45ded fdcc4cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
import sys, os
import torch
import argparse
import commons
import utils
from models import SynthesizerTrn
from text.symbols import symbols
from text import cleaned_text_to_sequence, get_bert
from text.cleaner import clean_text
import gradio as gr
import webbrowser
import soundfile as sf
from datetime import datetime
import pytz
net_g = None
models = {
"AdorableDarling": "./MODELS/adorabledarling.pth",
"Silverleg": "./MODELS/J8900.pth",
"Rrabbitt": "./MODELS/rabbit4900.pth",
"MistyNikki": "./MODELS/nikki9400.pth",
"LightHammer": "./MODELS/hammer.pth",
"VivaciousViolet": "./MODELS/vv.pth",
"AlluWin": "./MODELS/AW.pth",
"Mainlade": "./MODELS/DLM.pth",
}
def get_text(text, language_str, hps):
norm_text, phone, tone, word2ph = clean_text(text, language_str)
phone, tone, language = cleaned_text_to_sequence(phone, tone, language_str)
if hps.data.add_blank:
phone = commons.intersperse(phone, 0)
tone = commons.intersperse(tone, 0)
language = commons.intersperse(language, 0)
for i in range(len(word2ph)):
word2ph[i] = word2ph[i] * 2
word2ph[0] += 1
bert = get_bert(norm_text, word2ph, language_str)
del word2ph
assert bert.shape[-1] == len(phone)
phone = torch.LongTensor(phone)
tone = torch.LongTensor(tone)
language = torch.LongTensor(language)
return bert, phone, tone, language
def infer(text, sdp_ratio, noise_scale, noise_scale_w, length_scale, sid, model_dir):
global net_g
bert, phones, tones, lang_ids = get_text(text, "ZH", hps)
with torch.no_grad():
x_tst=phones.to(device).unsqueeze(0)
tones=tones.to(device).unsqueeze(0)
lang_ids=lang_ids.to(device).unsqueeze(0)
bert = bert.to(device).unsqueeze(0)
x_tst_lengths = torch.LongTensor([phones.size(0)]).to(device)
del phones
speakers = torch.LongTensor([hps.data.spk2id[sid]]).to(device)
audio = net_g.infer(x_tst, x_tst_lengths, speakers, tones, lang_ids, bert, sdp_ratio=sdp_ratio
, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=length_scale)[0][0,0].data.cpu().float().numpy()
del x_tst, tones, lang_ids, bert, x_tst_lengths, speakers
sf.write("tmp.wav", audio, 44100)
return audio
def convert_wav_to_mp3(wav_file):
tz = pytz.timezone('Asia/Shanghai')
now = datetime.now(tz).strftime('%m%d%H%M%S')
os.makedirs('out', exist_ok=True)
output_path_mp3 = os.path.join('out', f"{now}.mp3")
renamed_input_path = os.path.join('in', f"in.wav")
os.makedirs('in', exist_ok=True)
os.rename(wav_file.name, renamed_input_path)
command = ["ffmpeg", "-i", renamed_input_path, "-acodec", "libmp3lame", "-y", output_path_mp3]
os.system(" ".join(command))
return output_path_mp3
def tts_generator(text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale, model):
global net_g
model_path = models[model]
net_g, _, _, _ = utils.load_checkpoint(model_path, net_g, None, skip_optimizer=True)
try:
with torch.no_grad():
audio = infer(text, sdp_ratio=sdp_ratio, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=length_scale, sid=speaker,model_dir=model)
with open('tmp.wav', 'rb') as wav_file:
mp3 = convert_wav_to_mp3(wav_file)
return "生成语音成功", (hps.data.sampling_rate, audio), mp3
except Exception as e:
return "生成语音失败:" + str(e), None, None
if __name__ == "__main__":
hps = utils.get_hparams_from_file("./configs/config.json")
device = "cuda:0" if torch.cuda.is_available() else "cpu"
net_g = SynthesizerTrn(
len(symbols),
hps.data.filter_length // 2 + 1,
hps.train.segment_size // hps.data.hop_length,
n_speakers=hps.data.n_speakers,
**hps.model).to(device)
_ = net_g.eval()
speaker_ids = hps.data.spk2id
speakers = list(speaker_ids.keys())
with gr.Blocks() as app:
with gr.Row():
with gr.Column():
gr.Markdown("测试用")
text = gr.TextArea(label="Text", placeholder="Input Text Here",
value="在不在?能不能借给我三百块钱买可乐",
info="使用huggingface的免费CPU进行推理,因此速度不快,一次性不要输入超过500汉字")
model = gr.Radio(choices=list(models.keys()), value=list(models.keys())[0], label='音声模型')
#model = gr.Dropdown(choices=models,value=models[0], label='音声模型')
speaker = gr.Radio(choices=speakers, value=speakers[0], label='Speaker')
gr.Markdown(value="生成参数,效果玄学")
sdp_ratio = gr.Slider(minimum=0, maximum=1, value=0.2, step=0.01, label='语调变化')
noise_scale = gr.Slider(minimum=0.1, maximum=1.5, value=0.5, step=0.01, label='感情变化')
noise_scale_w = gr.Slider(minimum=0.1, maximum=1.4, value=0.9, step=0.01, label='音节长度')
length_scale = gr.Slider(minimum=0.1, maximum=2, value=1, step=0.01, label='生成语音总长度')
btn = gr.Button("生成", variant="primary")
with gr.Column():
text_output = gr.Textbox(label="Message")
audio_output = gr.Audio(label="试听")
MP3_output = gr.File(label="下载")
gr.Markdown("""
""")
btn.click(
tts_generator,
inputs=[text, speaker, sdp_ratio, noise_scale, noise_scale_w, length_scale, model],
outputs=[text_output, audio_output,MP3_output]
)
app.launch(show_error=True)
|