File size: 3,149 Bytes
fb6b9a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226b7bf
fb6b9a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226b7bf
 
 
fb6b9a8
 
 
 
226b7bf
fb6b9a8
 
 
 
 
 
 
 
 
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
"""Daily job: snapshot -> probe new/changed repos -> classify -> dashboard data -> push.

    python scripts/daily.py [--family h3 ...] [--push] [--skip-snapshot]

Runs the same way locally and on HF Jobs. With --push it first mirrors the dataset repo,
stores each family's raw snapshot before anything else (the one step that cannot be
redone later), then pushes labels, the probe cache and the dashboard data.
HF_TOKEN must be able to write the dataset repo and the Space repo.
"""
import argparse
import json
import sys
import time
from pathlib import Path

import pandas as pd

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from hfeco import authors, classify, discover, families, metrics, pipeline, store  # noqa: E402
from hfeco.hub import api  # noqa: E402

SPACE_REPO = "MiniMaxAI/oss-ecosystem"
ap = argparse.ArgumentParser()
ap.add_argument("--family", nargs="*", default=list(families.FAMILIES))
ap.add_argument("--push", action="store_true")
ap.add_argument("--skip-snapshot", action="store_true")
args = ap.parse_args()
t0 = time.time()


def log(m):
    print(f"[{time.time() - t0:6.0f}s] {m}", flush=True)


if args.push:
    log("pull dataset")
    store.pull()

for key in args.family:
    fam = families.get(key)
    log(f"== {fam.title}")
    if not args.skip_snapshot:
        raw_path = store.write_raw(discover.discover(fam, log=log), fam.key)
        if args.push:
            store.push([raw_path], f"{fam.key}: snapshot {raw_path.stem}")
    raw = store.read_raw(fam.key)
    today = raw[raw.day == raw.day.max()]
    cache, cache_path = pipeline.enrich_all(today, fam, log=log)
    labels = classify.classify_all(today, cache, fam)

    lab_path = store.LOCAL / "labels" / fam.key / "latest.parquet"
    lab_path.parent.mkdir(parents=True, exist_ok=True)
    ldf = pd.DataFrame(labels)
    for c in ("evidence", "flags"):
        ldf[c] = ldf[c].apply(lambda v: json.dumps(v, ensure_ascii=False))
    ldf.to_parquet(lab_path, index=False)

    fam_ids = [x["repo_id"] for x in labels if x.get("family") and x["kind"] == "model"]
    who, who_path = authors.update({i.split("/")[0] for i in fam_ids} | {b.split("/")[0] for b in fam.bases}, log=log)
    data = metrics.build(raw, labels, metrics.hf_tree_counts(today, fam), fam, who)
    data_path = store.LOCAL / "dashboard" / f"{fam.key}.json"
    metrics.dump(data, data_path)
    log(f"{fam.key}: {len(labels)} labels, {len(data['repos'])} repos on the dashboard")
    if args.push:
        store.push([lab_path, cache_path, who_path], f"{fam.key}: labels {data['today']}")
        api.upload_file(path_or_fileobj=str(data_path), path_in_repo=f"data/{fam.key}.json",
                        repo_id=SPACE_REPO, repo_type="space", commit_message=f"{fam.key}: data {data['today']}")

idx_path = store.LOCAL / "dashboard" / "index.json"
metrics.dump({"families": [{"key": f.key, "title": f.title} for f in families.FAMILIES.values()]}, idx_path)
if args.push:
    api.upload_file(path_or_fileobj=str(idx_path), path_in_repo="data/index.json", repo_id=SPACE_REPO,
                    repo_type="space", commit_message="index")
    log("pushed")