hubsync.ensure_repo must install creds first (real cause of the 401); correct E-017 diagnosis
Browse files- build/hubsync.py +113 -105
build/hubsync.py
CHANGED
|
@@ -1,105 +1,113 @@
|
|
| 1 |
-
# Hub sync for the mix build: the instance is scratch, so nothing that took an hour to produce may live
|
| 2 |
-
# only there (§3.13 "never let the instance be the only copy", applied to a CPU job rather than a
|
| 3 |
-
# checkpoint).
|
| 4 |
-
#
|
| 5 |
-
# Granularity is one *source*: staging a source takes 5-40 minutes, so publishing its shards the moment
|
| 6 |
-
# it finishes bounds the loss from any interruption to the one source in flight, and a resume can skip
|
| 7 |
-
# every source already on the Hub without re-reading it.
|
| 8 |
-
#
|
| 9 |
-
# The stage repo is private-in-practice but public by project rule; it holds no credentials and no
|
| 10 |
-
# benchmark material -- only token ids from the approved mix.
|
| 11 |
-
|
| 12 |
-
import json
|
| 13 |
-
import os
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def repo_id_from_env(default=None):
|
| 17 |
-
return os.environ.get("OUNCE100M_STAGE_REPO") or default
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def api():
|
| 21 |
-
import os
|
| 22 |
-
|
| 23 |
-
import ounce100m_credentials
|
| 24 |
-
|
| 25 |
-
ounce100m_credentials.install()
|
| 26 |
-
from huggingface_hub import HfApi
|
| 27 |
-
|
| 28 |
-
return HfApi(token=os.environ["HF_TOKEN"])
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def ensure_repo(repo):
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hub sync for the mix build: the instance is scratch, so nothing that took an hour to produce may live
|
| 2 |
+
# only there (§3.13 "never let the instance be the only copy", applied to a CPU job rather than a
|
| 3 |
+
# checkpoint).
|
| 4 |
+
#
|
| 5 |
+
# Granularity is one *source*: staging a source takes 5-40 minutes, so publishing its shards the moment
|
| 6 |
+
# it finishes bounds the loss from any interruption to the one source in flight, and a resume can skip
|
| 7 |
+
# every source already on the Hub without re-reading it.
|
| 8 |
+
#
|
| 9 |
+
# The stage repo is private-in-practice but public by project rule; it holds no credentials and no
|
| 10 |
+
# benchmark material -- only token ids from the approved mix.
|
| 11 |
+
|
| 12 |
+
import json
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def repo_id_from_env(default=None):
|
| 17 |
+
return os.environ.get("OUNCE100M_STAGE_REPO") or default
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def api():
|
| 21 |
+
import os
|
| 22 |
+
|
| 23 |
+
import ounce100m_credentials
|
| 24 |
+
|
| 25 |
+
ounce100m_credentials.install()
|
| 26 |
+
from huggingface_hub import HfApi
|
| 27 |
+
|
| 28 |
+
return HfApi(token=os.environ["HF_TOKEN"])
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def ensure_repo(repo):
|
| 32 |
+
# Must install() first: the module-level create_repo() resolves its token from the environment, and
|
| 33 |
+
# this is the FIRST hub call a build makes -- before any api() has run. Calling it unauthenticated
|
| 34 |
+
# is what produced the misleading 401 in the cold-resume rehearsal (E-017), and the diagnosis in
|
| 35 |
+
# these notes was wrong about the cause until p2-token-scope proved dataset writes are permitted.
|
| 36 |
+
import os
|
| 37 |
+
|
| 38 |
+
api()
|
| 39 |
+
from huggingface_hub import create_repo
|
| 40 |
+
|
| 41 |
+
# hub 1.11 (the image's version) takes the owner inside repo_id and has no `namespace` kwarg (E-013)
|
| 42 |
+
create_repo(repo_id=repo, repo_type="dataset", private=False, exist_ok=True,
|
| 43 |
+
token=os.environ["HF_TOKEN"])
|
| 44 |
+
return repo
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def remote_stage_files(repo):
|
| 48 |
+
"""Set of relative paths under stage/ that already exist on the Hub."""
|
| 49 |
+
import urllib.request
|
| 50 |
+
|
| 51 |
+
url = f"https://huggingface.co/api/datasets/{repo}/tree/main/stage?recursive=true"
|
| 52 |
+
try:
|
| 53 |
+
with urllib.request.urlopen(url, timeout=90) as r:
|
| 54 |
+
entries = json.loads(r.read().decode())
|
| 55 |
+
except Exception:
|
| 56 |
+
return set()
|
| 57 |
+
out = set()
|
| 58 |
+
for e in entries:
|
| 59 |
+
if e.get("type") != "file":
|
| 60 |
+
continue
|
| 61 |
+
out.add(e["path"])
|
| 62 |
+
# a directory in the tree listing means we must recurse; recursive=true should cover it, but
|
| 63 |
+
# if the server ignored the flag we get dirs and no files, which the caller detects as empty
|
| 64 |
+
return out
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def upload_source(root, key, repo):
|
| 68 |
+
"""Publish stage/<key>/* in one commit. Returns the file names pushed."""
|
| 69 |
+
a = api()
|
| 70 |
+
local = os.path.join(root, "stage", key)
|
| 71 |
+
files = sorted(f for f in os.listdir(local) if f.endswith((".bin", ".json")))
|
| 72 |
+
if not files:
|
| 73 |
+
return []
|
| 74 |
+
a.upload_folder(folder_path=local, path_in_repo=f"stage/{key}", repo_id=repo,
|
| 75 |
+
repo_type="dataset", commit_message=f"stage {key}: {len(files)} shard(s)",
|
| 76 |
+
allow_patterns=["*.bin", "*.json"])
|
| 77 |
+
return files
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def download_missing(root, keys_wanted, repo, remote=None):
|
| 81 |
+
"""Fetch stage/<key>/*.bin for every wanted key not already on disk. Returns {key: [filenames]}."""
|
| 82 |
+
from huggingface_hub import hf_hub_download
|
| 83 |
+
|
| 84 |
+
remote = remote if remote is not None else remote_stage_files(repo)
|
| 85 |
+
got = {}
|
| 86 |
+
for key in keys_wanted:
|
| 87 |
+
want = sorted(p for p in remote if p.startswith(f"stage/{key}/") and p.endswith(".bin"))
|
| 88 |
+
if not want:
|
| 89 |
+
got[key] = []
|
| 90 |
+
continue
|
| 91 |
+
local_dir = os.path.join(root, "stage", key)
|
| 92 |
+
os.makedirs(local_dir, exist_ok=True)
|
| 93 |
+
have = {f"stage/{key}/{f}" for f in os.listdir(local_dir)}
|
| 94 |
+
for rp in want:
|
| 95 |
+
if rp in have:
|
| 96 |
+
continue
|
| 97 |
+
hf_hub_download(repo_id=repo, repo_type="dataset", filename=rp,
|
| 98 |
+
local_dir=root, force_download=False)
|
| 99 |
+
got[key] = sorted(os.listdir(local_dir))
|
| 100 |
+
return got
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def status(repo):
|
| 104 |
+
import urllib.request
|
| 105 |
+
|
| 106 |
+
url = f"https://huggingface.co/api/datasets/{repo}"
|
| 107 |
+
try:
|
| 108 |
+
with urllib.request.urlopen(url, timeout=60) as r:
|
| 109 |
+
d = json.loads(r.read().decode())
|
| 110 |
+
return {"exists": True, "gated": d.get("gated"), "private": d.get("private"),
|
| 111 |
+
"lastModified": d.get("lastModified"), "id": d.get("id")}
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return {"exists": False, "error": f"{type(e).__name__}: {str(e)[:120]}"}
|