Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,22 @@ theme_css = """
|
|
| 29 |
}
|
| 30 |
body{ background:var(--bg); color:var(--text); }
|
| 31 |
.badge{ display:inline-block; padding:2px 8px; border:1px solid var(--border); border-radius:999px; margin:2px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
/* Make sure the prediction list can receive pointer events even if Plotly expands */
|
| 34 |
.predictions-panel { position: relative; z-index: 5; }
|
|
@@ -224,34 +240,49 @@ def AutoPredictWatcher():
|
|
| 224 |
@solara.component
|
| 225 |
def PredictionsList():
|
| 226 |
df = preds_rx.value
|
| 227 |
-
with solara.Column(gap="6px", style={"maxWidth":"720px"}):
|
| 228 |
solara.Markdown("### Prediction")
|
| 229 |
solara.Text(
|
| 230 |
" # probs token predicted next token",
|
| 231 |
style={
|
| 232 |
-
"color":"var(--muted)",
|
| 233 |
-
"fontFamily":'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
|
| 234 |
},
|
| 235 |
)
|
|
|
|
| 236 |
for i, row in df.iterrows():
|
| 237 |
-
tid = int(row["id"])
|
|
|
|
| 238 |
tok_disp = display_token_from_id(tid)
|
| 239 |
-
|
| 240 |
|
| 241 |
-
#
|
| 242 |
-
solara.
|
| 243 |
-
|
| 244 |
-
classes=["rowbtn"],
|
| 245 |
style={"justifyContent": "flex-start", "width": "100%"},
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
# hover preview (bind several to be robust)
|
| 249 |
on_mouse_enter=lambda *args, tid=tid: preview_token(tid),
|
| 250 |
on_mouse_over=lambda *args, tid=tid: preview_token(tid),
|
| 251 |
on_mouse_move=lambda *args, tid=tid: preview_token(tid),
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
# ---------- Page ----------
|
| 257 |
@solara.component
|
|
@@ -269,23 +300,27 @@ def Page():
|
|
| 269 |
solara.InputText("Enter text", value=text_rx, continuous_update=True, style={"minWidth":"520px"})
|
| 270 |
solara.Markdown(f"*{notice_rx.value}*")
|
| 271 |
|
| 272 |
-
with solara.Row(
|
|
|
|
| 273 |
with solara.Column(classes=["predictions-panel"]):
|
| 274 |
PredictionsList()
|
| 275 |
-
|
|
|
|
| 276 |
with solara.Column(classes=["plot-panel"]):
|
| 277 |
solara.Markdown("### Semantic Neighborhood")
|
| 278 |
if not coords:
|
| 279 |
solara.Markdown("> Embedding map unavailable – add `assets/embeddings/*.json`.")
|
| 280 |
else:
|
| 281 |
solara.FigurePlotly(fig_rx.value)
|
| 282 |
-
|
| 283 |
if neighbor_list_rx.value:
|
| 284 |
solara.Markdown("**Nearest neighbors:**")
|
| 285 |
-
with solara.Row(style={"flex-wrap":"wrap"}):
|
| 286 |
for tok, sim in neighbor_list_rx.value:
|
| 287 |
-
solara.HTML(
|
| 288 |
-
|
|
|
|
|
|
|
| 289 |
|
| 290 |
AutoPredictWatcher()
|
| 291 |
|
|
|
|
| 29 |
}
|
| 30 |
body{ background:var(--bg); color:var(--text); }
|
| 31 |
.badge{ display:inline-block; padding:2px 8px; border:1px solid var(--border); border-radius:999px; margin:2px; }
|
| 32 |
+
/* Highlight hovered prediction token */
|
| 33 |
+
.badge:hover {
|
| 34 |
+
background: var(--primary);
|
| 35 |
+
color: white;
|
| 36 |
+
border-color: var(--primary);
|
| 37 |
+
cursor: pointer;
|
| 38 |
+
transition: all 0.2s ease;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
/* Optional: style an active hovered token */
|
| 42 |
+
.badge.hovered {
|
| 43 |
+
background: var(--primary);
|
| 44 |
+
color: white;
|
| 45 |
+
border-color: var(--primary);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
|
| 49 |
/* Make sure the prediction list can receive pointer events even if Plotly expands */
|
| 50 |
.predictions-panel { position: relative; z-index: 5; }
|
|
|
|
| 240 |
@solara.component
|
| 241 |
def PredictionsList():
|
| 242 |
df = preds_rx.value
|
| 243 |
+
with solara.Column(gap="6px", style={"maxWidth": "720px"}):
|
| 244 |
solara.Markdown("### Prediction")
|
| 245 |
solara.Text(
|
| 246 |
" # probs token predicted next token",
|
| 247 |
style={
|
| 248 |
+
"color": "var(--muted)",
|
| 249 |
+
"fontFamily": 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
|
| 250 |
},
|
| 251 |
)
|
| 252 |
+
|
| 253 |
for i, row in df.iterrows():
|
| 254 |
+
tid = int(row["id"])
|
| 255 |
+
prob = row["probs"]
|
| 256 |
tok_disp = display_token_from_id(tid)
|
| 257 |
+
row_label = fmt_row(i, prob, tid, tok_disp)
|
| 258 |
|
| 259 |
+
# Wrapper DIV catches pointer/hover; Button handles click (and also hooks hover)
|
| 260 |
+
with solara.Div(
|
| 261 |
+
classes=["rowbtn"], # styling on the wrapper
|
|
|
|
| 262 |
style={"justifyContent": "flex-start", "width": "100%"},
|
| 263 |
+
attributes={"tabindex": "0", "role": "button"},
|
| 264 |
+
# ---- HOVER = preview neighborhood (wrapper) ----
|
|
|
|
| 265 |
on_mouse_enter=lambda *args, tid=tid: preview_token(tid),
|
| 266 |
on_mouse_over=lambda *args, tid=tid: preview_token(tid),
|
| 267 |
on_mouse_move=lambda *args, tid=tid: preview_token(tid),
|
| 268 |
+
on_pointer_enter=lambda *args, tid=tid: preview_token(tid),
|
| 269 |
+
on_pointer_move=lambda *args, tid=tid: preview_token(tid),
|
| 270 |
+
on_focus=lambda *args, tid=tid: preview_token(tid), # keyboard focus
|
| 271 |
+
):
|
| 272 |
+
solara.Button(
|
| 273 |
+
row_label,
|
| 274 |
+
classes=[], # keep wrapper styled; leave button unstyled
|
| 275 |
+
style={"justifyContent": "flex-start", "width": "100%"},
|
| 276 |
+
# ---- CLICK = append token to text ----
|
| 277 |
+
on_click=lambda *args, tid=tid: append_token(tid),
|
| 278 |
+
# Redundant hover hooks on the button too (helps on some builds)
|
| 279 |
+
on_mouse_enter=lambda *args, tid=tid: preview_token(tid),
|
| 280 |
+
on_mouse_over=lambda *args, tid=tid: preview_token(tid),
|
| 281 |
+
on_mouse_move=lambda *args, tid=tid: preview_token(tid),
|
| 282 |
+
on_pointer_enter=lambda *args, tid=tid: preview_token(tid),
|
| 283 |
+
on_pointer_move=lambda *args, tid=tid: preview_token(tid),
|
| 284 |
+
on_focus=lambda *args, tid=tid: preview_token(tid),
|
| 285 |
+
)
|
| 286 |
|
| 287 |
# ---------- Page ----------
|
| 288 |
@solara.component
|
|
|
|
| 300 |
solara.InputText("Enter text", value=text_rx, continuous_update=True, style={"minWidth":"520px"})
|
| 301 |
solara.Markdown(f"*{notice_rx.value}*")
|
| 302 |
|
| 303 |
+
with solara.Row(classes=["app-row"]):
|
| 304 |
+
# Left column: predictions list (fixed width, sits above plot for events)
|
| 305 |
with solara.Column(classes=["predictions-panel"]):
|
| 306 |
PredictionsList()
|
| 307 |
+
|
| 308 |
+
# Right column: plot + neighbor chips
|
| 309 |
with solara.Column(classes=["plot-panel"]):
|
| 310 |
solara.Markdown("### Semantic Neighborhood")
|
| 311 |
if not coords:
|
| 312 |
solara.Markdown("> Embedding map unavailable – add `assets/embeddings/*.json`.")
|
| 313 |
else:
|
| 314 |
solara.FigurePlotly(fig_rx.value)
|
| 315 |
+
|
| 316 |
if neighbor_list_rx.value:
|
| 317 |
solara.Markdown("**Nearest neighbors:**")
|
| 318 |
+
with solara.Row(style={"flex-wrap": "wrap"}):
|
| 319 |
for tok, sim in neighbor_list_rx.value:
|
| 320 |
+
solara.HTML(
|
| 321 |
+
tag="span",
|
| 322 |
+
unsafe_innerHTML=f'<span class="badge">{tok} {(sim*100):.1f}%</span>',
|
| 323 |
+
)
|
| 324 |
|
| 325 |
AutoPredictWatcher()
|
| 326 |
|