Download recipe/publish.py from ZipLime/us-treasury-events: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/datasets/ZipLime/us-treasury-events/resolve/main/recipe/publish.py
- Command line
-
hf download hf://datasets/ZipLime/us-treasury-events/recipe/publish.py
-
curl -L -o publish.py https://huggingface.co/datasets/ZipLime/us-treasury-events/resolve/main/recipe/publish.py
2.93 kB
| """Bootstrap and atomic same-revision publication through Hugging Face Hub.""" | |
| from __future__ import annotations | |
| import logging | |
| import os | |
| from pathlib import Path | |
| logger = logging.getLogger(__name__) | |
| def publish_to_hf(*, project_root: Path, repo_id: str, token: str | None = None) -> str: | |
| try: | |
| from huggingface_hub import HfApi | |
| except ImportError as error: # pragma: no cover - exercised in publish environment | |
| raise RuntimeError("install the 'publish' extra to publish to Hugging Face") from error | |
| api = HfApi(token=token) | |
| api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True) | |
| local = { | |
| str(path.relative_to(project_root)) | |
| for path in project_root.glob("data/**/*.parquet") | |
| } | |
| commit = api.upload_folder( | |
| repo_id=repo_id, | |
| repo_type="dataset", | |
| folder_path=project_root, | |
| # Without this, an upload only ever adds. A rebuild that drops a | |
| # quarantined release would leave its rows behind in the published | |
| # tree, and the dataset would then disagree with its own recipe. | |
| delete_patterns=["data/**"], | |
| allow_patterns=[ | |
| "data/**", | |
| "recipe/**", | |
| "config/**", | |
| "metadata/**", | |
| "tests/**", | |
| "jobs/**", | |
| "README.md", | |
| "PIPELINE.md", | |
| "manifest.json", | |
| "quality_report.json", | |
| "pyproject.toml", | |
| "LICENSE", | |
| "NOTICE", | |
| ], | |
| # The downloaded documents are a local cache. Every one of them is | |
| # reproducible from the URL and the digest recorded beside the row it | |
| # produced, so publishing them would be publishing the Board's website. | |
| ignore_patterns=["**/__pycache__/**", "**/*.pyc", ".cache/**"], | |
| commit_message="Update the FOMC events dataset", | |
| ) | |
| published = { | |
| sibling.rfilename | |
| for sibling in api.dataset_info(repo_id).siblings | |
| if sibling.rfilename.startswith("data/") and sibling.rfilename.endswith(".parquet") | |
| } | |
| stale, missing = published - local, local - published | |
| if stale or missing: | |
| raise RuntimeError( | |
| f"published tree does not match the local one: {len(stale)} stale file(s) " | |
| f"left behind, {len(missing)} missing. Examples: " | |
| f"stale={sorted(stale)[:3]} missing={sorted(missing)[:3]}" | |
| ) | |
| logger.info("published tree mirrors %d local data files", len(local)) | |
| return str(commit.oid) | |
| def hf_token() -> str | None: | |
| """The token to act with: the environment first, then a stored login.""" | |
| token = os.environ.get("HF_TOKEN") | |
| if token: | |
| return token | |
| try: | |
| from huggingface_hub import get_token | |
| except ImportError: # pragma: no cover - exercised in the publish environment | |
| return None | |
| return get_token() | |
| __all__ = ["hf_token", "publish_to_hf"] | |