aim / upload_to_hf_large_folder.py
weihan1's picture
Add files using upload-large-folder tool
cf8a617 verified
#!/usr/bin/env python3
"""Upload AiM_full_video (species folders + mp4) to a Hugging Face dataset repo."""
from __future__ import annotations
import argparse
from pathlib import Path
from huggingface_hub import HfApi
DEFAULT_REPO_ID = "weihan1/aim"
DEFAULT_IGNORE = (
"**/*.py",
"**/*.sh",
"**/*.ytdl",
"**/.git/**",
"**/__pycache__/**",
)
def upload_dataset(
*,
repo_id: str = DEFAULT_REPO_ID,
root: Path | None = None,
token: str | bool | None = None,
commit_message: str | None = None,
ignore_patterns: tuple[str, ...] | list[str] | None = None,
) -> None:
"""Create the dataset repo if needed, then upload ``root`` preserving subpaths.
``root`` should be the directory that contains ``dog/``, ``fox/``, etc.
Authentication uses the Hugging Face token from ``token``, or the
environment / cached login when ``token`` is None.
"""
root = root or Path(__file__).resolve().parent
root = root.resolve()
if not root.is_dir():
raise FileNotFoundError(f"root is not a directory: {root}")
api = HfApi(token=token)
api.create_repo(repo_id, repo_type="dataset", exist_ok=True)
ignores: list[str] = list(ignore_patterns or DEFAULT_IGNORE)
api.upload_large_folder(
repo_id=repo_id,
repo_type="dataset",
folder_path=str(root),
)
def main() -> None:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--repo-id", default=DEFAULT_REPO_ID, help="HF dataset repo id")
p.add_argument(
"--root",
type=Path,
default=Path(__file__).resolve().parent,
help="Species folder",
)
p.add_argument(
"--token",
default=None,
help="HF token (default: HF_TOKEN env or cached `hf auth login`)",
)
args = p.parse_args()
upload_dataset(
repo_id=args.repo_id,
root=args.root,
token=args.token,
)
print(f"Uploaded {args.root.resolve()} -> {args.repo_id} (dataset)")
if __name__ == "__main__":
main()