mattrguerra/transcription-pipelinemainPrivate on GitHub, published here in full

local.py

Language Python1.3 KB32 lines

transcription_pipeline/engines/local.py

  1. """Local transcription: faster-whisper large-v3-turbo through WhisperX.
  2. The default engine. Nothing leaves the machine. int8 on the CPU is the setting
  3. that runs everywhere, Apple Silicon included (CTranslate2 has no Metal backend,
  4. so there is no GPU path on a Mac for this step).
  5. """
  6. from __future__ import annotations
  7. from functools import lru_cache
  8. from pathlib import Path
  9. from ..audio import load_wav16k
  10. DEFAULT_MODEL = "large-v3-turbo"
  11. @lru_cache(maxsize=2)
  12. def _model(name: str, device: str, compute_type: str, language: str | None):
  13. import whisperx
  14. return whisperx.load_model(name, device, compute_type=compute_type, language=language)
  15. def transcribe(wav: Path, language: str | None = None, model: str = DEFAULT_MODEL,
  16. device: str = "cpu", compute_type: str = "int8", batch_size: int = 8) -> dict:
  17. audio = load_wav16k(wav)
  18. result = _model(model, device, compute_type, language).transcribe(
  19. audio, batch_size=batch_size, language=language)
  20. segments = [{"start": float(s["start"]), "end": float(s["end"]), "text": s["text"].strip()}
  21. for s in result.get("segments", []) if s.get("text", "").strip()]
  22. return {"segments": segments, "language": result.get("language") or language,
  23. "model": f"faster-whisper {model} ({compute_type}, {device})"}