"""Hugging Face transport for the payload repo. WHY A PAYLOAD REPO. Colab cannot see the college box and the college box cannot push into a Colab VM. Everything both sides need therefore has to live somewhere both can reach. The three embedding repos already exist; this module manages a fourth repo that carries the *small* things: raw text, candidate shards, model checkpoints and this source tree. PRIVACY. The competition data is licensed to participants, not to the public. Every upload here defaults to a PRIVATE repo and `ensure_repo` will refuse to create a public one unless `public=True` is passed explicitly. Do not change that default. """ from __future__ import annotations import os import sys # ---------------------------------------------------------------- credentials # Put your token here so nothing has to be exported before every command. # # HF_TOKEN = "hf_PASTE_YOURS" # # (That example is deliberately too short to match the leak detector below. A realistic # dummy in a comment would trip the guard and refuse every upload.) # # TWO THINGS THIS FILE DOES TO KEEP THAT SAFE, because a token written into source is one # copy away from being published: # 1. `push_code` REDACTS this constant before uploading the source tree to the payload # repo. Without that, the first `--what code` upload would publish your token inside # the very dataset repo it authenticates to. The redaction is asserted, not assumed. # 2. `token_source()` reports where the token came from, so a surprising one is visible. # # It is still a secret in a file on a box 26 other people can log into. Prefer # `--token` or the environment if you ever share this directory, and rotate it when the # competition is over. HF_TOKEN = "" # redacted for upload _TOKEN_ENV = ("HF_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HUGGINGFACE_TOKEN") _TOKEN_FILE = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "hf_token.txt") _OVERRIDE: str | None = None def set_token(tok: str) -> None: """Used by --token, which beats every other source.""" global _OVERRIDE _OVERRIDE = tok.strip() or None def token() -> str | None: if _OVERRIDE: return _OVERRIDE if HF_TOKEN.strip(): return HF_TOKEN.strip() if os.path.exists(_TOKEN_FILE): v = open(_TOKEN_FILE, encoding="utf-8").read().strip() if v: return v for k in _TOKEN_ENV: v = os.environ.get(k) if v: return v return None def token_source() -> str: if _OVERRIDE: return "--token" if HF_TOKEN.strip(): return "berx/io_hf.py:HF_TOKEN" if os.path.exists(_TOKEN_FILE) and open(_TOKEN_FILE, encoding="utf-8").read().strip(): return _TOKEN_FILE for k in _TOKEN_ENV: if os.environ.get(k): return f"${k}" return "none" def _api(): from huggingface_hub import HfApi return HfApi(token=token()) def ensure_repo(repo_id: str, public: bool = False) -> str: """Create the dataset repo if it does not exist. Private unless asked otherwise.""" from huggingface_hub.utils import HfHubHTTPError api = _api() try: api.create_repo(repo_id, repo_type="dataset", private=not public, exist_ok=True) except HfHubHTTPError as e: # already exists under another owner, etc. print(f" create_repo: {e}", file=sys.stderr) return repo_id def upload(local_path: str, path_in_repo: str, repo_id: str, public: bool = False) -> str: """Upload one file or one directory. Returns the path in the repo.""" ensure_repo(repo_id, public) api = _api() if os.path.isdir(local_path): api.upload_folder(folder_path=local_path, path_in_repo=path_in_repo, repo_id=repo_id, repo_type="dataset") else: api.upload_file(path_or_fileobj=local_path, path_in_repo=path_in_repo, repo_id=repo_id, repo_type="dataset") return path_in_repo def fetch(path_in_repo: str, repo_id: str, local_dir: str | None = None) -> str: """Download one file from the payload repo and return its local path.""" from huggingface_hub import hf_hub_download return hf_hub_download(repo_id=repo_id, filename=path_in_repo, repo_type="dataset", local_dir=local_dir, token=token()) def fetch_dir(repo_id: str, allow: list[str] | None = None, local_dir: str | None = None) -> str: """Download a subset of a repo. `allow` is a list of glob patterns. Always pass `allow`. The embedding repos hold tens of gigabytes and a bare snapshot_download of one of them will fill a Colab disk before it finishes. """ from huggingface_hub import snapshot_download return snapshot_download(repo_id=repo_id, repo_type="dataset", allow_patterns=allow, local_dir=local_dir, token=token(), max_workers=8)