Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ hr{ border-color:var(--border); }
|
|
| 33 |
width:100%; padding:10px 12px; border-radius:12px;
|
| 34 |
border:1px solid var(--border); background:#fff; color:var(--text);
|
| 35 |
display:flex; justify-content:flex-start; align-items:center;
|
| 36 |
-
text-align:left; cursor:pointer;
|
| 37 |
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
| 38 |
letter-spacing:.2px;
|
| 39 |
}
|
|
@@ -43,8 +43,8 @@ hr{ border-color:var(--border); }
|
|
| 43 |
# ------------------ App state ------------------
|
| 44 |
text_rx = solara.reactive("twinkle, twinkle, little ")
|
| 45 |
preds_rx = solara.reactive(pd.DataFrame(columns=["probs", "id", "tok"]))
|
| 46 |
-
selected_token_id_rx = solara.reactive(None)
|
| 47 |
-
neighbor_list_rx = solara.reactive([])
|
| 48 |
notice_rx = solara.reactive("Click a candidate (or hover to preview).")
|
| 49 |
auto_running_rx = solara.reactive(True)
|
| 50 |
last_hovered_id_rx = solara.reactive(None)
|
|
@@ -103,7 +103,7 @@ def predict_top10(prompt: str) -> pd.DataFrame:
|
|
| 103 |
topk = torch.topk(scores, 10)
|
| 104 |
ids = [int(topk.indices[0, i]) for i in range(10)]
|
| 105 |
probs = [float(topk.values[0, i]) for i in range(10)]
|
| 106 |
-
toks = [tokenizer.decode([i]) for i in ids] #
|
| 107 |
df = pd.DataFrame({"probs": probs, "id": ids, "tok": toks})
|
| 108 |
df["probs"] = df["probs"].map(lambda p: f"{p:.2%}")
|
| 109 |
return df
|
|
@@ -114,11 +114,9 @@ def on_predict():
|
|
| 114 |
preds_rx.set(df)
|
| 115 |
if len(df) == 0:
|
| 116 |
return
|
| 117 |
-
# Only auto-select the top-1 if we don't have a selection yet.
|
| 118 |
if selected_token_id_rx.value is None:
|
| 119 |
preview_token(int(df.iloc[0]["id"]))
|
| 120 |
else:
|
| 121 |
-
# Keep highlighting whatever the user last hovered/clicked.
|
| 122 |
fig_rx.set(highlight(int(selected_token_id_rx.value)))
|
| 123 |
|
| 124 |
# ------------------ Plotly figure ------------------
|
|
@@ -180,6 +178,8 @@ def highlight(token_id: int):
|
|
| 180 |
|
| 181 |
def preview_token(token_id: int):
|
| 182 |
token_id = int(token_id)
|
|
|
|
|
|
|
| 183 |
if last_hovered_id_rx.value == token_id:
|
| 184 |
return
|
| 185 |
last_hovered_id_rx.set(token_id)
|
|
@@ -190,9 +190,8 @@ def append_token(token_id: int):
|
|
| 190 |
# keep decode() here so spacing stays correct in the prompt
|
| 191 |
decoded = tokenizer.decode([int(token_id)])
|
| 192 |
text_rx.set(text_rx.value + decoded)
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
on_predict() # refresh next tokens but preserve highlight
|
| 196 |
|
| 197 |
# ------------------ Auto-predict on typing (debounced) ------------------
|
| 198 |
@solara.component
|
|
@@ -221,7 +220,7 @@ def AutoPredictWatcher():
|
|
| 221 |
solara.use_effect(effect, [text, auto])
|
| 222 |
return solara.Text("", style={"display": "none"})
|
| 223 |
|
| 224 |
-
# ------------------ UI:
|
| 225 |
@solara.component
|
| 226 |
def PredictionsList():
|
| 227 |
df = preds_rx.value
|
|
@@ -240,17 +239,19 @@ def PredictionsList():
|
|
| 240 |
tok_disp = display_token_from_id(tid)
|
| 241 |
label = fmt_row(i, prob, tid, tok_disp)
|
| 242 |
|
| 243 |
-
#
|
| 244 |
-
solara.
|
| 245 |
-
label,
|
| 246 |
-
on_click=lambda tid=tid: append_token(tid), # click to append
|
| 247 |
-
on_mouse_enter=lambda e=None, tid=tid: preview_token(tid), # hover preview
|
| 248 |
-
on_mouse_over=lambda e=None, tid=tid: preview_token(tid), # extra safety
|
| 249 |
-
on_mouse_move=lambda e=None, tid=tid: preview_token(tid), # tracks movement
|
| 250 |
-
on_focus=lambda e=None, tid=tid: preview_token(tid), # keyboard focus
|
| 251 |
classes=["rowbtn"],
|
| 252 |
style={"justifyContent": "flex-start", "width": "100%"},
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
# ------------------ Page ------------------
|
| 256 |
@solara.component
|
|
@@ -294,4 +295,4 @@ def Page():
|
|
| 294 |
|
| 295 |
# Seed initial predictions and mount
|
| 296 |
on_predict()
|
| 297 |
-
Page()
|
|
|
|
| 33 |
width:100%; padding:10px 12px; border-radius:12px;
|
| 34 |
border:1px solid var(--border); background:#fff; color:var(--text);
|
| 35 |
display:flex; justify-content:flex-start; align-items:center;
|
| 36 |
+
text-align:left; cursor:pointer; user-select:none;
|
| 37 |
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
| 38 |
letter-spacing:.2px;
|
| 39 |
}
|
|
|
|
| 43 |
# ------------------ App state ------------------
|
| 44 |
text_rx = solara.reactive("twinkle, twinkle, little ")
|
| 45 |
preds_rx = solara.reactive(pd.DataFrame(columns=["probs", "id", "tok"]))
|
| 46 |
+
selected_token_id_rx = solara.reactive(None) # currently highlighted token id
|
| 47 |
+
neighbor_list_rx = solara.reactive([]) # [(tok_display, sim), ...]
|
| 48 |
notice_rx = solara.reactive("Click a candidate (or hover to preview).")
|
| 49 |
auto_running_rx = solara.reactive(True)
|
| 50 |
last_hovered_id_rx = solara.reactive(None)
|
|
|
|
| 103 |
topk = torch.topk(scores, 10)
|
| 104 |
ids = [int(topk.indices[0, i]) for i in range(10)]
|
| 105 |
probs = [float(topk.values[0, i]) for i in range(10)]
|
| 106 |
+
toks = [tokenizer.decode([i]) for i in ids] # for append; display uses display_token_from_id
|
| 107 |
df = pd.DataFrame({"probs": probs, "id": ids, "tok": toks})
|
| 108 |
df["probs"] = df["probs"].map(lambda p: f"{p:.2%}")
|
| 109 |
return df
|
|
|
|
| 114 |
preds_rx.set(df)
|
| 115 |
if len(df) == 0:
|
| 116 |
return
|
|
|
|
| 117 |
if selected_token_id_rx.value is None:
|
| 118 |
preview_token(int(df.iloc[0]["id"]))
|
| 119 |
else:
|
|
|
|
| 120 |
fig_rx.set(highlight(int(selected_token_id_rx.value)))
|
| 121 |
|
| 122 |
# ------------------ Plotly figure ------------------
|
|
|
|
| 178 |
|
| 179 |
def preview_token(token_id: int):
|
| 180 |
token_id = int(token_id)
|
| 181 |
+
# TEMP DEBUG: verify hover fires in Space logs
|
| 182 |
+
print("preview ->", token_id)
|
| 183 |
if last_hovered_id_rx.value == token_id:
|
| 184 |
return
|
| 185 |
last_hovered_id_rx.set(token_id)
|
|
|
|
| 190 |
# keep decode() here so spacing stays correct in the prompt
|
| 191 |
decoded = tokenizer.decode([int(token_id)])
|
| 192 |
text_rx.set(text_rx.value + decoded)
|
| 193 |
+
preview_token(int(token_id)) # keep highlight on clicked token
|
| 194 |
+
on_predict() # refresh predictions, preserve selection
|
|
|
|
| 195 |
|
| 196 |
# ------------------ Auto-predict on typing (debounced) ------------------
|
| 197 |
@solara.component
|
|
|
|
| 220 |
solara.use_effect(effect, [text, auto])
|
| 221 |
return solara.Text("", style={"display": "none"})
|
| 222 |
|
| 223 |
+
# ------------------ UI: rows as Div (hover + click here) ------------------
|
| 224 |
@solara.component
|
| 225 |
def PredictionsList():
|
| 226 |
df = preds_rx.value
|
|
|
|
| 239 |
tok_disp = display_token_from_id(tid)
|
| 240 |
label = fmt_row(i, prob, tid, tok_disp)
|
| 241 |
|
| 242 |
+
# One Div per row: both hover and click handlers live here.
|
| 243 |
+
with solara.Div(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
classes=["rowbtn"],
|
| 245 |
style={"justifyContent": "flex-start", "width": "100%"},
|
| 246 |
+
attributes={"tabindex": "0", "role": "button"},
|
| 247 |
+
on_click=lambda e=None, tid=tid: append_token(tid), # click to append
|
| 248 |
+
on_mouse_enter=lambda e=None, tid=tid: preview_token(tid), # hover preview
|
| 249 |
+
on_mouse_over=lambda e=None, tid=tid: preview_token(tid),
|
| 250 |
+
on_mouse_move=lambda e=None, tid=tid: preview_token(tid),
|
| 251 |
+
on_pointer_enter=lambda e=None, tid=tid: preview_token(tid), # pointer events (more reliable)
|
| 252 |
+
on_focus=lambda e=None, tid=tid: preview_token(tid), # keyboard
|
| 253 |
+
):
|
| 254 |
+
solara.Text(label)
|
| 255 |
|
| 256 |
# ------------------ Page ------------------
|
| 257 |
@solara.component
|
|
|
|
| 295 |
|
| 296 |
# Seed initial predictions and mount
|
| 297 |
on_predict()
|
| 298 |
+
Page()
|