File size: 3,957 Bytes
b43d8da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Cell 01 — Install pinned dependencies.

Runs once at notebook boot. On Colab the notebook kernel is a bare Python 3
install, so we ``pip install`` the flat pin set from ``requirements.txt``.
Locally we skip reinstall if every pin is already importable.

Also authenticates with the Hugging Face Hub when an ``HF_TOKEN`` environment
variable is set; on interactive sessions the user can run ``hf auth login``
separately. No network calls are attempted when ``HF_TOKEN`` is absent — the
cell remains a no-op so offline unit tests pass.
"""

from __future__ import annotations

import importlib.util
import os
import subprocess
import sys
from pathlib import Path

REQUIREMENTS_FILENAME = "requirements.txt"

# Packages whose import name differs from their distribution name. Only list
# the handful we actually probe with ``is_installed``; everything else uses
# the distribution name verbatim.
_IMPORT_ALIASES: dict[str, str] = {
    "faster-whisper": "faster_whisper",
    "huggingface_hub": "huggingface_hub",
    "uvicorn[standard]": "uvicorn",
    "pytest-cov": "pytest_cov",
}


def is_installed(distribution: str) -> bool:
    """Return True iff the import name behind *distribution* is available."""

    base = distribution.split("[", 1)[0].split(">", 1)[0].split("<", 1)[0]
    base = base.split("==", 1)[0].split("~=", 1)[0].strip()
    module = _IMPORT_ALIASES.get(distribution, _IMPORT_ALIASES.get(base, base))
    module = module.replace("-", "_")
    return importlib.util.find_spec(module) is not None


def _find_requirements() -> Path | None:
    """Locate ``requirements.txt`` alongside the project root (worktree-safe)."""

    candidates = [
        Path.cwd() / REQUIREMENTS_FILENAME,
        Path(__file__).resolve().parent.parent / REQUIREMENTS_FILENAME,
    ]
    for candidate in candidates:
        if candidate.is_file():
            return candidate
    return None


def is_colab() -> bool:
    """Detect Google Colab runtime (``google.colab`` is always importable there)."""

    return importlib.util.find_spec("google.colab") is not None


def pip_install(requirements_path: Path) -> int:
    """Invoke ``pip install -r <requirements_path>`` via the current interpreter."""

    cmd = [sys.executable, "-m", "pip", "install", "--quiet", "-r", str(requirements_path)]
    completed = subprocess.run(cmd, check=False)
    return completed.returncode


def hf_login_if_token_present() -> bool:
    """Log into HF Hub using ``HF_TOKEN`` env var. Returns True on success."""

    token = os.environ.get("HF_TOKEN")
    if not token:
        return False
    try:
        from huggingface_hub import login
    except ImportError:
        return False
    login(token=token, add_to_git_credential=False)
    return True


def install(force: bool = False) -> int:
    """Top-level cell body. Idempotent: skips reinstall when pins already import.

    :param force: Reinstall even if every dependency is importable.
    :returns: 0 when deps already satisfied or pip succeeded; non-zero on pip failure.
    """

    requirements_path = _find_requirements()
    if requirements_path is None:
        return 0

    if not force and not is_colab():
        declared = [
            line.strip()
            for line in requirements_path.read_text(encoding="utf-8").splitlines()
            if line.strip() and not line.strip().startswith("#")
        ]
        if declared and all(is_installed(pkg) for pkg in declared):
            hf_login_if_token_present()
            return 0

    rc = pip_install(requirements_path)
    if rc == 0:
        hf_login_if_token_present()
    return rc


# Cell body: execute on import so the Colab notebook runs end-to-end.
# Skip the side effect when the cell is being imported under the pytest
# runner or when a caller opts out via ``DRIFTCALL_SKIP_INSTALL=1``.
_skip_marker = "pytest" in sys.modules or os.environ.get("DRIFTCALL_SKIP_INSTALL") == "1"
_rc = 0 if _skip_marker else install()