File size: 2,059 Bytes
a9e46a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from pathlib import Path
from typing import List

import click
from datasets import load_from_disk, Dataset
from huggingface_hub import HfApi


def find_dataset_dirs(root: Path) -> List[Path]:
    if not root.exists():
        return []
    candidates: List[Path] = []
    for info_file in root.rglob("dataset_info.json"):
        ds_dir = info_file.parent
        if (ds_dir / "state.json").exists():
            candidates.append(ds_dir)
    return sorted({p.resolve() for p in candidates})


def get_published_dataset_name(rel: Path) -> str:
    rel_str = rel.as_posix()
    rel_parts = rel_str.split("__")
    task_name = rel_parts[0]
    dataset_name = rel_parts[-1]

    return f"{task_name}.{dataset_name}"


@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@click.option(
    "--out-dir",
    type=click.Path(path_type=Path),
    default=Path("data") / "hf",
    show_default=True,
    help="Directory containing saved Hugging Face datasets (produced by the preprocessing script).",
)
@click.option(
    "--namespace",
    type=str,
    required=True,
    help="Hugging Face user or organization to publish under (e.g., 'your-username').",
)
def main(
    out_dir: Path,
    namespace: str,
) -> None:
    out_dir = out_dir.resolve()
    api = HfApi()

    ds_dirs = find_dataset_dirs(out_dir)
    if not ds_dirs:
        click.echo(f"No datasets found under: {out_dir}")
        raise SystemExit(0)

    for idx, ds_path in enumerate(ds_dirs, start=1):
        rel = ds_path.relative_to(out_dir)
        published_dataset_name = get_published_dataset_name(rel)
        repo_id = f"{namespace}/{published_dataset_name}"
        click.echo(f"[{idx}/{len(ds_dirs)}] Publishing {published_dataset_name}")

        api.create_repo(
            repo_id=repo_id, repo_type="dataset", private=False, exist_ok=True
        )
        ds = load_from_disk(str(ds_path))
        ds.push_to_hub(
            repo_id=repo_id, commit_message=f"Add {published_dataset_name} dataset"
        )


if __name__ == "__main__":
    main()