Spaces:
Running

Cbot / index.html
amshimba's picture
Cbot: rule-based chatbot in C, as WebAssembly (cbot, cbot-moe, cbot-flash)
7031091 verified
Raw History Blame Contribute Delete
7.12 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cbot: a rule-based chatbot in C</title>
<style>
:root {
--bg: #f7f7f5; --panel: #ffffff; --text: #1d1d1b; --muted: #6b6b66; --line: #e2e2dd;
--you: #1f4fd1; --you-text: #ffffff; --accent: #1a7f4b; --code: #f0f0ec;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #141413; --panel: #1c1c1a; --text: #ececea; --muted: #9a9a93; --line: #2e2e2b;
--you: #3b6cf0; --you-text: #ffffff; --accent: #4cc38a; --code: #262623;
}
}
* { box-sizing: border-box; }
body {
margin: 0; background: var(--bg); color: var(--text);
font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
display: flex; flex-direction: column; height: 100dvh;
}
header {
padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
}
header h1 { font-size: 16px; margin: 0 8px 0 0; }
header .sub { color: var(--muted); font-size: 13px; flex: 1 1 260px; }
label { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
select, input[type=checkbox] { font: inherit; }
select {
background: var(--panel); color: var(--text); border: 1px solid var(--line);
border-radius: 6px; padding: 3px 6px;
}
main { flex: 1; overflow-y: auto; padding: 16px; }
.log { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 85%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.you { align-self: flex-end; background: var(--you); color: var(--you-text); border-bottom-right-radius: 4px; }
.bot { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.meta { font-size: 12px; color: var(--muted); margin-top: 4px; font-variant-numeric: tabular-nums; }
.meta b { color: var(--accent); font-weight: 600; }
.why { margin-top: 6px; font-size: 12px; border-top: 1px dashed var(--line); padding-top: 6px; }
.why div { display: grid; grid-template-columns: 1.2em 6.5em 4.5em 1fr; gap: 4px; color: var(--muted); }
.why div.chosen { color: var(--text); }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin: 8px 0 16px; }
.hint code { background: var(--code); padding: 1px 5px; border-radius: 4px; cursor: pointer; }
form { border-top: 1px solid var(--line); background: var(--panel); padding: 12px 16px; }
.row { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; }
#input {
flex: 1; font: inherit; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--line);
background: var(--bg); color: var(--text);
}
#input:focus-visible, button:focus-visible, select:focus-visible { outline: 2px solid var(--you); outline-offset: 1px; }
button {
font: inherit; padding: 9px 16px; border-radius: 8px; border: 0; background: var(--you);
color: var(--you-text); cursor: pointer;
}
button.secondary { background: transparent; color: var(--muted); border: 1px solid var(--line); }
button:disabled { opacity: .5; cursor: default; }
</style>
</head>
<body>
<header>
<h1>Cbot</h1>
<span class="sub">A rule-based chatbot written in C and compiled to WebAssembly: experts and an arbiter, no neural network, no training.</span>
<label>Model
<select id="variant">
<option value="0">cbot (all experts)</option>
<option value="1">cbot-moe (top-3 experts)</option>
<option value="2">cbot-flash (top-1, small KB)</option>
</select>
</label>
<label>Context
<select id="context">
<option value="0">unlimited</option>
<option value="20">20 turns</option>
<option value="10">10 turns</option>
<option value="5">5 turns</option>
</select>
</label>
<label><input type="checkbox" id="why"> Show all proposals</label>
</header>
<main>
<div class="log" id="log" aria-live="polite">
<p class="hint" id="hint">Try
<code>what's 15% of 80</code> <code>5 miles in km</code> <code>capital of Peru</code>
<code>who wrote Dune</code> <code>my name is Sam</code> <code>what time is it in Tokyo</code>
<code>tell me a joke</code></p>
</div>
</main>
<form id="form" autocomplete="off">
<div class="row">
<label for="input" style="position:absolute;left:-9999px">Message</label>
<input id="input" placeholder="Loading…" maxlength="2000" disabled>
<button id="send" disabled>Send</button>
<button type="button" class="secondary" id="reset" disabled>Reset</button>
</div>
</form>
<script>
"use strict";
const $ = (id) => document.getElementById(id);
const log = $("log"), input = $("input"), send = $("send"), reset = $("reset");
const worker = new Worker("worker.js");
let busy = false;
function add(cls, text) {
const d = document.createElement("div");
d.className = "msg " + cls;
d.textContent = text;
log.appendChild(d);
d.scrollIntoView({ block: "end" });
return d;
}
function ready(on) {
input.disabled = send.disabled = reset.disabled = !on;
input.placeholder = on ? "Say something…" : "Loading…";
if (on) input.focus();
}
function init() {
ready(false);
worker.postMessage({ type: "init", variant: +$("variant").value, context: +$("context").value, maxMemory: 0 });
}
worker.onmessage = (ev) => {
const m = ev.data;
if (m.type === "ready") { ready(true); return; }
if (m.type === "error") { add("bot", "Error: " + m.message); busy = false; return; }
if (m.type !== "turn") return;
busy = false;
const r = m.result;
const d = add("bot", r.reply);
const meta = document.createElement("div");
meta.className = "meta";
meta.innerHTML = `<b></b> · ${r.ms.toFixed(2)} ms · ${r.run}/${r.total} experts ran`;
meta.querySelector("b").textContent = r.module;
d.appendChild(meta);
if ($("why").checked && r.candidates.length) {
const w = document.createElement("div");
w.className = "why";
for (const c of r.candidates) {
const row = document.createElement("div");
if (c.chosen) row.className = "chosen";
for (const t of [c.chosen ? "✓" : "", c.module, String(c.score), c.reply]) {
const s = document.createElement("span");
s.textContent = t;
row.appendChild(s);
}
w.appendChild(row);
}
d.appendChild(w);
}
d.scrollIntoView({ block: "end" });
};
$("form").onsubmit = (e) => {
e.preventDefault();
const text = input.value.trim();
if (!text || busy) return;
busy = true;
$("hint")?.remove();
add("you", text);
input.value = "";
worker.postMessage({ type: "turn", text });
};
reset.onclick = () => {
worker.postMessage({ type: "reset" });
log.replaceChildren();
add("bot", "Conversation reset.");
};
$("variant").onchange = $("context").onchange = () => { log.replaceChildren(); init(); };
log.addEventListener("click", (e) => {
if (e.target.tagName === "CODE") { input.value = e.target.textContent; input.focus(); }
});
init();
</script>
</body>
</html>