New Tool Radar

Local AI

How to Transcribe a Recording with Speaker Labels Using Whisper and Pyannote

Transcribe a two-speaker recording with Whisper, run pyannote speaker diarization on the same audio, and align the two sets of timestamps so every line of the transcript carries the speaker who said it.

Schematic diagram with three steps: transcribe the recording with Whisper, run pyannote diarization to get speaker turns, then align the timestamps so each line carries its speaker.

Whisper turns speech into text, but it does not tell you who spoke. Hand it a two-person interview and you get an accurate transcript with no speaker labels at all. Speaker diarization is the separate task of predicting “who spoke when”: it marks stretches of audio as SPEAKER_00, SPEAKER_01, and so on, but it writes no words. To get a transcript that says who said what, you run both models on the same recording and join them by their timestamps. This guide follows Hugging Face’s own audio course, which pairs Whisper with pyannote to build exactly that.

Run diarization to find who spoke when

Install the pyannote library, then load its pre-trained diarization pipeline. The Hugging Face course gives these steps:

pip install --upgrade pyannote.audio
from pyannote.audio import Pipeline
diarization_pipeline = Pipeline.from_pretrained(
    "pyannote/[email protected]", use_auth_token=<YOUR_HF_TOKEN>
)

The weights are hosted on the Hugging Face Hub, and you first have to agree to the speaker diarization model’s terms of use, and then the segmentation model’s terms of use, before you can load them. The course’s sample is two different speakers concatenated into one audio file, and the diarization output is a list of turns with start and end times:

[{'segment': {'start': 0.4978125, 'end': 14.520937500000002},
  'track': 'B',
  'label': 'SPEAKER_01'},
 {'segment': {'start': 15.364687500000002, 'end': 21.3721875},
  'track': 'A',
  'label': 'SPEAKER_00'}]

The first speaker is predicted as speaking up until the 14.5 second mark, and the second speaker from 15.4 seconds onwards. Note that pyannote.audio expects the audio input to be a PyTorch tensor of shape (channels, seq_len), so the course converts the audio array before running the model.

Transcribe the same recording with Whisper

Load the Whisper Base checkpoint — the course picks it because it is small enough to give good inference speed with reasonable transcription accuracy — and ask for segment-level timestamps so you know the start and end times for each segment:

from transformers import pipeline
asr_pipeline = pipeline(
    "automatic-speech-recognition",
    model="openai/whisper-base",
)
asr_pipeline(
    sample["audio"].copy(),
    generate_kwargs={"max_new_tokens": 256},
    return_timestamps=True,
)

Each segment of the transcript has a start and end time. In the course’s sample, the segments run from 0.0 to 3.56 seconds, then 3.56 to 7.84, 7.84 to 13.88, 13.88 to 15.48, 15.48 to 19.44, and 19.44 to 21.28 seconds — and the speakers change at the 15.48 second mark, inside a segment boundary.

Align the timestamps and read the labelled transcript

The two sets of timestamps do not match perfectly: the diarization model predicted the first speaker ending at 14.5 seconds and the second starting at 15.4, while Whisper predicted segment boundaries at 13.88, 15.48, and 19.44 seconds. So the course finds which boundaries are closest to 14.5 and 15.4 seconds and segments the transcription by speakers accordingly, using the Speechbox package’s ASRDiarizationPipeline to do the alignment:

pip install git+https://github.com/huggingface/speechbox
from speechbox import ASRDiarizationPipeline
pipeline = ASRDiarizationPipeline(
    asr_pipeline=asr_pipeline, diarization_pipeline=diarization_pipeline
)

The combined pipeline returns each speaker’s lines with their timestamps:

SPEAKER_01 (0.0, 15.5) The second and importance is as follows. Sovereignty may be defined to be the right of making laws.
SPEAKER_00 (15.5, 21.3) He was in a favored state of mind, owing to the blight his wife's action threatened to cast upon his entire future.
Check What you should see If you do not see it
Diarized Turns labelled SPEAKER_00 and SPEAKER_01 with start and end times The model terms were not accepted on the Hub — accept them and pass your token
Transcribed Whisper segments with timestamps, covering the whole recording Add return_timestamps=True — without it there is nothing to align
Aligned Each line carries one speaker and a timestamp range, with the change at about 15.5 seconds The two timestamp sets disagree — that is expected; the alignment step finds the closest boundaries

Expected result. We checked Hugging Face’s own audio course for the pyannote pipeline, the Whisper timestamps, and the Speechbox alignment, and a diarization guide for the warning that Whisper alone writes no speaker labels. We did not run Whisper or pyannote.

Limits

The labels are numbers, not names: SPEAKER_00 and SPEAKER_01 tell you two voices are different, not who they belong to — you match them to people yourself by listening. The alignment is approximate: the course notes the minimum-distance method is simple but works well in practice, and Whisper’s word times are estimates, not measured edges, so a few words at a turn boundary can land on the wrong side. Overlapping speech is the hardest case — when two people talk at once, a word cannot belong to both speakers. The sample audio and its timestamps above are the course’s own LibriSpeech example; use your own recording and check the labels against what you hear. We did not run Whisper or pyannote, so no transcript here is a recorded result.

Sources

See all