DEV Community

Ólafur Aron Jóhannsson
Ólafur Aron Jóhannsson

Posted on Originally published at kjarni.ai

Speech to Text in Python without PyTorch: Whisper in 27 MB

from kjarni import Transcriber

transcriber = Transcriber("whisper-small", language="en")
print(transcriber.transcribe_file("jfk.wav").text)
Enter fullscreen mode Exit fullscreen mode
 And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.
Enter fullscreen mode Exit fullscreen mode

That is OpenAI's Whisper, running on your own machine, from a package with no
dependencies. pip install openai-whisper gives the same sentence, after installing
PyTorch, Triton, Numba and LLVM: 2.2 GB in 25 packages, and only once ffmpeg is on the
system as well.

Try it

You need Python 3.9 or newer, on Linux, macOS or Windows.

mkdir transcribe && cd transcribe
python3 -m venv .venv && source .venv/bin/activate
pip install kjarni
curl -LO https://github.com/ggml-org/whisper.cpp/raw/master/samples/jfk.wav
Enter fullscreen mode Exit fullscreen mode

The sample is the 11-second public domain Kennedy clip that whisper.cpp ships for the
same purpose. Save this as transcribe.py:

from kjarni import Transcriber

transcriber = Transcriber("whisper-small", language="en")
result = transcriber.transcribe_file("jfk.wav")

print(result.text.strip())
for seg in result.segments:
    print(f"[{seg.start:5.2f} -> {seg.end:5.2f}] {seg.text.strip()}")
print(f"language {result.language}, {result.duration_secs:.1f} s of audio")
Enter fullscreen mode Exit fullscreen mode
python transcribe.py
Enter fullscreen mode Exit fullscreen mode
And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.
[ 0.00 -> 11.00] And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.
language en, 11.0 s of audio
Enter fullscreen mode Exit fullscreen mode

The first run downloads whisper-small from Hugging Face (920 MB) and caches it, with a
progress bar; quiet=True hides it. After that it needs no network.

MP3, FLAC and Ogg, with no ffmpeg

openai-whisper hands every file to ffmpeg to decode. Kjarni decodes WAV, MP3, FLAC and
Ogg Vorbis itself, so there is nothing else to install:

transcriber.transcribe_file("interview.mp3")
transcriber.transcribe_file("lecture.flac")
transcriber.transcribe_file("voice-note.ogg")
Enter fullscreen mode Exit fullscreen mode

M4A and video are not read directly. Convert them first:

ffmpeg -i meeting.m4a meeting.wav
Enter fullscreen mode Exit fullscreen mode

Audio you already have in memory

From a microphone, a web upload or a NumPy array, pass the samples and their rate. Mono
floats in -1..1, at any sample rate; Kjarni resamples to Whisper's 16 kHz:

import soundfile as sf
from kjarni import Transcriber

samples, rate = sf.read("jfk.wav", dtype="float32")
transcriber = Transcriber("whisper-small", language="en", quiet=True)
print(transcriber.transcribe(samples, rate).text)
Enter fullscreen mode Exit fullscreen mode

A float32 array is passed to the native library without being copied. A plain list
works too. Kjarni does not depend on NumPy; soundfile here is only for reading the file.

Other languages, and translation

Leave out language and Whisper decides from the first 30 seconds, as openai-whisper
does:

transcriber = Transcriber("whisper-small")
result = transcriber.transcribe_file("jfk.wav")
print(result.language)   # en
Enter fullscreen mode Exit fullscreen mode

translate=True transcribes speech in any of Whisper's languages into English text.
For better accuracy than whisper-small on difficult audio, use
Transcriber("whisper-large-v3"), a 3 GB download that is several times slower.

Against openai-whisper

The same script with openai-whisper, for comparison:

import whisper

model = whisper.load_model("small", device="cpu")
print(model.transcribe("jfk.wav", language="en", fp16=False)["text"])
Enter fullscreen mode Exit fullscreen mode

It prints the same sentence. Measured in fresh virtual environments on Python 3.13,
Linux x86-64, an Intel Core i7-13700, with PyTorch from its CPU-only index:

kjarni 0.7.0 openai-whisper + torch 2.15 (CPU)
Installed 27 MB, 1 package 2.2 GB, 25 packages
Needs ffmpeg no yes
Load whisper-small 0.44 s 2.0 s
Transcribe 58 s of speech 5.4 s (10.7× real time) 6.5 s (8.9× real time)

The default pip install openai-whisper on Linux brings the CUDA build of PyTorch,
which is larger again. faster-whisper is the other common choice: in the same benchmark
it took 7.6 s at full precision and 3.6 s with its int8 model, the fastest of the four.
Kjarni runs Whisper at full precision.

What it does not do

  • No word-level timestamps: segments only, as above.
  • No speaker diarization.
  • No live transcription from a microphone stream. Record a chunk, then transcribe it.

FAQ

Can I use OpenAI Whisper in Python without PyTorch?

Yes. pip install kjarni and Transcriber("whisper-small").transcribe_file("audio.wav")
run OpenAI's Whisper weights from a native library, with no PyTorch, no openai-whisper
and no ffmpeg. The model downloads once and then works offline.

Does it need ffmpeg?

No. WAV, MP3, FLAC and Ogg Vorbis are decoded by Kjarni itself. Other formats, such as
M4A, need converting to one of those first.

Is it as accurate as openai-whisper?

It runs the same Whisper weights at full precision and gives the same transcript on the
examples here. Accuracy depends on the model size you choose: whisper-small by default,
whisper-large-v3 for harder audio.

Does it need a GPU?

No. It runs on the CPU. Transcriber("whisper-small", device="gpu") uses a GPU through
Vulkan, Metal or DirectX 12 when one is present.

Getting it

Install: pip install kjarni
PyPI:    https://pypi.org/project/kjarni/
GitHub:  https://github.com/olafurjohannsson/kjarni
Docs:    https://kjarni.ai/docs/
Enter fullscreen mode Exit fullscreen mode

Related

Top comments (0)