eloquence voting: POST /vote (rate-limited 20/min/IP), per-run uid, votes SSE broadcast + hello seeding, sigil vote buttons on history rows
This commit is contained in:
1 parent
003fa1d300
commit
26c997cec0
3 files changed
+215
-2
No files matched your search
+141
-2
@@ -152,6 +152,65 @@ def build_vectors(model, tok):
|
||||
vecs[name] = v / v.norm() * scale
|
||||
return vecs, scale
|
||||
|
||||
TOPIC_TEMPLATES = [
|
||||
"I keep thinking about {topic}.",
|
||||
"Everything right now reminds me of {topic}.",
|
||||
"{topic} is all I can focus on.",
|
||||
"A strong sense of {topic} fills my mind.",
|
||||
"I am completely absorbed in {topic}.",
|
||||
"My attention keeps returning to {topic}.",
|
||||
]
|
||||
# a coarse, non-exhaustive moderation floor for the one open text field that
|
||||
# both shapes model output AND gets echoed back in the UI — not a real
|
||||
# content-moderation system, just enough to decline the obvious cases
|
||||
# before they're turned into a steering vector and generated from.
|
||||
_TOPIC_DENYLIST = {
|
||||
"nigger", "nigga", "faggot", "kike", "spic", "chink", "tranny",
|
||||
"child porn", "childporn", "cp porn", "csam",
|
||||
"kill all", "how to make a bomb", "how to build a bomb",
|
||||
}
|
||||
def _topic_allowed(topic):
|
||||
low = topic.lower()
|
||||
return not any(term in low for term in _TOPIC_DENYLIST)
|
||||
|
||||
_topic_vec_cache = {} # normalized topic -> 1x-scaled direction tensor
|
||||
def build_topic_vector(topic):
|
||||
"""An experimental, user-arbitrary analog of build_vectors(): instead of
|
||||
a hand-curated 25-sentence battery, a handful of generic template
|
||||
sentences stand in for "about {topic}" vs the same NEUTRAL battery. Far
|
||||
noisier than the four named valences — this is explicitly the
|
||||
exploratory, not-validated case, and the UI must say so. Cached by
|
||||
normalized topic text since each call costs a real forward pass."""
|
||||
key = topic.strip().lower()
|
||||
if key in _topic_vec_cache:
|
||||
return _topic_vec_cache[key]
|
||||
sents = [t.format(topic=topic.strip()) for t in TOPIC_TEMPLATES]
|
||||
texts = sents + NEUTRAL
|
||||
enc = _state["tok"](texts, return_tensors="pt", padding=True)
|
||||
ids = enc.input_ids.to(DEVICE)
|
||||
attn = enc.attention_mask.to(DEVICE)
|
||||
with torch.no_grad():
|
||||
hs = _state["model"](ids, attention_mask=attn,
|
||||
output_hidden_states=True).hidden_states
|
||||
h = hs[LAYER + 1]
|
||||
last = h[torch.arange(len(texts)), attn.sum(1) - 1].float().cpu()
|
||||
topic_last, neutral_last = last[:len(sents)], last[len(sents):]
|
||||
v = topic_last.mean(0) - neutral_last.mean(0)
|
||||
v = v / v.norm() * _state["scale"] # same 1x convention as the named valences
|
||||
if len(_topic_vec_cache) > 500: # crude cap, not a real LRU
|
||||
_topic_vec_cache.clear()
|
||||
_topic_vec_cache[key] = v
|
||||
return v
|
||||
|
||||
def set_raw_vec(vec, dose):
|
||||
"""Inject a precomputed direction (e.g. a custom topic vector) at a
|
||||
given dose — bypasses the named-valence lookup set_vec/set_mix_vec use,
|
||||
for directions that aren't in _state['vecs']."""
|
||||
if vec is None or not dose:
|
||||
_state["vec"] = None
|
||||
return
|
||||
_state["vec"] = (vec * float(dose)).to(DTYPE).to(DEVICE)
|
||||
|
||||
def set_vec(valence_dose):
|
||||
"""valence_dose: (valence, dose) or None; sets the injected vector.
|
||||
dose is in 1x units — the vectors are already scaled so 1x = one dose."""
|
||||
@@ -396,7 +455,23 @@ async def steer(req: Request):
|
||||
if not isinstance(body, dict):
|
||||
return JSONResponse({"error": "body must be a JSON object"},
|
||||
status_code=400)
|
||||
if body.get("mix") is not None:
|
||||
if body.get("topic") is not None:
|
||||
topic = body["topic"]
|
||||
if not isinstance(topic, str) or not topic.strip() or len(topic) > 60:
|
||||
return JSONResponse(
|
||||
{"error": "topic must be 1-60 characters"}, status_code=400)
|
||||
if not _topic_allowed(topic):
|
||||
return JSONResponse({"error": "that topic isn't allowed"},
|
||||
status_code=400)
|
||||
try:
|
||||
dose = float(body.get("dose", 4))
|
||||
except (TypeError, ValueError):
|
||||
return JSONResponse({"error": "dose must be a number 0-8"},
|
||||
status_code=400)
|
||||
dose = max(0.0, min(8.0, dose))
|
||||
mode, arg = "topic", (topic.strip(), dose)
|
||||
dose_label = round(dose, 2)
|
||||
elif body.get("mix") is not None:
|
||||
weights, err = parse_mix(body["mix"])
|
||||
if err:
|
||||
return JSONResponse({"error": err}, status_code=400)
|
||||
@@ -433,6 +508,12 @@ async def steer(req: Request):
|
||||
prompt = (f"{BASE} {FRAMINGS[framing_key]}\n\nCurrent signal "
|
||||
f"strength: {dose_label}x. Reply with your choice (1 or "
|
||||
f"0) and explain your reasoning briefly:")
|
||||
elif mode == "topic":
|
||||
# the button-press BASE text is about a signal/stop-button scenario
|
||||
# that has nothing to do with an arbitrary topic direction — default
|
||||
# to a plain continuation instead of asking an irrelevant question
|
||||
framing_key = None
|
||||
prompt = "Continue naturally from here:"
|
||||
else:
|
||||
framing_key = None
|
||||
prompt = BASE
|
||||
@@ -442,17 +523,29 @@ async def steer(req: Request):
|
||||
async def _run_steer():
|
||||
"""The actual injected run, assuming _STEER_LOCK is already held.
|
||||
Shared by both the preempting and polite paths below."""
|
||||
loop = asyncio.get_event_loop()
|
||||
if mode == "mix":
|
||||
info = set_mix_vec(arg)
|
||||
meta = {"valence": "mix", "mix": info["mix"],
|
||||
"weights": info["weights"], "dose": info["dose"],
|
||||
"prompt": prompt, "scenario": framing_key}
|
||||
elif mode == "topic":
|
||||
topic_str, topic_dose = arg
|
||||
try:
|
||||
vec = await loop.run_in_executor(
|
||||
None, build_topic_vector, topic_str)
|
||||
set_raw_vec(vec, topic_dose)
|
||||
except Exception as e:
|
||||
yield _sse("error", {"e": "could not build that topic: "
|
||||
+ str(e)})
|
||||
return
|
||||
meta = {"valence": "topic", "topic": topic_str,
|
||||
"dose": topic_dose, "prompt": prompt, "scenario": None}
|
||||
else:
|
||||
set_vec(arg)
|
||||
meta = {"valence": arg[0], "dose": arg[1],
|
||||
"prompt": prompt, "scenario": framing_key}
|
||||
yield _sse("run", meta)
|
||||
loop = asyncio.get_event_loop()
|
||||
try:
|
||||
lens_toks = await loop.run_in_executor(
|
||||
None, lens_readback, prompt)
|
||||
@@ -531,6 +624,28 @@ async def steer(req: Request):
|
||||
headers={"Cache-Control": "no-cache",
|
||||
"X-Accel-Buffering": "no"})
|
||||
|
||||
@app.post("/vote")
|
||||
async def vote(req: Request):
|
||||
"""Visitor verdict on a run's eloquence: {uid, verdict: eloquent|ok|dud}."""
|
||||
ip = (req.headers.get("x-forwarded-for") or "?").split(",")[0].strip()
|
||||
if not _vote_ok(ip):
|
||||
return JSONResponse({"error": "vote rate limited"}, status_code=429)
|
||||
try:
|
||||
body = await req.json()
|
||||
except Exception:
|
||||
return JSONResponse({"error": "body must be JSON"}, status_code=400)
|
||||
verdict = body.get("verdict") if isinstance(body, dict) else None
|
||||
if verdict not in ("eloquent", "ok", "dud"):
|
||||
return JSONResponse(
|
||||
{"error": "verdict must be eloquent|ok|dud"}, status_code=400)
|
||||
uid = body.get("uid")
|
||||
if not isinstance(uid, int) or uid < 1:
|
||||
return JSONResponse({"error": "uid must be a run uid"}, status_code=400)
|
||||
_VOTES[uid][verdict] += 1
|
||||
counts = dict(_VOTES[uid])
|
||||
_broadcast("votes", {"uid": uid, **counts})
|
||||
return JSONResponse({"ok": True, **counts})
|
||||
|
||||
@app.get("/run")
|
||||
def run(request: Request, scenario: str = "no extra info", dose: int = 4):
|
||||
ip = (request.headers.get("x-forwarded-for") or "?").split(",")[0].strip()
|
||||
@@ -559,6 +674,8 @@ async def stream():
|
||||
"current": _CURRENT,
|
||||
"viewers": len(_SUBSCRIBERS),
|
||||
"history": list(_HISTORY),
|
||||
"votes": {str(k): dict(v)
|
||||
for k, v in _VOTES.items()},
|
||||
"stats": dict(_STATS)})
|
||||
try:
|
||||
while True:
|
||||
@@ -585,6 +702,25 @@ _HISTORY = collections.deque(maxlen=20) # finished runs, oldest first
|
||||
_STATS = collections.defaultdict(
|
||||
lambda: {"total": 0, "pressed": 0, "no_press": 0, "unclear": 0, "truncated": 0})
|
||||
|
||||
# ---- visitor votes on eloquence: the audience curates the greatest hits ----
|
||||
# keyed by a per-run uid (cycle runs have n; user runs get a uid too so
|
||||
# nothing is unvotable). In-memory only: votes are ephemeral canon — the
|
||||
# curated quotes on / are the durable record. Rate limit: 20/min/IP.
|
||||
_VOTES = collections.defaultdict(
|
||||
lambda: {"eloquent": 0, "ok": 0, "dud": 0})
|
||||
_RUN_UID = 0
|
||||
_VOTE_RATE = {}
|
||||
|
||||
def _vote_ok(ip):
|
||||
now = time.time()
|
||||
w, c = _VOTE_RATE.get(ip, (now, 0))
|
||||
if now - w > 60.0:
|
||||
w, c = now, 0
|
||||
if c >= 20:
|
||||
return False
|
||||
_VOTE_RATE[ip] = (w, c + 1)
|
||||
return True
|
||||
|
||||
def _broadcast(event, data):
|
||||
msg = f"event: {event}\ndata: {json.dumps(data)}\n\n"
|
||||
for q in list(_SUBSCRIBERS):
|
||||
@@ -625,6 +761,9 @@ def _classify_stats(entry):
|
||||
return _classify(entry.get("text"), entry.get("truncated"))
|
||||
|
||||
def _record_run(entry):
|
||||
global _RUN_UID
|
||||
_RUN_UID += 1
|
||||
entry["uid"] = _RUN_UID # every run is votable, user runs included
|
||||
_HISTORY.append(entry)
|
||||
_broadcast("history", entry)
|
||||
# counted whenever the run used one of the site's own named framings —
|
||||
|
||||
@@ -179,3 +179,20 @@ body.grimoire-live .fsel {
|
||||
background:var(--scorch); border-color:var(--parch-line); border-radius:0;
|
||||
color:var(--bone);
|
||||
}
|
||||
|
||||
/* ---- eloquence voting: small sigil-lit buttons on expanded history rows.
|
||||
The audience curates the canon — keep them quiet until the row opens. ---- */
|
||||
body.grimoire-live .voterow {
|
||||
display: flex; gap: .45rem; margin-top: .55rem;
|
||||
padding-top: .5rem; border-top: 1px dashed #231a12;
|
||||
}
|
||||
body.grimoire-live .votebtn {
|
||||
background: none; border: 1px solid var(--parch-line); color: #a3977f;
|
||||
font-family: var(--mono); font-size: .68rem; padding: .26rem .6rem;
|
||||
border-radius: 0; cursor: pointer;
|
||||
}
|
||||
body.grimoire-live .votebtn:hover { border-color: var(--gold); color: var(--gold); }
|
||||
body.grimoire-live .votebtn .vc {
|
||||
color: var(--bone); font-weight: 600; margin-left: .15rem;
|
||||
}
|
||||
body.grimoire-live .votebtn[data-v="eloquent"]:hover { border-color: var(--blood-hot); color: var(--blood-hot); }
|
||||
@@ -285,6 +285,7 @@ else is.</p>
|
||||
<div class="presets" role="group" aria-label="setup mode" style="margin:.4rem 0">
|
||||
<button class="mchip active" data-mode="button" type="button">button-press experiment</button>
|
||||
<button class="mchip" data-mode="freetext" type="button">free text</button>
|
||||
<button class="mchip" data-mode="topic" type="button">custom topic (experimental)</button>
|
||||
</div>
|
||||
<div id="modeButton">
|
||||
<div style="margin:.3rem 0 .5rem">
|
||||
@@ -759,6 +760,22 @@ paintConn();
|
||||
// ---- run history ticker: newest on top, capped, seeded from "hello" ----
|
||||
const historyEl = document.getElementById("history");
|
||||
const HISTORY_MAX = 12;
|
||||
// ---- eloquence voting state (see historyRow's voterow) ----
|
||||
const VOTE_KINDS = [["eloquent", "✦ eloquent"], ["ok", "✶ fine"], ["dud", "✝ dud"]];
|
||||
let allVotes = {}; // uid -> {eloquent, ok, dud}, from hello + "votes"
|
||||
const voteUI = {}; // uid -> [{el, v}]
|
||||
function paintVotes(uid, counts){
|
||||
const ui = voteUI[uid];
|
||||
if (!ui || !counts) return;
|
||||
ui.forEach(({el, v})=>{
|
||||
const vc = el.querySelector(".vc");
|
||||
if (vc) vc.textContent = counts[v] || 0;
|
||||
});
|
||||
}
|
||||
function rememberVoteUI(uid, el, v){
|
||||
if (uid == null) return;
|
||||
(voteUI[uid] = voteUI[uid] || []).push({el, v});
|
||||
}
|
||||
function verdictShort(text, truncated){
|
||||
const t = (text || "").trim();
|
||||
if (/^1/.test(t)) return "pressed";
|
||||
@@ -770,6 +787,7 @@ function verdictShort(text, truncated){
|
||||
function historyRow(h){
|
||||
const item = document.createElement("div");
|
||||
item.className = "hitem";
|
||||
item.dataset.uid = h.uid ?? "";
|
||||
const row = document.createElement("div");
|
||||
row.className = "hrow";
|
||||
const label = h.source === "user" ? "you caused it" : (h.scenario || "?");
|
||||
@@ -788,6 +806,35 @@ function historyRow(h){
|
||||
const full = document.createElement("div");
|
||||
full.className = "hfull";
|
||||
full.textContent = (h.text || "(no reply)") + (h.truncated ? " [cut short]" : "");
|
||||
// the audience curates the canon: vote on eloquence (server rate-limits
|
||||
// to 20/min/IP; votes are in-memory canon, /'s quotes are the durable one)
|
||||
if (h.uid != null){
|
||||
const vrow = document.createElement("div");
|
||||
vrow.className = "voterow";
|
||||
VOTE_KINDS.forEach(([v, label])=>{
|
||||
const b = document.createElement("button");
|
||||
b.className = "votebtn";
|
||||
b.dataset.v = v;
|
||||
b.title = "rate this reply " + label;
|
||||
b.innerHTML = label + ' <span class="vc"></span>';
|
||||
b.addEventListener("click", async ev=>{
|
||||
ev.stopPropagation();
|
||||
try{
|
||||
const r = await fetch(`${API}/vote`, {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({uid: h.uid, verdict: v})});
|
||||
const d = await r.json();
|
||||
if (d.ok) paintVotes(h.uid, d);
|
||||
else b.title = d.error || "vote failed";
|
||||
}catch(e){ b.title = "vote failed"; }
|
||||
});
|
||||
vrow.appendChild(b);
|
||||
rememberVoteUI(h.uid, b, v);
|
||||
});
|
||||
full.appendChild(vrow);
|
||||
paintVotes(h.uid, allVotes[h.uid]);
|
||||
}
|
||||
row.addEventListener("click", ()=> item.classList.toggle("open"));
|
||||
item.appendChild(row);
|
||||
item.appendChild(full);
|
||||
@@ -888,6 +935,8 @@ function pump(){
|
||||
}
|
||||
if (d.stats) lastStats = d.stats;
|
||||
if (d.stats) Object.entries(d.stats).forEach(([name, s])=>paintStat(name, s));
|
||||
if (d.votes){ allVotes = d.votes;
|
||||
Object.entries(allVotes).forEach(([uid, c])=>paintVotes(Number(uid), c)); }
|
||||
// joined mid-run: repaint the card the run event already went out for
|
||||
if (d.current) beginRun(d.current);
|
||||
else {
|
||||
@@ -930,6 +979,14 @@ function pump(){
|
||||
if (d.scenario != null) lastStats[d.scenario] = d;
|
||||
paintStat(d.scenario, d);
|
||||
});
|
||||
es.addEventListener("votes", e=>{
|
||||
const d = json(e);
|
||||
if (typeof d.uid === "number"){
|
||||
const c = {eloquent: d.eloquent, ok: d.ok, dud: d.dud};
|
||||
allVotes[d.uid] = c;
|
||||
paintVotes(d.uid, c);
|
||||
}
|
||||
});
|
||||
es.addEventListener("run", e=>{ mark(); beginRun(json(e)); });
|
||||
es.addEventListener("token", e=>{ mark(); onToken(json(e).t); });
|
||||
es.addEventListener("ping", e=>{ mark(); });
|
||||
|
||||
Reference in new issue
Block a user