ba4be087d5
Create PR to main with cherry-pick from release / cherry-pick (push) Waiting to run
CICD NeMo / pre-flight (push) Waiting to run
CICD NeMo / configure (push) Blocked by required conditions
CICD NeMo / cicd-main-unit-tests (push) Blocked by required conditions
CICD NeMo / cicd-main-speech (push) Blocked by required conditions
CICD NeMo / code-linting (push) Blocked by required conditions
CICD NeMo / cicd-wait-in-queue (push) Blocked by required conditions
CICD NeMo / cicd-test-container-build (push) Blocked by required conditions
CICD NeMo / cicd-import-tests (push) Blocked by required conditions
CICD NeMo / L0_Setup_Test_Data_And_Models (push) Blocked by required conditions
CICD NeMo / Nemo_CICD_Test (push) Blocked by required conditions
CICD NeMo / Coverage (e2e) (push) Blocked by required conditions
CICD NeMo / Coverage (unit-test) (push) Blocked by required conditions
CodeQL / Analyze (python) (push) Waiting to run
Build, validate, and release Neural Modules / pre-flight (push) Waiting to run
Build, validate, and release Neural Modules / release (push) Blocked by required conditions
Build, validate, and release Neural Modules / release-summary (push) Blocked by required conditions
291 lines
11 KiB
Plaintext
291 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "_wIWPxBVc3_O"
|
|
},
|
|
"source": [
|
|
"# Getting Started: Voice swap application\n",
|
|
"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.\n",
|
|
"\n",
|
|
"At its core the demo does: \n",
|
|
"\n",
|
|
"* Automatic speech recognition of what is said in the file. E.g. converting audio to text\n",
|
|
"* Generating spectrogram from resulting text\n",
|
|
"* Generating waveform audio from the spectrogram."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "gzcsqceVdtj3"
|
|
},
|
|
"source": [
|
|
"## Installation\n",
|
|
"NeMo can be installed via simple pip command."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "I9eIxAyKHREB"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"BRANCH = 'main'\n",
|
|
"!python -m pip install \"nemo_toolkit[all] @ git+https://github.com/NVIDIA-NeMo/Speech.git@$BRANCH\"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "-X2OyAxreGfl"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Ignore pre-production warnings\n",
|
|
"import warnings\n",
|
|
"warnings.filterwarnings('ignore')\n",
|
|
"import nemo\n",
|
|
"# Import Speech Recognition collection\n",
|
|
"import nemo.collections.asr as nemo_asr\n",
|
|
"# Import Speech Synthesis collection\n",
|
|
"import nemo.collections.tts as nemo_tts\n",
|
|
"# We'll use this to listen to audio\n",
|
|
"import IPython"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "1vC2DHawIGt8"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Download audio sample which we'll try\n",
|
|
"# This is a sample from LibriSpeech Dev Clean dataset - the model hasn't seen it before\n",
|
|
"Audio_sample = '2086-149220-0033.wav'\n",
|
|
"!wget https://dldata-public.s3.us-east-2.amazonaws.com/2086-149220-0033.wav\n",
|
|
"# Listen to it\n",
|
|
"IPython.display.Audio(Audio_sample)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "zodyzdyTVXas"
|
|
},
|
|
"source": [
|
|
"## Instantiate pre-trained NeMo models which we'll use\n",
|
|
"``from_pretrained(...)`` API downloads and initialized model directly from the cloud.\n",
|
|
"\n",
|
|
"We will load audio_sample and convert it to text with QuartzNet ASR model (an action called transcribe).\n",
|
|
"To convert text back to audio, we actually need to generate spectrogram with FastPitch first and then convert it to actual audio signal using the HiFiGAN vocoder."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "f_J9cuU1H6Bn"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Speech Recognition model - FastConformer\n",
|
|
"asr_model = nemo_asr.models.ASRModel.from_pretrained(model_name=\"stt_en_fastconformer_ctc_large\").cuda()\n",
|
|
"\n",
|
|
"# Spectrogram generator which takes text as an input and produces spectrogram\n",
|
|
"spectrogram_generator = nemo_tts.models.FastPitchModel.from_pretrained(model_name=\"tts_en_fastpitch\").cuda()\n",
|
|
"\n",
|
|
"# Vocoder model which takes spectrogram and produces actual audio\n",
|
|
"vocoder = nemo_tts.models.HifiGanModel.from_pretrained(model_name=\"tts_en_hifigan\").cuda()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "jQSj-IhEhrtI"
|
|
},
|
|
"source": [
|
|
"## Using the models"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "s0ERrXIzKpwu"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Convert our audio sample to text\n",
|
|
"files = [Audio_sample]\n",
|
|
"raw_text = ''\n",
|
|
"text = ''\n",
|
|
"for fname, transcription in zip(files, asr_model.transcribe(audio=files)):\n",
|
|
" raw_text = transcription\n",
|
|
"\n",
|
|
"print(f'\\nRaw recognized text: {raw_text}')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "-0Sk0C9-LmAR"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# A helper function which combines TTS models to go directly from \n",
|
|
"# text to audio\n",
|
|
"def text_to_audio(text):\n",
|
|
" parsed = spectrogram_generator.parse(text)\n",
|
|
" spectrogram = spectrogram_generator.generate_spectrogram(tokens=parsed)\n",
|
|
" audio = vocoder.convert_spectrogram_to_audio(spec=spectrogram)\n",
|
|
" return audio.to('cpu').detach().numpy()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "Q8Jvwe4Ahncx"
|
|
},
|
|
"source": [
|
|
"## Results"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "-im5TDF-MP2N"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# This is our original audio sample\n",
|
|
"IPython.display.Audio(Audio_sample)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "SNOMquwviEEQ"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# This is what was recognized by the ASR model\n",
|
|
"print(raw_text)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "di2IzMsdiiWq"
|
|
},
|
|
"source": [
|
|
"Compare how the synthesized audio sounds when using text with and without punctuation."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"colab": {},
|
|
"colab_type": "code",
|
|
"id": "EIh8wTVs5uH7"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Without punctuation\n",
|
|
"IPython.display.Audio(text_to_audio(raw_text), rate=22050)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "JOEFYywbctbJ"
|
|
},
|
|
"source": [
|
|
"## Next steps\n",
|
|
"A demo like this is great for prototyping and experimentation. However, for real production deployment, you would want to use a service like [NVIDIA Riva](https://developer.nvidia.com/riva).\n",
|
|
"\n",
|
|
"**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:\n",
|
|
"\n",
|
|
"* [NeMo fundamentals](https://colab.research.google.com/github/NVIDIA/NeMo/blob/stable/tutorials/00_NeMo_Primer.ipynb)\n",
|
|
"* [NeMo models](https://colab.research.google.com/github/NVIDIA/NeMo/blob/stable/tutorials/01_NeMo_Models.ipynb)\n",
|
|
"* [Speech Recognition](https://colab.research.google.com/github/NVIDIA/NeMo/blob/stable/tutorials/asr/ASR_with_NeMo.ipynb)\n",
|
|
"* [Speech Synthesis](https://colab.research.google.com/github/NVIDIA/NeMo/blob/stable/tutorials/tts/Inference_ModelSelect.ipynb)\n",
|
|
"\n",
|
|
"\n",
|
|
"You can find scripts for training and fine-tuning ASR and TTS models [here](https://github.com/NVIDIA/NeMo/tree/main/examples). "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"colab_type": "text",
|
|
"id": "ahRh2Y0Lc0G1"
|
|
},
|
|
"source": [
|
|
"That's it folks! Head over to NeMo GitHub for more examples: https://github.com/NVIDIA/NeMo"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"accelerator": "GPU",
|
|
"colab": {
|
|
"collapsed_sections": [],
|
|
"name": "NeMo voice swap app",
|
|
"provenance": [],
|
|
"toc_visible": true
|
|
},
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|