Files
artcraft/models/tts/NeMo/tutorials/VoiceSwapSample.ipynb
T

9.1 KiB

Getting Started: Voice swap application

This notebook shows how to use NVIDIA NeMo (https://github.com/NVIDIA/NeMo) to construct a toy demo which will swap a voice in the audio fragment with a computer generated one.

At its core the demo does:

  • Automatic speech recognition of what is said in the file. E.g. converting audio to text
  • Adding punctuation and capitalization to the text
  • Generating spectrogram from resulting text
  • Generating waveform audio from the spectrogram.

Installation

NeMo can be installed via simple pip command.

In [ ]:
BRANCH = 'v1.0.2'
!python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[all]
In [ ]:
# Ignore pre-production warnings
import warnings
warnings.filterwarnings('ignore')
import nemo
# Import Speech Recognition collection
import nemo.collections.asr as nemo_asr
# Import Natural Language Processing colleciton
import nemo.collections.nlp as nemo_nlp
# Import Speech Synthesis collection
import nemo.collections.tts as nemo_tts
# We'll use this to listen to audio
import IPython
In [ ]:
# Download audio sample which we'll try
# This is a sample from LibriSpeech Dev Clean dataset - the model hasn't seen it before
Audio_sample = '2086-149220-0033.wav'
!wget https://dldata-public.s3.us-east-2.amazonaws.com/2086-149220-0033.wav
# Listen to it
IPython.display.Audio(Audio_sample)

Instantiate pre-trained NeMo models which we'll use

from_pretrained(...) API downloads and initialized model directly from the cloud.

We will load audio_sample and convert it to text with QuartzNet ASR model (an action called transcribe). To convert text back to audio, we actually need to generate spectrogram with Tacotron2 first and then convert it to actual audio signal using WaveGlow vocoder.

In [ ]:
# Speech Recognition model - QuartzNet
quartznet = nemo_asr.models.EncDecCTCModel.from_pretrained(model_name="stt_en_quartznet15x5").cuda()
# Punctuation and capitalization model
punctuation = nemo_nlp.models.PunctuationCapitalizationModel.from_pretrained(model_name='punctuation_en_distilbert').cuda()
# Spectrogram generator which takes text as an input and produces spectrogram
spectrogram_generator = nemo_tts.models.Tacotron2Model.from_pretrained(model_name="tts_en_tacotron2").cuda()
# Vocoder model which takes spectrogram and produces actual audio
vocoder = nemo_tts.models.WaveGlowModel.from_pretrained(model_name="tts_waveglow_88m").cuda()

Using the models

In [ ]:
# Convert our audio sample to text
files = [Audio_sample]
raw_text = ''
text = ''
for fname, transcription in zip(files, quartznet.transcribe(paths2audio_files=files)):
  raw_text = transcription

# Add capitalization and punctuation
res = punctuation.add_punctuation_capitalization(queries=[raw_text])
text = res[0]
print(f'\nRaw recognized text: {raw_text}. \nText with capitalization and punctuation: {text}')
In [ ]:
# A helper function which combines Tacotron2 and WaveGlow to go directly from 
# text to audio
def text_to_audio(text):
  parsed = spectrogram_generator.parse(text)
  spectrogram = spectrogram_generator.generate_spectrogram(tokens=parsed)
  audio = vocoder.convert_spectrogram_to_audio(spec=spectrogram)
  return audio.to('cpu').numpy()

Results

In [ ]:
# This is our original audio sample
IPython.display.Audio(Audio_sample)
In [ ]:
# This is what was recognized by the ASR model
print(raw_text)
In [ ]:
# This is how punctuation model changed it
print(text)

Compare how the synthesized audio sounds when using text with and without punctuation.

In [ ]:
# Without punctuation
IPython.display.Audio(text_to_audio(raw_text), rate=22050)
In [ ]:
# Final result - with punctuation
IPython.display.Audio(text_to_audio(text), rate=22050)

Next steps

A demo like this is great for prototyping and experimentation. However, for real production deployment, you would want to use a service like NVIDIA Jarvis.

NeMo is built for training. You can fine-tune, or train from scratch on your data all models used in this example. We recommend you checkout the following, more in-depth, tutorials next:

You can find scripts for training and fine-tuning ASR, NLP and TTS models here.

That's it folks! Head over to NeMo GitHub for more examples: https://github.com/NVIDIA/NeMo