File size: 3,480 Bytes
7f9a652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Build Lilambd Empire Eval question sets.



usage: python build.py --data-root ROOTS.json --out OUT_DIR [--only name,name]

ROOTS.json maps dataset short name -> local folder holding that dataset's files

(same relative paths as on the Hugging Face repo).

"""
import argparse, collections, importlib, json, os, sys, time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from evallib import Builder, Store, sha256, write_jsonl

MODULES = {
    'world-hazard': 'ds_hazard',
    'world-earth': 'ds_earth',
    'world-ground': 'ds_ground',
    'world-prices': 'ds_prices',
    'world-science': 'ds_science',
    'world-marketplaces': 'ds_marketplaces',
    'world-sound': 'ds_sound',
    'world-signals': 'ds_signals',
    'world-seeds': 'ds_rest:seeds',
    'japan-dpc-hospitals': 'ds_rest:dpc',
    'japan-prefecture-facts': 'ds_rest:prefecture',
    'world-biodiversity-cells': 'ds_rest:biodiversity',
    'machine-economy-daily': 'ds_rest:machine_economy',
    'security-daily': 'ds_security',
    'world-pulse-daily': 'ds_pulse',
    'world-markets': 'ds_rest:markets',
}


def load_module(name):
    spec = MODULES.get(name) or ('ds_worlds' if name.startswith('worlds-') else None)
    mod, _, attr = spec.partition(':')
    m = importlib.import_module(mod)
    return getattr(m, attr) if attr else m


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--data-root', required=True)
    ap.add_argument('--out', required=True)
    ap.add_argument('--only', default='')
    a = ap.parse_args()
    roots = json.load(open(a.data_root))
    only = [x for x in a.only.split(',') if x]
    os.makedirs(os.path.join(a.out, 'data'), exist_ok=True)
    manifest_path = os.path.join(a.out, 'manifest.json')
    manifest = json.load(open(manifest_path)) if os.path.exists(manifest_path) else {'datasets': {}}
    for name, root in roots.items():
        if only and name not in only:
            continue
        if name not in MODULES and not name.startswith('worlds-'):
            print('skip (no module)', name)
            continue
        t0 = time.time()
        m = load_module(name)
        if hasattr(m, 'setup'):
            m.setup(root)
        store = Store(root, getattr(m, 'STR', {}))
        rp = os.path.join(root, '_revision.json')
        rev = json.load(open(rp)).get('revision') if os.path.exists(rp) else getattr(m, 'REVISION', None)
        B = Builder(m.DS, store, m.KEYS, rev)
        m.build(B)
        if hasattr(m, 'import_original'):
            m.import_original(B, root)
        files = {}
        for f in sorted(store.files()):
            p = os.path.join(root, f)
            files[f] = {'sha256': sha256(p), 'bytes': os.path.getsize(p), 'rows': int(len(store[f]))}
        for it in B.items:
            it['revision'] = rev
        write_jsonl(B.items, os.path.join(a.out, 'data', f'{name}.jsonl'))
        c = collections.Counter(it['task'] for it in B.items)
        manifest['datasets'][name] = {'hf_repo': m.DS, 'revision': rev, 'n_questions': len(B.items), 'tasks': dict(c), 'files': files,
                                      'license': getattr(m, 'LICENSE', None)}
        print(f'{name}: {len(B.items)} q {dict(c)} in {time.time() - t0:.1f}s; skipped {sum(B.fail.values())} {dict(list(B.fail.items())[:6])}')
    json.dump(manifest, open(manifest_path, 'w'), indent=1, ensure_ascii=False)


if __name__ == '__main__':
    main()