QueryLensV2b / upload_binaries.py
Aniruddha7's picture
Full deploy of QueryLensV2 (code + demo db) to a fresh Space
19dfd32 verified
Raw History Blame Contribute Delete
2.38 kB
"""
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())