File size: 9,213 Bytes
e8e5451
ece3e79
 
e8e5451
 
 
 
 
 
99a0cab
e8e5451
 
 
 
 
8980e37
ece3e79
 
e8e5451
ece3e79
 
e8e5451
ece3e79
e8e5451
8980e37
ece3e79
e8e5451
ece3e79
e8e5451
ece3e79
99a0cab
e8e5451
 
 
 
 
 
 
 
 
 
 
ece3e79
 
 
 
 
 
e8e5451
 
 
99a0cab
e8e5451
 
99a0cab
 
 
e8e5451
 
99a0cab
e8e5451
ece3e79
e8e5451
 
 
99a0cab
 
 
 
ece3e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e5451
 
 
 
 
 
 
 
 
 
ece3e79
 
e8e5451
ece3e79
e8e5451
 
 
 
 
 
 
 
 
 
 
 
 
ece3e79
 
 
 
 
 
e8e5451
 
 
 
ece3e79
 
e8e5451
ece3e79
e8e5451
 
 
 
 
99a0cab
e8e5451
 
 
 
 
 
 
 
 
 
 
ece3e79
e8e5451
 
 
 
 
 
ece3e79
 
e8e5451
 
 
 
 
ece3e79
 
e8e5451
ece3e79
 
e8e5451
 
 
ece3e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e5451
 
ece3e79
 
 
 
 
e8e5451
 
 
 
 
 
 
 
ece3e79
e8e5451
ece3e79
e8e5451
 
 
 
ece3e79
 
 
 
 
e8e5451
ece3e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8e5451
ece3e79
 
 
 
 
e8e5451
 
 
 
8980e37
e8e5451
 
8980e37
e8e5451
 
 
 
 
8980e37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import os
import re
import html
from threading import Thread

import gradio as gr
import spaces
import torch
from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
    TextIteratorStreamer,
)

MODEL_ID = "OrionLLM/GRM-2.6-Opus"
TITLE = "GRM-2.6-Opus"
SUBTITLE = "Chat with GRM-2.6-Opus on ZeroGPU"
DESCRIPTION = (
    "Chat with GRM-2.6-Opus in a ZeroGPU Space, optimized with text-only chat, "
    "NF4 4-bit loading, bounded context, streaming output, and thinking parsing."
)

PLACEHOLDER = (
    "Ask GRM-2.6-Opus for code, debugging, planning, research, long-form reasoning, "
    "terminal-agent tasks, or complex multi-step workflows."
)

MAX_INPUT_TOKENS = 16384
INTERNAL_MAX_NEW_TOKENS = 4096
HF_TOKEN = os.environ.get("HF_TOKEN")

os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
torch.backends.cuda.matmul.allow_tf32 = True

BNB_CONFIG = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
)

tokenizer = AutoTokenizer.from_pretrained(
    MODEL_ID,
    trust_remote_code=True,
    token=HF_TOKEN,
)

if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    trust_remote_code=True,
    token=HF_TOKEN,
    device_map={"": 0},
    dtype=torch.bfloat16,
    quantization_config=BNB_CONFIG,
    attn_implementation="sdpa",
    low_cpu_mem_usage=True,
)

model.eval()


def model_input_device():
    return next(model.parameters()).device


def strip_thinking(text: str) -> str:
    if not text:
        return ""

    text = re.sub(
        r"(?is)<details[^>]*>\s*<summary>.*?</summary>.*?</details>",
        "",
        text,
    )

    text = re.sub(r"(?is)<think>.*?</think>", "", text)
    text = re.sub(r"(?is)<think>.*$", "", text)

    return text.strip()


def render_thinking(raw_text: str) -> str:
    """
    Converts model output like:

    <think>
    reasoning here
    </think>
    final answer here

    into a clean collapsible Thinking block in Gradio.
    Also handles incomplete streaming <think> blocks.
    """
    if not raw_text:
        return ""

    text = raw_text
    lower = text.lower()

    output_parts = []
    pos = 0

    while True:
        start = lower.find("<think>", pos)

        if start == -1:
            answer = text[pos:]
            if answer:
                output_parts.append(answer)
            break

        before = text[pos:start]
        if before:
            output_parts.append(before)

        think_content_start = start + len("<think>")
        end = lower.find("</think>", think_content_start)

        if end == -1:
            thinking = text[think_content_start:]
            thinking = html.escape(thinking.strip())

            output_parts.append(
                "\n\n<details open>"
                "<summary>🧠 Thinking</summary>\n\n"
                f"<pre>{thinking}</pre>\n\n"
                "</details>\n\n"
            )
            break

        thinking = text[think_content_start:end]
        thinking = html.escape(thinking.strip())

        output_parts.append(
            "\n\n<details>"
            "<summary>🧠 Thinking</summary>\n\n"
            f"<pre>{thinking}</pre>\n\n"
            "</details>\n\n"
        )

        pos = end + len("</think>")

    rendered = "".join(output_parts).strip()
    return rendered


def build_messages(history, message):
    messages = []

    trimmed_history = history[-8:]

    for user_text, assistant_text in trimmed_history:
        if user_text:
            messages.append(
                {
                    "role": "user",
                    "content": str(user_text).strip(),
                }
            )

        if assistant_text:
            clean_answer = strip_thinking(str(assistant_text))
            if clean_answer:
                messages.append(
                    {
                        "role": "assistant",
                        "content": clean_answer,
                    }
                )

    messages.append(
        {
            "role": "user",
            "content": message.strip(),
        }
    )

    return messages


def estimate_duration(
    message,
    history,
    enable_thinking,
    preserve_thinking,
    temperature,
    top_p,
    top_k,
    repetition_penalty,
):
    del message, history, enable_thinking, preserve_thinking
    del temperature, top_p, top_k, repetition_penalty

    return 180


@spaces.GPU(duration=estimate_duration, size="large")
def stream_chat(
    message: str,
    history: list,
    enable_thinking: bool,
    preserve_thinking: bool,
    temperature: float,
    top_p: float,
    top_k: int,
    repetition_penalty: float,
):
    if not message or not message.strip():
        yield ""
        return

    messages = build_messages(history, message)

    rendered_prompt = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True,
        enable_thinking=enable_thinking,
        preserve_thinking=preserve_thinking,
    )

    inputs = tokenizer(
        rendered_prompt,
        return_tensors="pt",
        truncation=True,
        max_length=MAX_INPUT_TOKENS,
    ).to(model_input_device())

    streamer = TextIteratorStreamer(
        tokenizer,
        timeout=120.0,
        skip_prompt=True,
        skip_special_tokens=True,
    )

    generation_kwargs = dict(
        **inputs,
        streamer=streamer,
        max_new_tokens=INTERNAL_MAX_NEW_TOKENS,
        do_sample=temperature > 0,
        temperature=max(temperature, 1e-5),
        top_p=top_p,
        top_k=top_k,
        repetition_penalty=repetition_penalty,
        use_cache=True,
        pad_token_id=tokenizer.pad_token_id,
        eos_token_id=tokenizer.eos_token_id,
    )

    worker = Thread(target=model.generate, kwargs=generation_kwargs)
    worker.start()

    raw_output = ""

    for chunk in streamer:
        raw_output += chunk
        yield render_thinking(raw_output)


CSS = """
.gradio-container {
    max-width: 1180px !important;
    margin: 0 auto !important;
}

.title h1 {
    text-align: center;
    margin-bottom: 0.2rem !important;
}

.subtitle p,
.meta p {
    text-align: center;
}

.meta p {
    font-size: 0.95rem;
    color: #6b7280;
    margin-top: 0.35rem !important;
}

.duplicate-button {
    margin: 0 auto 14px auto !important;
}

details {
    border: 1px solid #37415133;
    border-radius: 12px;
    padding: 0.75rem 1rem;
    margin: 0.5rem 0 1rem 0;
    background: rgba(127, 127, 127, 0.08);
}

summary {
    cursor: pointer;
    font-weight: 600;
}

pre {
    white-space: pre-wrap;
    word-break: break-word;
    margin: 0.75rem 0 0 0;
}
"""

chatbot = gr.Chatbot(
    height=680,
    placeholder=PLACEHOLDER,
    sanitize_html=False,
)

with gr.Blocks(css=CSS, theme="soft") as demo:
    gr.Markdown(f"# {TITLE}", elem_classes="title")
    gr.Markdown(SUBTITLE, elem_classes="subtitle")
    gr.Markdown(
        f"{DESCRIPTION} Model: [{MODEL_ID}](https://huggingface.co/{MODEL_ID})",
        elem_classes="meta",
    )

    gr.DuplicateButton("Duplicate Space", elem_classes="duplicate-button")

    gr.ChatInterface(
        fn=stream_chat,
        chatbot=chatbot,
        fill_height=True,
        additional_inputs_accordion=gr.Accordion(
            "⚙️ Parameters",
            open=False,
            render=False,
        ),
        additional_inputs=[
            gr.Checkbox(
                value=True,
                label="Enable thinking",
                render=False,
            ),
            gr.Checkbox(
                value=False,
                label="Preserve thinking across turns",
                render=False,
            ),
            gr.Slider(
                minimum=0.0,
                maximum=1.2,
                step=0.05,
                value=1.0,
                label="Temperature",
                render=False,
            ),
            gr.Slider(
                minimum=0.1,
                maximum=1.0,
                step=0.05,
                value=0.95,
                label="Top-p",
                render=False,
            ),
            gr.Slider(
                minimum=1,
                maximum=100,
                step=1,
                value=20,
                label="Top-k",
                render=False,
            ),
            gr.Slider(
                minimum=1.0,
                maximum=1.5,
                step=0.05,
                value=1.0,
                label="Repetition penalty",
                render=False,
            ),
        ],
        examples=[
            ["Design a production-ready architecture for a local AI terminal-agent platform using GRM-2.6-Opus."],
            ["Write a detailed debugging plan for a flaky async Python test suite."],
            ["Build a responsive landing page in React and Tailwind for a premium AI coding product."],
            ["Create an agentic workflow plan for solving a Terminal-Bench style task from scratch."],
        ],
        cache_examples=False,
    )

if __name__ == "__main__":
    demo.launch()