"""Sync refresh_data.json to the PRIVATE HF dataset, then restart the Space. Data lives in the private dataset (pramodmisra/pif-data), NOT in the public Space repo (which is anonymously downloadable). On Space restart, start.py pulls the dataset file via the HF_TOKEN Space secret and re-imports it. Flow: 1. Compare the freshly-built local file against the dataset's current copy. 2. If unchanged, do nothing (no upload, no restart). 3. If changed, upload to the dataset and restart the Space to re-import. Secret: HF_TOKEN (write scope) from the environment — never hardcoded. Dependency: huggingface_hub (CI-only; not in the Space requirements). """ from __future__ import annotations import os import sys from huggingface_hub import HfApi, hf_hub_download DATA_REPO_ID = "pramodmisra/pif-data" # private dataset SPACE_REPO_ID = "pramodmisra/pif" # public Space (web app) REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) REFRESH_FILE = os.path.join(REPO_ROOT, "refresh_data.json") def _remote_bytes(api: HfApi) -> bytes | None: """Current dataset copy, or None if it doesn't exist yet.""" try: path = hf_hub_download( repo_id=DATA_REPO_ID, repo_type="dataset", filename="refresh_data.json", token=api.token, ) with open(path, "rb") as fh: return fh.read() except Exception: return None def main() -> None: token = os.environ.get("HF_TOKEN") if not token: print("ERROR: HF_TOKEN not set in environment.", file=sys.stderr) sys.exit(1) if not os.path.exists(REFRESH_FILE): print(f"ERROR: {REFRESH_FILE} not found.", file=sys.stderr) sys.exit(1) api = HfApi(token=token) with open(REFRESH_FILE, "rb") as fh: local = fh.read() if local == _remote_bytes(api): print("No change vs. dataset; skipping upload + restart.") return print(f"Uploading refresh_data.json -> dataset/{DATA_REPO_ID} ...") api.upload_file( path_or_fileobj=REFRESH_FILE, path_in_repo="refresh_data.json", repo_id=DATA_REPO_ID, repo_type="dataset", commit_message="chore: daily client refresh from Google Sheet", ) print(f"Restarting Space {SPACE_REPO_ID} to re-import ...") api.restart_space(repo_id=SPACE_REPO_ID) print("Done; Space will re-pull the private dataset on boot.") if __name__ == "__main__": main()