Download research/check_hf.py from Helinity/python-security-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 2.43 kB
-
https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/research/check_hf.py
- Command line
-
hf download hf://datasets/Helinity/python-security-benchmark/research/check_hf.py
-
curl -L -o check_hf.py https://huggingface.co/datasets/Helinity/python-security-benchmark/resolve/main/research/check_hf.py
2.43 kB
| """Verify Hugging Face Datasets loads the local card and every configured split offline.""" | |
| import json | |
| import os | |
| from pathlib import Path | |
| from tempfile import TemporaryDirectory | |
| ROOT = Path(__file__).resolve().parents[1] | |
| FILES = { | |
| **{config: {split: f"data/research/v3c1/{scope}/{split}.jsonl" for split in ("train", "validation", "test")} | |
| for config, scope in (("corpus_3c1", "corpus"), ("official_3c1", "official"))}, | |
| "official": {split: f"data/research/v3b2/official/{split}.jsonl" for split in ("train", "validation", "test")}, | |
| "default": {"examples": "data/examples.jsonl"}, | |
| **{ | |
| configuration: { | |
| split: f"data/research/{configuration}/{split}.jsonl" | |
| for split in ("train", "validation", "test") | |
| } | |
| for configuration in ("corpus", "benchmark") | |
| }, | |
| } | |
| def main(): | |
| os.environ["HF_HUB_OFFLINE"] = "1" | |
| os.environ["HF_DATASETS_OFFLINE"] = "1" | |
| os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1" | |
| try: | |
| from datasets import __version__, load_dataset | |
| from huggingface_hub import DatasetCard | |
| except ImportError as error: | |
| raise SystemExit("Install datasets==5.2.0 in the local test environment.") from error | |
| card = DatasetCard.load(ROOT / "README.md") | |
| configs = { | |
| config["config_name"]: {entry["split"]: entry["path"] for entry in config["data_files"]} | |
| for config in card.data.configs | |
| } | |
| if configs != FILES: | |
| raise ValueError("Dataset Card paths differ from the audited configuration map") | |
| with TemporaryDirectory(prefix="codedna-hf-") as cache: | |
| for config, splits in FILES.items(): | |
| loaded = load_dataset(str(ROOT), config, cache_dir=cache, token=False) | |
| if loaded.keys() != splits.keys(): | |
| raise ValueError(f"{config}: split names differ from the card") | |
| for split, path in splits.items(): | |
| raw = (ROOT / path).read_text(encoding="utf-8") | |
| expected = [json.loads(line) for line in raw.splitlines()] | |
| if loaded[split].to_list() != expected: | |
| raise ValueError(f"{config}/{split}: loaded records differ from JSONL") | |
| print(f"{config}/{split}: {len(expected)} rows, {len(loaded[split].column_names)} fields") | |
| print(f"Datasets {__version__}: every configured split loaded offline and matched its JSONL.") | |
| if __name__ == "__main__": | |
| main() | |