Download examples/visualizer_code/source/d4rt_scripts/upload_scale4d_view_count.py from facebook/SCALE4D: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/d4rt_scripts/upload_scale4d_view_count.py
- Command line
-
hf download hf://facebook/SCALE4D/examples/visualizer_code/source/d4rt_scripts/upload_scale4d_view_count.py
-
curl -L -o upload_scale4d_view_count.py https://huggingface.co/facebook/SCALE4D/resolve/main/examples/visualizer_code/source/d4rt_scripts/upload_scale4d_view_count.py
3.27 kB
| """Upload a checksum-verified view-count bundle under a dedicated HF prefix.""" | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| from demo.scale4d_view_count.bundle import bundle_path, verify_bundle | |
| def upload_bundle( | |
| root: Path, repo_id: str, prefix: str = "examples/view_count" | |
| ) -> dict: | |
| from huggingface_hub import ( | |
| CommitOperationAdd, | |
| HfApi, | |
| get_hf_file_metadata, | |
| hf_hub_url, | |
| ) | |
| manifest = verify_bundle(root) | |
| prefix = prefix.strip("/") | |
| if not prefix.startswith("examples/") or ".." in prefix.split("/"): | |
| raise ValueError("Uploads must remain under an examples/ subdirectory") | |
| paths = [row["path"] for row in manifest["files"]] + ["manifest.json"] | |
| api = HfApi() | |
| previous = api.model_info(repo_id) | |
| operations = [ | |
| CommitOperationAdd( | |
| path_in_repo=f"{prefix}/{name}", path_or_fileobj=bundle_path(root, name) | |
| ) | |
| for name in paths | |
| ] | |
| commit = api.create_commit( | |
| repo_id=repo_id, | |
| operations=operations, | |
| commit_message=f"Add {manifest.get('title', 'SCALE4D visualization inputs and results')}", | |
| parent_commit=previous.sha, | |
| ) | |
| print(f"Created Hugging Face commit {commit.oid}; verifying objects", flush=True) | |
| verified = [] | |
| for name in paths: | |
| path = bundle_path(root, name) | |
| remote = get_hf_file_metadata( | |
| hf_hub_url(repo_id, f"{prefix}/{name}", revision=commit.oid), | |
| token=api.token or True, | |
| ) | |
| sha256 = next( | |
| (row["sha256"] for row in manifest["files"] if row["path"] == name), None | |
| ) | |
| if sha256 is None: | |
| sha256 = hashlib.sha256(path.read_bytes()).hexdigest() | |
| blob_hash = hashlib.sha1() | |
| blob_hash.update(f"blob {path.stat().st_size}\0".encode()) | |
| with path.open("rb") as handle: | |
| for block in iter(lambda: handle.read(8 * 1024 * 1024), b""): | |
| blob_hash.update(block) | |
| if remote.size != path.stat().st_size or remote.etag not in ( | |
| sha256, | |
| blob_hash.hexdigest(), | |
| ): | |
| raise RuntimeError(f"Uploaded object differs from the bundle: {name}") | |
| verified.append(name) | |
| return { | |
| "repo_id": repo_id, | |
| "prefix": prefix, | |
| "parent_commit": previous.sha, | |
| "commit": commit.oid, | |
| "url": f"https://huggingface.co/{repo_id}/tree/{commit.oid}/{prefix}", | |
| "verified_files": verified, | |
| } | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--bundle", type=Path, required=True) | |
| parser.add_argument("--repo-id", default="facebook/SCALE4D") | |
| parser.add_argument("--prefix", default="examples/view_count") | |
| parser.add_argument("--receipt", type=Path, required=True) | |
| args = parser.parse_args() | |
| if args.receipt.exists(): | |
| parser.error("The upload receipt already exists") | |
| result = upload_bundle(args.bundle, args.repo_id, args.prefix) | |
| args.receipt.parent.mkdir(parents=True, exist_ok=True) | |
| args.receipt.write_text(json.dumps(result, indent=2) + "\n") | |
| print(json.dumps(result, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |