Spaces:
Sleeping
Sleeping
Update app.py
Browse filesfunction 'synthesize' created to convert text to speech passing through Tacotron2-GST + MelGAN vocoder
app.py
CHANGED
|
@@ -1,7 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
from hyper_parameters import tacotron_params as hparams
|
| 4 |
+
from training import load_model
|
| 5 |
|
| 6 |
+
from text import text_to_sequence
|
| 7 |
+
|
| 8 |
+
from melgan.model.generator import Generator
|
| 9 |
+
from melgan.utils.hparams import load_hparam
|
| 10 |
+
|
| 11 |
+
torch.manual_seed(1234)
|
| 12 |
+
MAX_WAV_VALUE = 32768.0
|
| 13 |
+
|
| 14 |
+
# load trained tacotron2 + GST model:
|
| 15 |
+
model = load_model(hparams)
|
| 16 |
+
checkpoint_path = "trained_models/checkpoint_78000.model"
|
| 17 |
+
model.load_state_dict(torch.load(checkpoint_path)['state_dict'])
|
| 18 |
+
model.to('cuda')
|
| 19 |
+
_ = model.eval()
|
| 20 |
+
|
| 21 |
+
# load pre trained MelGAN model for mel2audio:
|
| 22 |
+
vocoder_checkpoint_path = "trained_models/nvidia_tacotron2_LJ11_epoch6400.pt"
|
| 23 |
+
checkpoint = torch.load(vocoder_checkpoint_path)
|
| 24 |
+
hp_melgan = load_hparam("melgan/config/default.yaml")
|
| 25 |
+
vocoder_model = Generator(80)
|
| 26 |
+
vocoder_model.load_state_dict(checkpoint['model_g'])
|
| 27 |
+
vocoder_model = vocoder_model.to('cuda')
|
| 28 |
+
vocoder_model.eval(inference=False)
|
| 29 |
+
|
| 30 |
+
gst_head_scores = np.array([0.5, 0.15, 0.35]) # originally ([0.5, 0.15, 0.35])
|
| 31 |
+
gst_scores = torch.from_numpy(gst_head_scores).cuda().float()
|
| 32 |
+
|
| 33 |
+
def synthesize(text):
|
| 34 |
+
sequence = np.array(text_to_sequence(text, ['english_cleaners']))[None, :]
|
| 35 |
+
sequence = torch.from_numpy(sequence).to(device='cuda', dtype=torch.int64)
|
| 36 |
+
|
| 37 |
+
mel_outputs, mel_outputs_postnet, _, alignments = model.inference(sequence, gst_scores)
|
| 38 |
+
|
| 39 |
+
# mel2wav inference:
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
audio = vocoder_model.inference(mel_outputs_postnet)
|
| 42 |
+
|
| 43 |
+
audio_numpy = audio.data.cpu().detach().numpy()
|
| 44 |
+
write(save_path, 22050, audio_numpy)
|
| 45 |
+
|
| 46 |
+
iface = gr.Interface(fn=synthesize, inputs="text", outputs="audio")
|
| 47 |
iface.launch()
|