CoolFace
Apppublic

akhaliq/TensorFlowTTS

sourceHugging Faceupdated 5y agoView on Hugging Face
10likes
app.py56 linesDownload Raw Back to root
1import numpy as np2import soundfile as sf3import yaml4 5import tensorflow as tf6 7from tensorflow_tts.inference import TFAutoModel8from tensorflow_tts.inference import AutoProcessor9import gradio as gr10 11# initialize fastspeech2 model.12fastspeech2 = TFAutoModel.from_pretrained("tensorspeech/tts-fastspeech2-ljspeech-en")13 14 15# initialize mb_melgan model16mb_melgan = TFAutoModel.from_pretrained("tensorspeech/tts-mb_melgan-ljspeech-en")17 18 19# inference20processor = AutoProcessor.from_pretrained("tensorspeech/tts-fastspeech2-ljspeech-en")21 22def inference(text):23  input_ids = processor.text_to_sequence(text)24  # fastspeech inference25  26  mel_before, mel_after, duration_outputs, _, _ = fastspeech2.inference(27      input_ids=tf.expand_dims(tf.convert_to_tensor(input_ids, dtype=tf.int32), 0),28      speaker_ids=tf.convert_to_tensor([0], dtype=tf.int32),29      speed_ratios=tf.convert_to_tensor([1.0], dtype=tf.float32),30      f0_ratios =tf.convert_to_tensor([1.0], dtype=tf.float32),31      energy_ratios =tf.convert_to_tensor([1.0], dtype=tf.float32),32  )33  34  # melgan inference35  audio_before = mb_melgan.inference(mel_before)[0, :, 0]36  audio_after = mb_melgan.inference(mel_after)[0, :, 0]37  38  # save to file39  sf.write('./audio_before.wav', audio_before, 22050, "PCM_16")40  sf.write('./audio_after.wav', audio_after, 22050, "PCM_16")41  return './audio_after.wav'42  43inputs = gr.inputs.Textbox(lines=5, label="Input Text")44outputs =  gr.outputs.Audio(type="file", label="Output Audio")45 46 47title = "Tensorflow TTS"48description = "Gradio demo for TensorFlowTTS: Real-Time State-of-the-art Speech Synthesis for Tensorflow 2. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."49article = "<p style='text-align: center'><a href='https://tensorspeech.github.io/TensorFlowTTS/'>TensorFlowTTS: Real-Time State-of-the-art Speech Synthesis for Tensorflow 2</a> | <a href='https://github.com/TensorSpeech/TensorFlowTTS'>Github Repo</a></p>"50 51examples = [52 ["TensorFlowTTS provides real-time state-of-the-art speech synthesis architectures such as Tacotron-2, Melgan, Multiband-Melgan, FastSpeech, FastSpeech2 based-on TensorFlow 2."],53 ["With Tensorflow 2, we can speed-up training/inference progress, optimizer further by using fake-quantize aware and pruning, make TTS models can be run faster than real-time and be able to deploy on mobile devices or embedded systems."]   54]55 56gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples).launch()