Sub topic 3
Reading segment and word timestamps
This chapter assumes you can run Python, install packages with pip, and load a Whisper model. You should also know that Whisper processes audio in 30-second windows.
Segment and word timestamps
Each segment in the openai-whisper result has a start time, an end time, and text, all in seconds. Passing word_timestamps=True adds a words list to each segment, with a timing for every word.
import whisper
model = whisper.load_model("small")
result = model.transcribe("talk.mp3", word_timestamps=True, fp16=False)
for seg in result["segments"]:
print(f"[{seg['start']:.2f} -> {seg['end']:.2f}] {seg['text']}")
for w in seg["words"]:
print(f" {w['word']!r} {w['start']:.2f}-{w['end']:.2f}")Measuring accuracy with word error rate
Word error rate (WER) is the number of substituted, deleted, and inserted words divided by the number of words in the reference transcript. Lower is better. Use jiwer to compute it.
import jiwer
print(jiwer.wer("the quick brown fox", "the quick brown box")) # 0.25Limitations: hallucinations, silence, accents, and no built-in speaker labels
Whisper can produce fluent text for silent or noisy stretches, a failure called hallucination. Check long silences and repeated phrases. Accuracy also drops for strong accents and rare vocabulary. Whisper does not label speakers, so you need a separate diarization tool to answer "who said what."
Speed and memory: GPU use, fp16, and quantization
Use a GPU when one is available. Half precision (fp16) roughly halves memory use on a GPU. On a CPU, set fp16=False, as shown above. Quantization stores weights at lower precision, such as int8, to cut memory further, usually with a small accuracy cost.
Running Whisper faster with faster-whisper
faster-whisper is a reimplementation of Whisper on the CTranslate2 runtime. It supports int8 quantization and yields segments lazily.
from faster_whisper import WhisperModel
model = WhisperModel("small", device="cpu", compute_type="int8")
segments, info = model.transcribe("talk.mp3", word_timestamps=True)
for s in segments:
print(f"[{s.start:.2f}s -> {s.end:.2f}s] {s.text}")Using Whisper through the transformers pipeline
The Hugging Face pipeline wraps Whisper checkpoints, splits long audio into chunks, and returns timestamps when asked.
from transformers import pipeline
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small", chunk_length_s=30)
result = asr("talk.mp3", return_timestamps=True)
print(result["text"])
for chunk in result["chunks"]:
print(chunk["timestamp"], chunk["text"])Exercise
You transcribe a file with word_timestamps=True and want the duration of the first word of the first segment. Which expression gives it?
Show answer
result["segments"][0]["words"][0]["end"] - result["segments"][0]["words"][0]["start"]