Datasets:
Add tools/validate.py; adding an asset without a clone
Browse files- README.md +17 -2
- tools/validate.py +52 -0
README.md
CHANGED
|
@@ -58,8 +58,23 @@ https://huggingface.co/datasets/SEA-AI/seashapes/resolve/<commit>/<id>/<id>.glb
|
|
| 58 |
|
| 59 |
## Adding an asset
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
## Licences
|
| 65 |
|
|
|
|
| 58 |
|
| 59 |
## Adding an asset
|
| 60 |
|
| 61 |
+
No clone needed: fetch the small files, add the asset, upload only what changed.
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
hf download SEA-AI/seashapes --repo-type dataset --local-dir seashapes --exclude "*.glb" --exclude "*.png"
|
| 65 |
+
cd seashapes
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
1. Put the model at `<id>/<id>.glb` and append its row to `metadata.jsonl`. Leave `sha256` and `size_bytes` out; the check fills them in.
|
| 69 |
+
2. Render its preview: `uv run tools/preview.py <id>`.
|
| 70 |
+
3. Check every row: `uv run tools/validate.py`. Rows whose folder is here are checked against their files, the rest against `schema.json`.
|
| 71 |
+
4. Upload in one commit:
|
| 72 |
+
|
| 73 |
+
```bash
|
| 74 |
+
hf upload SEA-AI/seashapes . . --repo-type dataset --include "<id>/*" --include metadata.jsonl --commit-message "Add <id>"
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
Pin the new commit, never `main`, wherever the asset is used.
|
| 78 |
|
| 79 |
## Licences
|
| 80 |
|
tools/validate.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SPDX-License-Identifier: Apache-2.0
|
| 2 |
+
# /// script
|
| 3 |
+
# requires-python = ">=3.11"
|
| 4 |
+
# dependencies = ["jsonschema"]
|
| 5 |
+
# ///
|
| 6 |
+
"""Check every row of metadata.jsonl, filling in each file's size and SHA-256.
|
| 7 |
+
|
| 8 |
+
uv run tools/validate.py
|
| 9 |
+
|
| 10 |
+
Run from the dataset root before an upload. A row whose folder is here is checked
|
| 11 |
+
against its files; one whose folder was not downloaded, against the schema only.
|
| 12 |
+
Exits non-zero, naming the rows that fail.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import hashlib
|
| 16 |
+
import json
|
| 17 |
+
import sys
|
| 18 |
+
from pathlib import Path
|
| 19 |
+
|
| 20 |
+
from jsonschema import Draft202012Validator
|
| 21 |
+
|
| 22 |
+
META = Path("metadata.jsonl")
|
| 23 |
+
schema = Draft202012Validator(json.loads(Path("schema.json").read_text()))
|
| 24 |
+
rows = [json.loads(line) for line in META.read_text().splitlines() if line.strip()]
|
| 25 |
+
errors = []
|
| 26 |
+
local = 0
|
| 27 |
+
for row in rows:
|
| 28 |
+
asset = row.get("id", "?")
|
| 29 |
+
model, preview = Path(row.get("file", "")), Path(row.get("file_name", ""))
|
| 30 |
+
if model.parts[:1] != (asset,) or preview.parts[:1] != (asset,):
|
| 31 |
+
errors.append(f"{asset}: file and file_name must sit in the folder {asset}/")
|
| 32 |
+
if not Path(asset).is_dir():
|
| 33 |
+
errors += [f"{asset}: {e.message}" for e in schema.iter_errors(row)]
|
| 34 |
+
continue
|
| 35 |
+
local += 1
|
| 36 |
+
if model.is_file():
|
| 37 |
+
with model.open("rb") as f:
|
| 38 |
+
row["sha256"] = hashlib.file_digest(f, "sha256").hexdigest()
|
| 39 |
+
row["size_bytes"] = model.stat().st_size
|
| 40 |
+
else:
|
| 41 |
+
errors.append(f"{asset}: no file {model}")
|
| 42 |
+
if not preview.is_file():
|
| 43 |
+
errors.append(f"{asset}: no preview {preview}; run tools/preview.py {asset}")
|
| 44 |
+
errors += [f"{asset}: {e.message}" for e in schema.iter_errors(row)]
|
| 45 |
+
ids = [row.get("id") for row in rows]
|
| 46 |
+
errors += [f"{i}: id used twice" for i in sorted({i for i in ids if ids.count(i) > 1})]
|
| 47 |
+
|
| 48 |
+
META.write_text("".join(json.dumps(row) + "\n" for row in rows))
|
| 49 |
+
print(
|
| 50 |
+
"\n".join(errors) or f"{len(rows)} rows valid, {local} checked against their files"
|
| 51 |
+
)
|
| 52 |
+
sys.exit(bool(errors))
|