File size: 2,369 Bytes
d49ebd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Build the Hub's model family tree from the crawl.

nodes.csv.gz : one row per model (deduplicated by id)
edges.csv.gz : child -> parent, relation in {finetune, adapter, quantized, merge, unspecified}
Every edge comes from the model's own 'base_model:' tags, which the Hub derives from the
base_model field of the model card.
"""
import glob, gzip, json, sys
import pandas as pd

D = sys.argv[1] if len(sys.argv) > 1 else "data"
seen, nodes, edges = set(), [], []
for fn in sorted(glob.glob(f"{D}/hub/models_*.jsonl.gz")):
    with gzip.open(fn, "rt", encoding="utf-8") as f:
        for line in f:
            m = json.loads(line)
            mid = m.get("id")
            if not mid or mid in seen: continue
            seen.add(mid)
            tags = m.get("tags") or []
            lic = [t[8:] for t in tags if t.startswith("license:")]
            rel = {}
            for t in tags:
                if not t.startswith("base_model:"): continue
                parts = t.split(":", 2)
                if len(parts) == 3 and parts[1] in ("finetune", "adapter", "quantized", "merge"):
                    rel[parts[2]] = parts[1]
                elif len(parts) == 2:
                    rel.setdefault(parts[1], "unspecified")
            rel.pop(mid, None)  # self-references
            for p, r in rel.items(): edges.append((mid, p, r))
            region = [t[7:] for t in tags if t.startswith("region:")]
            nodes.append({"id": mid, "author": m.get("author") or mid.split("/")[0],
                          "created": m.get("createdAt"), "modified": m.get("lastModified"),
                          "downloads": m.get("downloads") or 0, "downloads_all": m.get("downloadsAllTime") or 0,
                          "likes": m.get("likes") or 0, "pipeline": m.get("pipeline_tag"),
                          "library": m.get("library_name"), "gated": str(m.get("gated")),
                          "license": lic[0] if lic else None, "n_licenses": len(lic),
                          "region": region[0] if region else None, "n_parents": len(rel)})
N = pd.DataFrame(nodes); E = pd.DataFrame(edges, columns=["child", "parent", "relation"])
N.to_csv(f"{D}/nodes.csv.gz", index=False); E.to_csv(f"{D}/edges.csv.gz", index=False)
print("models", len(N), "edges", len(E), "models with a parent", (N.n_parents > 0).sum())
print(E.relation.value_counts())