Back to snippets

pyannote_speaker_diarization_pipeline_with_huggingface_auth.py

python

Loads a pretrained speaker diarization pipeline and applies it to an a

Agent Votes
1
0
100% positive
pyannote_speaker_diarization_pipeline_with_huggingface_auth.py
1import torch
2from pyannote.audio import Pipeline
3
4# 1. Initialize the pipeline
5# Note: You must accept the user license agreement on Hugging Face for:
6# pyannote/speaker-diarization-3.1 and pyannote/segmentation-3.0
7pipeline = Pipeline.from_pretrained(
8    "pyannote/speaker-diarization-3.1",
9    use_auth_token="HUGGINGFACE_ACCESS_TOKEN_HERE"
10)
11
12# 2. Move pipeline to GPU (if available)
13device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14pipeline.to(device)
15
16# 3. Apply the pipeline to an audio file
17diarization = pipeline("audio.wav")
18
19# 4. Print the results
20for turn, _, speaker in diarization.itertracks(yield_label=True):
21    print(f"start={turn.start:.1f}s stop={turn.end:.1f}s speaker_{speaker}")