File size: 2,048 Bytes
cf8a617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
#!/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()