OpenFrontiers / load_jsonl.py
FlippyDora's picture
OpenFrontiers corpus (2026-10-10 snapshot, 12,151 problems)
4081ea0 verified
Raw History Blame Contribute Delete
1.13 kB
"""Load JSONL into Hugging Face without automatic date-string conversion."""
import json
from hashlib import sha256
from pathlib import Path
from datasets import Dataset, Features
def iter_jsonl(path):
with open(path, encoding="utf-8") as source:
for line in source:
if line.strip():
yield json.loads(line)
def load_jsonl(path, features_path=None, cache_dir=None):
path = Path(path).resolve()
features_path = Path(features_path) if features_path else path.parent.parent / "features.json"
feature_text = features_path.read_text(encoding="utf-8")
features = Features.from_dict(json.loads(feature_text))
# A filename alone does not invalidate HF's generator cache after a rebuild.
fingerprint = sha256(feature_text.encode("utf-8"))
with path.open("rb") as source:
for chunk in iter(lambda: source.read(1024 * 1024), b""):
fingerprint.update(chunk)
return Dataset.from_generator(iter_jsonl, gen_kwargs={"path": str(path)}, features=features,
cache_dir=cache_dir, fingerprint=fingerprint.hexdigest())