everythingfades/vits_personal_exploration
0
1 2import os3import numpy as np4import torch5from torch import no_grad, LongTensor6import argparse7import commons8from mel_processing import spectrogram_torch9import utils10from models import SynthesizerTrn11import gradio as gr12import librosa13 14from text import text_to_sequence, _clean_text, cleaners, cleaned_text_to_sequence15os.system('pip install -r requirements.txt')#for installing monotonic_align16os.system('pip install ko_pron')17device = "cuda:0" if torch.cuda.is_available() else "cpu"18language = ["简体中文"]19language_marks = {20 "Japanese": "",21 "日本語": "[JA]",22 "简体中文": "[ZH]",23 "English": "[EN]",24 "Mix": "",25}26lang = ['日本語', '简体中文', 'English', 'Mix']27hps = utils.get_hparams_from_file("./configs/modified_finetune_speaker.json")28 29def create_tts_fn(model, hps, speaker_ids):30 def tts_fn(text, speaker, language, speed, noise_scale = 0, noise_scale_w = 0):31 #def tts_fn(text, speaker, language, length):32 if language is not None:33 text = language_marks[language] + text + language_marks[language]34 speaker_id = speaker_ids[speaker]35 stn_tst = get_text(text, hps)36 print("stn_tst:",stn_tst)37 with no_grad():38 x_tst = stn_tst.unsqueeze(0).to(device)39 x_tst_lengths = LongTensor([stn_tst.size(0)]).to(device)40 sid = LongTensor([speaker_id]).to(device)41 audio = model.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=0, noise_scale_w=0,42 #length_scale=length)[0][0, 0].data().cpu().float().numpy()43 length_scale=1.0 / speed)[0][0, 0].data.cpu().float().numpy()44 # alteration: length range in (0,1]45 # greater length, slower the audio.46 del stn_tst, x_tst, x_tst_lengths, sid47 print("sr:",hps.data.sampling_rate)48 return (44100, audio)49 return tts_fn50 51def create_tts_fn_cleaned(model, hps, speaker_ids):52 def tts_fn_cleaned(text, speaker, language, speed):53 speaker_id = speaker_ids[speaker]54 stn_tst = get_text_c(text, hps)55 print("stn_tst:",stn_tst)56 with no_grad():57 x_tst = stn_tst.unsqueeze(0).to(device)58 x_tst_lengths = LongTensor([stn_tst.size(0)]).to(device)59 sid = LongTensor([speaker_id]).to(device)60 audio = model.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=0, noise_scale_w=0,61 length_scale=1.0 / speed)[0][0, 0].data.cpu().float().numpy()62 del stn_tst, x_tst, x_tst_lengths, sid63 print(hps.data.sampling_rate)64 return (hps.data.sampling_rate, audio)65 return tts_fn_cleaned66net_g = SynthesizerTrn(67 len(hps.symbols),68 hps.data.filter_length // 2 + 1,69 hps.train.segment_size // hps.data.hop_length,70 n_speakers=hps.data.n_speakers,71 **hps.model).to(device)72_ = net_g.eval()73 74_ = utils.load_checkpoint("./OUTPUT_MODEL/previous_checkpoints/G_23000.pth", net_g, None)75speaker_ids = hps.speakers76speakers = list(hps.speakers.keys())77tts_fn = create_tts_fn(net_g, hps, speaker_ids)78tts_fn_cleaned = create_tts_fn_cleaned(net_g, hps, speaker_ids)79 80def get_text(text, hps):81 print(1)82 text_norm = text_to_sequence(text, hps.symbols, ["cjke_cleaners2"])83 print("text_norm:",text_norm)84 print("#")85 if hps.data.add_blank:86 text_norm = commons.intersperse(text_norm, 0)87 text_norm = LongTensor(text_norm)88 return text_norm#, clean_text89def get_text_c(text, hps):90 print(1)91 text_norm = text_to_sequence_c(text, hps.symbols)92 print("text_norm:",text_norm)93 print("#")94 if hps.data.add_blank:95 text_norm = commons.intersperse(text_norm, 0)96 text_norm = LongTensor(text_norm)97 return text_norm#, clean_text98 99import gradio as gr100with gr.Blocks() as demo:101 gr.Markdown("# VITS webUI 测试")102 gr.Markdown("数据来源为prts.wiki")103 gr.Markdown("主要是明日方舟角色语音,由于时间原因,部分角色效果不佳,但至少能用,之后再跑")104 gr.Markdown("#### 标点符号,空格等会影响语气,较为玄学")105 with gr.Column():106 with gr.Row():107 text_input = gr.Textbox(label = "需要生成的文本")#textbox108 with gr.Row():109 choose_character = gr.Dropdown(label="角色", choices=speakers)#char_dropdown110 with gr.Row():111 language_dropdown = gr.Dropdown(label = "语言,由于目前只找到完整的中文语音包,暂时只能用中文", choices=language)#language_dropdown112 with gr.Row():113 speed_slider = gr.Slider(label = "语速,语速过快可能吞音", minimum = 0.01, maximum = 10, step = 0.01, value = 1)114 with gr.Row():115 with gr.Column():116 noise_scale = gr.Slider(label = "玄学因素1", minimum = 0, maximum = 1, step = 0.01, value = 0)117 with gr.Column():118 noise_scale_w1 = gr.Slider(label = "玄学因素2", minimum = 0, maximum = 1, step = 0.01, value = 0)119 with gr.Column():120 with gr.Row():121 audio_output = gr.Audio(label="音频,点右侧三点下载")#audio_output122 with gr.Row():123 button = gr.Button()#actication_button124 125 126 127#tts_fn(textbox, char_dropdown, language_dropdown, duration_slider)128#textbox = "今天我们吃了好吃的,还有好喝的,喝了可乐,吃了炸鸡"129#char_dropdown = speakers[45]130#language_dropdown = lang[1]131#duration_slider = 1132#noise_scale1 = 0133#noise_scale_w1 = 0134 button.click(tts_fn,inputs = [text_input,choose_character,language_dropdown,speed_slider, noise_scale, noise_scale_w1],outputs = [audio_output])135demo.launch(share = True)