2023-12-14 08:15:26 +08:00
# Whisper
2023-11-30 00:17:26 +08:00
2023-12-08 03:15:54 +08:00
Speech recognition with Whisper in MLX. Whisper is a set of open source speech
2023-12-14 08:15:26 +08:00
recognition models from OpenAI, ranging from 39 million to 1.5 billion
2024-01-08 02:01:29 +08:00
parameters.[^1]
2023-11-30 00:17:26 +08:00
2023-12-08 03:15:54 +08:00
### Setup
2023-11-30 00:17:26 +08:00
2023-12-30 02:22:15 +08:00
First, install the dependencies:
2023-11-30 00:17:26 +08:00
```
pip install -r requirements.txt
```
Install [`ffmpeg` ](https://ffmpeg.org/ ):
2023-12-08 03:15:54 +08:00
```
2023-12-14 08:15:26 +08:00
# on macOS using Homebrew (https://brew.sh/)
2023-11-30 00:17:26 +08:00
brew install ffmpeg
```
2024-01-08 02:01:29 +08:00
Next, download the Whisper PyTorch checkpoint and convert the weights to the
MLX format. For example, to convert the `tiny` model use:
2023-12-30 02:22:15 +08:00
```
python convert.py --torch-name-or-path tiny --mlx-path mlx_models/tiny
```
Note you can also convert a local PyTorch checkpoint which is in the original OpenAI format.
To generate a 4-bit quantized model, use `-q` . For a full list of options:
```
python convert.py --help
```
By default, the conversion script will make the directory `mlx_models/tiny` and save
the converted `weights.npz` and `config.json` there.
> [!TIP]
> Alternatively, you can also download a few converted checkpoints from the
> [MLX Community](https://huggingface.co/mlx-community) organization on Hugging
> Face and skip the conversion step.
2023-12-08 03:15:54 +08:00
### Run
Transcribe audio with:
2023-11-30 00:17:26 +08:00
2024-01-08 02:01:29 +08:00
```python
2023-11-30 00:17:26 +08:00
import whisper
text = whisper.transcribe(speech_file)["text"]
```
2024-01-08 02:01:29 +08:00
The `transcribe` function also supports word-level timestamps. You can generate
these with:
```python
output = whisper.transcribe(speech_file, word_timestamps=True)
print(output["segments"][0]["words"])
```
To see more transcription options use:
```
>>> help(whisper.transcribe)
```
2023-12-08 03:15:54 +08:00
[^1]: Refer to the [arXiv paper ](https://arxiv.org/abs/2212.04356 ), [blog post ](https://openai.com/research/whisper ), and [code ](https://github.com/openai/whisper ) for more details.