Fix speaker diarization: WAV conversion, pyannote 4.0 compat, telemetry bug
- Convert non-WAV audio to 16kHz mono WAV before diarization (pyannote v4.0.4 AudioDecoder returns None duration for FLAC, causing crash) - Handle pyannote 4.0 DiarizeOutput return type (unwrap .speaker_diarization) - Disable pyannote telemetry (np.isfinite(None) bug with max_speakers) - Use huggingface_hub.login() to persist token for all sub-downloads - Pre-download sub-models (segmentation-3.0, speaker-diarization-community-1) - Add third required model license link in settings UI - Improve SpeakerManager hints based on settings state - Add word-wrap to transcript text Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,15 +2,67 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
# Disable pyannote telemetry — it has a bug in v4.0.4 where
|
||||
# np.isfinite(None) crashes when max_speakers is not set.
|
||||
os.environ.setdefault("PYANNOTE_METRICS_ENABLED", "false")
|
||||
|
||||
from voice_to_notes.ipc.messages import progress_message
|
||||
from voice_to_notes.ipc.protocol import write_message
|
||||
|
||||
|
||||
def _ensure_wav(file_path: str) -> tuple[str, str | None]:
|
||||
"""Convert audio to 16kHz mono WAV if needed.
|
||||
|
||||
pyannote.audio v4.0.4 has a bug where its AudioDecoder returns
|
||||
duration=None for some formats (FLAC, etc.), causing crashes.
|
||||
Converting to WAV ensures the duration header is always present.
|
||||
|
||||
Returns:
|
||||
(path_to_use, temp_path_or_None)
|
||||
If conversion was needed, temp_path is the WAV file to clean up.
|
||||
"""
|
||||
ext = Path(file_path).suffix.lower()
|
||||
if ext == ".wav":
|
||||
return file_path, None
|
||||
|
||||
tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
||||
tmp.close()
|
||||
try:
|
||||
subprocess.run(
|
||||
[
|
||||
"ffmpeg", "-y", "-i", file_path,
|
||||
"-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le",
|
||||
tmp.name,
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
print(
|
||||
f"[sidecar] Converted {ext} to WAV for diarization",
|
||||
file=sys.stderr,
|
||||
flush=True,
|
||||
)
|
||||
return tmp.name, tmp.name
|
||||
except (subprocess.CalledProcessError, FileNotFoundError) as e:
|
||||
# ffmpeg not available or failed — try original file and hope for the best
|
||||
print(
|
||||
f"[sidecar] WAV conversion failed ({e}), using original file",
|
||||
file=sys.stderr,
|
||||
flush=True,
|
||||
)
|
||||
os.unlink(tmp.name)
|
||||
return file_path, None
|
||||
|
||||
|
||||
@dataclass
|
||||
class SpeakerSegment:
|
||||
"""A time span assigned to a speaker."""
|
||||
@@ -40,14 +92,19 @@ class DiarizeService:
|
||||
if self._pipeline is not None:
|
||||
return self._pipeline
|
||||
|
||||
import os
|
||||
|
||||
print("[sidecar] Loading pyannote diarization pipeline...", file=sys.stderr, flush=True)
|
||||
|
||||
# Use token from argument, fall back to environment variable
|
||||
if not hf_token:
|
||||
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") or None
|
||||
|
||||
# Persist token globally so ALL huggingface_hub sub-downloads use auth.
|
||||
# Pyannote has internal dependencies that don't forward the token= param.
|
||||
if hf_token:
|
||||
os.environ["HF_TOKEN"] = hf_token
|
||||
import huggingface_hub
|
||||
huggingface_hub.login(token=hf_token, add_to_git_credential=False)
|
||||
|
||||
models = [
|
||||
"pyannote/speaker-diarization-3.1",
|
||||
"pyannote/speaker-diarization",
|
||||
@@ -118,8 +175,27 @@ class DiarizeService:
|
||||
if max_speakers is not None:
|
||||
kwargs["max_speakers"] = max_speakers
|
||||
|
||||
# Convert to WAV to work around pyannote v4.0.4 duration bug
|
||||
audio_path, temp_wav = _ensure_wav(file_path)
|
||||
|
||||
print(
|
||||
f"[sidecar] Running diarization on {audio_path} with kwargs: {kwargs}",
|
||||
file=sys.stderr,
|
||||
flush=True,
|
||||
)
|
||||
|
||||
# Run diarization
|
||||
diarization = pipeline(file_path, **kwargs)
|
||||
try:
|
||||
raw_result = pipeline(audio_path, **kwargs)
|
||||
finally:
|
||||
if temp_wav:
|
||||
os.unlink(temp_wav)
|
||||
|
||||
# pyannote 4.0+ returns DiarizeOutput; older versions return Annotation directly
|
||||
if hasattr(raw_result, "speaker_diarization"):
|
||||
diarization = raw_result.speaker_diarization
|
||||
else:
|
||||
diarization = raw_result
|
||||
|
||||
# Convert pyannote output to our format
|
||||
result = DiarizationResult()
|
||||
|
||||
Reference in New Issue
Block a user