File size: 1,818 Bytes
3495881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Push the research code to the HF Space. Never uploads data, checkpoints or secrets."""
import os
import sys
from pathlib import Path

from huggingface_hub import HfApi

ROOT = Path(__file__).parent.parent.resolve()
REPO = os.environ.get("HF_SPACE", "cazyundee/training")

INCLUDE = ["app.py", "train_selfplay.py", "inspect_trace.py", "requirements.txt",
           "README.md", "tinychess", "tests", "experiments", "configs", "scripts",
           "docs"]
SKIP_SUFFIX = (".pt", ".pkl", ".pgn", ".zst", ".parquet", ".log", ".pyc")
SKIP_PARTS = {"__pycache__", ".git", "runs", "data", "bin", ".venv"}


def collect():
    files = []
    for item in INCLUDE:
        p = ROOT / item
        if p.is_file():
            files.append(p)
        elif p.is_dir():
            for f in p.rglob("*"):
                if f.is_file() and not any(s in f.parts for s in SKIP_PARTS) \
                        and not f.name.endswith(SKIP_SUFFIX):
                    files.append(f)
    return files


def main():
    token = os.environ.get("HF_TOKEN")
    if not token:
        sys.exit("HF_TOKEN not set")
    api = HfApi(token=token)
    files = collect()
    total = sum(f.stat().st_size for f in files)
    print(f"uploading {len(files)} files ({total/1024:.0f} KB) -> {REPO}")
    ops = []
    from huggingface_hub import CommitOperationAdd
    for f in files:
        ops.append(CommitOperationAdd(path_in_repo=str(f.relative_to(ROOT)),
                                      path_or_fileobj=str(f)))
    api.create_commit(repo_id=REPO, repo_type="space", operations=ops,
                      commit_message=sys.argv[1] if len(sys.argv) > 1
                      else "update tinychess research code")
    print(f"done: https://huggingface.co/spaces/{REPO}")


if __name__ == "__main__":
    main()