Spaces:
Running on Zero
Running on Zero
automate what an arm needs, toggle what is taste: decode follows the arm (task arms greedy — measured off-distribution punctuation collapse under sampling), examples come from the arm own training frames, empty replies explain themselves; toggles for auto-decode, family examples, and an off-by-default degenerate-run cut
Browse files
app.py
CHANGED
|
@@ -482,6 +482,9 @@ def status_md(step, arm_path=CORE_ONLY):
|
|
| 482 |
+ (f" \n*{arm['behavior']}*" if arm.get("behavior") else "")
|
| 483 |
+ f" \n**Template:** {FRAME_TAG.get(t['frame'], t['frame'])}"
|
| 484 |
f" · turn ends on {t['turn_end']}"
|
|
|
|
|
|
|
|
|
|
| 485 |
+ (f" \n⚠️ {t['note']}" if t.get("note") else ""))
|
| 486 |
else:
|
| 487 |
arm_line = (f"**Arm:** none attached — raw core"
|
|
@@ -515,14 +518,46 @@ def status_md(step, arm_path=CORE_ONLY):
|
|
| 515 |
]))
|
| 516 |
|
| 517 |
|
| 518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
"""Repopulate the arm picker for the newly selected core: its own
|
| 520 |
arms only, defaulting to its chat arm if it has one and to the bare
|
| 521 |
core if it does not."""
|
| 522 |
step = int(step)
|
| 523 |
val = default_arm(step)
|
| 524 |
return (gr.update(choices=arm_choices(step), value=val),
|
| 525 |
-
|
| 526 |
|
| 527 |
|
| 528 |
# ------------------------------------------------------------ generation
|
|
@@ -541,7 +576,7 @@ def _fit(prompt, max_new, context):
|
|
| 541 |
|
| 542 |
@spaces.GPU(duration=120)
|
| 543 |
def _stream(prompt, max_new, temperature, top_p, use_cache, arm_path,
|
| 544 |
-
ckpt_step, stop=()):
|
| 545 |
"""Yields (text, stats).
|
| 546 |
|
| 547 |
Runs inside a ZeroGPU ForkProcess: this body executes in a forked
|
|
@@ -592,11 +627,21 @@ def _stream(prompt, max_new, temperature, top_p, use_cache, arm_path,
|
|
| 592 |
if cut >= 0:
|
| 593 |
yield text[:cut], stats
|
| 594 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
yield text, stats
|
| 596 |
|
| 597 |
|
| 598 |
def complete(prompt, max_new, temperature, top_p, use_cache, arm_path,
|
| 599 |
-
ckpt_step, request: gr.Request):
|
| 600 |
if not prompt or not prompt.strip():
|
| 601 |
yield "(enter a prompt)", ""
|
| 602 |
return
|
|
@@ -608,7 +653,7 @@ def complete(prompt, max_new, temperature, top_p, use_cache, arm_path,
|
|
| 608 |
try:
|
| 609 |
for text, stats in _stream(prompt, max_new, temperature, top_p,
|
| 610 |
use_cache, arm_path, ckpt_step,
|
| 611 |
-
stop=stops):
|
| 612 |
yield prompt + text, stats
|
| 613 |
done = True
|
| 614 |
finally:
|
|
@@ -642,7 +687,7 @@ def _transcript(history, message):
|
|
| 642 |
|
| 643 |
|
| 644 |
def chat(message, history, max_new, temperature, top_p, use_cache, arm_path,
|
| 645 |
-
ckpt_step, request: gr.Request):
|
| 646 |
if not message or not message.strip():
|
| 647 |
yield list(history or []), "", ""
|
| 648 |
return
|
|
@@ -663,12 +708,25 @@ def chat(message, history, max_new, temperature, top_p, use_cache, arm_path,
|
|
| 663 |
list(tmpl["stops"]) + ["\n" + USER_TAG.strip(),
|
| 664 |
"\n" + ASSISTANT_TAG,
|
| 665 |
USER_TAG.strip(), ASSISTANT_TAG]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
done = False
|
| 667 |
try:
|
| 668 |
for text, stats in _stream(transcript, max_new, temperature, top_p,
|
| 669 |
use_cache, arm_path, ckpt_step,
|
| 670 |
-
stop=stops):
|
| 671 |
-
history[-1]["content"] = _finish_reply(text, stops) or
|
| 672 |
yield history, "", stats
|
| 673 |
done = True
|
| 674 |
finally:
|
|
@@ -754,17 +812,30 @@ with gr.Blocks(title="Beatrix — AlephLLM chat") as demo:
|
|
| 754 |
label="Temperature")
|
| 755 |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 756 |
use_cache = gr.Checkbox(value=True, label="KV cache")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
stats_md = gr.Markdown("")
|
| 758 |
with gr.Tab("Chat"):
|
| 759 |
gr.Markdown(LOG_NOTE)
|
| 760 |
chatbot = gr.Chatbot(label="Beatrix", height=420)
|
| 761 |
msg = gr.Textbox(label="Message", placeholder="Say hello to Beatrix…")
|
| 762 |
-
gr.Examples(
|
| 763 |
-
|
| 764 |
-
inputs=msg)
|
| 765 |
msg.submit(chat,
|
| 766 |
[msg, chatbot, max_new, temperature, top_p, use_cache,
|
| 767 |
-
arm_pick, ckpt],
|
| 768 |
[chatbot, msg, stats_md])
|
| 769 |
gr.ClearButton([chatbot, msg])
|
| 770 |
with gr.Tab("Completion"):
|
|
@@ -774,11 +845,14 @@ with gr.Blocks(title="Beatrix — AlephLLM chat") as demo:
|
|
| 774 |
gr.Button("Complete", variant="primary").click(
|
| 775 |
complete,
|
| 776 |
[prompt, max_new, temperature, top_p, use_cache, arm_pick,
|
| 777 |
-
ckpt],
|
| 778 |
[comp_out, stats_md])
|
| 779 |
-
# arm list follows the core
|
| 780 |
-
# checkpoint
|
| 781 |
-
|
| 782 |
-
|
|
|
|
|
|
|
|
|
|
| 783 |
|
| 784 |
demo.launch()
|
|
|
|
| 482 |
+ (f" \n*{arm['behavior']}*" if arm.get("behavior") else "")
|
| 483 |
+ f" \n**Template:** {FRAME_TAG.get(t['frame'], t['frame'])}"
|
| 484 |
f" · turn ends on {t['turn_end']}"
|
| 485 |
+
+ (f" \n**Decode:** T={_decode_of(arm)[0]:g}, "
|
| 486 |
+
f"top-p {_decode_of(arm)[1]:g} — {_decode_of(arm)[2]}"
|
| 487 |
+
if _decode_of(arm)[2] else "")
|
| 488 |
+ (f" \n⚠️ {t['note']}" if t.get("note") else ""))
|
| 489 |
else:
|
| 490 |
arm_line = (f"**Arm:** none attached — raw core"
|
|
|
|
| 518 |
]))
|
| 519 |
|
| 520 |
|
| 521 |
+
CORE_EX = [["Hello!"], ["Who are you?"], ["What can you do?"],
|
| 522 |
+
["How are you today?"], ["Tell me about books."]]
|
| 523 |
+
|
| 524 |
+
|
| 525 |
+
def _decode_of(arm):
|
| 526 |
+
d = (arm or {}).get("decode") or {}
|
| 527 |
+
return (float(d.get("temperature", 0.7)), float(d.get("top_p", 0.95)),
|
| 528 |
+
d.get("why", ""))
|
| 529 |
+
|
| 530 |
+
|
| 531 |
+
def _examples_of(arm):
|
| 532 |
+
ex = (arm or {}).get("examples")
|
| 533 |
+
return [[e] for e in ex] if ex else CORE_EX
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
def on_arm(step, arm_path, auto_decode, fam_examples):
|
| 537 |
+
"""Selecting an arm re-tunes what the arm needs and shows what it
|
| 538 |
+
knows: a task arm collapses to turn-end off-distribution and turns
|
| 539 |
+
to punctuation under sampling (measured), so it is served greedy
|
| 540 |
+
and offered its own training-frame prompts. Both automations are
|
| 541 |
+
switchable — the checkboxes leave the controls alone when off."""
|
| 542 |
+
arm = ARM_BY_PATH.get(arm_path or "")
|
| 543 |
+
if arm and arm["step"] != int(step):
|
| 544 |
+
arm = None
|
| 545 |
+
t, p, _ = _decode_of(arm)
|
| 546 |
+
return (status_md(step, arm_path),
|
| 547 |
+
gr.update(value=t) if auto_decode else gr.update(),
|
| 548 |
+
gr.update(value=p) if auto_decode else gr.update(),
|
| 549 |
+
gr.update(samples=_examples_of(arm)) if fam_examples
|
| 550 |
+
else gr.update())
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
def on_checkpoint(step, auto_decode, fam_examples):
|
| 554 |
"""Repopulate the arm picker for the newly selected core: its own
|
| 555 |
arms only, defaulting to its chat arm if it has one and to the bare
|
| 556 |
core if it does not."""
|
| 557 |
step = int(step)
|
| 558 |
val = default_arm(step)
|
| 559 |
return (gr.update(choices=arm_choices(step), value=val),
|
| 560 |
+
*on_arm(step, val, auto_decode, fam_examples))
|
| 561 |
|
| 562 |
|
| 563 |
# ------------------------------------------------------------ generation
|
|
|
|
| 576 |
|
| 577 |
@spaces.GPU(duration=120)
|
| 578 |
def _stream(prompt, max_new, temperature, top_p, use_cache, arm_path,
|
| 579 |
+
ckpt_step, stop=(), guard=False):
|
| 580 |
"""Yields (text, stats).
|
| 581 |
|
| 582 |
Runs inside a ZeroGPU ForkProcess: this body executes in a forked
|
|
|
|
| 627 |
if cut >= 0:
|
| 628 |
yield text[:cut], stats
|
| 629 |
return
|
| 630 |
+
# OPTIONAL guard (off by default): a long unbroken run of
|
| 631 |
+
# punctuation is the measured off-distribution collapse, not
|
| 632 |
+
# content. Off by default because masking it would hide a
|
| 633 |
+
# real behaviour from anyone studying the arm.
|
| 634 |
+
if guard and len(out) >= 32:
|
| 635 |
+
tail = text[-24:]
|
| 636 |
+
if tail and all(not (c.isalnum() or c.isspace())
|
| 637 |
+
for c in tail):
|
| 638 |
+
yield text[:-24].rstrip() + " …[degenerate run cut]", stats
|
| 639 |
+
return
|
| 640 |
yield text, stats
|
| 641 |
|
| 642 |
|
| 643 |
def complete(prompt, max_new, temperature, top_p, use_cache, arm_path,
|
| 644 |
+
ckpt_step, guard, request: gr.Request):
|
| 645 |
if not prompt or not prompt.strip():
|
| 646 |
yield "(enter a prompt)", ""
|
| 647 |
return
|
|
|
|
| 653 |
try:
|
| 654 |
for text, stats in _stream(prompt, max_new, temperature, top_p,
|
| 655 |
use_cache, arm_path, ckpt_step,
|
| 656 |
+
stop=stops, guard=bool(guard)):
|
| 657 |
yield prompt + text, stats
|
| 658 |
done = True
|
| 659 |
finally:
|
|
|
|
| 687 |
|
| 688 |
|
| 689 |
def chat(message, history, max_new, temperature, top_p, use_cache, arm_path,
|
| 690 |
+
ckpt_step, guard, request: gr.Request):
|
| 691 |
if not message or not message.strip():
|
| 692 |
yield list(history or []), "", ""
|
| 693 |
return
|
|
|
|
| 708 |
list(tmpl["stops"]) + ["\n" + USER_TAG.strip(),
|
| 709 |
"\n" + ASSISTANT_TAG,
|
| 710 |
USER_TAG.strip(), ASSISTANT_TAG]))
|
| 711 |
+
# an arm that ends the turn instantly is SAYING something — that the
|
| 712 |
+
# prompt is off its distribution. A blank bubble reads as breakage,
|
| 713 |
+
# so the reason is shown instead (measured: task arms return empty
|
| 714 |
+
# at greedy on chat prompts, punctuation soup when sampled).
|
| 715 |
+
if tmpl["frame"] == "qa":
|
| 716 |
+
empty = ("*(this arm ended the turn immediately — it was trained "
|
| 717 |
+
"on single-turn task prompts, not conversation. Try one "
|
| 718 |
+
"of its examples below, or switch arms.)*")
|
| 719 |
+
elif not tmpl["chat_ok"]:
|
| 720 |
+
empty = ("*(representation arm — it shapes internal states and "
|
| 721 |
+
"has no turn behavior. Use the Completion tab.)*")
|
| 722 |
+
else:
|
| 723 |
+
empty = "*(empty reply)*"
|
| 724 |
done = False
|
| 725 |
try:
|
| 726 |
for text, stats in _stream(transcript, max_new, temperature, top_p,
|
| 727 |
use_cache, arm_path, ckpt_step,
|
| 728 |
+
stop=stops, guard=bool(guard)):
|
| 729 |
+
history[-1]["content"] = _finish_reply(text, stops) or empty
|
| 730 |
yield history, "", stats
|
| 731 |
done = True
|
| 732 |
finally:
|
|
|
|
| 812 |
label="Temperature")
|
| 813 |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 814 |
use_cache = gr.Checkbox(value=True, label="KV cache")
|
| 815 |
+
with gr.Row():
|
| 816 |
+
auto_decode = gr.Checkbox(
|
| 817 |
+
value=True, label="Auto decode per arm",
|
| 818 |
+
info="task arms switch to greedy on attach — they degenerate "
|
| 819 |
+
"into punctuation when sampled off-distribution")
|
| 820 |
+
fam_examples = gr.Checkbox(
|
| 821 |
+
value=True, label="Arm-family examples",
|
| 822 |
+
info="offer prompts from the selected arm's own training "
|
| 823 |
+
"frames instead of chat openers")
|
| 824 |
+
guard = gr.Checkbox(
|
| 825 |
+
value=False, label="Cut degenerate runs",
|
| 826 |
+
info="stop a reply once it collapses into unbroken "
|
| 827 |
+
"punctuation (off by default — the collapse is real "
|
| 828 |
+
"behaviour worth seeing)")
|
| 829 |
stats_md = gr.Markdown("")
|
| 830 |
with gr.Tab("Chat"):
|
| 831 |
gr.Markdown(LOG_NOTE)
|
| 832 |
chatbot = gr.Chatbot(label="Beatrix", height=420)
|
| 833 |
msg = gr.Textbox(label="Message", placeholder="Say hello to Beatrix…")
|
| 834 |
+
ex = gr.Examples(_examples_of(ARM_BY_PATH.get(default_arm(STEP))),
|
| 835 |
+
inputs=msg, label="Examples for the selected arm")
|
|
|
|
| 836 |
msg.submit(chat,
|
| 837 |
[msg, chatbot, max_new, temperature, top_p, use_cache,
|
| 838 |
+
arm_pick, ckpt, guard],
|
| 839 |
[chatbot, msg, stats_md])
|
| 840 |
gr.ClearButton([chatbot, msg])
|
| 841 |
with gr.Tab("Completion"):
|
|
|
|
| 845 |
gr.Button("Complete", variant="primary").click(
|
| 846 |
complete,
|
| 847 |
[prompt, max_new, temperature, top_p, use_cache, arm_pick,
|
| 848 |
+
ckpt, guard],
|
| 849 |
[comp_out, stats_md])
|
| 850 |
+
# arm list follows the core (only arms trained on the selected
|
| 851 |
+
# checkpoint, defaulting to its chat arm or nothing); decode and
|
| 852 |
+
# examples then follow the ARM, unless their toggles say otherwise
|
| 853 |
+
ckpt.change(on_checkpoint, [ckpt, auto_decode, fam_examples],
|
| 854 |
+
[arm_pick, status_box, temperature, top_p, ex.dataset])
|
| 855 |
+
arm_pick.change(on_arm, [ckpt, arm_pick, auto_decode, fam_examples],
|
| 856 |
+
[status_box, temperature, top_p, ex.dataset])
|
| 857 |
|
| 858 |
demo.launch()
|