How to run Whisper on Google Colaboratory
Tags: python, colab, ai, tutorial
Published: 2022-09-23 13:11:59

Whisper is a general-purpose speech recognition model open-sourced by OpenAI.

According to the official article, the automatic speech recognition system is trained on 680,000 hours of multilingual and multitask supervised data collected from the web. 

[📖 Introducing Whisper](https://openai.com/blog/whisper/)

I was surprised by Whisper’s high accuracy and ease of use.
Whisper provides so useful command line that you can feel free to try it.

[📖 Command-line usage](https://github.com/openai/whisper#command-line-usage)

I'll present how to run Whisper on Google Colaboratory here.
You can refer to this Colab notebook if you want to try Whisper immediately on Google Colaboratory.

[📖 Colaboratory whisper-mock-en](https://colab.research.google.com/gist/Kazuki-tam/04e85708e4fd1c4b8af180d317977f4d/whisper-mock-en.ipynb)

## Create a new Colab notebook
You need to crate a new Colab notebook from your Google Drive at the first.

![Colab notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9hmf1jwdfk9zt481tr7t.png)

You have to make sure your notebook is using a GPU. To do that, change a runtime type to GPU from the menu.

![GPU from the menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8gfujd5rb8346rslaze.png)

## Install package
You need to install a package like the following line to run Whisper.

```python
# Install packages
!pip install git+https://github.com/openai/whisper.git
```

## Add folders
Add this code to create new folders when you click the play button.

```python
import os

# Add folders
checkContentFolder = os.path.exists("content")
checkDownLoadFolder = os.path.exists("download")
if not checkContentFolder:
  os.mkdir("content")
if not checkDownLoadFolder:
  os.mkdir("download")
```

## Upload an audio file
You have to upload at least an audio file into the `content` folder after you've installed packages and added folders.

## Transcription with Python
You can modify the file name of the audio and target language to translate.

```python
import whisper

fileName = "sample.m4a"
lang = "en"
model = whisper.load_model("base")

# Load audio
audio = whisper.load_audio(f"content/{fileName}")
audio = whisper.pad_or_trim(audio)

mel = whisper.log_mel_spectrogram(audio).to(model.device)

# Output the recognized text
options = whisper.DecodingOptions(language=lang, without_timestamps=True)
result = whisper.decode(model, mel, options)
print(result.text)

# Write into a text file
with open(f"download/{fileName}.txt", "w") as f:
  f.write(f"▼ Transcription of {fileName}\n")
  f.write(result.text)
```

## Download a transcription file
It would be easy to download a transcription file if you added this code.

```python
from google.colab import files
!zip -r download.zip download
files.download("download.zip")
```

## Let's run Whisper on Google Colaboratory
Let's check how your code works on Colab notebook.
I prepared a Colab notebook to use Whisper, so you can copy this notebook.

[📖 Colaboratory whisper-mock-en](https://colab.research.google.com/gist/Kazuki-tam/04e85708e4fd1c4b8af180d317977f4d/whisper-mock-en.ipynb)

{% embed https://gist.github.com/Kazuki-tam/04e85708e4fd1c4b8af180d317977f4d %}

## Conclusion
I hope this tutorial got you to generate your first transcription.