File size: 3,896 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
"""Download the Lilambd dataset files the question generators read, pinned to the

current commit of each repo. Writes <mirror>/<name>/... plus _revision.json and

roots.json (input for build.py / verify.py).



usage: python fetch_hf.py --mirror MIRROR_DIR [--only name,name]

needs: pip install huggingface_hub

"""
import argparse, fnmatch, json, os, re
from huggingface_hub import HfApi, hf_hub_download

# name -> list of glob patterns; {LATEST} = newest dated file/folder for that pattern
WANT = {
    'world-signals': ['data/{LATEST}/steam_top.csv', 'data/{LATEST}/github_new_repos.csv', 'data/{LATEST}/hf_trending.csv',
                      'data/{LATEST}/package_downloads.csv', 'data/{LATEST}/podcast_charts.csv', 'data/{LATEST}/search_trends.csv'],
    'world-seeds': ['index.csv', 'tables/*.csv'],
    'japan-dpc-hospitals': ['hospitals.csv', 'top100_mdc.csv'],
    'japan-prefecture-facts': ['facts.csv'],
    'world-biodiversity-cells': ['cells.parquet', 'world_totals.csv'],
    'machine-economy-daily': ['x402/hosts/{LATEST}.csv', 'npm_top/{LATEST}.csv', 'hf_trending/{LATEST}.csv', 'mcp/new/{LATEST}.csv'],
    'security-daily': ['daily/cve_new/{LATEST}.csv', 'daily/patch_first/{LATEST}.csv', 'daily/summary/{LATEST}.csv', 'snapshot/breaches.csv',
                       'snapshot/cve_signals/published_2025.csv', 'snapshot/cve_signals/published_2026.csv', 'weekly/vendor/{LATEST}.csv'],
    'world-pulse-daily': [f'{t}/{{LATEST}}.csv' for t in ['fx_ecb', 'arxiv', 'stablecoins', 'hackernews_front', 'wikipedia_size', 'docker_official_images',
                                                         'stackoverflow_tags', 'ai_sdk_downloads', 'crossref', 'zenodo', 'hf_daily_papers', 'github_new_repos',
                                                         'crates_io', 'wikipedia_top', 'appstore_charts']],
    'world-markets': ['markets.parquet'],
    # these six are also built from local copies; fetch them to rebuild everything from HF
    'world-hazard': ['data/*.csv'], 'world-earth': ['data/*.csv'], 'world-ground': ['data/*.csv'], 'world-prices': ['data/*.csv'],
    'world-science': ['*.csv'], 'world-marketplaces': ['digital.csv', 'offline.parquet'],
}
DATE = re.compile(r'\d{4}-\d{2}-\d{2}')


def resolve(files, pat):
    if '{LATEST}' not in pat:
        return [f for f in files if fnmatch.fnmatch(f, pat)]
    rx = re.compile('^' + re.escape(pat).replace(re.escape('{LATEST}'), r'(\d{4}-\d{2}-\d{2})') + '$')
    hits = sorted((m.group(1), f) for f in files for m in [rx.match(f)] if m)
    return [hits[-1][1]] if hits else []


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--mirror', required=True)
    ap.add_argument('--only', default='')
    a = ap.parse_args()
    only = [x for x in a.only.split(',') if x]
    api = HfApi()
    roots = {}
    for name, pats in WANT.items():
        if only and name not in only:
            continue
        repo = f'Lilambd/{name}'
        info = api.dataset_info(repo)
        files = [s.rfilename for s in info.siblings]
        got = []
        dest = os.path.join(a.mirror, name)
        for p in pats:
            for f in resolve(files, p):
                hf_hub_download(repo, f, repo_type='dataset', revision=info.sha, local_dir=dest)
                got.append(f)
        json.dump({'repo': repo, 'revision': f'{info.sha[:12]} ({str(info.last_modified)[:10]})', 'sha': info.sha, 'files': got},
                  open(os.path.join(dest, '_revision.json'), 'w'), indent=1)
        roots[name] = os.path.abspath(dest)
        print(f'{name}: {len(got)} files @ {info.sha[:12]}')
    rp = os.path.join(a.mirror, 'roots.json')
    old = json.load(open(rp)) if os.path.exists(rp) else {}
    old.update(roots)
    json.dump(old, open(rp, 'w'), indent=1)
    print('roots ->', rp)


if __name__ == '__main__':
    main()