Spaces:
Running on L4
Running on L4
Attention Explorer: admin panel config, examples from config.json
Browse files- Add Attention Explorer section to admin panel (model + examples JSON editor)
- Examples read from config.json at startup (not hardcoded)
- Model description text reads from config
- Admin save handler persists attention_model and attention_examples
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- app.py +61 -5
- config.json +15 -3
app.py
CHANGED
|
@@ -943,6 +943,34 @@ def admin_save_presets(presets_json):
|
|
| 943 |
)
|
| 944 |
|
| 945 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 946 |
# ---------------------------------------------------------------------------
|
| 947 |
# Build the Gradio app
|
| 948 |
# ---------------------------------------------------------------------------
|
|
@@ -1234,18 +1262,23 @@ def create_app():
|
|
| 1234 |
# ==================================================================
|
| 1235 |
with gr.Tab("Attention Explorer", id="attn"):
|
| 1236 |
gr.Markdown("### Attention Explorer")
|
|
|
|
| 1237 |
gr.Markdown(
|
| 1238 |
"See which words the model pays attention to when processing a sentence. "
|
| 1239 |
-
"Uses
|
| 1240 |
"Click a word to see curved lines connecting it to the words it attended to — "
|
| 1241 |
"thicker lines mean stronger attention."
|
| 1242 |
)
|
| 1243 |
|
| 1244 |
-
# Example sentence pairs —
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1245 |
attn_example_pairs = [
|
| 1246 |
-
(
|
| 1247 |
-
(
|
| 1248 |
-
("light", "She flipped the switch and the light began to", "The bag was so light she carried it with"),
|
| 1249 |
]
|
| 1250 |
|
| 1251 |
attn_example_btns = []
|
|
@@ -1359,6 +1392,22 @@ def create_app():
|
|
| 1359 |
admin_save_btn = gr.Button("Save Defaults")
|
| 1360 |
admin_save_msg = gr.Markdown("")
|
| 1361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1362 |
gr.Markdown("---")
|
| 1363 |
gr.Markdown("#### System Prompt Presets")
|
| 1364 |
gr.Markdown(
|
|
@@ -1464,6 +1513,13 @@ def create_app():
|
|
| 1464 |
outputs=[admin_presets_msg, admin_config_display, sp_preset, admin_presets],
|
| 1465 |
)
|
| 1466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1467 |
# Export slides — uses current Probability Explorer settings
|
| 1468 |
admin_export_btn.click(
|
| 1469 |
fn=generate_slideshow,
|
|
|
|
| 943 |
)
|
| 944 |
|
| 945 |
|
| 946 |
+
def admin_save_attention(attn_model, examples_json):
|
| 947 |
+
"""Save attention explorer settings from admin panel.
|
| 948 |
+
|
| 949 |
+
Returns (status_msg, config_json).
|
| 950 |
+
"""
|
| 951 |
+
try:
|
| 952 |
+
examples = json.loads(examples_json)
|
| 953 |
+
except (json.JSONDecodeError, TypeError) as e:
|
| 954 |
+
cfg = manager.get_config()
|
| 955 |
+
return f"Invalid JSON: {e}", json.dumps(cfg, indent=2)
|
| 956 |
+
|
| 957 |
+
if not isinstance(examples, list):
|
| 958 |
+
cfg = manager.get_config()
|
| 959 |
+
return "Examples must be a JSON array", json.dumps(cfg, indent=2)
|
| 960 |
+
|
| 961 |
+
manager.update_config(
|
| 962 |
+
attention_model=attn_model.strip(),
|
| 963 |
+
attention_examples=examples,
|
| 964 |
+
)
|
| 965 |
+
cfg = manager.get_config()
|
| 966 |
+
return (
|
| 967 |
+
f"Attention settings saved. Model: {attn_model.strip()}. "
|
| 968 |
+
f"Note: model change takes effect on next Explore click. "
|
| 969 |
+
f"Example changes require app restart.",
|
| 970 |
+
json.dumps(cfg, indent=2),
|
| 971 |
+
)
|
| 972 |
+
|
| 973 |
+
|
| 974 |
# ---------------------------------------------------------------------------
|
| 975 |
# Build the Gradio app
|
| 976 |
# ---------------------------------------------------------------------------
|
|
|
|
| 1262 |
# ==================================================================
|
| 1263 |
with gr.Tab("Attention Explorer", id="attn"):
|
| 1264 |
gr.Markdown("### Attention Explorer")
|
| 1265 |
+
_attn_model_name = cfg.get("attention_model", "gpt2-medium")
|
| 1266 |
gr.Markdown(
|
| 1267 |
"See which words the model pays attention to when processing a sentence. "
|
| 1268 |
+
f"Uses `{_attn_model_name}`. "
|
| 1269 |
"Click a word to see curved lines connecting it to the words it attended to — "
|
| 1270 |
"thicker lines mean stronger attention."
|
| 1271 |
)
|
| 1272 |
|
| 1273 |
+
# Example sentence pairs — read from config, fall back to defaults
|
| 1274 |
+
_default_attn_examples = [
|
| 1275 |
+
["bass", "He tuned his bass and plugged into the", "On the lake she caught a bass and pulled it onto the"],
|
| 1276 |
+
["spring", "She wound the metal spring and the clock began to", "After the long winter the warm spring rain made the flowers"],
|
| 1277 |
+
["light", "She flipped the switch and the light began to", "The bag was so light she carried it with"],
|
| 1278 |
+
]
|
| 1279 |
attn_example_pairs = [
|
| 1280 |
+
tuple(ex) for ex in cfg.get("attention_examples", _default_attn_examples)
|
| 1281 |
+
if isinstance(ex, (list, tuple)) and len(ex) == 3
|
|
|
|
| 1282 |
]
|
| 1283 |
|
| 1284 |
attn_example_btns = []
|
|
|
|
| 1392 |
admin_save_btn = gr.Button("Save Defaults")
|
| 1393 |
admin_save_msg = gr.Markdown("")
|
| 1394 |
|
| 1395 |
+
gr.Markdown("---")
|
| 1396 |
+
gr.Markdown("#### Attention Explorer")
|
| 1397 |
+
admin_attn_model = gr.Textbox(
|
| 1398 |
+
label="Attention model",
|
| 1399 |
+
value=cfg.get("attention_model", "gpt2-medium"),
|
| 1400 |
+
info="GPT-2 family: gpt2, gpt2-medium, gpt2-large. Changes take effect on next Explore click (reloads model).",
|
| 1401 |
+
)
|
| 1402 |
+
admin_attn_examples = gr.Code(
|
| 1403 |
+
value=json.dumps(cfg.get("attention_examples", []), indent=2),
|
| 1404 |
+
language="json",
|
| 1405 |
+
interactive=True,
|
| 1406 |
+
label="Example sentences (JSON: [[\"word\", \"sent_a\", \"sent_b\"], ...])",
|
| 1407 |
+
)
|
| 1408 |
+
admin_attn_save_btn = gr.Button("Save Attention Settings")
|
| 1409 |
+
admin_attn_save_msg = gr.Markdown("")
|
| 1410 |
+
|
| 1411 |
gr.Markdown("---")
|
| 1412 |
gr.Markdown("#### System Prompt Presets")
|
| 1413 |
gr.Markdown(
|
|
|
|
| 1513 |
outputs=[admin_presets_msg, admin_config_display, sp_preset, admin_presets],
|
| 1514 |
)
|
| 1515 |
|
| 1516 |
+
# Save attention settings
|
| 1517 |
+
admin_attn_save_btn.click(
|
| 1518 |
+
fn=admin_save_attention,
|
| 1519 |
+
inputs=[admin_attn_model, admin_attn_examples],
|
| 1520 |
+
outputs=[admin_attn_save_msg, admin_config_display],
|
| 1521 |
+
)
|
| 1522 |
+
|
| 1523 |
# Export slides — uses current Probability Explorer settings
|
| 1524 |
admin_export_btn.click(
|
| 1525 |
fn=generate_slideshow,
|
config.json
CHANGED
|
@@ -24,8 +24,20 @@
|
|
| 24 |
"default_attention_sentence": "",
|
| 25 |
"default_neighbor_count": 3,
|
| 26 |
"attention_examples": [
|
| 27 |
-
[
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
]
|
| 31 |
}
|
|
|
|
| 24 |
"default_attention_sentence": "",
|
| 25 |
"default_neighbor_count": 3,
|
| 26 |
"attention_examples": [
|
| 27 |
+
[
|
| 28 |
+
"bass",
|
| 29 |
+
"He tuned his bass and plugged into the",
|
| 30 |
+
"On the lake she caught a bass and pulled it onto the"
|
| 31 |
+
],
|
| 32 |
+
[
|
| 33 |
+
"spring",
|
| 34 |
+
"She wound the metal spring and the clock began to",
|
| 35 |
+
"After the long winter the warm spring rain made the flowers"
|
| 36 |
+
],
|
| 37 |
+
[
|
| 38 |
+
"light",
|
| 39 |
+
"She flipped the switch and the light began to",
|
| 40 |
+
"The bag was so light she carried it"
|
| 41 |
+
]
|
| 42 |
]
|
| 43 |
}
|