Download sync_to_hf.py from eduzrh/helloworld: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/datasets/eduzrh/helloworld/resolve/main/sync_to_hf.py
- Command line
-
hf download hf://datasets/eduzrh/helloworld/sync_to_hf.py
-
curl -L -o sync_to_hf.py https://huggingface.co/datasets/eduzrh/helloworld/resolve/main/sync_to_hf.py
2.73 kB
| #!/usr/bin/env python3 | |
| """把本地工作同步到 HuggingFace 数据集仓库。 | |
| 用法: | |
| export HF_TOKEN=hf_xxx | |
| python3 sync_to_hf.py # 代码、结果、图件、文档 | |
| python3 sync_to_hf.py --all # 额外同步原始格点数据 | |
| python3 sync_to_hf.py --dry-run # 只列出将要上传的文件 | |
| """ | |
| import argparse | |
| import os | |
| import sys | |
| from pathlib import Path | |
| from huggingface_hub import HfApi | |
| REPO = os.environ.get("HF_REPO", "eduzrh/helloworld") | |
| ROOT = Path(__file__).resolve().parent | |
| # 本地目录 -> 仓库内目录 | |
| MAP = { | |
| "cdhe": { | |
| "code": ["compute_cdhe.py", "plot_results.py", "fig_case.py", "build_demo_data.py"], | |
| "docs": ["报告素材_全球CDHE状态评估.md"], | |
| "results": ["results"], | |
| "figures": ["figures"], | |
| "demo": ["demo"], | |
| }, | |
| } | |
| DATA = {"local": "cdhe/data", "remote": "data"} | |
| def plan(include_data): | |
| items = [] | |
| for local_dir, sub in MAP.items(): | |
| for remote_sub, names in sub.items(): | |
| for name in names: | |
| p = ROOT / local_dir / name | |
| if not p.exists(): | |
| print(f"skip (missing): {p}", file=sys.stderr) | |
| continue | |
| items.append((p, f"{remote_sub}/{p.name}" if p.is_file() else remote_sub)) | |
| if include_data: | |
| p = ROOT / DATA["local"] | |
| if p.exists(): | |
| items.append((p, DATA["remote"])) | |
| return items | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--all", action="store_true", help="包含原始数据") | |
| ap.add_argument("--dry-run", action="store_true") | |
| args = ap.parse_args() | |
| token = os.environ.get("HF_TOKEN") | |
| if not token and not args.dry_run: | |
| sys.exit("需要设置环境变量 HF_TOKEN") | |
| items = plan(args.all) | |
| for p, r in items: | |
| print(f"{p.relative_to(ROOT)} -> {r}") | |
| if args.dry_run: | |
| return | |
| api = HfApi(token=token) | |
| for p, r in items: | |
| if p.is_dir(): | |
| api.upload_folder(folder_path=str(p), path_in_repo=r, | |
| repo_id=REPO, repo_type="dataset", | |
| allow_patterns=["*.json", "*.png", "*.html", "*.js", "*.md"]) | |
| else: | |
| api.upload_file(path_or_fileobj=str(p), path_in_repo=r, | |
| repo_id=REPO, repo_type="dataset") | |
| print(f"uploaded {r}") | |
| readme = ROOT / "README.md" | |
| if readme.exists(): | |
| api.upload_file(path_or_fileobj=str(readme), path_in_repo="README.md", | |
| repo_id=REPO, repo_type="dataset") | |
| print("uploaded README.md") | |
| print(f"完成:https://huggingface.co/datasets/{REPO}") | |
| if __name__ == "__main__": | |
| main() | |