"""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