Datasets:
Download tools/validate.py from SEA-AI/seashapes: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/datasets/SEA-AI/seashapes/resolve/main/tools/validate.py
- Command line
-
hf download hf://datasets/SEA-AI/seashapes/tools/validate.py
-
curl -L -o validate.py https://huggingface.co/datasets/SEA-AI/seashapes/resolve/main/tools/validate.py
1.93 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| # /// script | |
| # requires-python = ">=3.11" | |
| # dependencies = ["jsonschema"] | |
| # /// | |
| """Check every row of metadata.jsonl, filling in each file's size and SHA-256. | |
| uv run tools/validate.py | |
| Run from the dataset root before an upload. A row whose folder is here is checked | |
| against its files; one whose folder was not downloaded, against the schema only. | |
| Exits non-zero, naming the rows that fail. | |
| """ | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| from jsonschema import Draft202012Validator | |
| META = Path("metadata.jsonl") | |
| schema = Draft202012Validator(json.loads(Path("schema.json").read_text())) | |
| rows = [json.loads(line) for line in META.read_text().splitlines() if line.strip()] | |
| errors = [] | |
| local = 0 | |
| for row in rows: | |
| asset = row.get("id", "?") | |
| model, preview = Path(row.get("file", "")), Path(row.get("file_name", "")) | |
| if model.parts[:1] != (asset,) or preview.parts[:1] != (asset,): | |
| errors.append(f"{asset}: file and file_name must sit in the folder {asset}/") | |
| if not Path(asset).is_dir(): | |
| errors += [f"{asset}: {e.message}" for e in schema.iter_errors(row)] | |
| continue | |
| local += 1 | |
| if model.is_file(): | |
| with model.open("rb") as f: | |
| row["sha256"] = hashlib.file_digest(f, "sha256").hexdigest() | |
| row["size_bytes"] = model.stat().st_size | |
| else: | |
| errors.append(f"{asset}: no file {model}") | |
| if not preview.is_file(): | |
| errors.append(f"{asset}: no preview {preview}; run tools/preview.py {asset}") | |
| errors += [f"{asset}: {e.message}" for e in schema.iter_errors(row)] | |
| ids = [row.get("id") for row in rows] | |
| errors += [f"{i}: id used twice" for i in sorted({i for i in ids if ids.count(i) > 1})] | |
| META.write_text("".join(json.dumps(row) + "\n" for row in rows)) | |
| print( | |
| "\n".join(errors) or f"{len(rows)} rows valid, {local} checked against their files" | |
| ) | |
| sys.exit(bool(errors)) | |