| """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 |
|
|