Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files2125 unmodified lines212621272128212921302131213214 unmodified lines214721482149215021512152215360 unmodified lines2214221522162217221822192220614 unmodified lines283528362837283828392840229 unmodified lines3070307130723073307430752125 unmodified lines)return f"{prefix}\n{final_snippet}".strip() if prefix else final_snippetreturn clean_codedef _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 linesreturn f"{clean_code}\nprint({var_name})"return clean_codedef _extract_valid_python(raw: str) -> str:converted = _convert_xml_tool_calls(raw)if converted is not None:raw = converted60 unmodified linesif tag_matches:raw_block = tag_matches[-1].strip()converted = _convert_xml_tool_calls(raw_block)inner = converted if converted is not None else raw_blockinner = _guard_unbound_lines(inner)inner = _ensure_observable_output(inner)614 unmodified linesr"transcript/video analysis status:\s*unavailable)",re.IGNORECASE,)if tech_error_pattern.search(cleaned):return ""229 unmodified linesf"\n\nTask ID: {task_id}. The task attachment is downloaded at '{prefetched_path}'. "f"{tool_hint}")else:prompt += f"\n\nTask ID: {task_id}"if clean_file_name:2125 unmodified lines212621272128212921302131213214 unmodified lines214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219560 unmodified lines2256225722582259226022612262614 unmodified lines28772878287928802881288228832884229 unmodified lines311431153116311731183119312031213122312331242125 unmodified lines)return f"{prefix}\n{final_snippet}".strip() if prefix else final_snippetreturn clean_codedef _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 linesreturn f"{clean_code}\nprint({var_name})"return clean_codedef _convert_json_tool_calls(raw: str) -> str | None:stripped = raw.strip()if not (stripped.startswith("{") or stripped.startswith("[")):return Nonetry:payload = json.loads(stripped)except Exception:return Nonecalls = []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 = payloadpy_lines = []for idx, item in enumerate(calls):if not isinstance(item, dict):continuefn_obj = item.get("function") if isinstance(item.get("function"), dict) else itemfn_name = fn_obj.get("name")fn_args = fn_obj.get("arguments", {})if not fn_name or not isinstance(fn_name, str):continueif 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):continuevar_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 Nonedef _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 linesif tag_matches:raw_block = tag_matches[-1].strip()converted = _convert_json_tool_calls(raw_block) or _convert_xml_tool_calls(raw_block)inner = converted if converted is not None else raw_blockinner = _guard_unbound_lines(inner)inner = _ensure_observable_output(inner)614 unmodified linesr"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 linesf"\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