File size: 3,088 Bytes
e593510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""Turn published results JSONL back into the per-document layout the public
evaluators read, so ``parse-bench run <pipeline> --skip_inference`` or
``extract-bench run <pipeline> --skip_inference`` scores them without any
inference.

    python tools/explode.py --benchmark parsebench results/<pipeline> output/<pipeline>
    python tools/explode.py --benchmark parsebench-2 results/<pipeline> output/parsebench-2

Writes ``output/<pipeline>/<category>/<stem>.result.json`` and a minimal
``_metadata.json`` (for ParseBench-2, ``output/parsebench-2/<subset>/<pipeline>/<stem>.result.json``,
which ``parse-bench run <pipeline> --dataset parsebench-2 --skip_inference`` reads). Python 3.9+, standard library only.
"""

import argparse
import json
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from hfresults_common import BENCHMARKS, metadata_for, output_dir_for, read_jsonl, record_stem  # noqa: E402


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--benchmark", required=True, choices=sorted(BENCHMARKS))
    parser.add_argument("results_dir", help="results/<pipeline> folder holding <category>.jsonl files")
    parser.add_argument("output_dir", help="output/<pipeline> folder to create")
    parser.add_argument("--provider", default=None, help="provider name for _metadata.json (default: pipeline)")
    args = parser.parse_args()

    spec = BENCHMARKS[args.benchmark]
    pipeline = os.path.basename(os.path.normpath(args.results_dir))
    os.makedirs(args.output_dir, exist_ok=True)
    written = {}
    for category in spec["categories"]:
        path = os.path.join(args.results_dir, category + ".jsonl")
        if not os.path.exists(path):
            continue
        group_dir = output_dir_for(args.benchmark, args.output_dir, category, pipeline)
        os.makedirs(group_dir, exist_ok=True)
        count = 0
        for _number, _line, record in read_jsonl(path):
            _group, stem = record_stem(record)
            target = os.path.join(group_dir, stem + ".result.json")
            with open(target, "w", encoding="utf-8") as fh:
                json.dump(record, fh, ensure_ascii=False)
            count += 1
        written[category] = count
    if not written:
        print(f"no <category>.jsonl files found in {args.results_dir}", file=sys.stderr)
        return 1
    metadata_dirs = [args.output_dir]
    if spec.get("output_layout") == "category/pipeline":
        metadata_dirs = [output_dir_for(args.benchmark, args.output_dir, c, pipeline) for c in written]
    for directory in metadata_dirs:
        with open(os.path.join(directory, "_metadata.json"), "w", encoding="utf-8") as fh:
            json.dump(metadata_for(pipeline, args.provider, spec["product_type"]), fh, indent=2)
    for category, count in written.items():
        print("%s: %d documents" % (category, count))
    print(f"wrote {args.output_dir}")
    return 0


if __name__ == "__main__":
    sys.exit(main())