File size: 1,602 Bytes
8cbfa56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""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})")