Datasets:
Download source/scripts/diarize_pyannote.py from lance-format/small-overlapping-speech-bench-lance: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/datasets/lance-format/small-overlapping-speech-bench-lance/resolve/refs%2Fpr%2F1/source/scripts/diarize_pyannote.py
- Command line
-
hf download hf://datasets/lance-format/small-overlapping-speech-bench-lance@refs/pr/1/source/scripts/diarize_pyannote.py
-
curl -L -o diarize_pyannote.py https://huggingface.co/datasets/lance-format/small-overlapping-speech-bench-lance/resolve/refs%2Fpr%2F1/source/scripts/diarize_pyannote.py
1.54 kB
| #!/usr/bin/env python3 | |
| """Stage A — pyannote 3.1 speaker diarization (handles overlapping speech). | |
| Writes pyannote_diar.json: {clip_id: {"n_speakers": N, "segments": [{speaker,start,end}]}}. | |
| Run in venv_pyannote. Needs a HF token with the gated pyannote repos accepted.""" | |
| import os, sys, json, time | |
| os.environ["HF_HUB_DISABLE_XET"]="1"; sys.modules["flash_attn"]=None | |
| import torch | |
| # PyTorch 2.6 defaults torch.load(weights_only=True), which breaks pyannote's trusted | |
| # checkpoint loading. Force full load. | |
| _orig_load = torch.load | |
| def _full_load(*a, **k): | |
| k["weights_only"] = False | |
| return _orig_load(*a, **k) | |
| torch.load = _full_load | |
| from pyannote.audio import Pipeline | |
| TOKEN=os.environ["HF_TOKEN"] | |
| pipe=Pipeline.from_pretrained("pyannote/speaker-diarization-3.1", use_auth_token=TOKEN) | |
| pipe.to(torch.device("cuda")) | |
| print("pyannote 3.1 pipeline loaded", flush=True) | |
| idx=json.load(open("/mnt/nvme/uvp/overlap_testset/index.json")) | |
| out={} | |
| for i,it in enumerate(idx): | |
| t0=time.time() | |
| dia=pipe(it["wav"]) | |
| segs=[] | |
| for turn,_,spk in dia.itertracks(yield_label=True): | |
| segs.append({"speaker":str(spk),"start":round(float(turn.start),3),"end":round(float(turn.end),3)}) | |
| nspk=len({s["speaker"] for s in segs}) | |
| out[it["clip_id"]]={"n_speakers":nspk,"segments":segs} | |
| if i%10==0: print(f" [{it['clip_id']}] {nspk} spk, {len(segs)} segs ({time.time()-t0:.1f}s)",flush=True) | |
| json.dump(out,open("/mnt/nvme/uvp/overlap_testset/pyannote_diar.json","w"),indent=2) | |
| print("PYANNOTE DIARIZE DONE",flush=True) | |