File size: 1,127 Bytes
4081ea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""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())