File size: 4,372 Bytes
aa991fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358e603
 
 
aa991fc
 
 
 
 
 
 
 
 
 
 
 
 
 
358e603
 
 
aa991fc
358e603
 
aa991fc
 
358e603
aa991fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
#!/usr/bin/env python
"""Push the staging tree to the HuggingFace dataset repo.

Two upload paths:
  * SMALL files (README, previews/, assets/, scripts/, cameras.json, smplx.npz,
    capture.json, preview.jpg) -> `upload_folder`, one atomic commit.
  * BIG files (data/<PxCy>/videos/*.mp4, ~2 GiB per capture) -> `upload_large_folder`,
    which is chunked, multi-worker and RESUMABLE: it keeps per-file state under
    <staging>/.cache/huggingface, so re-running after a network drop skips what is done.

Usage:
    python hf_upload.py --staging .../staging --capture P1C1          # meta + that capture
    python hf_upload.py --staging .../staging --meta-only             # README/previews/scripts
    python hf_upload.py --staging .../staging --capture P1C1 --videos-only
"""
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from huggingface_hub import HfApi

REPO_ID = "initialneil/DREAMS-AVATAR"

META_PATTERNS = [
    "README.md", "LICENSE",
    "previews/*.jsonl", "previews/*.jpg", "assets/**/*.jpg",
    "scripts/*.py", "scripts/*.sh",
]
CAPTURE_SMALL = ["data/{cap}/cameras.json", "data/{cap}/smplx.npz",
                 "data/{cap}/capture.json", "data/{cap}/preview.jpg"]
CAPTURE_BIG = ["data/{cap}/videos/*.mp4"]


def ensure_repo(api: HfApi, repo_id: str, private: bool) -> str:
    url = api.create_repo(repo_id=repo_id, repo_type="dataset", private=private,
                          exist_ok=True)
    info = api.repo_info(repo_id=repo_id, repo_type="dataset")
    print(f"[repo] {url}  private={info.private}", flush=True)
    return str(url)


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--staging", type=Path, required=True)
    ap.add_argument("--repo-id", default=REPO_ID)
    ap.add_argument("--capture", default=None)
    ap.add_argument("--meta-only", action="store_true")
    ap.add_argument("--videos-only", action="store_true")
    ap.add_argument("--private", action="store_true", help="create as private (default PUBLIC)")
    ap.add_argument("--prune-previews", action="store_true",
                    help="delete repo previews/showcase jpgs that are no longer in staging "
                         "(needed when frame labels change). Never touches data/ or scripts/.")
    ap.add_argument("--workers", type=int, default=4)
    a = ap.parse_args()

    api = HfApi()
    who = api.whoami()["name"]
    print(f"[hf] logged in as {who}", flush=True)
    ensure_repo(api, a.repo_id, a.private)

    if not a.videos_only:
        pats = list(META_PATTERNS)
        if a.capture:
            pats += [p.format(cap=a.capture) for p in CAPTURE_SMALL]
        msg = (f"Add {a.capture} metadata + SMPL-X + previews" if a.capture
               else "Update dataset card / previews / scripts")
        # scoped on purpose: only preview/showcase images may be pruned, so a stale
        # frame labelling cannot leave orphan rows in the viewer. data/ is never touched.
        dels = ["previews/*.jpg", "assets/showcase/*.jpg"] if a.prune_previews else None
        print(f"[upload] small files: {pats}", flush=True)
        if dels:
            print(f"[upload] pruning stale: {dels}", flush=True)
        ci = api.upload_folder(repo_id=a.repo_id, repo_type="dataset",
                               folder_path=str(a.staging), allow_patterns=pats,
                               delete_patterns=dels, commit_message=msg)
        print(f"[upload] commit {getattr(ci, 'oid', ci)}", flush=True)

    if a.capture and not a.meta_only:
        pats = [p.format(cap=a.capture) for p in CAPTURE_BIG]
        print(f"[upload] LARGE (resumable): {pats}", flush=True)
        api.upload_large_folder(repo_id=a.repo_id, repo_type="dataset",
                                folder_path=str(a.staging), allow_patterns=pats,
                                num_workers=a.workers, print_report=True,
                                print_report_every=30)
        print("[upload] large folder done", flush=True)

    files = api.list_repo_files(repo_id=a.repo_id, repo_type="dataset")
    print(json.dumps({"repo": f"https://huggingface.co/datasets/{a.repo_id}",
                      "n_files": len(files)}))
    return 0


if __name__ == "__main__":
    sys.exit(main())