File size: 14,250 Bytes
07edc6a
 
b6c8fe4
1ca8bf3
5a61b9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07edc6a
5a61b9e
 
 
 
 
 
 
 
 
 
 
07edc6a
52547eb
07edc6a
 
 
 
 
 
 
5a61b9e
1ca8bf3
b6c8fe4
1ca8bf3
a6334ba
 
 
 
 
 
5a61b9e
 
 
a6334ba
 
 
 
 
 
 
 
07edc6a
5a96cd2
b6c8fe4
07edc6a
 
5a61b9e
07edc6a
a6334ba
07edc6a
5a61b9e
07edc6a
 
a6334ba
07edc6a
5a61b9e
 
 
a6334ba
07edc6a
5a61b9e
 
07edc6a
5a61b9e
07edc6a
a6334ba
 
5a96cd2
1ca8bf3
 
a6334ba
07edc6a
 
 
a6334ba
07edc6a
1ca8bf3
07edc6a
 
a6334ba
07edc6a
5a61b9e
07edc6a
5a61b9e
07edc6a
5a61b9e
07edc6a
 
a6334ba
07edc6a
5a96cd2
07edc6a
 
 
 
 
a6334ba
1ca8bf3
a6334ba
 
1ca8bf3
a6334ba
07edc6a
1ca8bf3
5a61b9e
07edc6a
 
 
1ca8bf3
5a61b9e
a6334ba
415770d
07edc6a
a6334ba
 
 
db04229
a6334ba
07edc6a
 
5a61b9e
07edc6a
5a61b9e
07edc6a
 
a6334ba
0fbd9d7
07edc6a
8c2a839
415770d
5a96cd2
415770d
8c2a839
b6c8fe4
3c3ea5a
 
b6c8fe4
 
 
3c3ea5a
 
 
b6c8fe4
 
db04229
3c3ea5a
 
b6c8fe4
8c2a839
 
 
0fbd9d7
8c2a839
3c3ea5a
b6c8fe4
3c3ea5a
b6c8fe4
db04229
8c2a839
0fbd9d7
3c3ea5a
 
b6c8fe4
8c2a839
db04229
3c3ea5a
8c2a839
b6c8fe4
3c3ea5a
 
 
0fbd9d7
db04229
3c3ea5a
 
8c2a839
db04229
b6c8fe4
8c2a839
b6c8fe4
0fbd9d7
b6c8fe4
db04229
8c2a839
3c3ea5a
 
db04229
 
3c3ea5a
 
8c2a839
3c3ea5a
b6c8fe4
8c2a839
 
3c3ea5a
b6c8fe4
 
 
0fbd9d7
 
3c3ea5a
 
 
 
0fbd9d7
 
 
3c3ea5a
 
8c2a839
0fbd9d7
3c3ea5a
 
 
 
b6c8fe4
 
 
8c2a839
db04229
b6c8fe4
0fbd9d7
b6c8fe4
0fbd9d7
 
3c3ea5a
 
0fbd9d7
db04229
3c3ea5a
 
8c2a839
 
 
b6c8fe4
3c3ea5a
0fbd9d7
3c3ea5a
 
b6c8fe4
0fbd9d7
3c3ea5a
 
b6c8fe4
8c2a839
3c3ea5a
0fbd9d7
b6c8fe4
db04229
0fbd9d7
8c2a839
3c3ea5a
db04229
 
 
3c3ea5a
 
 
b6c8fe4
 
db04229
3c3ea5a
b6c8fe4
db04229
3c3ea5a
db04229
b6c8fe4
0fbd9d7
 
 
3c3ea5a
b6c8fe4
0fbd9d7
db04229
3c3ea5a
 
b6c8fe4
8c2a839
db04229
3c3ea5a
 
b6c8fe4
3c3ea5a
 
db04229
3c3ea5a
b6c8fe4
3c3ea5a
 
 
 
 
 
 
 
0fbd9d7
8c2a839
0fbd9d7
3c3ea5a
8c2a839
3c3ea5a
0fbd9d7
db04229
 
3c3ea5a
db04229
0fbd9d7
3c3ea5a
b6c8fe4
3c3ea5a
db04229
 
0fbd9d7
3c3ea5a
 
 
 
 
 
 
 
 
 
 
 
1ca8bf3
 
 
 
3c3ea5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6c8fe4
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
import gradio as gr
import time
import spaces
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModelForCausalLM, pipeline

# ── Device ────────────────────────────────────────────────────────────────────
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"πŸ–₯️ Device: {DEVICE}")

# ── Reasoner β€” loads locally, no API calls ────────────────────────────────────
REASONER_MODEL = "Qwen/Qwen2.5-1.5B-Instruct"
print(f"πŸ”„ Loading reasoner: {REASONER_MODEL}")
reasoner_tokenizer = AutoTokenizer.from_pretrained(REASONER_MODEL, trust_remote_code=True)
reasoner_model = AutoModelForCausalLM.from_pretrained(
    REASONER_MODEL,
    trust_remote_code=True,
    torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
    device_map="auto",
)
reasoner_pipe = pipeline(
    "text-generation",
    model=reasoner_model,
    tokenizer=reasoner_tokenizer,
    max_new_tokens=600,
    temperature=0.3,
    do_sample=True,
)
print("βœ… Reasoner loaded!")

# ── Step Probe β€” fine-tuned on PRM800K ───────────────────────────────────────
PROBE_MODEL = "realArceus/twt-probe"
print(f"πŸ”„ Loading probe: {PROBE_MODEL}")
probe_tokenizer = AutoTokenizer.from_pretrained(PROBE_MODEL, trust_remote_code=True)
probe_model = AutoModelForSequenceClassification.from_pretrained(
    PROBE_MODEL,
    trust_remote_code=True,
    torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
).to(DEVICE)
probe_model.eval()
print("βœ… Probe loaded!")

SYSTEM_PROMPT = """You are a careful step-by-step reasoner. When given a problem, solve it by thinking through exactly numbered steps.
Format EVERY step as:
Step 1: <your reasoning>
Step 2: <your reasoning>
...
Final Answer: <answer>
Be deliberate. Show your full working. Each step should be one clear thought."""

# ── Step Probe inference ──────────────────────────────────────────────────────
def probe_step(problem: str, steps_so_far: list) -> tuple:
    context = " ".join(steps_so_far[:-1]) if len(steps_so_far) > 1 else ""
    current = steps_so_far[-1] if steps_so_far else ""
    input_text = (
        f"[PROBLEM] {problem.strip()} "
        f"[STEPS SO FAR] {context} "
        f"[CURRENT STEP] {current}"
    )
    inputs = probe_tokenizer(
        input_text, return_tensors="pt",
        truncation=True, max_length=512,
    ).to(DEVICE)
    with torch.no_grad():
        logits = probe_model(**inputs).logits
        probs  = torch.softmax(logits, dim=-1)[0]
    ok_conf   = probs[1].item()
    fail_conf = probs[0].item()
    label = "OK" if ok_conf >= 0.5 else "FAIL"
    conf  = round(ok_conf if label == "OK" else fail_conf, 2)
    return label, conf

# ── Core pipeline ─────────────────────────────────────────────────────────────
@spaces.GPU
def run_twt(problem: str):
    if not problem.strip():
        yield ("", "<div class='msg warn'>⚠️ Enter a problem to analyze.</div>", "<div class='msg empty'>Waiting...</div>")
        return

    cot_html = ""

    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user",   "content": problem.strip()}
    ]
    prompt = reasoner_tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=True
    )

    try:
        result = reasoner_pipe(prompt)
        full_response = result[0]["generated_text"][len(prompt):]
    except Exception as e:
        yield ("", f"<div class='msg error'>❌ Reasoner error: {str(e)}</div>", "")
        return

    lines        = full_response.strip().split("\n")
    parsed_steps = [l.strip() for l in lines if l.strip()]
    displayed_steps = []
    first_fail_seen = False

    for i, step in enumerate(parsed_steps):
        displayed_steps.append(step)
        is_final = step.lower().startswith("final answer")

        if not is_final:
            label, conf = probe_step(problem, displayed_steps)
        else:
            label, conf = "FINAL", 1.0

        if is_final:
            icon, badge_class, badge_text = "🏁", "badge-final", "ANSWER"
        elif label == "OK":
            icon, badge_class, badge_text = "🟒", "badge-ok", f"OK · {int(conf*100)}%"
        else:
            icon, badge_class, badge_text = "πŸ”΄", "badge-fail", f"FAULT Β· {int(conf*100)}%"
            if not first_fail_seen:
                first_fail_seen = True

        step_card = f"""
        <div class='step-card {"step-fault" if label == "FAIL" else ""}'>
            <span class='step-icon'>{icon}</span>
            <span class='step-text'>{step}</span>
            <span class='badge {badge_class}'>{badge_text}</span>
        </div>"""
        cot_html += step_card

        yield (full_response, cot_html, build_trace(problem, displayed_steps, first_fail_seen))
        time.sleep(0.1)

    yield (full_response, cot_html, build_trace(problem, displayed_steps, first_fail_seen, done=True))


def build_trace(problem, displayed, first_fail, done=False):
    faults, rows = 0, ""
    for i, s in enumerate(displayed):
        is_final = s.lower().startswith("final answer")
        if not is_final:
            lbl, conf = probe_step(problem, displayed[:i+1])
            if lbl == "FAIL": faults += 1
            dot   = f"<span class='dot dot-{'ok' if lbl == 'OK' else 'fail'}'></span>"
            rows += f"<div class='trace-row'>{dot} Step {i+1} <span class='trace-conf'>&mdash; {int(conf*100)}%</span></div>"
        else:
            rows += "<div class='trace-row'><span class='dot dot-final'></span> Final Answer</div>"

    health    = max(0, 100 - faults * 20)
    bar_color = "#10b981" if health > 60 else "#e11d48"
    summary   = f"""
    <div class='trace-summary'>
        <div class='trace-label'>Reasoning Health</div>
        <div class='trace-bar-bg'><div class='trace-bar' style='width:{health}%;background:{bar_color}'></div></div>
        <div class='trace-pct'>{health}%</div>
    </div>""" if done else ""
    return f"{rows}{summary}"


# ── CSS (Compact Enterprise Corporate Navy Theme) ──────────────────────────────
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap');

*, *::before, *::after { box-sizing: border-box; }
body, .gradio-container, .gradio-container * {
    font-family: 'Inter', -apple-system, sans-serif !important;
    color: #1e293b !important;
}
.gradio-container {
    background-color: #f4f6f8 !important;
    max-width: 1280px !important;
    padding-top: 1rem !important;
}
.twt-header {
    text-align: center;
    padding: 0 1rem 1rem;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid #cbd5e1;
}
.twt-title {
    font-size: 2rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    font-family: 'JetBrains Mono', monospace !important;
    margin-bottom: 0.25rem;
    color: #0f172a !important;
}
.twt-title span { color: #0033a0 !important; }
.twt-sub {
    font-size: 0.85rem;
    color: #475569 !important;
    font-weight: 500;
    letter-spacing: 0.02em;
}
.input-label, .panel-label {
    font-size: 0.7rem;
    font-weight: 700;
    color: #334155 !important;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 0.5rem;
    display: block;
}
.panel-label {
    padding-bottom: 0.4rem;
    border-bottom: 2px solid #cbd5e1;
}
textarea {
    background: #ffffff !important;
    border: 1px solid #94a3b8 !important;
    border-radius: 6px !important;
    color: #0f172a !important;
    font-size: 0.85rem !important;
    line-height: 1.5 !important;
    padding: 0.75rem !important;
    box-shadow: inset 0 1px 2px rgba(15, 23, 42, 0.05) !important;
    transition: all 0.2s ease !important;
}
textarea:focus {
    border-color: #0033a0 !important;
    box-shadow: 0 0 0 3px rgba(0, 51, 160, 0.15) !important;
    outline: none !important;
}
textarea::placeholder { color: #94a3b8 !important; }
button.primary {
    background: #0033a0 !important;
    color: #ffffff !important;
    font-weight: 600 !important;
    border: none !important;
    border-radius: 6px !important;
    padding: 0.6rem 1.25rem !important;
    font-size: 0.85rem !important;
    box-shadow: 0 2px 4px -1px rgba(0, 51, 160, 0.2) !important;
    transition: all 0.15s ease-in-out !important;
    cursor: pointer !important;
    width: 100% !important;
}
button.primary:hover {
    background: #002266 !important;
    transform: translateY(-1px) !important;
    box-shadow: 0 4px 6px -2px rgba(0, 51, 160, 0.3) !important;
}
button.primary:active {
    transform: translateY(0) !important;
    box-shadow: 0 1px 2px rgba(0, 51, 160, 0.2) !important;
}
.step-card {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    margin-bottom: 0.5rem;
    background: #ffffff;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    line-height: 1.5;
    animation: fadeUp 0.2s ease-out both;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
    transition: all 0.15s ease;
}
.step-card:hover {
    box-shadow: 0 2px 6px rgba(15, 23, 42, 0.06);
    border-color: #94a3b8;
}
.step-card.step-fault {
    border-color: #fca5a5;
    background: #fff1f2;
}
.step-card.step-fault:hover { border-color: #f87171; }
@keyframes fadeUp {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}
.step-icon { font-size: 1rem; margin-top: 1px; }
.step-text { flex: 1; color: #1e293b !important; font-size: 0.85rem; }
.badge {
    flex-shrink: 0;
    font-size: 0.6rem;
    font-weight: 700;
    font-family: 'JetBrains Mono', monospace !important;
    letter-spacing: 0.02em;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    margin-top: 2px;
    text-transform: uppercase;
}
.badge-ok    { background: #f0fdf4; color: #15803d !important; border: 1px solid #86efac; }
.badge-fail  { background: #fff1f2; color: #be123c !important; border: 1px solid #fda4af; }
.badge-final { background: #f0f4ff; color: #0033a0 !important; border: 1px solid #bfdbfe; }
.trace-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.75rem;
    color: #334155 !important;
    font-family: 'JetBrains Mono', monospace !important;
    padding: 0.35rem 0;
    border-bottom: 1px solid #e2e8f0;
}
.trace-conf { color: #64748b !important; }
.dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.dot-ok    { background: #10b981; box-shadow: 0 0 0 2px #d1fae5; }
.dot-fail  { background: #e11d48; box-shadow: 0 0 0 2px #ffe4e6; }
.dot-final { background: #0033a0; box-shadow: 0 0 0 2px #dbeafe; }
.trace-summary {
    margin-top: 1rem;
    padding-top: 0.75rem;
    border-top: 1px solid #cbd5e1;
}
.trace-label {
    font-size: 0.65rem;
    font-weight: 700;
    color: #475569 !important;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.4rem;
}
.trace-bar-bg {
    background: #cbd5e1;
    border-radius: 999px;
    height: 4px;
    overflow: hidden;
}
.trace-bar {
    height: 100%;
    border-radius: 999px;
    transition: width 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}
.trace-pct {
    font-size: 0.75rem;
    color: #0f172a !important;
    margin-top: 0.4rem;
    font-family: 'JetBrains Mono', monospace !important;
    font-weight: 600;
}
.msg { padding: 0.75rem; border-radius: 6px; font-size: 0.8rem; font-weight: 500; }
.msg.warn  { background: #fefce8; color: #a16207 !important; border: 1px solid #fde047; }
.msg.error { background: #fff1f2; color: #be123c !important; border: 1px solid #fca5a5; }
.msg.empty {
    color: #64748b !important;
    font-size: 0.8rem;
    font-family: 'JetBrains Mono', monospace !important;
    padding: 1.5rem 1rem;
    text-align: center;
    background: #ffffff;
    border: 1px dashed #94a3b8;
    border-radius: 6px;
}
.gr-box, .gr-form { background: transparent !important; border: none !important; }
"""

HEADER = """
<div class='twt-header'>
    <div class='twt-title'>Think<span>While</span>Thinking</div>
    <div class='twt-sub'>Real-time reasoning failure detection Β· Step-level process supervision</div>
</div>
"""

EXAMPLES = [
    "If a bat and a ball cost $1.10 in total, and the bat costs $1 more than the ball, how much does the ball cost?",
    "A farmer has 17 sheep. All but 9 die. How many sheep are left?",
    "What is 15% of 80? Then add that to 25% of 60.",
    "If you have a 3-gallon jug and a 5-gallon jug, how do you measure exactly 4 gallons?",
]

with gr.Blocks(css=CSS, title="ThinkWhileThinking") as demo:
    gr.HTML(HEADER)
    with gr.Row():
        with gr.Column(scale=2):
            gr.HTML("<div class='input-label'>Problem</div>")
            problem_input = gr.Textbox(placeholder="Enter a math, logic, or reasoning problem...", lines=5, show_label=False)
            run_btn = gr.Button("β–Ά  Analyze Reasoning", variant="primary")
            gr.Examples(examples=EXAMPLES, inputs=problem_input, label="Try an example")
        with gr.Column(scale=3):
            with gr.Row():
                with gr.Column(scale=3):
                    gr.HTML("<div class='panel-label'>Chain of Thought Β· Step Scores</div>")
                    cot_output = gr.HTML(value="<div class='msg empty'>// awaiting problem input</div>")
                with gr.Column(scale=1):
                    gr.HTML("<div class='panel-label'>Step Trace</div>")
                    trace_output = gr.HTML(value="<div class='msg empty'>// trace</div>")
    raw_output = gr.Textbox(visible=False)
    run_btn.click(fn=run_twt, inputs=[problem_input], outputs=[raw_output, cot_output, trace_output])
    problem_input.submit(fn=run_twt, inputs=[problem_input], outputs=[raw_output, cot_output, trace_output])

demo.launch()