Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files2125 unmodified lines2126212721282129212921302131213214 unmodified lines214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219560 unmodified lines22562257225822172259226022612262614 unmodified lines28772878287928802881288228832884229 unmodified lines311431153116311731183119312031213122312331242125 unmodified lines ) return f"{prefix}\n{final_snippet}".strip() if prefix else final_snippet return clean_code
def _ensure_observable_output(code_str: str) -> str: clean_code = (code_str or "").strip() if not clean_code or "final_answer(" in clean_code:14 unmodified lines return f"{clean_code}\nprint({var_name})" return clean_code
def _convert_json_tool_calls(raw: str) -> str | None: stripped = raw.strip() if not (stripped.startswith("{") or stripped.startswith("[")): return None try: payload = json.loads(stripped) except Exception: return None
calls = [] if isinstance(payload, dict) and isinstance(payload.get("tool_calls"), list): calls = payload["tool_calls"] elif isinstance(payload, dict) and ("name" in payload or "function" in payload): calls = [payload] elif isinstance(payload, list): calls = payload
py_lines = [] for idx, item in enumerate(calls): if not isinstance(item, dict): continue fn_obj = item.get("function") if isinstance(item.get("function"), dict) else item fn_name = fn_obj.get("name") fn_args = fn_obj.get("arguments", {}) if not fn_name or not isinstance(fn_name, str): continue if isinstance(fn_args, str): try: fn_args = json.loads(fn_args) if fn_args.strip() else {} except Exception: fn_args = {"query": fn_args} if not isinstance(fn_args, dict): continue var_name = f"tool_res_{idx}" py_lines.append(f"{var_name} = {fn_name}(**{json.dumps(fn_args, ensure_ascii=False)})") py_lines.append(f"print({var_name})") return "\n".join(py_lines) if py_lines else None
def _extract_valid_python(raw: str) -> str: json_converted = _convert_json_tool_calls(raw) if json_converted is not None: return _ensure_observable_output(_guard_unbound_lines(json_converted))
converted = _convert_xml_tool_calls(raw) if converted is not None: raw = converted60 unmodified lines
if tag_matches: raw_block = tag_matches[-1].strip() converted = _convert_xml_tool_calls(raw_block) converted = _convert_json_tool_calls(raw_block) or _convert_xml_tool_calls(raw_block) inner = converted if converted is not None else raw_block inner = _guard_unbound_lines(inner) inner = _ensure_observable_output(inner)614 unmodified lines r"transcript/video analysis status:\s*unavailable)", re.IGNORECASE, ) if '"tool_calls"' in cleaned or re.search(r'^\s*\{\s*["\'](?:id|type|function|name)["\']\s*:', cleaned): return "" if tech_error_pattern.search(cleaned): return ""
229 unmodified lines f"\n\nTask ID: {task_id}. The task attachment is downloaded at '{prefetched_path}'. " f"{tool_hint}" ) else: prompt += ( f"\nThe file is ALREADY downloaded locally at '{prefetched_path}'. " "Do NOT call download_task_file again. Write Python code directly to compute the answer and call final_answer(...)." ) else: prompt += f"\n\nTask ID: {task_id}" if clean_file_name:app.py+51-2