## VCTK import torch import os import commons import utils from models import SynthesizerTrn from text.symbols import symbols from text import text_to_sequence from scipy.io.wavfile import write import gradio as gr print("Running GRadio", gr.__version__) model_path = "vits2_pytorch/G_390000.pth" config_path = "vits2_pytorch/vits2_vctk_cat_inference.json" hps = utils.get_hparams_from_file(config_path) if ( "use_mel_posterior_encoder" in hps.model.keys() and hps.model.use_mel_posterior_encoder == True ): print("Using mel posterior encoder for VITS2") posterior_channels = 80 # vits2 hps.data.use_mel_posterior_encoder = True else: print("Using lin posterior encoder for VITS1") posterior_channels = hps.data.filter_length // 2 + 1 hps.data.use_mel_posterior_encoder = False net_g = SynthesizerTrn( len(symbols), posterior_channels, hps.train.segment_size // hps.data.hop_length, n_speakers=hps.data.n_speakers, **hps.model ) _ = net_g.eval() _ = utils.load_checkpoint(model_path, net_g, None) def get_text(text, hps): text_norm = text_to_sequence(text, hps.data.text_cleaners) #text_norm = cleaned_text_to_sequence(text) # if model was trained with text if hps.data.add_blank: text_norm = commons.intersperse(text_norm, 0) text_norm = torch.LongTensor(text_norm) return text_norm def tts(text:str, speaker_id:int, speed:float, noise_scale:float=0.667, noise_scale_w:float=0.8): stn_tst = get_text(text, hps) with torch.no_grad(): x_tst = stn_tst.unsqueeze(0) x_tst_lengths = torch.LongTensor([stn_tst.size(0)]) sid = torch.LongTensor([speaker_id]) waveform = ( net_g.infer( x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale, noise_scale_w=noise_scale_w, length_scale=1/speed, )[0][0, 0] .data.cpu() .float() .numpy() ) return gr.make_waveform((22050, waveform)) ## GUI space title = """