File size: 2,964 Bytes
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
77
78
79
80
81
82
83
"""Select candidates worth probing, probe them (cached by repo sha)."""
import json
import re
from concurrent.futures import ThreadPoolExecutor

from . import enrich, store

NLP_TAGS = {"text-classification", "token-classification", "fill-mask", "question-answering",
            "sentence-similarity", "feature-extraction", "text-ranking", "robotics",
            "reinforcement-learning", "automatic-speech-recognition", "translation", "summarization"}


def worth_probing(r, fam):
    """Cheap pre-filter: skip name-search hits whose only link to the family is a substring
    of another word (MiniLM-L12-H384, H3K27ac ...)."""
    if r["kind"] != "model":
        return False
    if any(s.split(":")[0] in ("tree", "tag", "fulltext") for s in r["sources"]):
        return True
    if fam.name_hint and fam.name_hint.search(r["repo_id"].split("/")[1]):
        return r["pipeline_tag"] not in NLP_TAGS
    return bool(fam.family_text.search(r["repo_id"]))


def cache_path(fam):
    return store.LOCAL / "cache" / fam.key / "enrich.jsonl"


def load_cache(fam):
    out = {}
    p = cache_path(fam)
    if p.exists():
        for line in p.open():
            e = json.loads(line)
            out[e["repo_id"]] = e
    return out


def enrich_all(raw_today, fam, workers=8, log=print):
    cache = load_cache(fam)
    rows = [r for r in raw_today.to_dict("records") if worth_probing(r, fam)]
    todo = [r["repo_id"] for r in rows if cache.get(r["repo_id"], {}).get("sha") != r["sha"]]
    log(f"probe: {len(rows)} worth probing, {len(todo)} new or changed")

    def one(rid):
        try:
            return enrich.enrich_model(rid, fam)
        except Exception as e:
            return {"repo_id": rid, "sha": None, "errors": [f"fatal: {type(e).__name__}: {e}"]}
    done = 0
    with ThreadPoolExecutor(workers) as ex:
        for ev in ex.map(one, todo):
            cache[ev["repo_id"]] = ev
            done += 1
            if done % 100 == 0:
                log(f"probe: {done}/{len(todo)}")
    p = cache_path(fam)
    p.parent.mkdir(parents=True, exist_ok=True)
    with p.open("w") as f:
        for e in cache.values():
            f.write(json.dumps(e, default=str) + "\n")
    return cache, p


def reprobe(raw_today, fam, cache, predicate, workers=8, log=print):
    """Probe again the cached repos matching predicate(ev) (after an enrich.py upgrade)."""
    todo = [rid for rid, ev in cache.items() if predicate(ev)]
    log(f"re-probe: {len(todo)} repos")

    def one(rid):
        try:
            return enrich.enrich_model(rid, fam)
        except Exception as e:
            return {"repo_id": rid, "sha": None, "errors": [f"fatal: {type(e).__name__}: {e}"]}
    with ThreadPoolExecutor(workers) as ex:
        for ev in ex.map(one, todo):
            cache[ev["repo_id"]] = ev
    p = cache_path(fam)
    with p.open("w") as f:
        for e in cache.values():
            f.write(json.dumps(e, default=str) + "\n")
    return cache