File size: 2,598 Bytes
4f24d15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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>

Writes ``output/<pipeline>/<category>/<stem>.result.json`` and a minimal
``_metadata.json``. 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, group_dir_for, metadata_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 = os.path.join(args.output_dir, group_dir_for(args.benchmark, category))
        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("no <category>.jsonl files found in %s" % args.results_dir, file=sys.stderr)
        return 1
    with open(os.path.join(args.output_dir, "_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("wrote %s" % args.output_dir)
    return 0


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