<jupyter_start><jupyter_text>Tacotron2 + WaveGlow inference  Import libraries<jupyter_code>import os
import sys
sys.path.append('./tacotron2/')

import IPython.display as ipd

import numpy as np
import torch

from hparams import create_hparams
from model import Tacotron2
from text import text_to_sequence

from griffin_lim import GriffinLim
from WaveGlow import WaveGlow<jupyter_output><empty_output><jupyter_text>Load WaveGlow and Tacotron2 models<jupyter_code>def load_waveglow(path):
    assert os.path.isfile(path)

    param_dict = torch.load(path, map_location='cpu')
    model = param_dict['model_class']
    model.load_state_dict(param_dict['model'])
    
    return model
def load_tacotron2(hparams, path):
    assert os.path.isfile(path)
    
    model = Tacotron2(hparams)
    model.load_state_dict(torch.load(path)['state_dict'])
    
    return model
# change parameters
tacotron2_path = "tacotron2_statedict.pt"
waveglow_path = 'WaveGlow_13000.ckpt'

device = torch.device('cuda')
hparams = create_hparams()
sr = 22050
hparams.sampling_rate = sr
tacotron2 = load_tacotron2(hparams, tacotron2_path)
tacotron2 = tacotron2.to(device).eval()
waveglow = load_waveglow(waveglow_path)
waveglow = waveglow.to(device).eval()<jupyter_output><empty_output><jupyter_text>GriffinLim<jupyter_code>griffin_lim = GriffinLim(
    sample_rate=sr,
    num_frequencies=1025,
    frame_length=1024 / sr + 1e-6,
    frame_shift=256 / sr + 1e-6,
    mel_channels=80,
    min_frequency=0,
    max_frequency=8000,
    ref_db=20,
    min_db=-100,
    num_iter=50)<jupyter_output><empty_output><jupyter_text>Text-to-audio inference function<jupyter_code>def text_to_audio(text, sigma=0.5):
    sequence = np.array(text_to_sequence(text, ['english_cleaners']))[None, :]
    sequence = torch.from_numpy(sequence).long().to(device)
    with torch.no_grad():
        mel_outputs, mel_outputs_postnet, _, alignments = tacotron2.inference(sequence)
        waveglow_audio = waveglow.infer(mel_outputs_postnet, sigma=sigma)[0].data.cpu().numpy()
    griffin_audio = griffin_lim.inv_melspectrogram(mel_outputs_postnet[0].data.cpu().numpy())
    
    return waveglow_audio, griffin_audio<jupyter_output><empty_output><jupyter_text>Synthesize audio from text<jupyter_code># text = "Waveglow is really awesome!"
text = "Nikita is my best friend."
waveglow_audio, griffin_audio = text_to_audio(text, 0.66)
# Waveglow output
ipd.Audio(waveglow_audio, rate=sr)
# Griffin-Lim output
ipd.Audio(griffin_audio, rate=sr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10837633981738785, 'fraction_numerical': 0.023818975784041286, 'mean_word_length': 5.7924528301886795, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '233858', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1001, 'n_tokens_neox': 943, 'n_words': 151, 'path': 'inference.ipynb', 'repo_name': 'neyudin/wavenetglow', 'size': '2519'}