Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files908 unmodified lines9099109119129139129139149159169171010 unmodified lines192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199124 unmodified lines201620172018196120192020202120222023202419672025202620272028292 unmodified lines2321232223232266232423252268232623272270232823292272233023312332233323342277233523362279233723382339234022832341234222852343234423452346191 unmodified lines253825392540254125422543254464 unmodified lines260926102611261226132614261520 unmodified lines2636263726382639264026412642264325792580258125822583258425852586258726442645264626472648264926502651265226532654265534 unmodified lines269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719908 unmodified lines "openrouter/google/gemma-3-27b-it:free", ]) if self.provider_keys.get("nvidia_nim"): vision_candidates.extend([ "nvidia_nim/moonshotai/kimi-k2.5", candidate_models.extend([ "nvidia_nim/nvidia/nemotron-3-ultra-550b-a55b", "nvidia_nim/moonshotai/kimi-k2.5", ])
for m in getattr(self, "working_models", []):1010 unmodified lines return code_str return "lines = []\n" + code_str
def _ensure_final_answer_call(code_str: str) -> str: clean_code = (code_str or "").strip() if not clean_code or "final_answer(" in clean_code: return clean_code try: tree = ast.parse(clean_code) except SyntaxError: return clean_code if not tree.body: return clean_code
observation_tools = { "web_search", "search_wikipedia", "fetch_url_text", "download_task_file", "read_local_file", "inspect_image", "inspect_youtube", "transcribe_audio", "ask_web", } for node in ast.walk(tree): if isinstance(node, ast.Call): func_name = None if isinstance(node.func, ast.Name): func_name = node.func.id elif isinstance(node.func, ast.Attribute): func_name = node.func.attr if func_name in observation_tools: return clean_code
last_node = tree.body[-1] if ( isinstance(last_node, ast.Expr) and isinstance(last_node.value, ast.Call) and isinstance(last_node.value.func, ast.Name) and last_node.value.func.id == "print" and len(last_node.value.args) == 1 ): lines_list = clean_code.splitlines() start_idx = max(0, (getattr(last_node, "lineno", len(lines_list)) - 1)) prefix = "\n".join(lines_list[:start_idx]).strip() printed_arg_src = ast.get_source_segment(clean_code, last_node.value.args[0]) if not printed_arg_src: line_txt = lines_list[start_idx].strip() m_p = re.match(r"^print\((.*)\)$", line_txt) if m_p: printed_arg_src = m_p.group(1).strip() if printed_arg_src: final_snippet = ( f"_final_out = {printed_arg_src}\n" f"print(_final_out)\n" f"final_answer(_final_out)" ) return f"{prefix}\n{final_snippet}".strip() if prefix else final_snippet return clean_code def _extract_valid_python(raw: str) -> str: converted = _convert_xml_tool_calls(raw) if converted is not None:24 unmodified lines ): ident = tree.body[0].value.id return f"final_answer({json.dumps(ident, ensure_ascii=False)})" return _guard_unbound_lines(candidate) return _ensure_final_answer_call(_guard_unbound_lines(candidate)) except SyntaxError: continue 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 _guard_unbound_lines(plain) return _ensure_final_answer_call(_guard_unbound_lines(plain))
converted_top = _convert_xml_tool_calls(text) if converted_top is not None:292 unmodified lines kwargs.pop("temperature", None) if isinstance(getattr(self, "kwargs", None), dict): self.kwargs.pop("temperature", None) kwargs["max_tokens"] = 512 kwargs["max_tokens"] = 1536 if isinstance(getattr(self, "kwargs", None), dict): self.kwargs["max_tokens"] = 512 self.kwargs["max_tokens"] = 1536 if model_name.startswith("openrouter/"): kwargs["max_tokens"] = 512 kwargs["max_tokens"] = 1536 if isinstance(getattr(self, "kwargs", None), dict): self.kwargs["max_tokens"] = 512 self.kwargs["max_tokens"] = 1536 kwargs.pop("reasoning_effort", None) if isinstance(getattr(self, "kwargs", None), dict): self.kwargs.pop("reasoning_effort", None) if model_name.startswith("groq/"): kwargs["max_tokens"] = 384 kwargs["max_tokens"] = 1024 if isinstance(getattr(self, "kwargs", None), dict): self.kwargs["max_tokens"] = 384 self.kwargs["max_tokens"] = 1024 if "qwen3" in model_name.lower(): kwargs["reasoning_effort"] = "none" if model_name.startswith("nvidia_nim/"): kwargs["max_tokens"] = 450 kwargs["max_tokens"] = 1536 if isinstance(getattr(self, "kwargs", None), dict): self.kwargs["max_tokens"] = 450 self.kwargs["max_tokens"] = 1536
kwargs["num_retries"] = 0 kwargs["max_retries"] = 0191 unmodified lines "open": open, "os": os, "posixpath": posixpath, "repr": repr, } accepted_params = set(sig.parameters) | set(inspect.signature(MultiStepAgent.__init__).parameters) agent_kwargs = {64 unmodified lines print("CodeAgent initialized with LiteLLMModel.")
def _clean_exact_match_answer(self, text: str) -> str: import ast import re
if text is None:20 unmodified lines
cleaned = re.sub(r"(?im)^\s*(?:#\s*)?(?:thought|reasoning|plan)\s*:.*$", "", cleaned).strip()
if cleaned.startswith("final_answer(") and cleaned.endswith(")"): inner_fa = cleaned[len("final_answer("):-1].strip() inner_fa = re.sub(r"^answer\s*=\s*", "", inner_fa).strip() cleaned = inner_fa else: fa_call = re.search( r"final_answer\s*\(\s*(?:answer\s*=\s*)?" r"(?:(['\"])(.*?)\1|([^()]*?))" r"\s*\)", cleaned, flags=re.DOTALL, ) if fa_call: cleaned = (fa_call.group(2) or fa_call.group(3) or "").strip() fa_call = re.search( r"final_answer\s*\(\s*(?:answer\s*=\s*)?" r"(?:(['\"])(.*?)\1|([^()]*?))" r"\s*\)", cleaned, flags=re.DOTALL, ) if fa_call: cleaned = (fa_call.group(2) or fa_call.group(3) or "").strip()
arrow_match = re.search( r"\b(?:answer|result|output|final\s+answer)\s*->\s*(.+)$",34 unmodified lines if junk_pattern.match(cleaned): return ""
if ( (cleaned.startswith("[") and cleaned.endswith("]")) or (cleaned.startswith("(") and cleaned.endswith(")")) ): try: parsed_seq = ast.literal_eval(cleaned) if isinstance(parsed_seq, (list, tuple)): cleaned = ", ".join( str(item).strip() for item in parsed_seq if str(item).strip() ) except Exception: pass
if len(cleaned) >= 2 and cleaned[0] == cleaned[-1] and cleaned[0] in ('"', "'"): cleaned = cleaned[1:-1].strip()
if ( cleaned.endswith(".") and not cleaned.endswith("...") and not re.search(r"(?:^|\b[A-Za-z]\.)[A-Za-z]\.$", cleaned) and not re.search(r"\b(?:Mr|Mrs|Ms|Dr|Prof|Sr|Jr|St|Inc|Ltd|Co|vs|etc|e\.g|i\.e)\.$", cleaned) ): cleaned = cleaned[:-1].strip()
return cleaned
def __call__(self, question: str, task_id: str = "", file_name: str = "") -> str:app.py+110-21