pythonadvanced

Speaker Diarization with Whisper + pyannote

Transcribe audio and identify speakers by combining OpenAI Whisper with pyannote.audio diarization.

python
import whisper
from pyannote.audio import Pipeline
from pathlib import Path
import json

# Load models
whisper_model = whisper.load_model('base')
diarization_pipeline = Pipeline.from_pretrained('pyannote/speaker-diarization-3.1', use_auth_token='YOUR_HF_TOKEN')

audio_path = 'meeting.wav'

# Transcribe
transcription = whisper_model.transcribe(audio_path, word_timestamps=True)

# Diarize
diarization = diarization_pipeline(audio_path)

# Merge results
results = []
for segment, _, speaker in diarization.itertracks(yield_label=True):
    words = [w for seg in transcription['segments'] for w in seg.get('words', []) if segment.start <= w['start'] <= segment.end]
    text  = ' '.join(w['word'] for w in words)
    if text.strip():
        results.append({'speaker': speaker, 'start': round(segment.start, 2), 'end': round(segment.end, 2), 'text': text.strip()})

for r in results[:5]:
    print(f"[{r['start']:.1f}s-{r['end']:.1f}s] {r['speaker']}: {r['text']}")

Use Cases

  • meeting transcription
  • speaker identification
  • audio analytics

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.