From fb51ceccb862c2b72a10f8663638589e46c6f24a Mon Sep 17 00:00:00 2001 From: hwangjeff Date: Tue, 12 Apr 2022 20:17:09 -0700 Subject: [PATCH] Add nightly build installation code snippet to prototype feature tutorials (#2325) Summary: Tutorial notebooks that leverage TorchAudio prototype features don't run as-is on Google Colab due to its runtime's not having nightly builds pre-installed. To make it easier for users to run said notebooks in Colab, this PR adds a code block that installs nightly Pytorch and TorchAudio builds as a comment that users can copy and run locally. Pull Request resolved: https://github.com/pytorch/audio/pull/2325 Reviewed By: xiaohui-zhang Differential Revision: D35597753 Pulled By: hwangjeff fbshipit-source-id: 59914e492ad72e31c0136a48cd88d697e8ea5f6c --- ...asr_inference_with_ctc_decoder_tutorial.py | 20 +++++++++++ examples/tutorials/device_asr.py | 4 ++- examples/tutorials/online_asr_tutorial.py | 36 +++++++++++++------ examples/tutorials/streaming_api_tutorial.py | 30 +++++++++++----- 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/examples/tutorials/asr_inference_with_ctc_decoder_tutorial.py b/examples/tutorials/asr_inference_with_ctc_decoder_tutorial.py index 57b93e35d9..aa79e9d740 100644 --- a/examples/tutorials/asr_inference_with_ctc_decoder_tutorial.py +++ b/examples/tutorials/asr_inference_with_ctc_decoder_tutorial.py @@ -72,6 +72,26 @@ import torch import torchaudio +try: + import torchaudio.prototype.ctc_decoder +except ModuleNotFoundError: + try: + import google.colab + + print( + """ + To enable running this notebook in Google Colab, install nightly + torch and torchaudio builds by adding the following code block to the top + of the notebook before running it: + + !pip3 uninstall -y torch torchvision torchaudio + !pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu + """ + ) + except ModuleNotFoundError: + pass + raise + ###################################################################### # Acoustic Model and Data diff --git a/examples/tutorials/device_asr.py b/examples/tutorials/device_asr.py index 46804a5d97..6aeaf7a8a3 100644 --- a/examples/tutorials/device_asr.py +++ b/examples/tutorials/device_asr.py @@ -10,7 +10,7 @@ .. note:: - This tutorial requires prototype Streaming API and ffmpeg>=4.1. + This tutorial requires prototype Streaming API, ffmpeg>=4.1, and SentencePiece. Prototype features are not part of binary releases, but available in nightly build. Please refer to https://pytorch.org for installing @@ -20,6 +20,8 @@ ``conda install -c anaconda ffmpeg`` will install the required libraries. + You can install SentencePiece by running ``pip install sentencepiece``. + .. note:: This tutorial was tested on MacBook Pro and Dynabook with Windows 10. diff --git a/examples/tutorials/online_asr_tutorial.py b/examples/tutorials/online_asr_tutorial.py index 71e0e75aa5..0c1d279346 100644 --- a/examples/tutorials/online_asr_tutorial.py +++ b/examples/tutorials/online_asr_tutorial.py @@ -13,8 +13,8 @@ # # .. note:: # -# This tutorial requires torchaudio with prototype features and -# FFmpeg libraries (>=4.1). +# This tutorial requires torchaudio with prototype features, +# FFmpeg libraries (>=4.1), and SentencePiece. # # torchaudio prototype features are available on nightly builds. # Please refer to https://pytorch.org/get-started/locally/ @@ -30,13 +30,7 @@ # ``conda install -c anaconda ffmpeg`` will install # the required libraries. # -# When running this tutorial in Google Colab, the following -# command should do. -# -# .. code:: -# -# !add-apt-repository -y ppa:savoury1/ffmpeg4 -# !apt-get -qq install -y ffmpeg +# You can install SentencePiece by running ``pip install sentencepiece``. ###################################################################### # 1. Overview @@ -59,10 +53,32 @@ import torch import torchaudio +try: + from torchaudio.prototype.io import Streamer +except ModuleNotFoundError: + try: + import google.colab + + print( + """ + To enable running this notebook in Google Colab, install nightly + torch and torchaudio builds and the requisite third party libraries by + adding the following code block to the top of the notebook before running it: + + !pip3 uninstall -y torch torchvision torchaudio + !pip3 install --pre torch torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu + !pip3 install sentencepiece + !add-apt-repository -y ppa:savoury1/ffmpeg4 + !apt-get -qq install -y ffmpeg + """ + ) + except ModuleNotFoundError: + pass + raise + print(torch.__version__) print(torchaudio.__version__) -from torchaudio.prototype.io import Streamer ###################################################################### # 3. Construct the pipeline diff --git a/examples/tutorials/streaming_api_tutorial.py b/examples/tutorials/streaming_api_tutorial.py index 106366afe7..ec5a85ac3d 100644 --- a/examples/tutorials/streaming_api_tutorial.py +++ b/examples/tutorials/streaming_api_tutorial.py @@ -29,13 +29,6 @@ # ``conda install -c anaconda ffmpeg`` will install # the required libraries. # -# When running this tutorial in Google Colab, the following -# command should do. -# -# .. code:: -# -# !add-apt-repository -y ppa:savoury1/ffmpeg4 -# !apt-get -qq install -y ffmpeg ###################################################################### # 1. Overview @@ -77,7 +70,28 @@ import matplotlib.pyplot as plt import torch import torchaudio -from torchaudio.prototype.io import Streamer + +try: + from torchaudio.prototype.io import Streamer +except ModuleNotFoundError: + try: + import google.colab + + print( + """ + To enable running this notebook in Google Colab, install nightly + torch and torchaudio builds and the requisite third party libraries by + adding the following code block to the top of the notebook before running it: + + !pip3 uninstall -y torch torchvision torchaudio + !pip3 install --pre torch torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu + !add-apt-repository -y ppa:savoury1/ffmpeg4 + !apt-get -qq install -y ffmpeg + """ + ) + except ModuleNotFoundError: + pass + raise print(torch.__version__) print(torchaudio.__version__)