diff --git a/app/diarize.py b/app/diarize.py index 0d37b4d..5e5cb65 100644 --- a/app/diarize.py +++ b/app/diarize.py @@ -1,11 +1,13 @@ -"""Label who speaks (sherpa-onnx, CUDA with CPU fallback): run(name) -> out/.spk.txt, diar_segments.json, DB lines.""" +"""Label who speaks (sherpa-onnx, CPU): run(name) -> out/.spk.txt, diar_segments.json, DB lines.""" import json, re, subprocess, time from pathlib import Path import numpy as np -import common, db # common: puts the pip nvidia-* CUDA DLLs on PATH +import db M = Path(__file__).parent / "models" -THREADS = 8 # CPU fallback; sherpa-onnx default is 1 thread: a 40-min call took >10 min +# sherpa-onnx default is 1 thread (>10 min for a 40-min call); 8 threads: ~6 min. CUDA measured slower (>7 min, GPU ~5 W): +# tiny models + one embedding per segment -> transfer/launch overhead dominates. +THREADS = 8 def run(name, speakers=5, threshold=0.85): @@ -14,19 +16,20 @@ def run(name, speakers=5, threshold=0.85): raw = subprocess.run(["ffmpeg", "-v", "error", "-i", str(src), "-vn", "-ac", "1", "-ar", "16000", "-f", "f32le", "-"], capture_output=True, check=True).stdout audio = np.frombuffer(raw, np.float32) - def diarizer(provider): - return sherpa_onnx.OfflineSpeakerDiarization(sherpa_onnx.OfflineSpeakerDiarizationConfig( - segmentation=sherpa_onnx.OfflineSpeakerSegmentationModelConfig( - pyannote=sherpa_onnx.OfflineSpeakerSegmentationPyannoteModelConfig(model=str(M / "sherpa-onnx-pyannote-segmentation-3-0/model.onnx")), - num_threads=THREADS, provider=provider), - embedding=sherpa_onnx.SpeakerEmbeddingExtractorConfig(model=str(M / "emb.onnx"), num_threads=THREADS, provider=provider), - clustering=sherpa_onnx.FastClusteringConfig(num_clusters=speakers, threshold=threshold), - min_duration_on=0.3, min_duration_off=0.5)) - t0 = time.time() - try: sd, used = diarizer("cuda"), "cuda" # CUDA build: sherpa-onnx==...+cuda12.cudnn9 (flow.py deps) - except Exception as e: print("diarize: CUDA unavailable, using CPU:", e, flush=True); sd, used = diarizer("cpu"), "cpu" - segs = [(s.start, s.end, s.speaker) for s in sd.process(audio).sort_by_start_time()] - print(len(segs), "segments,", len({s[2] for s in segs}), f"speakers ({used}, {time.time() - t0:.0f} s)", flush=True) + sd = sherpa_onnx.OfflineSpeakerDiarization(sherpa_onnx.OfflineSpeakerDiarizationConfig( + segmentation=sherpa_onnx.OfflineSpeakerSegmentationModelConfig( + pyannote=sherpa_onnx.OfflineSpeakerSegmentationPyannoteModelConfig(model=str(M / "sherpa-onnx-pyannote-segmentation-3-0/model.onnx")), + num_threads=THREADS), + embedding=sherpa_onnx.SpeakerEmbeddingExtractorConfig(model=str(M / "emb.onnx"), num_threads=THREADS), + clustering=sherpa_onnx.FastClusteringConfig(num_clusters=speakers, threshold=threshold), + min_duration_on=0.3, min_duration_off=0.5)) + t0, last = time.time(), [-1] + def progress(done, total): # sherpa calls this per processed chunk; print every 10 % + pct = done * 100 // max(total, 1) + if pct // 10 > last[0]: last[0] = pct // 10; print(f"diarize {pct}% ({done}/{total}, {time.time() - t0:.0f} s)", flush=True) + return 0 + segs = [(s.start, s.end, s.speaker) for s in sd.process(audio, callback=progress).sort_by_start_time()] + print(len(segs), "segments,", len({s[2] for s in segs}), f"speakers ({time.time() - t0:.0f} s)", flush=True) lines = [] for l in (db.OUT / f"{name}.txt").read_text(encoding="utf-8").splitlines(): diff --git a/app/flow.py b/app/flow.py index 6e35f40..6254c72 100644 --- a/app/flow.py +++ b/app/flow.py @@ -1,8 +1,6 @@ # /// script # requires-python = ">=3.10,<3.13" -# dependencies = ["prefect>=3", "faster-whisper", "av>=14,<17", "nvidia-cublas-cu12", "nvidia-cudnn-cu12==9.*", "nvidia-cuda-runtime-cu12", "nvidia-cufft-cu12", "sherpa-onnx==2.0.1+cuda12.cudnn9", "numpy", "imagehash", "Pillow", "markdown", "openvino-genai", "huggingface_hub"] -# [tool.uv] -# find-links = ["https://k2-fsa.github.io/sherpa/onnx/cuda.html"] # sherpa-onnx CUDA wheels (diarization on GPU, CPU fallback in diarize.py) +# dependencies = ["prefect>=3", "faster-whisper", "av>=14,<17", "nvidia-cublas-cu12", "nvidia-cudnn-cu12==9.*", "sherpa-onnx", "numpy", "imagehash", "Pillow", "markdown", "openvino-genai", "huggingface_hub"] # /// """Meeting pipeline. uv run --script flow.py [--from STEP] [--speakers N] [--device cuda|npu] [--bench-npu]""" import os