File size: 1,573 Bytes
cc7d399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Runtime cache configuration for local and cloud runs."""

from __future__ import annotations

import os
from pathlib import Path


def _choose_cache_root(project_root: Path) -> Path:
    explicit = os.environ.get("SFR_CACHE_ROOT")
    if explicit:
        return Path(explicit).expanduser()

    workspace = Path("/workspace")
    if workspace.exists() and os.access(workspace, os.W_OK):
        return workspace / ".cache"

    return project_root / ".cache"


def configure_runtime_cache(project_root: Path) -> Path:
    """Route Hugging Face, evaluate, and temp files to a writable cache root."""

    cache_root = _choose_cache_root(project_root)
    hf_root = cache_root / "huggingface"
    tmp_root = cache_root / "tmp"

    defaults = {
        "HF_HOME": hf_root,
        "HF_DATASETS_CACHE": hf_root / "datasets",
        "HUGGINGFACE_HUB_CACHE": hf_root / "hub",
        "TRANSFORMERS_CACHE": hf_root / "hub",
        "HF_EVALUATE_CACHE": hf_root / "evaluate",
        "HF_METRICS_CACHE": hf_root / "metrics",
        "HF_MODULES_CACHE": hf_root / "modules",
        "HF_DATASETS_DOWNLOADED_EVALUATE_PATH": hf_root / "evaluate" / "downloads",
        "HF_DATASETS_EXTRACTED_EVALUATE_PATH": hf_root / "evaluate" / "extracted",
        "MPLCONFIGDIR": cache_root / "matplotlib",
        "NUMBA_CACHE_DIR": cache_root / "numba",
        "TMPDIR": tmp_root,
    }

    for var, path in defaults.items():
        os.environ.setdefault(var, str(path))

    for var in defaults:
        Path(os.environ[var]).mkdir(parents=True, exist_ok=True)

    return cache_root