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

steps.py

Language Python1.4 KB43 lines

transcription_pipeline/steps.py

  1. """The real model-backed steps, wired to pipeline.process's callables.
  2. Imports of WhisperX and torch happen inside the steps, so the rest of the
  3. package (and its tests) run without them.
  4. """
  5. from __future__ import annotations
  6. import tempfile
  7. from pathlib import Path
  8. from . import audio
  9. from .secrets import get_secret
  10. def transcriber(opts):
  11. if opts.engine == "deepinfra":
  12. from .engines import deepinfra
  13. token = get_secret("DEEPINFRA_TOKEN")
  14. def run(wav: Path, source: Path, o):
  15. # Upload FLAC, not the WAV: same audio, about half the bytes.
  16. with tempfile.TemporaryDirectory(prefix="tp-up-") as tmp:
  17. flac = audio.to_flac16k(wav, Path(tmp) / "audio.flac")
  18. return deepinfra.transcribe(flac, token, language=o.language)
  19. return run
  20. if opts.engine == "local":
  21. from .engines import local
  22. return lambda wav, source, o: local.transcribe(wav, language=o.language, model=o.model)
  23. raise ValueError(f"Unknown engine {opts.engine!r}: use local or deepinfra.")
  24. def aligner(opts):
  25. from .align import align
  26. return lambda result, wav, o: align(result, wav)
  27. def diarizer(opts):
  28. if not opts.diarize:
  29. return None
  30. from .diarize import diarize
  31. token = get_secret("HF_TOKEN")
  32. return lambda result, wav, o: diarize(result, wav, token, o.min_speakers, o.max_speakers)