File size: 2,376 Bytes
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
upload_binaries.py -- push binary files to a HF Space via the Xet backend.

HF Spaces now reject binary files on plain `git push`; the supported path is
`huggingface_hub` (>= 0.32 with `hf_xet`), which uploads through Xet storage.

Usage:
    python upload_binaries.py -r OWNER/REPO -p path/in/repo.db -f local/file.db

Auth: uses your cached HF token (created by `huggingface-cli login`).
      Skips the upload if the remote file already has identical bytes.
"""
import argparse
import hashlib
import sys

from huggingface_hub import HfApi, hf_hub_download, get_token


def sha256_of(path: str) -> str:
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("-r", "--repo", required=True, help="repo id, e.g. OWNER/SPACE")
    ap.add_argument("-p", "--path-in-repo", required=True)
    ap.add_argument("-f", "--file", required=True, help="local file to upload")
    args = ap.parse_args()

    if not get_token():
        print("ERROR: no HF token cached. Run 'huggingface-cli login' (token needs write access) and retry.")
        return 2

    api = HfApi()
    repo_type = "space"  # this script targets Spaces ("OWNER/SPACE" ids)

    remote = None
    try:
        remote = hf_hub_download(
            repo_id=args.repo, repo_type=repo_type, filename=args.path_in_repo
        )
    except Exception:
        remote = None

    local_hash = sha256_of(args.file)
    if remote is not None:
        try:
            if sha256_of(remote) == local_hash:
                print(f"[XET] {args.path_in_repo} already identical on {args.repo} - skipping upload")
                return 0
            print(f"[XET] {args.path_in_repo} differs on the remote {args.repo} - uploading new revision")
        except OSError:
            pass

    print(f"[XET] uploading {args.path_in_repo} ({args.file}) to {args.repo} ...")
    api.upload_file(
        repo_id=args.repo,
        repo_type=repo_type,
        path_in_repo=args.path_in_repo,
        path_or_fileobj=args.file,
        commit_message=f"Add binary demo database via Xet: {args.path_in_repo}",
    )
    print(f"[XET] uploaded OK (sha256 {local_hash[:12]}...)")
    return 0


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