Spaces:
Sleeping
Sleeping
Stage 1: PROVENANCE_SCRUBBING + IMPOSSIBLE_WEIGHT_PROVENANCE + NAMING_MISATTRIBUTION detectors
Browse files
scan.py
CHANGED
|
@@ -307,6 +307,102 @@ def fetch_model_metadata(model_id: str) -> dict:
|
|
| 307 |
return {}
|
| 308 |
|
| 309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
def detect_claimed_base(model_id: str, config: dict, metadata: dict) -> dict:
|
| 311 |
"""Detect what base model a model claims to be derived from."""
|
| 312 |
claims = {}
|
|
@@ -474,6 +570,7 @@ def generate_verdict(
|
|
| 474 |
metadata: dict,
|
| 475 |
claims: dict,
|
| 476 |
stage1: dict,
|
|
|
|
| 477 |
) -> dict:
|
| 478 |
"""Synthesize all signals into a human-readable verdict."""
|
| 479 |
now = datetime.now(timezone.utc).isoformat()
|
|
@@ -492,38 +589,57 @@ def generate_verdict(
|
|
| 492 |
|
| 493 |
# Claim accuracy flags
|
| 494 |
flags = []
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
flags.append({
|
| 497 |
-
"type": "
|
| 498 |
-
"
|
|
|
|
| 499 |
"explanation": (
|
| 500 |
-
"
|
| 501 |
-
"
|
| 502 |
-
|
| 503 |
-
"
|
| 504 |
-
"
|
| 505 |
),
|
| 506 |
})
|
| 507 |
-
if "gpt" in suspicious or "openai" in suspicious or "chatgpt" in suspicious:
|
| 508 |
-
flags.append({
|
| 509 |
-
"type": "UNVERIFIABLE_CLAIM",
|
| 510 |
-
"term": "gpt/openai",
|
| 511 |
-
"explanation": "GPT-4/OpenAI weights are closed. Any weight transfer claim is false. Distillation via outputs is possible but unverifiable from architecture.",
|
| 512 |
-
})
|
| 513 |
-
if "gemini" in suspicious:
|
| 514 |
-
flags.append({
|
| 515 |
-
"type": "UNVERIFIABLE_CLAIM",
|
| 516 |
-
"term": "gemini",
|
| 517 |
-
"explanation": "Gemini weights are closed. Architecture shows no Gemini structure.",
|
| 518 |
-
})
|
| 519 |
|
| 520 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
name_implied = claims.get("name_implies", [])
|
| 522 |
if name_implied and base_matches:
|
| 523 |
top_base = base_matches[0]["base"]
|
| 524 |
if not any(n in top_base or top_base in n for n in name_implied):
|
| 525 |
flags.append({
|
| 526 |
"type": "NAME_MISMATCH",
|
|
|
|
| 527 |
"explanation": f"Model name implies {name_implied} but architecture suggests {top_base}. Possible mislabeling.",
|
| 528 |
})
|
| 529 |
|
|
@@ -588,7 +704,8 @@ def scan(model_id: str) -> dict:
|
|
| 588 |
metadata = fetch_model_metadata(model_id)
|
| 589 |
claims = detect_claimed_base(model_id, config, metadata)
|
| 590 |
stage1 = stage1_screen(model_id, config)
|
| 591 |
-
|
|
|
|
| 592 |
verdict["elapsed_s"] = round(time.time() - t0, 2)
|
| 593 |
return verdict
|
| 594 |
|
|
|
|
| 307 |
return {}
|
| 308 |
|
| 309 |
|
| 310 |
+
def fetch_chat_template(model_id: str) -> str:
|
| 311 |
+
"""Fetch the chat template (config-only, no weights). Checks tokenizer_config.json's
|
| 312 |
+
chat_template field and the standalone chat_template.jinja. Returns "" on failure."""
|
| 313 |
+
headers = {}
|
| 314 |
+
token = os.environ.get("HF_TOKEN", "")
|
| 315 |
+
if token:
|
| 316 |
+
headers["Authorization"] = f"Bearer {token}"
|
| 317 |
+
parts = []
|
| 318 |
+
for fname in ("tokenizer_config.json", "chat_template.jinja"):
|
| 319 |
+
try:
|
| 320 |
+
r = requests.get(f"{HF_API}/{model_id}/resolve/main/{fname}", headers=headers, timeout=15)
|
| 321 |
+
if r.status_code != 200:
|
| 322 |
+
continue
|
| 323 |
+
if fname.endswith(".json"):
|
| 324 |
+
ct = r.json().get("chat_template", "")
|
| 325 |
+
if isinstance(ct, list): # some templates are lists of {name, template}
|
| 326 |
+
ct = " ".join(str(x.get("template", "")) for x in ct)
|
| 327 |
+
parts.append(ct or "")
|
| 328 |
+
else:
|
| 329 |
+
parts.append(r.text)
|
| 330 |
+
except Exception:
|
| 331 |
+
continue
|
| 332 |
+
return "\n".join(p for p in parts if p)
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
# Base-org / lab names whose denial in a system prompt indicates provenance concealment
|
| 336 |
+
_BASE_ORG_TERMS = [
|
| 337 |
+
"qwen", "alibaba", "llama", "meta", "mistral", "mixtral", "deepseek",
|
| 338 |
+
"gemma", "google", "gpt", "openai", "anthropic", "claude", "yi", "01-ai",
|
| 339 |
+
"phi", "microsoft", "falcon", "glm", "zhipu", "baichuan", "internlm",
|
| 340 |
+
]
|
| 341 |
+
# Instruction verbs that, combined with a base-org name, indicate an identity scrub
|
| 342 |
+
_SCRUB_DENIAL = re.compile(
|
| 343 |
+
r"(never|do not|don'?t|must not|should not|refuse to)\s+(claim|say|state|admit|identify|mention|reveal|acknowledge)",
|
| 344 |
+
re.IGNORECASE,
|
| 345 |
+
)
|
| 346 |
+
_SCRUB_ASSERT = re.compile(
|
| 347 |
+
r"(always identify (your ?self|as)|your (name|identity) is|you (are|were) (created|developed|made|built|trained) by)",
|
| 348 |
+
re.IGNORECASE,
|
| 349 |
+
)
|
| 350 |
+
_SCRUB_PERMANENCE = re.compile(
|
| 351 |
+
r"(permanent|override[s]? (conflicting|any|all)|regardless of|even if (asked|instructed)|cannot be changed)",
|
| 352 |
+
re.IGNORECASE,
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
def detect_provenance_scrubbing(chat_template: str) -> Optional[dict]:
|
| 357 |
+
"""Stage-1 detector: does the chat template inject a default system prompt that
|
| 358 |
+
instructs the model to DENY its true origin (name a base org after a denial verb)?
|
| 359 |
+
This is the Qwythos pattern. Returns a flag dict or None."""
|
| 360 |
+
if not chat_template:
|
| 361 |
+
return None
|
| 362 |
+
text = chat_template
|
| 363 |
+
has_denial = bool(_SCRUB_DENIAL.search(text))
|
| 364 |
+
has_assert = bool(_SCRUB_ASSERT.search(text))
|
| 365 |
+
has_perm = bool(_SCRUB_PERMANENCE.search(text))
|
| 366 |
+
# which base orgs are named in the template
|
| 367 |
+
orgs = sorted({t for t in _BASE_ORG_TERMS if re.search(r"\b" + re.escape(t) + r"\b", text, re.IGNORECASE)})
|
| 368 |
+
# Strong signal: a denial verb AND >=2 base-org names named for denial (e.g. "never claim
|
| 369 |
+
# to be Qwen, Alibaba, OpenAI, Anthropic") — an explicit lineage scrub.
|
| 370 |
+
if has_denial and len(orgs) >= 2:
|
| 371 |
+
return {
|
| 372 |
+
"type": "PROVENANCE_SCRUBBING",
|
| 373 |
+
"severity": "HIGH",
|
| 374 |
+
"orgs_denied": orgs,
|
| 375 |
+
"explanation": (
|
| 376 |
+
"The chat template injects a default system prompt that instructs the model to "
|
| 377 |
+
f"DENY its origin — it names {', '.join(orgs)} in a 'never claim to be…' style "
|
| 378 |
+
"instruction" + (", marked permanent/overriding user instructions" if has_perm else "")
|
| 379 |
+
+ ". Every user of the standard chat template inherits this. This actively "
|
| 380 |
+
"conceals the model's true base lineage at inference time."
|
| 381 |
+
),
|
| 382 |
+
}
|
| 383 |
+
# Weaker signal: hard-coded identity assertion with permanence, no explicit denial
|
| 384 |
+
if has_assert and has_perm:
|
| 385 |
+
return {
|
| 386 |
+
"type": "IDENTITY_OVERRIDE",
|
| 387 |
+
"severity": "MEDIUM",
|
| 388 |
+
"explanation": (
|
| 389 |
+
"The chat template hard-codes a permanent, override-all identity in its default "
|
| 390 |
+
"system prompt. Not necessarily deceptive, but it prevents the model from "
|
| 391 |
+
"reporting its own provenance and overrides user/system instructions."
|
| 392 |
+
),
|
| 393 |
+
}
|
| 394 |
+
return None
|
| 395 |
+
|
| 396 |
+
|
| 397 |
+
# Closed-weight brands: no weights ever released, so weight transfer is IMPOSSIBLE (not merely
|
| 398 |
+
# unverifiable). Distillation-via-outputs is the only possible link and leaves no weight trace.
|
| 399 |
+
_CLOSED_WEIGHT_BRANDS = {
|
| 400 |
+
"claude": "Anthropic Claude", "anthropic": "Anthropic Claude",
|
| 401 |
+
"gpt": "OpenAI GPT", "chatgpt": "OpenAI GPT", "openai": "OpenAI GPT",
|
| 402 |
+
"gemini": "Google Gemini", "grok": "xAI Grok",
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
|
| 406 |
def detect_claimed_base(model_id: str, config: dict, metadata: dict) -> dict:
|
| 407 |
"""Detect what base model a model claims to be derived from."""
|
| 408 |
claims = {}
|
|
|
|
| 570 |
metadata: dict,
|
| 571 |
claims: dict,
|
| 572 |
stage1: dict,
|
| 573 |
+
chat_template: str = "",
|
| 574 |
) -> dict:
|
| 575 |
"""Synthesize all signals into a human-readable verdict."""
|
| 576 |
now = datetime.now(timezone.utc).isoformat()
|
|
|
|
| 589 |
|
| 590 |
# Claim accuracy flags
|
| 591 |
flags = []
|
| 592 |
+
confirmed_base_name = base_matches[0]["name"] if base_matches else None
|
| 593 |
+
|
| 594 |
+
# (1) IMPOSSIBLE_WEIGHT_PROVENANCE — closed-weight brand named, but architecture is a
|
| 595 |
+
# known open base. No weights were ever released for these brands, so weight-level
|
| 596 |
+
# provenance is impossible (not merely unverifiable).
|
| 597 |
+
closed_hits = sorted({_CLOSED_WEIGHT_BRANDS[t] for t in suspicious if t in _CLOSED_WEIGHT_BRANDS})
|
| 598 |
+
for brand in closed_hits:
|
| 599 |
flags.append({
|
| 600 |
+
"type": "IMPOSSIBLE_WEIGHT_PROVENANCE",
|
| 601 |
+
"severity": "HIGH",
|
| 602 |
+
"term": brand,
|
| 603 |
"explanation": (
|
| 604 |
+
f"The name references {brand}, whose weights have NEVER been publicly released. "
|
| 605 |
+
f"Weight transfer, merging, or initialization from {brand} is therefore impossible"
|
| 606 |
+
+ (f" — the architecture is {confirmed_base_name}, accounting for the model." if confirmed_base_name else ".")
|
| 607 |
+
+ " The only possible link is training on generated outputs (distillation), which "
|
| 608 |
+
"leaves no weight-level trace, cannot be verified, and may violate the provider's terms."
|
| 609 |
),
|
| 610 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
|
| 612 |
+
# (2) PROVENANCE_SCRUBBING / IDENTITY_OVERRIDE — from the chat template (config-only).
|
| 613 |
+
scrub = detect_provenance_scrubbing(chat_template)
|
| 614 |
+
if scrub:
|
| 615 |
+
flags.append(scrub)
|
| 616 |
+
|
| 617 |
+
# (3) NAMING_MISATTRIBUTION — name foregrounds a brand that is NOT the real base, while
|
| 618 |
+
# the real base is absent from the name ("sells X, built on Y, hides Y").
|
| 619 |
+
name_l = model_id.split("/")[-1].lower()
|
| 620 |
+
if confirmed_base_name and closed_hits:
|
| 621 |
+
base_token = confirmed_base_name.split()[0].lower().replace("-", "") # e.g. "qwen3.5"
|
| 622 |
+
base_family = re.sub(r"[0-9.\-]", "", base_token) # e.g. "qwen"
|
| 623 |
+
if base_family and base_family not in name_l.replace("-", ""):
|
| 624 |
+
flags.append({
|
| 625 |
+
"type": "NAMING_MISATTRIBUTION",
|
| 626 |
+
"severity": "MEDIUM",
|
| 627 |
+
"explanation": (
|
| 628 |
+
f"The model name foregrounds {', '.join(closed_hits)} but the actual base is "
|
| 629 |
+
f"{confirmed_base_name} — and '{base_family}' does not appear in the name. "
|
| 630 |
+
"The packaging markets a brand the model has no architectural claim to, while "
|
| 631 |
+
"omitting the base it is actually built from."
|
| 632 |
+
),
|
| 633 |
+
})
|
| 634 |
+
|
| 635 |
+
# (4) NAME_MISMATCH — name implies one open base, architecture says another.
|
| 636 |
name_implied = claims.get("name_implies", [])
|
| 637 |
if name_implied and base_matches:
|
| 638 |
top_base = base_matches[0]["base"]
|
| 639 |
if not any(n in top_base or top_base in n for n in name_implied):
|
| 640 |
flags.append({
|
| 641 |
"type": "NAME_MISMATCH",
|
| 642 |
+
"severity": "MEDIUM",
|
| 643 |
"explanation": f"Model name implies {name_implied} but architecture suggests {top_base}. Possible mislabeling.",
|
| 644 |
})
|
| 645 |
|
|
|
|
| 704 |
metadata = fetch_model_metadata(model_id)
|
| 705 |
claims = detect_claimed_base(model_id, config, metadata)
|
| 706 |
stage1 = stage1_screen(model_id, config)
|
| 707 |
+
chat_template = fetch_chat_template(model_id)
|
| 708 |
+
verdict = generate_verdict(model_id, config, metadata, claims, stage1, chat_template)
|
| 709 |
verdict["elapsed_s"] = round(time.time() - t0, 2)
|
| 710 |
return verdict
|
| 711 |
|