File size: 13,858 Bytes
561f65d
 
44777fd
 
561f65d
44777fd
561f65d
931a76f
 
bec627d
387f325
3937a09
44777fd
 
 
d233a16
44777fd
bec627d
 
44777fd
561f65d
 
 
44777fd
 
931a76f
 
4f5ef71
 
561f65d
519919a
4f5ef71
c76dc8d
 
519919a
4f5ef71
 
 
 
f8ca812
4f5ef71
931a76f
4f5ef71
 
 
 
 
775aa53
676ee71
4f5ef71
 
 
775aa53
4f5ef71
931a76f
0e43a26
4f5ef71
c76dc8d
 
 
 
 
 
 
 
 
 
 
 
4f5ef71
 
 
 
 
 
931a76f
 
44777fd
4f5ef71
44777fd
4f5ef71
44777fd
bec627d
 
 
f8ca812
d233a16
44777fd
bec627d
561f65d
 
931a76f
561f65d
 
 
 
 
 
bec627d
 
561f65d
 
bec627d
561f65d
44777fd
bec627d
775aa53
 
 
0e43a26
775aa53
 
bec627d
44777fd
775aa53
 
44777fd
775aa53
f8ca812
44777fd
 
561f65d
 
44777fd
 
561f65d
44777fd
561f65d
 
 
 
44777fd
561f65d
bec627d
931a76f
 
 
44777fd
931a76f
 
561f65d
 
931a76f
 
 
08c4a63
 
 
44777fd
08c4a63
44777fd
 
5e6de6d
44777fd
561f65d
 
931a76f
 
 
 
bec627d
931a76f
 
561f65d
4f5ef71
561f65d
 
931a76f
561f65d
 
3937a09
931a76f
561f65d
f8ca812
931a76f
 
 
f8ca812
561f65d
931a76f
 
d233a16
 
561f65d
931a76f
d233a16
 
 
 
561f65d
 
d233a16
 
931a76f
d233a16
561f65d
bec627d
 
561f65d
 
bec627d
931a76f
561f65d
bec627d
931a76f
 
 
 
 
 
 
bec627d
931a76f
 
561f65d
 
44777fd
931a76f
44777fd
bec627d
f8ca812
 
 
 
 
561f65d
931a76f
44777fd
931a76f
 
bec627d
 
561f65d
44777fd
 
931a76f
 
 
 
 
 
 
 
 
 
 
 
bec627d
931a76f
 
 
 
 
 
 
 
 
 
 
f8ca812
931a76f
44777fd
 
b373af6
 
44777fd
c76dc8d
b373af6
 
 
44777fd
b373af6
 
 
 
 
44777fd
c76dc8d
 
 
b373af6
44777fd
b373af6
 
 
 
c76dc8d
 
 
 
 
b373af6
 
 
 
 
 
 
 
 
c76dc8d
b373af6
 
 
 
 
 
 
44777fd
b373af6
 
 
44777fd
 
b373af6
 
c76dc8d
44777fd
 
b373af6
44777fd
931a76f
 
44777fd
c9b807e
 
 
4f5ef71
c9b807e
 
 
 
 
519919a
44777fd
 
c9b807e
 
44777fd
c9b807e
d233a16
 
772abbd
 
 
 
 
 
 
 
 
44777fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931a76f
bec627d
387f325
 
931a76f
561f65d
c76dc8d
561f65d
 
4f5ef71
 
 
561f65d
44777fd
bec627d
561f65d
519919a
65a9c4c
931a76f
44777fd
65a9c4c
931a76f
 
 
 
 
44777fd
931a76f
 
44777fd
931a76f
2a55a01
 
 
 
d233a16
2a55a01
561f65d
f8ca812
561f65d
44777fd
bec627d
931a76f
bec627d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# app.py
import json
import threading
import time
from pathlib import Path

import solara
import pandas as pd
import plotly.graph_objects as go
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# for robust hover/click from the browser
import anywidget
import traitlets as t
import html  # for escaping token text in the HTML label


# ---------- Model ----------
MODEL_ID = "Qwen/Qwen3-0.6B"  # same as the working HF Space
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID)


# ---------- Theme & layout (light blue / white / black accents) ----------
theme_css = """
:root{
  --primary:#38bdf8;  --bg:#ffffff;  --text:#0b0f14;  --muted:#6b7280;  --border:#e5e7eb;
  --mono:'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace';
}

/* Base */
body{ background:var(--bg); color:var(--text); margin:0;}
h1{ margin:6px 0 8px; }   

/* Two-column layout */
.app-row { display:flex; align-items:flex-start; gap:16px; }        /* was 24px */
.predictions-panel { flex:0 0 320px; position:relative; z-index:10;}/* was 360px */
.plot-panel        { flex:1 1 auto; position:relative; z-index:1; overflow:hidden; }

/* Prediction rows (tighter) */
.rowbtn{
  width:100%;
  padding:6px 10px;               /* was 10px 12px */
  border-radius:10px;             /* was 12px */
  border:1px solid var(--border);
  background:#fff; color:var(--text);
  display:flex; justify-content:flex-start; align-items:center;
  text-align:left; cursor:pointer; user-select:none;
  font-family: var(--mono);
  font-size:13px;                 /* was default ~14–16 */
  line-height:1.15;
  letter-spacing:.2px;
  margin-bottom:6px;              /* explicit, keeps list consistent */
}
.rowbtn:hover{ background:#f7fbff; border-color:#c3e8fb; }

/* New: 4-column grid inside each row button */
.rowbtn-grid{
  display:grid;
  grid-template-columns: 28px 72px 72px 1fr; /* # | probs | tokenID | token */
  column-gap:8px;
  align-items:center;
  width:100%;
  font-family: var(--mono);
  font-size:13px;
  line-height:1.15;
}

/* Neighbor chips (smaller) */
.badge{
  display:inline-block; padding:2px 6px;       /* was 2px 8px */
  border:1px solid var(--border); border-radius:999px; margin:2px;
  font-size:12px; line-height:1.15;
}
"""



# ---------- Reactive state ----------
text_rx = solara.reactive("Twinkle, twinkle, little ")
preds_rx = solara.reactive(pd.DataFrame(columns=["probs", "id", "tok"]))
selected_token_id_rx = solara.reactive(None)
neighbor_list_rx = solara.reactive([])
last_hovered_id_rx = solara.reactive(None)
auto_running_rx = solara.reactive(True)
neigh_msg_rx = solara.reactive("")  # message shown when no neighborhood is available

# ---------- Embedding assets ----------
ASSETS = Path("assets/embeddings")
COORDS_PATH = ASSETS / "pca_top5k_coords.json"
NEIGH_PATH  = ASSETS / "neighbors_top5k_k40.json"

coords = {}
neighbors = {}
ids_set = set()

if COORDS_PATH.exists() and NEIGH_PATH.exists():
    coords = json.loads(COORDS_PATH.read_text("utf-8"))
    neighbors = json.loads(NEIGH_PATH.read_text("utf-8"))
    ids_set = set(map(int, coords.keys()))
else:
    notice_rx.set("Embedding files not found β€” add assets/embeddings/*.json to enable the map.")


# ---------- Helpers ----------
def display_token_from_id(tid: int) -> str:
    toks = tokenizer.convert_ids_to_tokens([int(tid)], skip_special_tokens=True)
    t = toks[0] if toks else ""
    for lead in ("▁", "Δ "):
        if t.startswith(lead):
            t = t[len(lead):]
    t = t.replace("\n","↡")
    return t if t.strip() else "␠"

def fmt_row(idx: int, prob: str, tid: int, tok_disp: str) -> str:
    # columns: index, probability, token id, token text
    return f"{idx:<2}  {prob:<7}  {tid:<6}  {tok_disp}"


# ---------- Prediction ----------
def predict_top10(prompt: str) -> pd.DataFrame:
    if not prompt:
        return pd.DataFrame(columns=["probs", "id", "tok"])
    tokens = tokenizer(prompt, return_tensors="pt", padding=False)
    out = model.generate(
        **tokens,
        max_new_tokens=1,
        output_scores=True,
        return_dict_in_generate=True,
        pad_token_id=tokenizer.eos_token_id,
        do_sample=False,  # greedy; temp/top_k are ignored (by design)
    )
    scores = torch.softmax(out.scores[0], dim=-1)
    topk = torch.topk(scores, 10)
    ids = [int(topk.indices[0, i]) for i in range(10)]
    probs = [float(topk.values[0, i]) for i in range(10)]
    toks = [tokenizer.decode([i]) for i in ids]  # for append
    df = pd.DataFrame({"probs": probs, "id": ids, "tok": toks})
    df["probs"] = df["probs"].map(lambda p: f"{p:.2%}")
    return df

def on_predict():
    df = predict_top10(text_rx.value)
    preds_rx.set(df)
    if len(df) == 0:
        return
    if selected_token_id_rx.value is None:
        preview_token(int(df.iloc[0]["id"]))   # only first time
    else:
        fig_rx.set(highlight(int(selected_token_id_rx.value)))  # preserve selection


# ---------- Plot / neighborhood ----------
def base_scatter():
    fig = go.Figure()
    if coords:
        xs, ys = zip(*[coords[k] for k in coords.keys()])
        fig.add_trace(go.Scattergl(
            x=xs, y=ys, mode="markers",
            marker=dict(size=3, opacity=1.0, color="rgba(56,189,248,0.15)"),
            hoverinfo="skip",
        ))
    fig.update_layout(
        height=380, margin=dict(l=6,r=6,t=6,b=6),
        paper_bgcolor="white", plot_bgcolor="white",
        xaxis=dict(visible=False), yaxis=dict(visible=False),
        showlegend=False,
    )
    return fig

fig_rx = solara.reactive(base_scatter())

def get_neighbor_list(token_id: int, k: int = 20):
    if not ids_set or token_id not in ids_set:
        return []
    raw = neighbors.get("neighbors", {}).get(str(token_id), [])
    return raw[:k]

def highlight(token_id: int):
    fig = base_scatter()

    # Not in map (or missing map) β†’ clear chips and show message
    if not coords or token_id not in ids_set:
        neighbor_list_rx.set([])
        if not coords:
            neigh_msg_rx.set("Embedding map unavailable – add `assets/embeddings/*.json`.")
        else:
            neigh_msg_rx.set("Neighborhood unavailable for this token (not in the top-5k set).")
        return fig

    # In map β†’ clear message and draw neighbors/target
    neigh_msg_rx.set("")
    nbrs = get_neighbor_list(token_id, k=20)

    if nbrs:
        nx = [coords[str(nid)][0] for nid,_ in nbrs]
        ny = [coords[str(nid)][1] for nid,_ in nbrs]
        fig.add_trace(go.Scattergl(
            x=nx, y=ny, mode="markers",
            marker=dict(size=6, color="rgba(56,189,248,0.75)"),
            hoverinfo="skip",
        ))
        chips = [(display_token_from_id(int(nid)), float(sim)) for nid,sim in nbrs]
        neighbor_list_rx.set(chips)
    else:
        neighbor_list_rx.set([])

    tx, ty = coords[str(token_id)]
    fig.add_trace(go.Scattergl(
        x=[tx], y=[ty], mode="markers",
        marker=dict(size=10, color="rgba(34,211,238,1.0)", line=dict(width=1)),
        hoverinfo="skip",
    ))
    return fig


def preview_token(token_id: int):
    # print("preview ->", token_id)  # enable for debugging in Space logs
    token_id = int(token_id)
    if last_hovered_id_rx.value == token_id:
        return
    last_hovered_id_rx.set(token_id)
    selected_token_id_rx.set(token_id)
    fig_rx.set(highlight(token_id))

def append_token(token_id: int):
    # print("append ->", token_id)
    decoded = tokenizer.decode([int(token_id)])
    text_rx.set(text_rx.value + decoded)
    preview_token(int(token_id))
    on_predict()


# ---------- Debounced auto-predict ----------
@solara.component
def AutoPredictWatcher():
    text = text_rx.value
    auto = auto_running_rx.value

    def effect():
        if not auto:
            return
        cancelled = False
        snap = text

        def worker():
            time.sleep(0.25)
            if not cancelled and snap == text_rx.value:
                on_predict()

        threading.Thread(target=worker, daemon=True).start()

        def cleanup():
            nonlocal cancelled
            cancelled = True
        return cleanup

    solara.use_effect(effect, [text, auto])
    return solara.Text("", style={"display": "none"})


# ---------- Hover-enabled list (browser) ----------
class HoverList(anywidget.AnyWidget):
    """
    Renders the prediction rows in the browser and streams hover/click events
    back to Python via synced traitlets. Supports HTML row labels via `label_html`.
    """
    _esm = """
    export function render({ model, el }) {
      const renderList = () => {
        const items = model.get('items') || [];
        el.innerHTML = "";
        const wrap = document.createElement('div');
        wrap.style.display = 'flex';
        wrap.style.flexDirection = 'column';

        items.forEach((item) => {
          const { tid, label, label_html } = item;

          const btn = document.createElement('button');
          btn.className = 'rowbtn';
          btn.setAttribute('type', 'button');
          btn.setAttribute('role', 'button');
          btn.setAttribute('tabindex', '0');

          // Prefer HTML layout if provided; fall back to plain text
          if (label_html) { btn.innerHTML = label_html; }
          else            { btn.textContent = label || ""; }

          // Hover β†’ preview (bind several events for reliability)
          const preview = () => {
            model.set('hovered_id', tid);
            model.save_changes();
          };
          btn.addEventListener('mouseenter', preview);
          btn.addEventListener('mouseover',  preview);
          btn.addEventListener('mousemove',  preview);
          btn.addEventListener('focus',      preview);

          // Click β†’ append
          btn.addEventListener('click', () => {
            model.set('clicked_id', tid);
            model.save_changes();
          });

          wrap.appendChild(btn);
        });

        el.appendChild(wrap);
      };

      renderList();
      model.on('change:items', renderList);
    }
    """
    items      = t.List(trait=t.Dict()).tag(sync=True)   # [{tid:int, label?:str, label_html?:str}, ...]
    hovered_id = t.Int(allow_none=True).tag(sync=True)
    clicked_id = t.Int(allow_none=True).tag(sync=True)

# ---------- Predictions list (uses HoverList) ----------
@solara.component
def PredictionsList():
    df = preds_rx.value
    with solara.Column(gap="6px", style={"maxWidth": "720px"}):
        solara.Markdown("### Prediction")
        solara.Text(
            " #  probs    tokenID  next predicted",
            style={
                "color": "var(--muted)",
                "fontFamily": 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
            },
        )

        # Build items for the browser widget
        items = []
        for i, row in df.iterrows():
            tid = int(row["id"])
            prob = row["probs"]                 # already a formatted string like "4.12%"
            tok_disp = display_token_from_id(tid)
            tok_safe = html.escape(tok_disp)   # protect the HTML label

            label_html = (
                f'<div class="rowbtn-grid">'
                f'  <span class="c0">{i}</span>'
                f'  <span class="c1">{prob}</span>'
                f'  <span class="c2">{tid}</span>'
                f'  <span class="c3">{tok_safe}</span>'
                f'</div>'
            )
            items.append({"tid": tid, "label_html": label_html})   # <-- note label_html

        w = HoverList()
        w.items = items

        # Hover β†’ preview (updates plot + neighbor chips)
        def _on_hover(change):
            tid = change["new"]
            if tid is not None:
                preview_token(int(tid))
        w.observe(_on_hover, names="hovered_id")

        # Click β†’ append
        def _on_click(change):
            tid = change["new"]
            if tid is not None:
                append_token(int(tid))
        w.observe(_on_click, names="clicked_id")

        solara.display(w)


# ---------- Page ----------
@solara.component
def Page():
    solara.Style(theme_css)

    with solara.Column(margin=8, gap="10px"):
        solara.Markdown("# Next-Token Predictor + Semantic Neighborhood")
        solara.Markdown(
            "Type text to see AI's top predictions for the next token. "
            "Click a token to append it to your text. "
            "Hover over a token to preview its **semantic neighborhood**."
        )

        solara.InputText("Enter text", value=text_rx, continuous_update=True, style={"minWidth":"520px"})

        with solara.Row(classes=["app-row"]):
            with solara.Column(classes=["predictions-panel"]):
                PredictionsList()

            with solara.Column(classes=["plot-panel"]):
                solara.Markdown("### Semantic Neighborhood")
                if not coords:
                    solara.Markdown("> Embedding map unavailable – add `assets/embeddings/*.json`.")
                else:
                    solara.FigurePlotly(fig_rx.value)

                if neighbor_list_rx.value:
                    solara.Markdown("**Nearest neighbors:**")
                    with solara.Row(style={"flex-wrap":"wrap"}):
                        for tok, sim in neighbor_list_rx.value:
                            solara.HTML(
                                tag="span",
                                unsafe_innerHTML=f'<span class="badge">{tok} &nbsp; {(sim*100):.1f}%</span>'
                            )
                elif neigh_msg_rx.value:
                    solara.Text(neigh_msg_rx.value, style={"color":"var(--muted)"})

        AutoPredictWatcher()


# ---------- Kickoff ----------
on_predict()
Page()