File size: 1,424 Bytes
331b5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
"""Lossless decoding of the standard-Parquet FindStatBench distribution."""
import json
from pathlib import Path

JSON_FIELDS = {"prompts": (), "tasks": ("metadata", "public_examples"),
               "hidden": ("evaluation_input", "gold_output_object", "metadata")}


def encode_record(component, record):
    if component not in JSON_FIELDS:
        raise ValueError("Unknown component")
    output = dict(record)
    for name in JSON_FIELDS[component]:
        value = output.pop(name)
        output[name + "_json"] = None if value is None else json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False)
    return output


def decode_record(component, record):
    if component not in JSON_FIELDS:
        raise ValueError("Unknown component")
    output = dict(record)
    for name in JSON_FIELDS[component]:
        value = output.pop(name + "_json")
        output[name] = None if value is None else json.loads(value)
    return output


def load_local(folder, component="prompts", streaming=True):
    from datasets import load_dataset
    if component not in JSON_FIELDS:
        raise ValueError("Unknown component")
    return load_dataset(str(Path(folder).resolve()), name=component, split="test", streaming=streaming, token=False)


def iter_decoded(folder, component="hidden"):
    for record in load_local(folder, component):
        yield decode_record(component, record)