pocket-math-ops / code /kaggle /bootstrap.py
aafimalek2032's picture
code 9509a58e65b8
8cbfa56 verified
Raw History Blame Contribute Delete
1.6 kB
"""The only code in the Kaggle notebook. It never changes, so friends' copies never go stale.
It reads the HF token, downloads the current code from the ops repo on the Hub, and hands
over to kaggle/session.py from that download. JOB_ID and OPS_REPO are the only settings.
"""
JOB_ID = "auto" # leave as "auto": the session picks the next job in the queue
OPS_REPO = "ORG/pocket-math-ops"
import glob # noqa: E402
import os # noqa: E402
import subprocess # noqa: E402
import sys # noqa: E402
import time # noqa: E402
os.environ["PM_SESSION_START"] = str(time.time())
def _hf_token() -> str:
try:
from kaggle_secrets import UserSecretsClient
return UserSecretsClient().get_secret("HF_TOKEN")
except Exception: # noqa: BLE001 - fall back to a private dataset (own-account script kernels)
hits = glob.glob("/kaggle/input/**/hf_token", recursive=True)
if hits:
return open(hits[0]).read().strip()
return os.environ.get("HF_TOKEN", "")
os.environ["HF_TOKEN"] = _hf_token()
if not os.environ["HF_TOKEN"]:
raise SystemExit("No HF token. Add a Kaggle Secret named HF_TOKEN (Add-ons > Secrets) and attach it.")
from huggingface_hub import snapshot_download # noqa: E402
root = snapshot_download(OPS_REPO, repo_type="dataset", allow_patterns=["code/**"], local_dir="/tmp/pm-ops",
token=os.environ["HF_TOKEN"])
rc = subprocess.call([sys.executable, f"{root}/code/kaggle/session.py", "--job", JOB_ID, "--ops-repo", OPS_REPO])
print(f"session finished (exit code {rc})")