File size: 2,732 Bytes
46709a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
"""v2 step 6: publish the staged v2 folder to a NEW BRANCH of the Hugging Face repo (never main).

  a. create <branch> from main's pinned commit (refuses if main moved, or if <branch> exists at another base)
  b. upload_large_folder(<staged>, revision=<branch>)  (resumable; commits only to <branch>)
  c. one commit on <branch> deleting the v1 files that v2 replaces; aborts if any path to delete is not a
     v1 data file / v1 split manifest / the stray .wt files
  usage: v2_upload.py <repo_id> <main_sha> <branch> <staged_dir> [a|b|c ...]
"""
import re, sys
from huggingface_hub import HfApi, CommitOperationDelete

repo, main_sha, branch, staged = sys.argv[1:5]; steps = sys.argv[5:] or ["a", "b", "c"]
assert branch not in ("main", "master") and re.fullmatch(r"[A-Za-z0-9._-]+", branch), branch
api = HfApi(); T = "dataset"

if "a" in steps:
    head = api.list_repo_refs(repo, repo_type=T)
    main = [b for b in head.branches if b.name == "main"][0].target_commit
    assert main == main_sha, f"main moved: {main} != {main_sha}"
    existing = {b.name: b.target_commit for b in head.branches}
    if branch in existing:
        print(f"branch {branch} exists at {existing[branch]}")
    else:
        api.create_branch(repo, branch=branch, revision=main_sha, repo_type=T)
        print(f"created branch {branch} at {main_sha}")

if "b" in steps:
    api.upload_large_folder(repo, staged, repo_type=T, revision=branch, num_workers=16,
                            ignore_patterns=[".cache/**", ".manifest.tsv", "**/*.tmp", "**/*-lock"])

if "c" in steps:
    import os
    local = set()
    for d, _, fs in os.walk(staged):
        if "/.cache" in d or d.endswith(".cache"): continue
        for f in fs: local.add(os.path.relpath(os.path.join(d, f), staged))
    remote = api.list_repo_files(repo, repo_type=T, revision=branch)
    old = sorted(set(remote) - local)
    ok = re.compile(r"^((lemat|oc20|oc22|mp20bat)/collected_sp_rank_\d+\.aselmdb|splits/(lemat|oc20|oc22|mp20bat)/(train|val|test)\.parquet|\.wt|\.wt2)$")
    bad = [p for p in old if not ok.match(p)]
    print(f"{len(old)} v1 paths to delete on {branch}; unexpected: {bad}")
    assert not bad, "refusing to delete unexpected paths"
    missing = sorted(local - set(remote)); assert not missing, f"not uploaded yet: {missing[:10]}"
    head = api.list_repo_refs(repo, repo_type=T); parent = [b for b in head.branches if b.name == branch][0].target_commit
    if old:
        ci = api.create_commit(repo, [CommitOperationDelete(p) for p in old], repo_type=T, revision=branch, parent_commit=parent,
                               commit_message="v2: remove v1 files replaced by the deduplicated train/val/test layout")
        print("delete commit", ci.oid)