Spaces:
Running
Running
Download preprocessing/artifact_manager.py from anikethans06/capstone: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/spaces/anikethans06/capstone/resolve/main/preprocessing/artifact_manager.py
- Command line
-
hf download hf://spaces/anikethans06/capstone/preprocessing/artifact_manager.py
-
curl -L -o artifact_manager.py https://huggingface.co/spaces/anikethans06/capstone/resolve/main/preprocessing/artifact_manager.py
4.26 kB
| """ | |
| preprocessing/artifact_manager.py | |
| βββββββββββββββββββββββββββββββββββ | |
| Unified save/load for all preprocessing artifacts. | |
| Dispatches to joblib (sklearn objects), pickle (vocab), or JSON (configs). | |
| """ | |
| import json | |
| import pickle | |
| import pathlib | |
| import logging | |
| from datetime import datetime, timezone | |
| from typing import Any | |
| import joblib | |
| from preprocessing.config import ARTIFACTS_DIR, ARTIFACT_PREP_LOG | |
| logger = logging.getLogger(__name__) | |
| # ββ Save ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def save_joblib(obj: Any, path: pathlib.Path) -> pathlib.Path: | |
| """Save a sklearn-compatible object with joblib (efficient for sparse matrices).""" | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| joblib.dump(obj, path) | |
| logger.info("Saved (joblib) β %s", path) | |
| return path | |
| def save_pickle(obj: Any, path: pathlib.Path) -> pathlib.Path: | |
| """Save an arbitrary Python object with pickle.""" | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with open(path, "wb") as f: | |
| pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL) | |
| logger.info("Saved (pickle) β %s", path) | |
| return path | |
| def save_json(data: dict, path: pathlib.Path) -> pathlib.Path: | |
| """Save a dict as pretty-printed JSON.""" | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| with open(path, "w", encoding="utf-8") as f: | |
| json.dump(data, f, indent=2, ensure_ascii=False) | |
| logger.info("Saved (json) β %s", path) | |
| return path | |
| # ββ Load ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def load_joblib(path: pathlib.Path) -> Any: | |
| _require_exists(path) | |
| obj = joblib.load(path) | |
| logger.info("Loaded (joblib) β %s", path) | |
| return obj | |
| def load_pickle(path: pathlib.Path) -> Any: | |
| _require_exists(path) | |
| with open(path, "rb") as f: | |
| obj = pickle.load(f) | |
| logger.info("Loaded (pickle) β %s", path) | |
| return obj | |
| def load_json(path: pathlib.Path) -> dict: | |
| _require_exists(path) | |
| with open(path, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| logger.info("Loaded (json) β %s", path) | |
| return data | |
| # ββ Metadata / log ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def save_preprocessing_log(entries: dict) -> pathlib.Path: | |
| """ | |
| Append a timestamped entry to the preprocessing run log. | |
| Useful for tracking when each artifact was created and with what config. | |
| """ | |
| log_path = ARTIFACTS_DIR / ARTIFACT_PREP_LOG | |
| existing: list = [] | |
| if log_path.exists(): | |
| with open(log_path) as f: | |
| existing = json.load(f) | |
| entries["timestamp"] = datetime.now(timezone.utc).isoformat() | |
| existing.append(entries) | |
| with open(log_path, "w") as f: | |
| json.dump(existing, f, indent=2) | |
| logger.info("Preprocessing log updated β %s", log_path) | |
| return log_path | |
| def list_artifacts() -> list[pathlib.Path]: | |
| """Return all files currently saved in the artifacts directory.""" | |
| files = sorted(ARTIFACTS_DIR.rglob("*")) | |
| files = [f for f in files if f.is_file()] | |
| if files: | |
| logger.info("Artifacts in %s:", ARTIFACTS_DIR) | |
| for f in files: | |
| logger.info(" %s (%d bytes)", f.relative_to(ARTIFACTS_DIR), f.stat().st_size) | |
| else: | |
| logger.info("No artifacts found in %s", ARTIFACTS_DIR) | |
| return files | |
| # ββ Internal ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _require_exists(path: pathlib.Path) -> None: | |
| if not path.exists(): | |
| raise FileNotFoundError( | |
| f"Artifact not found: {path}\n" | |
| f"Run run_preprocessing.py first to generate it." | |
| ) | |