File size: 12,603 Bytes
7934b29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
{
"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",
"* Adding punctuation and capitalization to the 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 = 'r1.17.0'\n",
"!python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[all]\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 Natural Language Processing collection\n",
"import nemo.collections.nlp as nemo_nlp\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 - QuartzNet\n",
"quartznet = nemo_asr.models.EncDecCTCModel.from_pretrained(model_name=\"stt_en_quartznet15x5\").cuda()\n",
"\n",
"# Punctuation and capitalization model\n",
"punctuation = nemo_nlp.models.PunctuationCapitalizationModel.from_pretrained(model_name='punctuation_en_distilbert').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, quartznet.transcribe(paths2audio_files=files)):\n",
" raw_text = transcription\n",
"\n",
"# Add capitalization and punctuation\n",
"res = punctuation.add_punctuation_capitalization(queries=[raw_text])\n",
"text = res[0]\n",
"print(f'\\nRaw recognized text: {raw_text}. \\nText with capitalization and punctuation: {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": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "6qRpDPfNiLOU"
},
"outputs": [],
"source": [
"# This is how punctuation model changed it\n",
"print(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": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "_qgKa9L954bJ"
},
"outputs": [],
"source": [
"# Final result - with punctuation\n",
"IPython.display.Audio(text_to_audio(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",
"* [Punctuation and Capitalization](https://colab.research.google.com/github/NVIDIA/NeMo/blob/stable/tutorials/nlp/Punctuation_and_Capitalization.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, NLP 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
}
|