from models import SynthesizerTrn from vits_pinyin import VITS_PinYin from text import cleaned_text_to_sequence from text.symbols import symbols import gradio as gr import utils import torch import argparse import os import re import logging logging.getLogger('numba').setLevel(logging.WARNING) limitation = os.getenv("SYSTEM") == "spaces" def create_calback(net_g: SynthesizerTrn, tts_front: VITS_PinYin): def tts_calback(text, dur_scale): if limitation: text_len = len(re.sub("\[([A-Z]{2})\]", "", text)) max_len = 150 if text_len > max_len: return "Error: Text is too long", None phonemes, char_embeds = tts_front.chinese_to_phonemes(text) input_ids = cleaned_text_to_sequence(phonemes) with torch.no_grad(): x_tst = torch.LongTensor(input_ids).unsqueeze(0).to(device) x_tst_lengths = torch.LongTensor([len(input_ids)]).to(device) x_tst_prosody = torch.FloatTensor( char_embeds).unsqueeze(0).to(device) audio = net_g.infer(x_tst, x_tst_lengths, x_tst_prosody, noise_scale=0.5, length_scale=dur_scale)[0][0, 0].data.cpu().float().numpy() del x_tst, x_tst_lengths, x_tst_prosody return "Success", (16000, audio) return tts_calback example = [['这星球,天天有五十亿人,在错过,多幸运,有你一起看星星,在争宠,这一刻不再问为什么不再去猜测人和人,心和心,有什么不同。', 1], ['当你说,太聪明,往往还是,会寂寞,我笑着,倾听孤单终结后的静寞,看月亮像夜空的瞳孔,静静凝视你我,和我们闹嚷的星球。', 1], ['床前明月光,疑是地上霜,举头望明月,低头思故乡。', 1],] if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--share", action="store_true", default=False, help="share gradio app") args = parser.parse_args() device = torch.device("cpu") # pinyin tts_front = VITS_PinYin("./bert", device) # config hps = utils.get_hparams_from_file("./configs/bert_vits.json") # model net_g = SynthesizerTrn( len(symbols), hps.data.filter_length // 2 + 1, hps.train.segment_size // hps.data.hop_length, **hps.model) model_path = "vits_bert_model.pth" utils.load_model(model_path, net_g) net_g.eval() net_g.to(device) tts_calback = create_calback(net_g, tts_front) app = gr.Blocks() with app: gr.Markdown("# Mandarin-TTS\n\n" ) with gr.Tabs(): with gr.TabItem("TTS"): with gr.Row(): with gr.Column(): textbox = gr.TextArea(label="Text", placeholder="Type your sentence here (Maximum 150 words)", value="中文语音合成", elem_id=f"tts-input") duration_slider = gr.Slider(minimum=0.1, maximum=5, value=1, step=0.1, label='速度 Speed') with gr.Column(): text_output = gr.Textbox(label="Message") audio_output = gr.Audio( label="Output Audio", elem_id="tts-audio") btn = gr.Button("Generate!") btn.click(tts_calback, inputs=[textbox, duration_slider], outputs=[text_output, audio_output]) gr.Examples( examples=example, inputs=[textbox, duration_slider], outputs=[text_output, audio_output], fn=tts_calback ) app.queue(concurrency_count=3).launch(show_api=False, share=args.share)