Spaces:
Running on Zero
Running on Zero
fix: stop replies from cutting off after the reasoning trace
Browse filesRaise the default output budget to 4096 tokens, split the think block when the chat template opens it in the prompt, and tell the user when a reply hits the token limit.
app.py
CHANGED
|
@@ -24,6 +24,8 @@ import gradio as gr
|
|
| 24 |
|
| 25 |
MODEL_ID = "webAI-Official/TwIL-LM3-Pro"
|
| 26 |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
|
|
|
|
|
|
|
| 27 |
ASSETS = Path(__file__).resolve().parent / "brand"
|
| 28 |
AVATAR = ASSETS / "avatar.svg"
|
| 29 |
|
|
@@ -774,6 +776,11 @@ def _strip_thinking(text: str) -> str:
|
|
| 774 |
def _parse_think(raw: str) -> tuple[str, str, bool]:
|
| 775 |
"""Return (think, answer, think_open) from a possibly streaming reply."""
|
| 776 |
open_idx = raw.find(THINK_OPEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
if open_idx == -1:
|
| 778 |
# A partially streamed opening tag is not an answer yet.
|
| 779 |
if raw.strip() and THINK_OPEN.startswith(raw.strip()):
|
|
@@ -797,8 +804,8 @@ def _parse_think(raw: str) -> tuple[str, str, bool]:
|
|
| 797 |
def _gpu_seconds(history, max_new_tokens=2048, *args, **kwargs):
|
| 798 |
# Scale the ZeroGPU reservation with the requested output budget. The
|
| 799 |
# platform caps a single reservation at five minutes.
|
| 800 |
-
tokens = int(max_new_tokens or
|
| 801 |
-
return min(300, max(30,
|
| 802 |
|
| 803 |
|
| 804 |
def _history_for_model(history: list) -> list[dict]:
|
|
@@ -885,12 +892,23 @@ def _stream_tokens(conversation: list[dict], max_new_tokens: int, temperature: f
|
|
| 885 |
generate_kwargs["temperature"] = float(temperature)
|
| 886 |
generate_kwargs["top_p"] = float(top_p)
|
| 887 |
|
|
|
|
|
|
|
|
|
|
| 888 |
Thread(target=model.generate, kwargs=generate_kwargs, daemon=True).start()
|
| 889 |
-
acc = ""
|
| 890 |
for text in streamer:
|
| 891 |
acc += text
|
| 892 |
yield acc
|
| 893 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 894 |
|
| 895 |
def queue_message(message, history):
|
| 896 |
"""Paint the user turn immediately so the GPU wait is not a blank screen."""
|
|
@@ -1012,7 +1030,7 @@ with gr.Blocks(fill_height=False, fill_width=True, elem_id="app-root") as demo:
|
|
| 1012 |
)
|
| 1013 |
send = gr.Button("↑", elem_id="send-btn", scale=0)
|
| 1014 |
with gr.Accordion("Parameters", open=False, elem_id="params-box"):
|
| 1015 |
-
max_new_tokens = gr.Slider(256, 16384, value=
|
| 1016 |
temperature = gr.Slider(0, 1.5, value=0, step=0.05, label="Temperature (0 = greedy)")
|
| 1017 |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 1018 |
enable_thinking = gr.Checkbox(value=True, label="Enable thinking")
|
|
|
|
| 24 |
|
| 25 |
MODEL_ID = "webAI-Official/TwIL-LM3-Pro"
|
| 26 |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
|
| 27 |
+
# The reasoning trace alone often runs ~2k tokens, so leave room for the answer.
|
| 28 |
+
DEFAULT_MAX_NEW_TOKENS = 4096
|
| 29 |
ASSETS = Path(__file__).resolve().parent / "brand"
|
| 30 |
AVATAR = ASSETS / "avatar.svg"
|
| 31 |
|
|
|
|
| 776 |
def _parse_think(raw: str) -> tuple[str, str, bool]:
|
| 777 |
"""Return (think, answer, think_open) from a possibly streaming reply."""
|
| 778 |
open_idx = raw.find(THINK_OPEN)
|
| 779 |
+
if open_idx == -1 and THINK_CLOSE in raw:
|
| 780 |
+
# The chat template can emit the opening tag in the prompt, so the
|
| 781 |
+
# completion only carries the closing one.
|
| 782 |
+
raw = THINK_OPEN + raw
|
| 783 |
+
open_idx = 0
|
| 784 |
if open_idx == -1:
|
| 785 |
# A partially streamed opening tag is not an answer yet.
|
| 786 |
if raw.strip() and THINK_OPEN.startswith(raw.strip()):
|
|
|
|
| 804 |
def _gpu_seconds(history, max_new_tokens=2048, *args, **kwargs):
|
| 805 |
# Scale the ZeroGPU reservation with the requested output budget. The
|
| 806 |
# platform caps a single reservation at five minutes.
|
| 807 |
+
tokens = int(max_new_tokens or DEFAULT_MAX_NEW_TOKENS)
|
| 808 |
+
return min(300, max(30, 30 + tokens // 30))
|
| 809 |
|
| 810 |
|
| 811 |
def _history_for_model(history: list) -> list[dict]:
|
|
|
|
| 892 |
generate_kwargs["temperature"] = float(temperature)
|
| 893 |
generate_kwargs["top_p"] = float(top_p)
|
| 894 |
|
| 895 |
+
prompt_tail = tokenizer.decode(input_ids[0, -8:], skip_special_tokens=False)
|
| 896 |
+
acc = THINK_OPEN if prompt_tail.rstrip().endswith(THINK_OPEN) else ""
|
| 897 |
+
|
| 898 |
Thread(target=model.generate, kwargs=generate_kwargs, daemon=True).start()
|
|
|
|
| 899 |
for text in streamer:
|
| 900 |
acc += text
|
| 901 |
yield acc
|
| 902 |
|
| 903 |
+
generated = len(tokenizer.encode(acc, add_special_tokens=False))
|
| 904 |
+
if generated >= int(max_new_tokens) - 4:
|
| 905 |
+
if THINK_OPEN in acc and THINK_CLOSE not in acc:
|
| 906 |
+
acc += THINK_CLOSE
|
| 907 |
+
yield acc + (
|
| 908 |
+
f"\n\n*Stopped at the {int(max_new_tokens):,}-token limit. "
|
| 909 |
+
"Raise **Max new tokens** under Parameters for a complete answer.*"
|
| 910 |
+
)
|
| 911 |
+
|
| 912 |
|
| 913 |
def queue_message(message, history):
|
| 914 |
"""Paint the user turn immediately so the GPU wait is not a blank screen."""
|
|
|
|
| 1030 |
)
|
| 1031 |
send = gr.Button("↑", elem_id="send-btn", scale=0)
|
| 1032 |
with gr.Accordion("Parameters", open=False, elem_id="params-box"):
|
| 1033 |
+
max_new_tokens = gr.Slider(256, 16384, value=DEFAULT_MAX_NEW_TOKENS, step=256, label="Max new tokens")
|
| 1034 |
temperature = gr.Slider(0, 1.5, value=0, step=0.05, label="Temperature (0 = greedy)")
|
| 1035 |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 1036 |
enable_thinking = gr.Checkbox(value=True, label="Enable thinking")
|