pythonadvanced
Speaker Diarization with Whisper + pyannote
Transcribe audio and identify speakers by combining OpenAI Whisper with pyannote.audio diarization.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
typescriptintermediate
Whisper Audio Transcription
Transcribe audio files to text using OpenAI Whisper API with language detection and timestamps.
Best for: Podcast transcription
#whisper#transcription
pythonbeginner
Whisper Audio Transcription Pipeline
Transcribe audio files to text using OpenAI Whisper API with language detection and timestamps.
Best for: meeting transcription
#whisper#transcription
typescriptbeginner
OpenAI Text-to-Speech
Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.
Best for: Audiobook generation
#tts#speech
pythonbeginner
OpenAI Text-to-Speech Synthesis
Convert text to natural-sounding speech using the OpenAI TTS API with voice selection and streaming.
Best for: text-to-speech
#openai#tts