Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files1800 unmodified lines18011802180318041805180618071808180918101811181215 unmodified lines1828182918301831183218331834107 unmodified lines19421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199524 unmodified lines202020212022202320242025412 unmodified lines243824392440244124422443244433 unmodified lines24782479248024812482248395 unmodified lines25792580258125822583258425852586258725882589259025911800 unmodified linescall_kwargs["num_retries"] = 0call_kwargs["max_retries"] = 0req_timeout = min(float(call_kwargs.get("timeout") or getattr(self, "timeout", None) or 14.0),14.0,)call_kwargs["timeout"] = req_timeoutlitellm.request_timeout = req_timeouthard_timeout = req_timeout + 2.0executor = ThreadPoolExecutor(max_workers=1)future = executor.submit(super().generate, *args, **call_kwargs)15 unmodified linesexecutor.shutdown(wait=False, cancel_futures=True)print(f"[LLM] {self.model_id}: {time.monotonic() - t0:.1f}s", flush=True)@classmethod@classmethoddef _sanitize_code_tags(cls, text: str) -> str:import ast107 unmodified linesif converted_top is not None:return f"{open_t}\n{_extract_valid_python(converted_top)}\n{close_t}"cleaned_text = re.sub(r"<\|[^|]+\|>", "", text)extracted_main = _extract_valid_python(cleaned_text)if extracted_main:return f"{open_t}\n{extracted_main}\n{close_t}"return cleaned_textdef _extract_valid_python(raw: str) -> str:converted = _convert_xml_tool_calls(raw)if converted is not None:raw = convertedlines = []for line in raw.splitlines():stripped = line.lstrip()if re.match(r"^(?:thoughts?|thinking)\s*:", stripped, flags=re.IGNORECASE):lines.append("# " + stripped)else:lines.append(line)n = len(lines)for start in range(n):if not lines[start].strip():continuefor end in range(n, start, -1):candidate = "\n".join(lines[start:end]).strip()if not candidate:continuetry:ast.parse(candidate)return candidateexcept SyntaxError:continuereturn "\n".join(lines).strip()converted_top = _convert_xml_tool_calls(text)if converted_top is not None:return f"{open_t}\n{_extract_valid_python(converted_top)}\n{close_t}"cleaned_text = re.sub(r"<\|[^|]+\|>", "", text)cleaned_text = re.sub(r"</?code[^>]*>\s*```", "<code>\n", cleaned_text, flags=re.IGNORECASE)cleaned_text = re.sub(r"```\s*</?code[^>]*>", "\n</code>", cleaned_text, flags=re.IGNORECASE)html_match = re.search(r"<code>(.*?)</code>", cleaned_text, flags=re.DOTALL | re.IGNORECASE)if html_match:inner = html_match.group(1)inner = re.sub(r"```(?:py|python)?\s*\n?", "", inner, flags=re.IGNORECASE)inner = re.sub(r"\n?```", "", inner)inner = re.sub(r"</?code[^>]*>", "", inner, flags=re.IGNORECASE)return f"{open_t}\n{_extract_valid_python(inner)}\n{close_t}"md_match = re.search(24 unmodified linesif "final_answer(" in extracted:return f"{open_t}\n{extracted}\n{close_t}"return cleaned_text@classmethod412 unmodified linesexact_match_rule = (f"Answer ONLY with the exact value (number, short phrase, or comma-separated list) inside {code_open_tag}...{code_close_tag}. ""No explanations, no units, no sentences, no 'final answer' prefix. ""For numbers, omit commas (1500 not 1,500).")accepted_params = set(sig.parameters) | set(inspect.signature(MultiStepAgent.__init__).parameters)33 unmodified lines"operator","openpyxl",],"instructions": exact_match_rule,"code_block_tags": (code_open_tag, code_close_tag),"max_steps": 4,95 unmodified linesanswer_line = lnbreakif not answer_line:return ""cleaned = answer_lineif not lines:return ""cleaned = lines[-1]if re.match(r"^(?:the wikipedia article|the youtube video|the search results show|this scene is from|the page is quite long|the task doesn't have|the download failed|let me try|let me search|let me check|now i need to find|i found the|the operation is not commutative|the scene occurs)\b",1800 unmodified lines18011802180318041805180618071808180918101811181215 unmodified lines1828182918301831183218331834107 unmodified lines194219431944194519461947194819491950195119521953195424 unmodified lines197919801981198219831984198519861987412 unmodified lines24002401240224032404240524062407240833 unmodified lines244224432444244524462447244895 unmodified lines254425452546254725482549255025512552255325542555255625572558255925601800 unmodified linescall_kwargs["num_retries"] = 0call_kwargs["max_retries"] = 0req_timeout = min(float(call_kwargs.get("timeout") or getattr(self, "timeout", None) or 22.0),22.0,)call_kwargs["timeout"] = req_timeoutlitellm.request_timeout = req_timeouthard_timeout = req_timeout + 3.0executor = ThreadPoolExecutor(max_workers=1)future = executor.submit(super().generate, *args, **call_kwargs)15 unmodified linesexecutor.shutdown(wait=False, cancel_futures=True)print(f"[LLM] {self.model_id}: {time.monotonic() - t0:.1f}s", flush=True)@classmethoddef _sanitize_code_tags(cls, text: str) -> str:import ast107 unmodified linesif converted_top is not None:return f"{open_t}\n{_extract_valid_python(converted_top)}\n{close_t}"cleaned_text = re.sub(r"<\|[^|]+\|>", "", text)cleaned_text = re.sub(r"</?code[^>]*>\s*```", "<code>\n", cleaned_text, flags=re.IGNORECASE)cleaned_text = re.sub(r"```\s*</?code[^>]*>", "\n</code>", cleaned_text, flags=re.IGNORECASE)html_match = re.search(r"<code>(.*?)</code>", cleaned_text, flags=re.DOTALL | re.IGNORECASE)if html_match:inner = re.sub(r"```(?:py|python)?\s*\n?|\n?```|</?code[^>]*>", "", html_match.group(1), flags=re.IGNORECASE)return f"{open_t}\n{_extract_valid_python(inner)}\n{close_t}"md_match = re.search(24 unmodified linesif "final_answer(" in extracted:return f"{open_t}\n{extracted}\n{close_t}"extracted_main = _extract_valid_python(cleaned_text)if extracted_main:return f"{open_t}\n{extracted_main}\n{close_t}"return cleaned_text@classmethod412 unmodified linesexact_match_rule = (f"Answer ONLY with the exact value (number, short phrase, or comma-separated list) inside {code_open_tag}...{code_close_tag}. ""No explanations, no units, no sentences, no 'final answer' prefix. ""For numbers, omit commas (1500 not 1,500). ""In Python code, always define every variable before using it (never reference undefined variables like `lines`), ""do not import `requests` or `posixpath`, and call `final_answer(...)` as soon as you have the exact value.")accepted_params = set(sig.parameters) | set(inspect.signature(MultiStepAgent.__init__).parameters)33 unmodified lines"operator","openpyxl",],"additional_functions": {"open": open},"instructions": exact_match_rule,"code_block_tags": (code_open_tag, code_close_tag),"max_steps": 4,95 unmodified linesanswer_line = lnbreaklines = [ln.strip() for ln in cleaned.splitlines() if ln.strip()]answer_line = ""for ln in reversed(lines):if not junk_pattern.match(ln):answer_line = lnbreakif not answer_line:return ""cleaned = answer_lineif re.match(r"^(?:the wikipedia article|the youtube video|the search results show|this scene is from|the page is quite long|the task doesn't have|the download failed|let me try|let me search|let me check|now i need to find|i found the|the operation is not commutative|the scene occurs)\b",
app.py+20-51