realArceus commited on
Commit
098fc5e
·
verified ·
1 Parent(s): b9d5bf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -68
app.py CHANGED
@@ -48,23 +48,9 @@ Final Answer: <answer>
48
  Be deliberate. Show your full working. Each step should be one clear thought."""
49
 
50
  # ── Step Probe inference ──────────────────────────────────────────────────────
51
- def is_notation_step(step: str) -> bool:
52
- """Detect pure LaTeX/notation steps — restatements, not new reasoning."""
53
- s = step.strip()
54
- return (
55
- s.startswith("\\[") or s.startswith("$$") or
56
- s.startswith("\\(") or
57
- (s.startswith("\\") and len(s.split()) < 8)
58
- )
59
-
60
- def probe_step(problem: str, steps_so_far: list, prev_result: tuple = None) -> tuple:
61
- current = steps_so_far[-1] if steps_so_far else ""
62
-
63
- # Notation-only steps inherit previous score — they restate, not reason
64
- if is_notation_step(current):
65
- return prev_result if prev_result is not None else ("OK", 0.9)
66
-
67
  context = " ".join(steps_so_far[:-1]) if len(steps_so_far) > 1 else ""
 
68
  input_text = (
69
  f"[PROBLEM] {problem.strip()} "
70
  f"[STEPS SO FAR] {context} "
@@ -91,6 +77,7 @@ def run_twt(problem: str):
91
 
92
  cot_html = ""
93
 
 
94
  messages = [
95
  {"role": "system", "content": SYSTEM_PROMPT},
96
  {"role": "user", "content": problem.strip()}
@@ -108,17 +95,15 @@ def run_twt(problem: str):
108
 
109
  lines = full_response.strip().split("\n")
110
  parsed_steps = [l.strip() for l in lines if l.strip()]
111
- displayed_steps = []
112
- first_fail_seen = False
113
- prev_probe_result = None
114
 
115
  for i, step in enumerate(parsed_steps):
116
  displayed_steps.append(step)
117
  is_final = step.lower().startswith("final answer")
118
 
119
  if not is_final:
120
- label, conf = probe_step(problem, displayed_steps, prev_probe_result)
121
- prev_probe_result = (label, conf)
122
  else:
123
  label, conf = "FINAL", 1.0
124
 
@@ -139,20 +124,18 @@ def run_twt(problem: str):
139
  </div>"""
140
  cot_html += step_card
141
 
142
- yield (full_response, cot_html, build_trace(problem, displayed_steps, first_fail_seen, prev_probe_result))
143
  time.sleep(0.1)
144
 
145
- yield (full_response, cot_html, build_trace(problem, displayed_steps, first_fail_seen, prev_probe_result, done=True))
146
 
147
 
148
- def build_trace(problem, displayed, first_fail, prev_result=None, done=False):
149
  faults, rows = 0, ""
150
- last_result = None
151
  for i, s in enumerate(displayed):
152
  is_final = s.lower().startswith("final answer")
153
  if not is_final:
154
- lbl, conf = probe_step(problem, displayed[:i+1], last_result)
155
- last_result = (lbl, conf)
156
  if lbl == "FAIL": faults += 1
157
  dot = f"<span class='dot dot-{'ok' if lbl == 'OK' else 'fail'}'></span>"
158
  rows += f"<div class='trace-row'>{dot} Step {i+1} <span class='trace-conf'>&mdash; {int(conf*100)}%</span></div>"
@@ -177,32 +160,34 @@ CSS = """
177
  *, *::before, *::after { box-sizing: border-box; }
178
  body, .gradio-container, .gradio-container * {
179
  font-family: 'Inter', -apple-system, sans-serif !important;
180
- color: #1e293b !important;
181
  }
182
  .gradio-container {
183
- background-color: #f4f6f8 !important;
184
- max-width: 1280px !important;
185
- padding-top: 1rem !important;
186
  }
187
 
188
  /* Header */
189
  .twt-header {
190
  text-align: center;
191
- padding: 0 1rem 1rem;
192
- margin-bottom: 1.5rem;
193
  border-bottom: 1px solid #cbd5e1;
194
  }
195
  .twt-title {
196
- font-size: 2rem;
197
  font-weight: 800;
198
  letter-spacing: -0.03em;
199
  font-family: 'JetBrains Mono', monospace !important;
200
  margin-bottom: 0.25rem;
201
  color: #0f172a !important;
202
  }
203
- .twt-title span { color: #0033a0 !important; }
 
 
204
  .twt-sub {
205
- font-size: 0.85rem;
206
  color: #475569 !important;
207
  font-weight: 500;
208
  letter-spacing: 0.02em;
@@ -210,12 +195,12 @@ body, .gradio-container, .gradio-container * {
210
 
211
  /* Labels */
212
  .input-label, .panel-label {
213
- font-size: 0.7rem;
214
  font-weight: 700;
215
  color: #334155 !important;
216
  text-transform: uppercase;
217
  letter-spacing: 0.08em;
218
- margin-bottom: 0.5rem;
219
  display: block;
220
  }
221
  .panel-label {
@@ -227,11 +212,11 @@ body, .gradio-container, .gradio-container * {
227
  textarea {
228
  background: #ffffff !important;
229
  border: 1px solid #94a3b8 !important;
230
- border-radius: 6px !important;
231
  color: #0f172a !important;
232
- font-size: 0.85rem !important;
233
  line-height: 1.5 !important;
234
- padding: 0.75rem !important;
235
  box-shadow: inset 0 1px 2px rgba(15, 23, 42, 0.05) !important;
236
  transition: all 0.2s ease !important;
237
  }
@@ -244,13 +229,13 @@ textarea::placeholder { color: #94a3b8 !important; }
244
 
245
  /* Primary Button */
246
  button.primary {
247
- background: #0033a0 !important;
248
  color: #ffffff !important;
249
  font-weight: 600 !important;
250
  border: none !important;
251
- border-radius: 6px !important;
252
- padding: 0.6rem 1.25rem !important;
253
- font-size: 0.85rem !important;
254
  box-shadow: 0 2px 4px -1px rgba(0, 51, 160, 0.2) !important;
255
  transition: all 0.15s ease-in-out !important;
256
  cursor: pointer !important;
@@ -270,14 +255,14 @@ button.primary:active {
270
  .step-card {
271
  display: flex;
272
  align-items: flex-start;
273
- gap: 0.75rem;
274
- padding: 0.75rem 1rem;
275
- margin-bottom: 0.5rem;
276
  background: #ffffff;
277
  border: 1px solid #cbd5e1;
278
- border-radius: 6px;
279
  line-height: 1.5;
280
- animation: fadeUp 0.2s ease-out both;
281
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
282
  transition: all 0.15s ease;
283
  }
@@ -289,22 +274,24 @@ button.primary:active {
289
  border-color: #fca5a5;
290
  background: #fff1f2;
291
  }
292
- .step-card.step-fault:hover { border-color: #f87171; }
 
 
293
  @keyframes fadeUp {
294
  from { opacity: 0; transform: translateY(6px); }
295
  to { opacity: 1; transform: translateY(0); }
296
  }
297
- .step-icon { font-size: 1rem; margin-top: 1px; }
298
  .step-text { flex: 1; color: #1e293b !important; font-size: 0.85rem; }
299
 
300
  /* Status Badges */
301
  .badge {
302
  flex-shrink: 0;
303
- font-size: 0.6rem;
304
  font-weight: 700;
305
  font-family: 'JetBrains Mono', monospace !important;
306
  letter-spacing: 0.02em;
307
- padding: 0.15rem 0.4rem;
308
  border-radius: 4px;
309
  margin-top: 2px;
310
  text-transform: uppercase;
@@ -317,37 +304,37 @@ button.primary:active {
317
  .trace-row {
318
  display: flex;
319
  align-items: center;
320
- gap: 0.5rem;
321
- font-size: 0.75rem;
322
  color: #334155 !important;
323
  font-family: 'JetBrains Mono', monospace !important;
324
- padding: 0.35rem 0;
325
  border-bottom: 1px solid #e2e8f0;
326
  }
327
  .trace-conf { color: #64748b !important; }
328
- .dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
329
  .dot-ok { background: #10b981; box-shadow: 0 0 0 2px #d1fae5; }
330
  .dot-fail { background: #e11d48; box-shadow: 0 0 0 2px #ffe4e6; }
331
  .dot-final { background: #0033a0; box-shadow: 0 0 0 2px #dbeafe; }
332
 
333
  /* Progress Bar */
334
  .trace-summary {
335
- margin-top: 1rem;
336
  padding-top: 0.75rem;
337
  border-top: 1px solid #cbd5e1;
338
  }
339
  .trace-label {
340
- font-size: 0.65rem;
341
  font-weight: 700;
342
  color: #475569 !important;
343
  text-transform: uppercase;
344
  letter-spacing: 0.05em;
345
- margin-bottom: 0.4rem;
346
  }
347
  .trace-bar-bg {
348
  background: #cbd5e1;
349
  border-radius: 999px;
350
- height: 4px;
351
  overflow: hidden;
352
  }
353
  .trace-bar {
@@ -371,7 +358,7 @@ button.primary:active {
371
  color: #64748b !important;
372
  font-size: 0.8rem;
373
  font-family: 'JetBrains Mono', monospace !important;
374
- padding: 1.5rem 1rem;
375
  text-align: center;
376
  background: #ffffff;
377
  border: 1px dashed #94a3b8;
@@ -388,11 +375,10 @@ HEADER = """
388
  """
389
 
390
  EXAMPLES = [
391
- "Janet has 3 times as many apples as John. John has 8 apples. How many apples does Janet have?",
392
- "A store had 50 chocolates. They sold 18 in the morning and 14 in the afternoon. How many chocolates are left?",
393
- "Tom reads 12 pages every day. How many pages will he read in 3 weeks?",
394
- "A train travels 60 miles per hour. How far will it travel in 2 hours and 30 minutes?",
395
- "Lucy earns $15 per hour. She worked 6 hours on Monday and 4 hours on Tuesday. How much did she earn in total?",
396
  ]
397
 
398
  with gr.Blocks(css=CSS, title="ThinkWhileThinking") as demo:
 
48
  Be deliberate. Show your full working. Each step should be one clear thought."""
49
 
50
  # ── Step Probe inference ──────────────────────────────────────────────────────
51
+ def probe_step(problem: str, steps_so_far: list) -> tuple:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  context = " ".join(steps_so_far[:-1]) if len(steps_so_far) > 1 else ""
53
+ current = steps_so_far[-1] if steps_so_far else ""
54
  input_text = (
55
  f"[PROBLEM] {problem.strip()} "
56
  f"[STEPS SO FAR] {context} "
 
77
 
78
  cot_html = ""
79
 
80
+ # Build prompt
81
  messages = [
82
  {"role": "system", "content": SYSTEM_PROMPT},
83
  {"role": "user", "content": problem.strip()}
 
95
 
96
  lines = full_response.strip().split("\n")
97
  parsed_steps = [l.strip() for l in lines if l.strip()]
98
+ displayed_steps = []
99
+ first_fail_seen = False
 
100
 
101
  for i, step in enumerate(parsed_steps):
102
  displayed_steps.append(step)
103
  is_final = step.lower().startswith("final answer")
104
 
105
  if not is_final:
106
+ label, conf = probe_step(problem, displayed_steps)
 
107
  else:
108
  label, conf = "FINAL", 1.0
109
 
 
124
  </div>"""
125
  cot_html += step_card
126
 
127
+ yield (full_response, cot_html, build_trace(problem, displayed_steps, first_fail_seen))
128
  time.sleep(0.1)
129
 
130
+ yield (full_response, cot_html, build_trace(problem, displayed_steps, first_fail_seen, done=True))
131
 
132
 
133
+ def build_trace(problem, displayed, first_fail, done=False):
134
  faults, rows = 0, ""
 
135
  for i, s in enumerate(displayed):
136
  is_final = s.lower().startswith("final answer")
137
  if not is_final:
138
+ lbl, conf = probe_step(problem, displayed[:i+1])
 
139
  if lbl == "FAIL": faults += 1
140
  dot = f"<span class='dot dot-{'ok' if lbl == 'OK' else 'fail'}'></span>"
141
  rows += f"<div class='trace-row'>{dot} Step {i+1} <span class='trace-conf'>&mdash; {int(conf*100)}%</span></div>"
 
160
  *, *::before, *::after { box-sizing: border-box; }
161
  body, .gradio-container, .gradio-container * {
162
  font-family: 'Inter', -apple-system, sans-serif !important;
163
+ color: #1e293b !important; /* Forces dark slate text universally */
164
  }
165
  .gradio-container {
166
+ background-color: #f4f6f8 !important; /* Cool corporate gray backdrop */
167
+ max-width: 1280px !important; /* Widened slightly to fit dense data */
168
+ padding-top: 1rem !important; /* Reduced top padding */
169
  }
170
 
171
  /* Header */
172
  .twt-header {
173
  text-align: center;
174
+ padding: 0 1rem 1rem; /* Compact padding */
175
+ margin-bottom: 1.5rem; /* Reduced margin */
176
  border-bottom: 1px solid #cbd5e1;
177
  }
178
  .twt-title {
179
+ font-size: 2rem; /* Scaled down for density */
180
  font-weight: 800;
181
  letter-spacing: -0.03em;
182
  font-family: 'JetBrains Mono', monospace !important;
183
  margin-bottom: 0.25rem;
184
  color: #0f172a !important;
185
  }
186
+ .twt-title span {
187
+ color: #0033a0 !important; /* Morgan Stanley signature blue */
188
+ }
189
  .twt-sub {
190
+ font-size: 0.85rem; /* Scaled down */
191
  color: #475569 !important;
192
  font-weight: 500;
193
  letter-spacing: 0.02em;
 
195
 
196
  /* Labels */
197
  .input-label, .panel-label {
198
+ font-size: 0.7rem; /* Compact label */
199
  font-weight: 700;
200
  color: #334155 !important;
201
  text-transform: uppercase;
202
  letter-spacing: 0.08em;
203
+ margin-bottom: 0.5rem; /* Reduced margin */
204
  display: block;
205
  }
206
  .panel-label {
 
212
  textarea {
213
  background: #ffffff !important;
214
  border: 1px solid #94a3b8 !important;
215
+ border-radius: 6px !important; /* Sharper corners */
216
  color: #0f172a !important;
217
+ font-size: 0.85rem !important; /* Scaled down text */
218
  line-height: 1.5 !important;
219
+ padding: 0.75rem !important; /* Compact padding */
220
  box-shadow: inset 0 1px 2px rgba(15, 23, 42, 0.05) !important;
221
  transition: all 0.2s ease !important;
222
  }
 
229
 
230
  /* Primary Button */
231
  button.primary {
232
+ background: #0033a0 !important; /* Morgan Stanley Deep Navy */
233
  color: #ffffff !important;
234
  font-weight: 600 !important;
235
  border: none !important;
236
+ border-radius: 6px !important; /* Sharper corners */
237
+ padding: 0.6rem 1.25rem !important; /* Compact padding */
238
+ font-size: 0.85rem !important; /* Scaled down */
239
  box-shadow: 0 2px 4px -1px rgba(0, 51, 160, 0.2) !important;
240
  transition: all 0.15s ease-in-out !important;
241
  cursor: pointer !important;
 
255
  .step-card {
256
  display: flex;
257
  align-items: flex-start;
258
+ gap: 0.75rem; /* Reduced gap */
259
+ padding: 0.75rem 1rem; /* Compact padding */
260
+ margin-bottom: 0.5rem; /* Reduced margin to pack them closer */
261
  background: #ffffff;
262
  border: 1px solid #cbd5e1;
263
+ border-radius: 6px; /* Sharper corners */
264
  line-height: 1.5;
265
+ animation: fadeUp 0.2s ease-out both; /* Faster animation */
266
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
267
  transition: all 0.15s ease;
268
  }
 
274
  border-color: #fca5a5;
275
  background: #fff1f2;
276
  }
277
+ .step-card.step-fault:hover {
278
+ border-color: #f87171;
279
+ }
280
  @keyframes fadeUp {
281
  from { opacity: 0; transform: translateY(6px); }
282
  to { opacity: 1; transform: translateY(0); }
283
  }
284
+ .step-icon { font-size: 1rem; margin-top: 1px; } /* Slightly smaller icon */
285
  .step-text { flex: 1; color: #1e293b !important; font-size: 0.85rem; }
286
 
287
  /* Status Badges */
288
  .badge {
289
  flex-shrink: 0;
290
+ font-size: 0.6rem; /* Scaled down */
291
  font-weight: 700;
292
  font-family: 'JetBrains Mono', monospace !important;
293
  letter-spacing: 0.02em;
294
+ padding: 0.15rem 0.4rem; /* Compact padding */
295
  border-radius: 4px;
296
  margin-top: 2px;
297
  text-transform: uppercase;
 
304
  .trace-row {
305
  display: flex;
306
  align-items: center;
307
+ gap: 0.5rem; /* Tighter gap */
308
+ font-size: 0.75rem; /* Compact font */
309
  color: #334155 !important;
310
  font-family: 'JetBrains Mono', monospace !important;
311
+ padding: 0.35rem 0; /* Reduced padding */
312
  border-bottom: 1px solid #e2e8f0;
313
  }
314
  .trace-conf { color: #64748b !important; }
315
+ .dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; } /* Smaller dots */
316
  .dot-ok { background: #10b981; box-shadow: 0 0 0 2px #d1fae5; }
317
  .dot-fail { background: #e11d48; box-shadow: 0 0 0 2px #ffe4e6; }
318
  .dot-final { background: #0033a0; box-shadow: 0 0 0 2px #dbeafe; }
319
 
320
  /* Progress Bar */
321
  .trace-summary {
322
+ margin-top: 1rem; /* Compact top margin */
323
  padding-top: 0.75rem;
324
  border-top: 1px solid #cbd5e1;
325
  }
326
  .trace-label {
327
+ font-size: 0.65rem; /* Scaled down */
328
  font-weight: 700;
329
  color: #475569 !important;
330
  text-transform: uppercase;
331
  letter-spacing: 0.05em;
332
+ margin-bottom: 0.4rem; /* Tighter margin */
333
  }
334
  .trace-bar-bg {
335
  background: #cbd5e1;
336
  border-radius: 999px;
337
+ height: 4px; /* Thinner progress bar */
338
  overflow: hidden;
339
  }
340
  .trace-bar {
 
358
  color: #64748b !important;
359
  font-size: 0.8rem;
360
  font-family: 'JetBrains Mono', monospace !important;
361
+ padding: 1.5rem 1rem; /* Compact empty state */
362
  text-align: center;
363
  background: #ffffff;
364
  border: 1px dashed #94a3b8;
 
375
  """
376
 
377
  EXAMPLES = [
378
+ "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?",
379
+ "A farmer has 17 sheep. All but 9 die. How many sheep are left?",
380
+ "What is 15% of 80? Then add that to 25% of 60.",
381
+ "If you have a 3-gallon jug and a 5-gallon jug, how do you measure exactly 4 gallons?",
 
382
  ]
383
 
384
  with gr.Blocks(css=CSS, title="ThinkWhileThinking") as demo: