Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files1899 unmodified lines1900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933190819341935193619371912193819391914194019411916194219431918194419451946192119471948194919505 unmodified lines19561957195819331959196019611936196219631964193919651966196719681899 unmodified lines return "\n".join(py_lines) return None
def _guard_unbound_lines(code_str: str) -> str: if not code_str or "lines" not in code_str: return code_str try: tree = ast.parse(code_str) except SyntaxError: return code_str
assigned_before_use = False for node in ast.walk(tree): if isinstance(node, (ast.Assign, ast.AugAssign, ast.AnnAssign)): targets = [] if isinstance(node, ast.Assign): targets = node.targets elif isinstance(node, ast.AugAssign): targets = [node.target] elif isinstance(node, ast.AnnAssign) and node.value is not None: targets = [node.target] if any(isinstance(t, ast.Name) and t.id == "lines" for t in targets): assigned_before_use = True break
if assigned_before_use: return code_str return "lines = []\n" + code_str
def _extract_valid_python(raw: str) -> str: converted = _convert_xml_tool_calls(raw) if converted is not None: raw = converted
lines = [] src_lines = [] for line in raw.splitlines(): stripped = line.lstrip() if re.match(r"^(?:thoughts?|thinking)\s*:", stripped, flags=re.IGNORECASE): lines.append("# " + stripped) src_lines.append("# " + stripped) else: lines.append(line) src_lines.append(line)
n = len(lines) n = len(src_lines) for start in range(n): if not lines[start].strip(): if not src_lines[start].strip(): continue for end in range(n, start, -1): candidate = "\n".join(lines[start:end]).strip() candidate = "\n".join(src_lines[start:end]).strip() if not candidate: continue try:5 unmodified lines ): ident = tree.body[0].value.id return f"final_answer({json.dumps(ident, ensure_ascii=False)})" return candidate return _guard_unbound_lines(candidate) except SyntaxError: continue plain = "\n".join(lines).strip() plain = "\n".join(src_lines).strip() if plain and len(plain) <= 250 and "\n" not in plain: return f"final_answer({json.dumps(plain, ensure_ascii=False)})" return plain return _guard_unbound_lines(plain)
converted_top = _convert_xml_tool_calls(text) if converted_top is not None:app.py+35-9