trohrbaugh commited on
Commit
6b60a0a
Β·
verified Β·
1 Parent(s): 08b464e

Wave 2: verified-lab recognition + badge + lineage graph

Browse files
Files changed (2) hide show
  1. app.py +86 -18
  2. scan.py +38 -9
app.py CHANGED
@@ -111,7 +111,7 @@ def format_verdict(result: dict) -> tuple[str, str, str]:
111
  flags = v.get("flags", [])
112
 
113
  # Header
114
- confidence_emoji = {"HIGH": "βœ…", "MODERATE": "⚠️", "ORIGINAL": "🧬", "NONE": "❓"}.get(v.get("confidence",""), "❓")
115
  header = f"{confidence_emoji} **{v.get('architecture', 'Unknown')}**"
116
  # DNA-001: provenance risk score gauge
117
  if "risk_score" in v:
@@ -138,12 +138,92 @@ def format_verdict(result: dict) -> tuple[str, str, str]:
138
  for s in e["modelatlas_similar"][:3]:
139
  details += f"- `{s['model_id']}`\n"
140
 
 
 
 
141
  # Flags β€” lead with the DNA-003 engine panel, then the detail
142
  flag_text = _fmt_engine_panel(v.get("engines", [])) + _fmt_flags(flags)
143
 
144
  return header, details, flag_text
145
 
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  def _fmt_engine_panel(engines: list) -> str:
148
  if not engines:
149
  return ""
@@ -228,25 +308,13 @@ def run_scan(model_id: str):
228
  src_btn = _gr.update(visible=bool(src), value=f"πŸ”¬ Scan source: {src}") if src else hide_btn
229
  return h, d, fl, disc, _gr.update(visible=True), src_btn, src, model_id
230
 
231
- # Find unattributed derivatives
232
- base = result.get("verdict", {}).get("base_model_confirmed", "")
233
- derivatives = find_unattributed_derivatives(base, model_id) if base else []
234
-
235
  header, details, flags = format_verdict(result)
236
 
237
- # Derivative discovery section
238
- discovery = ""
239
- if derivatives:
240
- discovery = f"### πŸ” {len(derivatives)} Related Models Found Without Attribution\n\n"
241
- discovery += "These models share the same architecture base but don't declare it:\n\n"
242
- for d in derivatives:
243
- discovery += f"- `{d['model_id']}` ({d.get('downloads',0):,} downloads)\n"
244
- else:
245
- discovery = (
246
- "### πŸ” Derivative Discovery\n\n"
247
- "This scan has been stored. As similar models are scanned, "
248
- "derivatives that don't properly attribute their source will appear here."
249
- )
250
 
251
  return header, details, flags, discovery, hide_row, hide_btn, "", ""
252
 
 
111
  flags = v.get("flags", [])
112
 
113
  # Header
114
+ confidence_emoji = {"HIGH": "βœ…", "MODERATE": "⚠️", "ORIGINAL": "🧬", "NEW": "πŸ†•", "NONE": "❓"}.get(v.get("confidence",""), "❓")
115
  header = f"{confidence_emoji} **{v.get('architecture', 'Unknown')}**"
116
  # DNA-001: provenance risk score gauge
117
  if "risk_score" in v:
 
138
  for s in e["modelatlas_similar"][:3]:
139
  details += f"- `{s['model_id']}`\n"
140
 
141
+ # DNA-004: shareable badge + model-card snippet
142
+ details += "\n\n---\n\n" + build_badge(result.get("model_id", ""), v)
143
+
144
  # Flags β€” lead with the DNA-003 engine panel, then the detail
145
  flag_text = _fmt_engine_panel(v.get("engines", [])) + _fmt_flags(flags)
146
 
147
  return header, details, flag_text
148
 
149
 
150
+ def build_badge(model_id: str, v: dict) -> tuple:
151
+ """DNA-004: zero-backend shields.io badge + copyable model-card snippet."""
152
+ from urllib.parse import quote
153
+ score = v.get("risk_score"); band = v.get("risk_band", "")
154
+ color = {"CLEAN": "brightgreen", "LOW": "yellowgreen", "MODERATE": "orange",
155
+ "HIGH": "red"}.get(band, "blue")
156
+ if score is None:
157
+ msg = "scanned"
158
+ elif band == "CLEAN":
159
+ msg = "clean"
160
+ else:
161
+ msg = f"{band.lower()} risk {score} of 100"
162
+ badge = f"https://img.shields.io/badge/{quote('ModelDNA')}-{quote(msg)}-{color}"
163
+ space = "https://huggingface.co/spaces/RadicalNotionAI/modeldna"
164
+ md = f"[![ModelDNA provenance]({badge})]({space})"
165
+ block = ("### πŸ”— Share / embed this result\n\n"
166
+ f"![badge]({badge})\n\n"
167
+ "Add to your model card (Markdown):\n\n"
168
+ f"```\n{md}\n```")
169
+ return block
170
+
171
+
172
+ _GENERIC_TOKENS = {"qwen", "qwen2", "qwen3", "qwen35", "llama", "gemma", "mistral",
173
+ "phi", "deepseek", "model", "base", "instruct"}
174
+
175
+ def build_lineage(model_id: str, result: dict) -> str:
176
+ """DNA-002 v1: lineage view β€” where it came from + the derivative/quant family it spawned.
177
+ Uses the HF API (reachable from the Space) to find same-family models by name."""
178
+ import os, re
179
+ from huggingface_hub import HfApi
180
+ v = result.get("verdict", {})
181
+ org, short = (model_id.split("/", 1) + [""])[:2] if "/" in model_id else ("", model_id)
182
+ came_from = result.get("resolved_source") or (v.get("base_model_confirmed") or "").strip()
183
+
184
+ # distinctive search token = first name segment, unless it's a generic base word
185
+ token = re.split(r"[-_.]", short)[0]
186
+ if len(token) < 4 or token.lower() in _GENERIC_TOKENS:
187
+ token = short
188
+ api = HfApi(token=os.environ.get("HF_TOKEN"))
189
+ try:
190
+ found = list(api.list_models(search=token, limit=60))
191
+ except Exception:
192
+ found = []
193
+ groups = {"GGUF": [], "MLX": [], "AWQ/GPTQ": [], "other quant": [], "fine-tune / variant": []}
194
+ for m in found:
195
+ if m.id.lower() == model_id.lower():
196
+ continue
197
+ low = m.id.lower()
198
+ dl = getattr(m, "downloads", 0) or 0
199
+ if "gguf" in low: g = "GGUF"
200
+ elif "mlx" in low: g = "MLX"
201
+ elif "awq" in low or "gptq" in low: g = "AWQ/GPTQ"
202
+ elif any(q in low for q in ("nvfp4", "fp8", "int4", "int8", "-4bit", "-8bit", "exl2")): g = "other quant"
203
+ else: g = "fine-tune / variant"
204
+ groups[g].append((m.id, dl))
205
+ total = sum(len(x) for x in groups.values())
206
+ if total == 0 and not came_from:
207
+ return ("### 🌳 Lineage & Derivatives\n\nNo related models found yet. "
208
+ "As similar models are scanned, the family tree grows here.")
209
+ out = "### 🌳 Lineage & Derivatives\n\n"
210
+ if came_from and came_from not in ("Unknown", ""):
211
+ out += f"**⬆ Descends from:** `{came_from}`\n\n"
212
+ out += f"**⬇ Derivative family** (`{token}` β€” {total} related models found on HF):\n\n"
213
+ for g, items in groups.items():
214
+ if not items:
215
+ continue
216
+ items.sort(key=lambda x: -x[1])
217
+ out += f"- **{g}** ({len(items)}): "
218
+ out += ", ".join(f"`{i}` ({d:,})" for i, d in items[:4])
219
+ if len(items) > 4:
220
+ out += f", +{len(items)-4} more"
221
+ out += "\n"
222
+ out += ("\n*Downloads in parentheses. Quants inherit the source's provenance β€” a scrub or "
223
+ "misattribution in the parent travels to every derivative here.*")
224
+ return out
225
+
226
+
227
  def _fmt_engine_panel(engines: list) -> str:
228
  if not engines:
229
  return ""
 
308
  src_btn = _gr.update(visible=bool(src), value=f"πŸ”¬ Scan source: {src}") if src else hide_btn
309
  return h, d, fl, disc, _gr.update(visible=True), src_btn, src, model_id
310
 
 
 
 
 
311
  header, details, flags = format_verdict(result)
312
 
313
+ # DNA-002: lineage & derivative family (HF-API-backed, works on the Space)
314
+ try:
315
+ discovery = build_lineage(model_id, result)
316
+ except Exception:
317
+ discovery = "### 🌳 Lineage & Derivatives\n\n(temporarily unavailable)"
 
 
 
 
 
 
 
 
318
 
319
  return header, details, flags, discovery, hide_row, hide_btn, "", ""
320
 
scan.py CHANGED
@@ -535,16 +535,20 @@ def _risk_score(flags: list) -> tuple:
535
  return score, band
536
 
537
 
538
- def _engine_panel(flags: list, arch_known: bool, chat_template_seen: bool) -> list:
539
- """DNA-003: group detectors into named 'engines' with per-engine verdicts (VirusTotal-style)."""
 
540
  by_type = {f.get("type") for f in flags}
541
  def verdict(triggered, ran=True):
542
  return "πŸ”΄ flagged" if triggered else ("βœ… pass" if ran else "βž– not run")
 
 
 
 
 
 
543
  return [
544
- {"engine": "Architecture Match",
545
- "status": "βœ… pass" if arch_known else "❓ unrecognized",
546
- "detail": "base architecture identified against ModelAtlas reference" if arch_known
547
- else "no known base match"},
548
  {"engine": "Provenance Integrity",
549
  "status": verdict({"PROVENANCE_SCRUBBING", "IDENTITY_OVERRIDE"} & by_type, chat_template_seen),
550
  "detail": "chat-template lineage scrub / identity override"},
@@ -697,6 +701,17 @@ def stage1_screen(model_id: str, config: dict) -> dict:
697
  except Exception:
698
  pass
699
 
 
 
 
 
 
 
 
 
 
 
 
700
  # Query ModelAtlas reference parquet for architecturally similar models
701
  db_matches = []
702
  try:
@@ -750,6 +765,7 @@ def stage1_screen(model_id: str, config: dict) -> dict:
750
  "base_matches": sorted(base_matches, key=lambda x: -x["score"]),
751
  "modelatlas_similar": db_matches,
752
  "self_catalogued": self_catalogued,
 
753
  }
754
 
755
 
@@ -768,6 +784,7 @@ def generate_verdict(
768
 
769
  # Headline verdict
770
  self_cat = stage1.get("self_catalogued")
 
771
  if base_matches:
772
  top = base_matches[0]
773
  if top["confidence"] == "HIGH":
@@ -784,6 +801,13 @@ def generate_verdict(
784
  + ", catalogued in ModelAtlas (not a derivative of a tracked base model)"
785
  )
786
  headline_confidence = "ORIGINAL"
 
 
 
 
 
 
 
787
  else:
788
  architecture_verdict = "UNRECOGNIZED β€” architecture does not match any known base model"
789
  headline_confidence = "NONE"
@@ -851,12 +875,17 @@ def generate_verdict(
851
  "architecture": architecture_verdict,
852
  "base_model_confirmed": (
853
  base_matches[0]["name"] if base_matches
854
- else (f"{self_cat.get('model_type')} (original architecture)" if self_cat else "Unknown")
 
855
  ),
856
  "confidence": headline_confidence,
857
  "risk_score": _risk_score(flags)[0],
858
  "risk_band": _risk_score(flags)[1],
859
- "engines": _engine_panel(flags, bool(base_matches or self_cat), bool(chat_template)),
 
 
 
 
860
  "flags": flags,
861
  "flag_count": len(flags),
862
  "stage": "Stage 1 (config-only β€” no weight download)",
@@ -934,7 +963,7 @@ def inspect_gguf(model_id: str) -> dict:
934
  else "UNRECOGNIZED",
935
  "risk_score": _risk_score(flags)[0],
936
  "risk_band": _risk_score(flags)[1],
937
- "engines": _engine_panel(flags, bool(arch_raw), bool(chat_template)),
938
  "flags": flags, "flag_count": len(flags),
939
  "stage": "Stage 1 (GGUF metadata β€” header only, no download)",
940
  },
 
535
  return score, band
536
 
537
 
538
+ def _engine_panel(flags: list, arch_state: str, chat_template_seen: bool) -> list:
539
+ """DNA-003: group detectors into named 'engines' with per-engine verdicts (VirusTotal-style).
540
+ arch_state: 'confirmed' | 'original' | 'new' | 'unknown'."""
541
  by_type = {f.get("type") for f in flags}
542
  def verdict(triggered, ran=True):
543
  return "πŸ”΄ flagged" if triggered else ("βœ… pass" if ran else "βž– not run")
544
+ arch_row = {
545
+ "confirmed": ("βœ… pass", "base architecture identified against ModelAtlas reference"),
546
+ "original": ("βœ… pass", "recognized original architecture, catalogued in ModelAtlas"),
547
+ "new": ("πŸ†• new", "novel architecture from a known lab; not yet catalogued"),
548
+ "unknown": ("❓ unrecognized", "no known base match"),
549
+ }.get(arch_state, ("❓ unrecognized", "no known base match"))
550
  return [
551
+ {"engine": "Architecture Match", "status": arch_row[0], "detail": arch_row[1]},
 
 
 
552
  {"engine": "Provenance Integrity",
553
  "status": verdict({"PROVENANCE_SCRUBBING", "IDENTITY_OVERRIDE"} & by_type, chat_template_seen),
554
  "detail": "chat-template lineage scrub / identity override"},
 
701
  except Exception:
702
  pass
703
 
704
+ # DNA-016: is the publisher a known lab? (β‰₯3 other models catalogued in the reference)
705
+ lab_catalogued = None
706
+ try:
707
+ if ref is not None and "/" in model_id:
708
+ org = model_id.split("/")[0]
709
+ n = int(ref["model_id"].str.lower().str.startswith(org.lower() + "/").sum())
710
+ if n >= 3:
711
+ lab_catalogued = {"org": org, "model_count": n}
712
+ except Exception:
713
+ pass
714
+
715
  # Query ModelAtlas reference parquet for architecturally similar models
716
  db_matches = []
717
  try:
 
765
  "base_matches": sorted(base_matches, key=lambda x: -x["score"]),
766
  "modelatlas_similar": db_matches,
767
  "self_catalogued": self_catalogued,
768
+ "lab_catalogued": lab_catalogued,
769
  }
770
 
771
 
 
784
 
785
  # Headline verdict
786
  self_cat = stage1.get("self_catalogued")
787
+ lab_cat = stage1.get("lab_catalogued")
788
  if base_matches:
789
  top = base_matches[0]
790
  if top["confidence"] == "HIGH":
 
801
  + ", catalogued in ModelAtlas (not a derivative of a tracked base model)"
802
  )
803
  headline_confidence = "ORIGINAL"
804
+ elif lab_cat and not suspicious:
805
+ # not a derivative, no foreign-brand claim, published by a known lab -> a new original
806
+ architecture_verdict = (
807
+ f"NEW β€” original architecture from {lab_cat['org']} (known lab, "
808
+ f"{lab_cat['model_count']} catalogued models); not yet in the reference"
809
+ )
810
+ headline_confidence = "NEW"
811
  else:
812
  architecture_verdict = "UNRECOGNIZED β€” architecture does not match any known base model"
813
  headline_confidence = "NONE"
 
875
  "architecture": architecture_verdict,
876
  "base_model_confirmed": (
877
  base_matches[0]["name"] if base_matches
878
+ else (f"{self_cat.get('model_type')} (original architecture)" if self_cat
879
+ else (f"original / new (from {lab_cat['org']})" if lab_cat else "Unknown"))
880
  ),
881
  "confidence": headline_confidence,
882
  "risk_score": _risk_score(flags)[0],
883
  "risk_band": _risk_score(flags)[1],
884
+ "engines": _engine_panel(
885
+ flags,
886
+ ("confirmed" if base_matches else "original" if self_cat
887
+ else "new" if (lab_cat and not suspicious) else "unknown"),
888
+ bool(chat_template)),
889
  "flags": flags,
890
  "flag_count": len(flags),
891
  "stage": "Stage 1 (config-only β€” no weight download)",
 
963
  else "UNRECOGNIZED",
964
  "risk_score": _risk_score(flags)[0],
965
  "risk_band": _risk_score(flags)[1],
966
+ "engines": _engine_panel(flags, "confirmed" if arch_raw else "unknown", bool(chat_template)),
967
  "flags": flags, "flag_count": len(flags),
968
  "stage": "Stage 1 (GGUF metadata β€” header only, no download)",
969
  },