"""Storage utilities for loading/saving match results from HuggingFace Hub.""" from __future__ import annotations import json import os from huggingface_hub import HfApi, hf_hub_download STORAGE_REPO = "Koddenbrock/WorldcupPredictionGame-storage" def load_results() -> list[dict]: """Load actual match results from HF storage repo. Returns [] if not available.""" try: path = hf_hub_download( repo_id=STORAGE_REPO, filename="results/match_results.json", repo_type="dataset", ) with open(path) as f: return json.load(f) except Exception: return [] def save_results(results: list[dict]) -> None: """Upload results to HF storage repo (requires HF_TOKEN env var).""" api = HfApi() api.upload_file( path_or_fileobj=json.dumps(results, indent=2).encode(), path_in_repo="results/match_results.json", repo_id=STORAGE_REPO, repo_type="dataset", token=os.environ.get("HF_TOKEN"), )