liveBook · Manning

Mlx_whisper.transcribe

As explained in Build Applications with Local AI Models on a Mac

An MLX Whisper function that transcribes audio from a file path or audio array.

`mlx_whisper.transcribe()` turns speech in an audio file into text on an Apple silicon Mac. Pass an audio file path and choose a Whisper model with `path_or_hf_repo`. For example, `mlx_whisper.transcribe(str(audio_file), path_or_hf_repo="mlx-community/whisper-small-mlx")` returns a Python dictionary; `result["text"]` is the transcript. The audio should be mono at 16 kHz, using int16 for a WAV file. section 9.11 section 9.6

import mlx_whisper
from pathlib import Path

AUDIO_FILE    = Path(__file__).with_name("recording.wav")
WHISPER_MODEL = "mlx-community/whisper-small-mlx"

def transcribe(audio_path: str, model: str = WHISPER_MODEL) -> str:
    """Transcribe an audio file and return the text."""
    if not Path(audio_path).is_file():
        raise FileNotFoundError(
            f"Audio file not found: {audio_path}. Run python record.py first."
        )
    print(f"Transcribing with {model.split('/')[-1]}...")
    result = mlx_whisper.transcribe(str(audio_path), path_or_hf_repo=model)
    return result["text"].strip()


if __name__ == "__main__":
    text = transcribe(AUDIO_FILE)
    print(f"\nTranscript:\n{text}")

A runnable wrapper checks that the recording exists, calls MLX Whisper, and returns stripped transcript text. Its path is based on the script’s location, so running it from another directory does not change which recording it opens. section 9.6

The result also has `segments` and `language` keys. Keep the dictionary when you need details such as detected language; a wrapper that returns only `result["text"]` discards them. A Hugging Face model name triggers an automatic download and local cache on first use; a local model-folder path is another option. section 9.6 section 9.10

Useful next steps:

  • Transcribe a short voice memo or other audio file; convert an MP3 to mono, 16 kHz WAV first if needed. section 9.10
  • Compare tiny and small models on the same recording. Run each once before timing it so download and model loading do not skew the comparison. section 9.10
  • For browser-recorded WAV bytes, write a temporary file before passing its path to `transcribe()`, then delete the file in a `finally` block. section 10.9

How Build Applications with Local AI Models on a Mac covers it

How deep it goes
Works through a guarded file-path wrapper and dictionary results, without explaining speech-recognition internals.
Examples
Uses a saved microphone recording, an existing audio-file exercise, and a timed tiny-versus-small model comparison.
What it assumes
Assumes a usable audio file and shows how to locate a saved recording; recording and WAV conversion have separate steps.
What you can do after
Apply it via MLX Whisper to obtain transcript text and inspect the detected language when needed.
How it connects
Links audio recording, Whisper model selection, language detection, and browser audio input.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Applications with Local AI Models on a Mac. The section links point to the parts of the book it draws on.