everydaytok commited on
Commit
b6b4919
Β·
verified Β·
1 Parent(s): 7c518af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -13
app.py CHANGED
@@ -10,8 +10,7 @@ Architecture:
10
  """
11
 
12
  import os, time, threading, json, re, textwrap, copy
13
- import traceback, html
14
- import requests
15
  from collections import deque, defaultdict
16
  from typing import Dict, List, Tuple, Any, Optional, Set
17
  from dataclasses import dataclass, field
@@ -180,23 +179,45 @@ def build_core_problem(axl_def: AXLProblemDef) -> core.Problem:
180
  # ══════════════════════════════════════════════════════════════════════
181
 
182
  def _extract_json_from_text(text: str) -> str:
183
- """Bulletproof JSON extractor to handle conversational AI padding."""
184
  text = text.strip()
185
  if not text:
186
  return ""
187
 
188
- # 1. Try to find explicit markdown json blocks
189
- match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', text, re.DOTALL | re.IGNORECASE)
190
- if match:
191
- return match.group(1).strip()
 
 
 
 
 
192
 
193
- # 2. Try to find the outermost curly braces
194
- start = text.find('{')
195
- end = text.rfind('}')
196
- if start != -1 and end != -1 and end >= start:
197
- return text[start:end+1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
- # 3. Fallback to raw text
200
  return text
201
 
202
  def call_external_ai(url: str, model: str, system_prompt: str, prompt: str) -> str:
 
10
  """
11
 
12
  import os, time, threading, json, re, textwrap, copy
13
+ import traceback, html, itertools, math
 
14
  from collections import deque, defaultdict
15
  from typing import Dict, List, Tuple, Any, Optional, Set
16
  from dataclasses import dataclass, field
 
179
  # ══════════════════════════════════════════════════════════════════════
180
 
181
  def _extract_json_from_text(text: str) -> str:
182
+ """Robust, lexer-style JSON extractor that handles LaTeX groupings."""
183
  text = text.strip()
184
  if not text:
185
  return ""
186
 
187
+ # 1. First, search for explicit markdown blocks
188
+ blocks = re.findall(r'```(?:json)?\s*(\{.*?\})\s*```', text, re.DOTALL | re.IGNORECASE)
189
+ for b in blocks:
190
+ cleaned = b.strip()
191
+ # Verify it looks like our AXL/Collapser schema
192
+ if any(k in cleaned for k in ['"variables"', '"constraints"', '"hypothesis_markdown"', '"name"']):
193
+ return cleaned
194
+ if blocks:
195
+ return blocks[0].strip()
196
 
197
+ # 2. Scope-matching parser to isolate valid candidates
198
+ candidates = []
199
+ for match in re.finditer(r'\{', text):
200
+ start_idx = match.start()
201
+ depth = 0
202
+ for i in range(start_idx, len(text)):
203
+ if text[i] == '{':
204
+ depth += 1
205
+ elif text[i] == '}':
206
+ depth -= 1
207
+ if depth == 0:
208
+ candidates.append(text[start_idx:i+1])
209
+ break
210
+
211
+ # Prioritize candidates containing key schema elements
212
+ for cand in candidates:
213
+ cleaned = cand.strip()
214
+ if any(k in cleaned for k in ['"variables"', '"constraints"', '"hypothesis_markdown"', '"name"']):
215
+ return cleaned
216
+
217
+ # Fallback to the largest bracket scope
218
+ if candidates:
219
+ return max(candidates, key=len).strip()
220
 
 
221
  return text
222
 
223
  def call_external_ai(url: str, model: str, system_prompt: str, prompt: str) -> str: