"""HTML report for System One vs Laya vs DeepSeek comparison.""" from __future__ import annotations import html def _esc(s) -> str: return html.escape(str(s) if s is not None else "") def _bar(probs, options, chosen_index, fill_class="bar-fill"): parts = [] for i, (p, label) in enumerate(zip(probs or [], options or [])): w = max(2, int(float(p) * 100)) hot = i == chosen_index parts.append( f'
' f'{_esc(label)}' f'' f'{float(p) * 100:.1f}%
' ) return "".join(parts) def _choice_of(block: dict | None) -> tuple[str, float]: if not block: return "—", 0.0 return str(block.get("chosen") or "—"), float(block.get("conf") or 0.0) def _norm(s: str | None) -> str: return (s or "").strip().lower() def _agree_pill(a: str | None, b: str | None, label: str) -> str: if not a or not b or a == "—" or b == "—": return f'{_esc(label)}: —' if _norm(a) == _norm(b): return f'{_esc(label)}: match' return f'{_esc(label)}: differ' def render_report(jev: dict, laya: dict, deepseek: dict, qs: list[dict]) -> str: from deepseek_lane import answers_by_id ds_answers = answers_by_id(deepseek.get("validation") or {}) jev_qs = {b["id"]: b for b in (jev.get("questions") or [])} laya_qs = {b["id"]: b for b in (laya.get("questions") or [])} all3 = 0 comparable = 0 rows = [] for q in qs: jid = q["id"] jb = jev_qs.get(jid) lb = laya_qs.get(jid) j_choice, j_conf = _choice_of(jb) l_choice, l_conf = _choice_of(lb) ds_val = ds_answers.get(jid) picks = [p for p in (j_choice, l_choice, ds_val) if p and p != "—"] if len(picks) >= 2: comparable += 1 if len({_norm(p) for p in picks}) == 1 and len(picks) == 3: all3 += 1 j_bars = _bar(jb["probs"], jb["options"], jb["chosen_index"]) if jb else "" l_bars = ( _bar(lb["probs"], lb["options"], lb["chosen_index"], fill_class="bar-fill laya") if lb else "" ) ds_status = "" for a in (deepseek.get("validation") or {}).get("answers") or []: if a["id"] == jid: ds_status = a["status"] break pills = " ".join( [ _agree_pill(j_choice, l_choice, "S1↔Laya"), _agree_pill(j_choice, ds_val, "S1↔DS"), _agree_pill(l_choice, ds_val, "Laya↔DS"), ] ) jev_err = "" laya_body = l_bars if laya.get("error") and not lb: laya_body = f'

{_esc(laya.get("error"))}

' rows.append( f'
' f'

{_esc(jid)} · {_esc(q["type"])}

' f'

{_esc(q["question"])}

' f'
' f'
System One
' f'{j_bars or jev_err}' f'

→ {_esc(j_choice)} ({j_conf * 100:.1f}%)

' f'
Laya
' f'{laya_body}' f'

→ {_esc(l_choice)} ({l_conf * 100:.1f}%)

' f'
DeepSeek (API)
' f'

→ {_esc(ds_val or "—")}' f' {_esc(ds_status)}

' f'
{pills}
' f"
" ) ratio = f"{all3}/{comparable}" if comparable else "—" plan = jev.get("plan") or {} warn = "" if plan.get("beyond_train_len"): warn += ( '

State exceeds System One training length (384 tokens) — ' "treat S1 probabilities as exploratory.

" ) if plan.get("truncated"): warn += '

State was truncated to fit the System One scorer limit.

' if jev.get("error"): warn += f'

System One: {_esc(jev["error"])}

' if laya.get("error"): warn += f'

Laya: {_esc(laya["error"])}

' ds_err = deepseek.get("error") ds_meta = ( f'

DeepSeek: {_esc(ds_err)}

' if ds_err else ( f'

DeepSeek: {_esc(deepseek.get("route", ""))} · ' f'valid JSON {deepseek.get("validation", {}).get("n_valid", 0)}/{len(qs)} · ' f'Laya: {_esc(laya.get("checkpoint", "—"))}

' ) ) return f"""
{jev.get("total_ms", 0):.0f} ms
System One
{laya.get("total_ms", 0):.0f} ms
Laya
{deepseek.get("latency_ms", 0):.0f} ms
DeepSeek API
{ratio}
All-3 agree
{warn} {ds_meta} {"".join(rows)}
""" def render_error(msg: str) -> str: return f'

{_esc(msg)}

'