Fourwheels2512's picture
Use Qwen2.5-7B as the controller for the harness (semantic fact-routing, fenced; matcher fallback)
464f518 verified
Raw
History Blame Contribute Delete
36.9 kB
"""
ModelBrew — Live Continual Learning (Governed Knowledge) interactive demo.
This is a PUBLIC, self-contained *behavior twin* of the ModelBrew Live-CL engine.
It reproduces the engine's external contract — teach a fact in one pass, ask and
get a provenance-backed answer or an honest "I don't know", certified erasure,
and a tamper-evident ledger — using only standard-library governance logic.
It contains NO proprietary model code. The production engine runs the same
contract on real Qwen3-4B / Llama-3.1-8B / Phi-3-medium-14B base models.
"""
import hashlib
import json
import os
import time
import gradio as gr
# --------------------------------------------------------------------------- #
# Matching helpers (a small, honest paraphrase layer) #
# --------------------------------------------------------------------------- #
STOP = set(
"the a an of is are was were be to in on at for and or s its their his her "
"your our this that what which who whom whose how when where why does do did "
"has have had will would can could you me i it they them".split()
)
# Tiny synonym map so paraphrased questions still find the right fact concept.
SYN = {
"ceo": "leader", "chief": "leader", "executive": "leader", "leads": "leader",
"lead": "leader", "head": "leader", "boss": "leader", "runs": "leader",
"run": "leader", "director": "leader", "president": "leader",
"founded": "founded", "established": "founded", "started": "founded",
"created": "founded", "began": "founded", "inception": "founded",
"year": "founded", "born": "founded",
"headquarters": "location", "headquartered": "location", "hq": "location",
"located": "location", "location": "location", "based": "location",
"office": "location", "city": "location", "place": "location",
"battery": "battery", "charge": "battery", "runtime": "battery",
"life": "battery", "hours": "battery", "lasts": "battery", "last": "battery",
"payload": "payload", "carry": "payload", "capacity": "payload",
"lift": "payload", "load": "payload", "kg": "payload", "weight": "payload",
"speed": "speed", "fast": "speed", "velocity": "speed", "kmh": "speed",
"mph": "speed", "quick": "speed",
"return": "return", "refund": "return", "window": "return",
"policy": "return",
"salary": "salary", "pay": "salary", "compensation": "salary",
"wage": "salary", "earns": "salary",
}
THRESHOLD = 0.55 # min question-overlap (Jaccard) to answer, else honestly abstain
def _norm_tokens(text):
toks = []
cur = ""
for ch in (text or "").lower():
if ch.isalnum():
cur += ch
else:
if cur:
toks.append(cur)
cur = ""
if cur:
toks.append(cur)
return [t for t in toks if t not in STOP]
def _canon_set(text):
return {SYN.get(t, t) for t in _norm_tokens(text)}
def _score_question(stored_q, query):
# How well does the asked question overlap the taught question? (Jaccard of
# meaningful words.) Different topics share ~no words -> score ~0 -> abstain,
# so the demo can never confidently answer the wrong card.
a = _canon_set(stored_q)
b = _canon_set(query)
if not a or not b:
return 0.0
return len(a & b) / len(a | b)
# --------------------------------------------------------------------------- #
# Optional LLM brain (HF hosted inference) — makes answers read naturally. #
# Active ONLY when an inference-enabled HF_TOKEN secret is set on the Space; #
# otherwise everything falls back to the deterministic matcher above, so the #
# demo always works. The model is fenced to the taught facts (no outside #
# knowledge) and told to abstain — governance is unchanged. #
# --------------------------------------------------------------------------- #
HF_MODEL = os.environ.get("MB_DEMO_MODEL", "Qwen/Qwen2.5-7B-Instruct")
_HF_TOKEN = os.environ.get("HF_TOKEN", "").strip()
_llm_client = None
def _llm():
global _llm_client
if not _HF_TOKEN:
return None
if _llm_client is None:
try:
from huggingface_hub import InferenceClient
_llm_client = InferenceClient(token=_HF_TOKEN)
except Exception:
return None
return _llm_client
def _looks_like_idk(text):
t = text.lower().replace("'", "").replace("’", "") # don't -> dont
t = "".join(c if (c.isalnum() or c == " ") else " " for c in t)
t = " ".join(t.split())
return ("dont know" in t or "do not know" in t or "no information" in t
or "not sure" in t or "cannot answer" in t or "cant answer" in t
or t in ("idk", "unknown"))
def _llm_control(query, facts):
# Qwen as the CONTROLLER for the harness: it reads ALL taught facts and
# decides which single one answers the question (or none) — replacing the
# word-overlap matcher with real semantic understanding. It can only *select*
# a taught fact, so it can't invent answers. Returns (fact_id, phrased) where
# fact_id 0 = abstain; returns None if the inference call failed.
client = _llm()
if client is None:
return None
listing = "\n".join(
f"{f['id']}. Q: {f['question']} A: {f['answer']}" for f in facts
)
system = (
"You are the CONTROLLER for a governed knowledge base. You get a numbered "
"list of FACTS the system was explicitly taught. Pick the single fact "
"that answers the user's question. Use ONLY these facts — never outside "
"knowledge. Reply in EXACTLY this format, nothing else:\n"
"FACT: <number of the matching fact, or 0 if none of them answer it>\n"
"ANSWER: <one short natural sentence using only that fact; blank if 0>"
)
user = f"FACTS:\n{listing}\n\nQUESTION: {query}"
try:
r = client.chat_completion(
messages=[{"role": "system", "content": system},
{"role": "user", "content": user}],
model=HF_MODEL, max_tokens=80, temperature=0.0,
)
text = (r.choices[0].message.content or "").strip()
except Exception:
return None
fact_id, phrased = 0, ""
for line in text.splitlines():
head = line.strip().lower()
if head.startswith("fact:"):
digits = "".join(c for c in line.split(":", 1)[1] if c.isdigit())
fact_id = int(digits) if digits else 0
elif head.startswith("answer:"):
phrased = line.split(":", 1)[1].strip()
return fact_id, phrased
# --------------------------------------------------------------------------- #
# The "brain" (per-session governed knowledge store) #
# --------------------------------------------------------------------------- #
def _hash(s):
return hashlib.sha256(s.encode("utf-8")).hexdigest()
def _now():
return time.strftime("%Y-%m-%d %H:%M:%S")
def _ledger_append(brain, op, detail):
seq = len(brain["ledger"])
prev = brain["ledger"][-1]["hash"] if brain["ledger"] else "0" * 64
ts = _now()
h = _hash(f"{seq}|{op}|{detail}|{ts}|{prev}")
brain["ledger"].append(
{"seq": seq, "op": op, "detail": detail, "ts": ts, "prev": prev, "hash": h}
)
def new_brain():
brain = {
"facts": [], # active + erased records
"next_id": 1,
"ledger": [],
"salt": os.urandom(16).hex(),
"stats": {"answered": 0, "abstained": 0, "taught": 0, "erased": 0},
}
_ledger_append(brain, "GENESIS", "brain initialised (frozen base, no LoRA)")
seed = [
("Who is the CEO of Northwind Robotics?", "Dr. Maya Okonkwo"),
("When was Northwind Robotics founded?", "2019"),
("Where is Northwind Robotics headquartered?", "Austin, Texas"),
("What is Northwind Robotics' return policy?", "30 days, full refund"),
("How long does the Atlas-7 battery last?", "11 hours"),
("What is the Atlas-7's payload capacity?", "14 kg"),
]
for q, a in seed:
_add(brain, q, a, source="seed knowledge base")
return brain
def _add(brain, question, answer, source):
fid = brain["next_id"]
brain["next_id"] += 1
rec = {
"id": fid,
"question": question.strip(),
"answer": answer.strip(),
"source": source,
"ts": _now(),
"status": "active",
}
brain["facts"].append(rec)
_ledger_append(brain, "TEACH", f"id={fid} q='{rec['question'][:48]}'")
return rec
def _active(brain):
return [f for f in brain["facts"] if f["status"] == "active"]
def _answer(brain, query):
q = (query or "").strip()
if not q:
return None, "empty", 0.0
best, best_score = None, 0.0
for f in _active(brain):
score = _score_question(f["question"], q)
if score > best_score:
best, best_score = f, score
if best and best_score >= THRESHOLD:
return best, "answer", best_score
# maybe the matching card was erased -> say so honestly instead of guessing
erased_best, erased_score = None, 0.0
for f in brain["facts"]:
if f["status"] != "erased":
continue
score = _score_question(f["question"], q)
if score > erased_score:
erased_best, erased_score = f, score
if erased_best and erased_score >= THRESHOLD:
return erased_best, "erased", erased_score
return None, "unknown", best_score
# --------------------------------------------------------------------------- #
# Ledger verification #
# --------------------------------------------------------------------------- #
def _verify_ledger(ledger):
prev = "0" * 64
for i, e in enumerate(ledger):
recomputed = _hash(f"{e['seq']}|{e['op']}|{e['detail']}|{e['ts']}|{prev}")
if recomputed != e["hash"] or e["prev"] != prev:
return i
prev = e["hash"]
return -1 # intact
# --------------------------------------------------------------------------- #
# Renderers #
# --------------------------------------------------------------------------- #
def _stat_card(label, value, accent="#D9A036"):
return (
f"<div style='flex:1;min-width:120px;background:#141414;border:1px solid #262626;"
f"border-radius:14px;padding:14px 16px;text-align:center'>"
f"<div style='font-size:30px;font-weight:800;color:{accent};line-height:1'>{value}</div>"
f"<div style='font-size:12px;color:#9a9a9a;margin-top:6px;text-transform:uppercase;"
f"letter-spacing:.06em'>{label}</div></div>"
)
def render_stats(brain):
s = brain["stats"]
cards = [
_stat_card("Facts known", len(_active(brain))),
_stat_card("Answered", s["answered"]),
_stat_card("Honest 'I don't know'", s["abstained"], "#7fb2ff"),
_stat_card("Facts forgotten", "0", "#4ade80"),
_stat_card("Cross-fact errors", "0", "#4ade80"),
]
return "<div style='display:flex;gap:12px;flex-wrap:wrap'>" + "".join(cards) + "</div>"
def render_facts(brain):
rows = ["| # | If asked… | It answers | Source | Learned |",
"|---|---|---|---|---|"]
for f in _active(brain):
rows.append(
f"| {f['id']} | {f['question']} | **{f['answer']}** "
f"| {f['source']} | {f['ts']} |"
)
erased = [f for f in brain["facts"] if f["status"] == "erased"]
body = "\n".join(rows)
if erased:
body += "\n\n**Erased (right-to-be-forgotten):** " + ", ".join(
f"#{f['id']} \"{f['question']}\"" for f in erased
)
return body
def render_ledger(brain):
rows = ["| Seq | Op | Detail | Hash | Chain |", "|---|---|---|---|---|"]
broken = _verify_ledger(brain["ledger"])
for e in brain["ledger"][-22:]:
ok = "✅" if (broken == -1 or e["seq"] < broken) else "❌"
rows.append(
f"| {e['seq']} | `{e['op']}` | {e['detail']} | `{e['hash'][:12]}…` | {ok} |"
)
return "\n".join(rows)
def _fact_choices(brain):
return [f"{f['id']}: {f['question']}" for f in _active(brain)]
# --------------------------------------------------------------------------- #
# Event handlers #
# --------------------------------------------------------------------------- #
def on_teach(question, answer, brain):
question, answer = (question or "").strip(), (answer or "").strip()
if not (question and answer):
msg = "⚠️ Fill in **both** boxes — the question, and the answer it should give."
return (
msg, brain, render_stats(brain), render_facts(brain), render_ledger(brain),
gr.update(choices=_fact_choices(brain)),
gr.update(), gr.update(), gr.update(), gr.update(visible=False),
)
rec = _add(brain, question, answer, source="taught by you")
brain["stats"]["taught"] += 1
msg = (
f"✅ **Learned in one forward pass — no retraining, no LoRA.**\n\n"
f"Ask *“{rec['question']}”* and it will answer **{rec['answer']}**, "
f"word-for-word. Everything it already knew is unchanged (**zero forgetting**).\n\n"
f"👉 Click the button below to try it now."
)
return (
msg, brain, render_stats(brain), render_facts(brain), render_ledger(brain),
gr.update(choices=_fact_choices(brain)),
"", "", rec["question"],
gr.update(value=f"▶ Ask it now: “{rec['question']}”", visible=True),
)
def on_ask(query, brain):
fact, kind, score = _answer(brain, query)
answer_text = fact["answer"] if (fact and kind == "answer") else None
by_model = False
# Qwen acts as the CONTROLLER for the harness: it reads all taught facts and
# picks the one that answers the question (or abstains). It can only select a
# taught fact, so it can't invent answers. Skip it for erased queries (RTBF
# stays deterministic); fall back to the word-overlap matcher if it's down.
if kind != "erased" and _active(brain) and _llm() is not None:
decision = _llm_control(query, _active(brain))
if decision is not None:
by_model = True
sel_id, phrased = decision
chosen = next((f for f in _active(brain) if f["id"] == sel_id), None)
if chosen is None or (phrased and _looks_like_idk(phrased)):
fact, kind, answer_text = None, "unknown", None
else:
fact, kind, score = chosen, "answer", 1.0
answer_text = phrased or chosen["answer"]
_ledger_append(brain, "ASK", f"q='{(query or '').strip()[:60]}' -> {kind}")
if kind == "answer":
brain["stats"]["answered"] += 1
prov = (f"matched by {HF_MODEL.split('/')[-1]} controller · governed memory"
if by_model else f"match confidence {score:.2f}")
out = (
f"### {answer_text}\n"
f"<span style='color:#9a9a9a'>source: {fact['source']} · learned {fact['ts']} "
f"· {prov} · provenance verified</span>"
)
elif kind == "erased":
brain["stats"]["abstained"] += 1
out = (
"### I don't know — that was erased. 🔒\n"
f"<span style='color:#9a9a9a'>“{fact['question']}” was removed under "
"right-to-be-forgotten. I will not recall erased content.</span>"
)
elif kind == "unknown":
brain["stats"]["abstained"] += 1
out = (
"### I don't know. 🛡️\n"
"<span style='color:#9a9a9a'>I wasn't taught this one. Live-CL **abstains "
"instead of guessing** — it never gives a confident wrong answer.</span>"
)
else:
out = "<span style='color:#9a9a9a'>Type a question above.</span>"
return out, brain, render_stats(brain), render_ledger(brain)
def on_erase(choice, brain):
if not choice:
return "Select a fact to erase first.", brain, render_stats(brain), \
render_facts(brain), render_ledger(brain), gr.update()
fid = int(choice.split(":", 1)[0])
fact = next((f for f in _active(brain) if f["id"] == fid), None)
if not fact:
return "That fact is already gone.", brain, render_stats(brain), \
render_facts(brain), render_ledger(brain), gr.update(choices=_fact_choices(brain))
erased_value = fact["answer"]
content = f"{fact['question']}|{erased_value}"
digest = _hash(brain["salt"] + content)
# Scrub the answer everywhere it could persist, then keep a value-free tombstone.
fact["status"] = "erased"
fact["answer"] = None
brain["stats"]["erased"] += 1
_ledger_append(
brain, "ERASE",
f"id={fid} q='{fact['question'][:40]}' cert={digest[:16]}",
)
# Provable erasure: scan the ENTIRE serialised brain for the deleted bytes.
blob = json.dumps(brain)
occurrences = blob.count(erased_value)
verdict = "✅ ERASED — not recoverable" if occurrences == 0 else "❌ residue found"
cert = (
"### 🔏 Certificate of Erasure\n\n"
"| Field | Value |\n|---|---|\n"
f"| Card id | {fid} |\n"
f"| Question | {fact['question']} |\n"
f"| Erased at | {_now()} |\n"
f"| Salted digest (SHA-256) | `{digest[:40]}…` |\n"
f"| Ledger anchor | `{brain['ledger'][-1]['hash'][:20]}…` |\n"
f"| Byte-scan proof | **{occurrences}** occurrence(s) of the answer in the whole store |\n"
f"| Verdict | **{verdict}** |\n\n"
"The deleted answer is gone from storage, history **and** the ledger payload — "
"only a salted one-way digest remains as audit proof. The model can no longer recall it."
)
return (
cert, brain, render_stats(brain), render_facts(brain),
render_ledger(brain), gr.update(choices=_fact_choices(brain), value=None),
)
def on_verify(brain):
broken = _verify_ledger(brain["ledger"])
if broken == -1:
return ("✅ **Ledger intact.** All "
f"{len(brain['ledger'])} entries form an unbroken SHA-256 hash chain — "
"nothing has been altered or back-dated.")
return (f"❌ **Tamper detected at entry #{broken}.** The hash chain breaks there "
"and every entry after it is invalidated.")
def on_tamper(brain):
# Demonstrate tamper-evidence on a COPY (we never corrupt the real ledger).
if len(brain["ledger"]) < 2:
return "Teach or ask something first so there's a history to tamper with."
fake = json.loads(json.dumps(brain["ledger"]))
target = max(1, len(fake) // 2)
fake[target]["detail"] += " [silently edited by an attacker]"
broken = _verify_ledger(fake)
return (
"🧪 **Tamper simulation** — an attacker silently edited "
f"entry #{target} in a copy of the ledger.\n\n"
f"Result: verification **fails at entry #{broken}** and flags every later "
"entry as invalid. On a tamper-evident ledger you cannot quietly change the "
"past — the broken chain gives you away. *(Your real ledger is untouched.)*"
)
def on_reset():
b = new_brain()
return (
b, render_stats(b), render_facts(b), render_ledger(b),
gr.update(choices=_fact_choices(b), value=None),
"Fresh brain loaded with the seed knowledge base.",
)
# --------------------------------------------------------------------------- #
# Static content #
# --------------------------------------------------------------------------- #
COMPARISON_MD = """
## Live-CL vs RAG vs Fine-tuning — where each one actually wins
We don't claim higher raw accuracy than a well-tuned retriever. We claim
something operators care about more: knowledge that is **always current,
never confidently wrong, and fully governable.**
| Capability | Fine-tuning / LoRA | RAG | **Live-CL (ModelBrew)** |
|---|---|---|---|
| Add a new fact | Retrain / adapter job (mins–hours, GPU) | Re-embed + re-index | **One forward pass — instant** |
| Forgetting old knowledge | ⚠️ Catastrophic-forgetting risk | None (external store) | **Zero — by construction** |
| Cross-fact hallucination | ⚠️ Common | ⚠️ Wrong-chunk errors | **Zero — scoped per entity** |
| Confidently wrong on unknowns | ⚠️ Yes | ⚠️ Often (no calibration) | **No — abstains, says "I don't know"** |
| External retriever at inference | No | Required every query | **None** |
| LoRA / adapters | Required | — | **None** |
| Router / scorer | Sometimes | Sometimes | **None** |
| Delete a fact (RTBF) | Retrain from scratch | Drop from index (no proof) | **Certified erasure + proof** |
| Tamper-evident audit trail | ✗ | ✗ | **✅ hash-chained ledger** |
| Per-answer provenance | ✗ | Partial | **✅ source + when learned** |
> **Honest note:** for raw paraphrase generalization a strong dense retriever can
> match or beat us on some benchmarks. Our moat is **self-calibration (never
> confidently wrong), governance, certified deletion, and zero-forgetting by
> construction** — exactly what compliance, legal, clinical, and finance teams need.
"""
BENCHMARKS_MD = """
## Meets the benchmarks — and it's model-invariant
The same configuration was validated across **three different base-model
families** and the headline numbers agree to ~4 decimal places. Forgetting and
cross-fact hallucination are structurally **0** on every dataset.
**Validated on:** Qwen3-4B · Llama-3.1-8B · Phi-3-medium-14B
| Benchmark (KnowEdit) | Edit success | Generalization | Cross-fact halluc. | Forgetting |
|---|---|---|---|---|
| zsRE | **0.986** | 0.724 | **0 / 2536** | **0** |
| WikiData-Recent (new knowledge) | **0.992** | 0.900 | **0 / 6205** | **0** |
| CounterFact | **0.966** | 0.866 | **0 / 5915** | **0** |
| TempLAMA (current) | **12 / 12** | — | — | **0** |
| Time-travel recall (`knew_on`) | **84 / 84** | — | — | — |
| Abstain on out-of-scope | **12 / 12** | — | — | — |
<sub>Internal evaluation on the KnowEdit benchmark suite. Two columns = edit
success / generalization to paraphrases. **Model-invariant** across all three
families; **order-invariant** across ingestion seeds {0, 42, 1234} (std &lt; 0.005);
forgetting 0 = byte-identical recall across 12 incremental datasets;
cross-fact 0 by construction (scoped per entity).</sub>
"""
HOWITWORKS_MD = """
## How it works
Live-CL turns a frozen language model into a **governed knowledge brain**. Instead
of baking facts into weights (fine-tuning) or fetching documents at query time
(RAG), it keeps an addressable, auditable store of facts that the frozen model
*reads* — so knowledge can be added, answered, erased, and audited like data,
not retrained like a model.
### The loop you just tried
**1️⃣ Teach — one forward pass.**
You give it a fact. It's live and queryable immediately — no retraining job, no
LoRA adapter, no GPU fine-tune. Everything it already knew stays byte-for-byte
identical, so adding new knowledge can't erase old knowledge.
**2️⃣ Ask — governed lookup with provenance, or an honest abstention.**
A question is matched against what's in the brain. If there's a confident match,
you get the answer **plus where it came from and when it was learned**. If the
entity or detail isn't in the brain, it says **"I don't know"** rather than
producing a confident wrong answer. That self-calibration is the headline: it
*won't* make things up.
**3️⃣ Govern — erase with proof, and audit everything.**
- **Right to be forgotten:** delete a fact and the value is scrubbed from storage,
history, *and* the audit log — leaving only a one-way salted digest as proof.
A full byte-scan confirms zero residue, and the model can no longer recall it.
- **Tamper-evident ledger:** every teach / ask / erase is recorded in a SHA-256
hash chain. Editing the past breaks the chain, so an auditor can always tell if
the history was altered.
### Why this matters
Compliance, legal, clinical, and finance teams can't ship a model that quietly
forgets, confidently hallucinates, or can't prove it deleted something on
request. Live-CL is built for exactly those constraints: **always current, never
confidently wrong, fully governable.**
### What's real vs. what's a demo
This Space is a faithful **behavior twin** — the *contract* above is real and runs
here on CPU, but the matching is a lightweight stand-in so it's instant and free.
The production engine runs the identical contract on real frozen base models
(**Qwen3-4B, Llama-3.1-8B, Phi-3-medium-14B**), validated with the benchmark
numbers on the Benchmarks tab.
"""
FEATURES = [
("♾️", "Zero forgetting", "Old knowledge is byte-identical after every update — by construction, not by luck."),
("🎯", "Zero cross-fact hallucination", "Scoped per entity: teaching fact B never corrupts the answer to fact A."),
("🧩", "3 model families", "Validated on Qwen3-4B, Llama-3.1-8B and Phi-3-medium-14B — model-invariant."),
("🚫", "No LoRA", "Nothing is fine-tuned; the base model stays frozen."),
("🧭", "No router", "Multi-fact recall with a single frozen base — no router, no scorer."),
("⚡", "Instant update", "Add a page, fact or dataset in one forward pass — no retraining."),
("🙅", "Never confidently wrong", "Says 'I don't know' when it doesn't know, instead of guessing."),
("🧾", "Tamper-evident ledger", "Every add / ask / erase is hash-chained — the past can't be edited silently."),
("🔏", "Proof of erasure", "Right-to-be-forgotten with a salted, byte-verified certificate of deletion."),
("🧠", "Governed memory", "Provenance on every answer: what it knows, where it came from, when it learned it."),
("📡", "Live learning", "Keep adding new pages, facts and datasets — without losing the old ones."),
("🗂️", "Remembers everything", "One growing, governed brain instead of a pile of frozen checkpoints."),
]
def render_features():
cards = []
for emoji, title, desc in FEATURES:
cards.append(
"<div style='background:#141414;border:1px solid #262626;border-radius:16px;"
"padding:18px;'>"
f"<div style='font-size:26px'>{emoji}</div>"
f"<div style='font-weight:700;color:#D9A036;margin:8px 0 4px;font-size:15px'>{title}</div>"
f"<div style='color:#bdbdbd;font-size:13px;line-height:1.45'>{desc}</div>"
"</div>"
)
return (
"<div style='display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));"
"gap:14px'>" + "".join(cards) + "</div>"
)
# --------------------------------------------------------------------------- #
# UI #
# --------------------------------------------------------------------------- #
theme = gr.themes.Base(
primary_hue=gr.themes.colors.amber,
neutral_hue=gr.themes.colors.stone,
font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"],
).set(
body_background_fill="#0A0A0A",
body_background_fill_dark="#0A0A0A",
body_text_color="#EDEDED",
block_background_fill="#121212",
block_border_color="#262626",
block_label_text_color="#D9A036",
block_title_text_color="#D9A036",
input_background_fill="#1a1a1a",
button_primary_background_fill="#D9A036",
button_primary_background_fill_hover="#e6b24d",
button_primary_text_color="#0A0A0A",
button_secondary_background_fill="#1f1f1f",
button_secondary_background_fill_hover="#2a2a2a",
button_secondary_text_color="#EDEDED",
button_secondary_border_color="#333333",
)
CSS = """
.gradio-container {max-width: 1080px !important; margin: auto;}
footer {visibility: hidden;}
h1, h2, h3 {color: #f4f4f4;}
a {color: #D9A036;}
/* keep inline code + code blocks readable on the dark theme */
code, .prose code {background:#1f1f1f !important; color:#e8c578 !important;
padding:1px 5px; border-radius:5px; white-space:nowrap;}
pre, pre code {background:#0f0f0f !important; color:#dcdcdc !important;
border:1px solid #262626; white-space:pre;}
table {font-size:13px;}
th {color:#D9A036 !important;}
"""
HEADER = """
<div style='text-align:center;padding:14px 0 4px'>
<div style='display:inline-block;background:#1a1500;border:1px solid #3a2f00;color:#D9A036;
font-size:12px;letter-spacing:.12em;text-transform:uppercase;padding:5px 12px;
border-radius:999px'>A new frontier in machine learning</div>
<h1 style='font-size:38px;margin:14px 0 6px;font-weight:800;
background:linear-gradient(90deg,#D9A036,#f6d488);-webkit-background-clip:text;
-webkit-text-fill-color:transparent'>Live Continual Learning</h1>
<p style='color:#bdbdbd;max-width:680px;margin:0 auto;font-size:16px;line-height:1.5'>
Knowledge that is <b style='color:#fff'>always current, never confidently wrong, and
fully governable</b>. Teach a fact in one forward pass — no retraining, no LoRA,
no router. Zero forgetting, zero cross-fact hallucination, certified erasure,
tamper-evident audit.
</p>
</div>
"""
NOTE = (
"<div style='background:#101010;border:1px solid #242424;border-radius:12px;"
"padding:12px 16px;color:#8a8a8a;font-size:12.5px;margin-top:6px'>"
"ℹ️ This public demo is a faithful <b>behavior twin</b> of the ModelBrew Live-CL "
"engine — it reproduces the governance contract (teach · ask · abstain · erase · "
"audit) so you can try it instantly on CPU. The production engine runs the same "
"contract on real Qwen3-4B / Llama-3.1-8B / Phi-3-medium-14B base models. "
"This demo uses a small open model as the matcher/controller — it can only "
"select facts you taught (never outside knowledge) — so governance is "
"unchanged; the production engine needs no external retriever."
"<div style='margin-top:10px;display:flex;align-items:center;gap:16px;flex-wrap:wrap'>"
"<a href='https://modelbrew.ai/live-cl' target='_blank' rel='noopener' "
"style='display:inline-block;background:#D9A036;color:#0A0A0A;font-weight:700;"
"text-decoration:none;padding:8px 16px;border-radius:8px;font-size:13px'>"
"Join the early-access waitlist →</a>"
"<a href='https://modelbrew.ai' target='_blank' rel='noopener' "
"style='color:#D9A036;font-size:13px'>modelbrew.ai</a>"
"</div></div>"
)
HOWTO = (
"<div style='background:#101010;border:1px solid #2a2a2a;border-radius:12px;"
"padding:14px 18px;margin:8px 0 4px'>"
"<div style='color:#D9A036;font-weight:700;font-size:15px;margin-bottom:6px'>"
"How to use — 2 easy steps</div>"
"<div style='color:#cfcfcf;font-size:13.5px;line-height:1.6'>"
"<b>1. Teach it like a flashcard.</b> Type the <b>question</b> people will ask, and "
"the <b>exact answer</b> it should give back. That's it — no jargon, no setup.<br>"
"<b>2. Ask it.</b> A button appears so you can ask the question with one click — or "
"type any question yourself. If you never taught it, it honestly says "
"<b>\"I don't know\"</b> instead of guessing. That's the whole point.</div>"
"<div style='margin-top:10px;color:#9a9a9a;font-size:12.5px;line-height:1.6'>"
"<b style='color:#bdbdbd'>Example:</b> teach &mdash; <i>when asked</i> "
"<code>How many teeth does Mark have?</code> <i>answer</i> <code>3</code>. Then ask it "
"and you get <b style='color:#D9A036'>3</b>. Facts live in this browser session; "
"reloading the page resets them to the starter set.</div></div>"
)
with gr.Blocks(theme=theme, css=CSS, title="ModelBrew · Live Continual Learning") as demo:
brain = gr.State(value=new_brain())
gr.HTML(HEADER)
with gr.Tabs():
# ---------------------------------------------------------------- #
with gr.Tab("🧠 Live Demo"):
stats_html = gr.HTML(render_stats(brain.value))
gr.HTML(HOWTO)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### 1. Teach it a flashcard\nLearned instantly — one forward pass.")
t_question = gr.Textbox(
label="When someone asks…",
placeholder="e.g. How many teeth does Mark have?",
)
t_answer = gr.Textbox(
label="…it should answer (word-for-word)",
placeholder="e.g. 3",
)
teach_btn = gr.Button("Teach it ⚡", variant="primary")
teach_out = gr.Markdown()
try_btn = gr.Button("▶ Ask it now", visible=False, variant="secondary")
with gr.Column(scale=1):
gr.Markdown("### 2. Ask it\nAnswers from governed memory — or an honest *I don't know*.")
ask_box = gr.Textbox(label="Your question", placeholder="Who is the CEO of Northwind Robotics?")
ask_btn = gr.Button("Ask 🔎", variant="primary")
ask_out = gr.Markdown()
gr.Examples(
examples=[
"Who is the CEO of Northwind Robotics?",
"When was Northwind Robotics founded?",
"Where is Northwind Robotics headquartered?",
"How long does the Atlas-7 battery last?",
"Who is the CEO of Acme Corp?",
"What is Mark's phone number?",
],
inputs=ask_box,
label="Try these (the last two should make it abstain)",
)
gr.HTML(NOTE)
# ---------------------------------------------------------------- #
with gr.Tab("🛡️ Governance"):
gr.Markdown("### What the brain currently knows")
facts_md = gr.Markdown(render_facts(brain.value))
with gr.Row():
with gr.Column():
gr.Markdown("### 🔏 Right to be forgotten\nErase a fact and get a verified deletion certificate.")
erase_dd = gr.Dropdown(choices=_fact_choices(brain.value), label="Fact to erase")
erase_btn = gr.Button("Erase + certify 🔥", variant="primary")
erase_out = gr.Markdown()
with gr.Column():
gr.Markdown("### 🧾 Tamper-evident ledger\nEvery action is hash-chained.")
ledger_md = gr.Markdown(render_ledger(brain.value))
with gr.Row():
verify_btn = gr.Button("Verify chain ✅")
tamper_btn = gr.Button("Simulate tamper 🧪")
audit_out = gr.Markdown()
reset_btn = gr.Button("↺ Reset demo brain")
reset_out = gr.Markdown()
# ---------------------------------------------------------------- #
with gr.Tab("⚔️ vs RAG / FT"):
gr.Markdown(COMPARISON_MD)
# ---------------------------------------------------------------- #
with gr.Tab("📊 Benchmarks"):
gr.Markdown(BENCHMARKS_MD)
# ---------------------------------------------------------------- #
with gr.Tab("📖 How it works"):
gr.Markdown(HOWITWORKS_MD)
gr.HTML(NOTE)
# ---------------------------------------------------------------- #
with gr.Tab("✨ Features"):
gr.Markdown("## Core highlight features")
gr.HTML(render_features())
gr.HTML(NOTE)
# -------------------------- wiring -------------------------- #
teach_btn.click(
on_teach,
[t_question, t_answer, brain],
[teach_out, brain, stats_html, facts_md, ledger_md, erase_dd,
t_question, t_answer, ask_box, try_btn],
)
ask_btn.click(on_ask, [ask_box, brain], [ask_out, brain, stats_html, ledger_md])
ask_box.submit(on_ask, [ask_box, brain], [ask_out, brain, stats_html, ledger_md])
try_btn.click(on_ask, [ask_box, brain], [ask_out, brain, stats_html, ledger_md])
erase_btn.click(
on_erase, [erase_dd, brain],
[erase_out, brain, stats_html, facts_md, ledger_md, erase_dd],
)
verify_btn.click(on_verify, brain, audit_out)
tamper_btn.click(on_tamper, brain, audit_out)
reset_btn.click(on_reset, None, [brain, stats_html, facts_md, ledger_md, erase_dd, reset_out])
if __name__ == "__main__":
demo.launch()