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

secrets.py

Language Python1.1 KB34 lines

transcription_pipeline/secrets.py

  1. """API tokens come from the environment or the macOS Keychain, never a file.
  2. security add-generic-password -a "$USER" -s transcription-pipeline.deepinfra -w
  3. security add-generic-password -a "$USER" -s transcription-pipeline.huggingface -w
  4. (-w with no value prompts, so the token never lands in shell history.)
  5. """
  6. from __future__ import annotations
  7. import os
  8. import shutil
  9. import subprocess
  10. SERVICES = {
  11. "DEEPINFRA_TOKEN": "transcription-pipeline.deepinfra",
  12. "HF_TOKEN": "transcription-pipeline.huggingface",
  13. }
  14. def get_secret(name: str) -> str | None:
  15. value = os.environ.get(name, "").strip()
  16. if value:
  17. return value
  18. service = SERVICES.get(name)
  19. if service and shutil.which("security"):
  20. try:
  21. out = subprocess.run(["security", "find-generic-password", "-s", service, "-w"],
  22. capture_output=True, text=True, timeout=10)
  23. except (OSError, subprocess.TimeoutExpired):
  24. return None
  25. if out.returncode == 0 and out.stdout.strip():
  26. return out.stdout.strip()
  27. return None