|
import gradio as gr |
|
import torch |
|
import soundfile as sf |
|
from speechbrain.inference.TTS import Tacotron2 |
|
from speechbrain.inference.vocoders import HIFIGAN |
|
from speechbrain.dataio.dataio import read_audio |
|
|
|
|
|
hifi_gan = HIFIGAN.from_hparams(source="speechbrain/tts-hifigan-ljspeech", savedir="tmpdir_vocoder") |
|
tacotron2 = Tacotron2.from_hparams(source="speechbrain/tts-tacotron2-ljspeech", savedir="tmpdir_tts") |
|
|
|
|
|
def synthesize_speech(text): |
|
|
|
text = text.lower() |
|
tokenized = tacotron2.hparams.tokenize(text, phonemize=False) |
|
|
|
tokens = torch.LongTensor(tokenized) |
|
|
|
|
|
mel_output, mel_length, alignment = tacotron2.encode_batch(tokens) |
|
|
|
|
|
waveforms = hifi_gan.decode_batch(mel_output) |
|
|
|
|
|
sf.write("speech.wav", waveforms.squeeze().cpu().numpy(), samplerate=hifi_gan.hparams.sample_rate) |
|
return "speech.wav" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=synthesize_speech, |
|
inputs=gr.Textbox(lines=5, label="Input Text"), |
|
outputs=gr.Audio(label="Output Audio", type="filepath"), |
|
title="TTS Demo", |
|
description="Enter text to synthesize speech." |
|
) |
|
|
|
iface.launch() |