Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files2497 unmodified lines24982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582497 unmodified linesprint("CodeAgent initialized with LiteLLMModel.")def _clean_exact_match_answer(self, text: str) -> str:import re, jsonif text is None:return ""cleaned = str(text).strip()if not cleaned or cleaned.startswith("AGENT ERROR:"):return cleaned# 1. usuń tagi code i markdowncleaned = re.sub(r"</?code[^>]*>", "", cleaned, flags=re.IGNORECASE)cleaned = re.sub(r"^```(?:\w+)?\s*|\s*```$", "", cleaned.strip(), flags=re.MULTILINE).strip()# 2. final_answer(...) → wartośćfa = re.fullmatch(r"final_answer\s*\(\s*(?:answer\s*=\s*)?(.*?)\s*\)", cleaned, flags=re.DOTALL)if fa:inner = fa.group(1).strip()if (inner.startswith('"') and inner.endswith('"')) or (inner.startswith("'") and inner.endswith("'")):try:cleaned = json.loads(inner) if inner.startswith('"') else inner[1:-1]except Exception:cleaned = inner.strip("\"'")else:cleaned = inner# 3. jedna linia = odpowiedź; wiele linii = ostatnia niepustalines = [ln.strip() for ln in cleaned.splitlines() if ln.strip()]if len(lines) > 1:for ln in reversed(lines):m = re.match(r"^(?:\*\*)?(?:final\s+answer|answer|result)\s*:?\s*(.+)$", ln, re.IGNORECASE)if m:cleaned = m.group(1).strip()breakelse:cleaned = lines[-1]elif lines:cleaned = lines[0]else:return ""# 4. zdejmij otaczające cudzysłowy / bold / backtickif len(cleaned) >= 2 and ((cleaned[0] == cleaned[-1] and cleaned[0] in "\"'`")or (cleaned.startswith("**") and cleaned.endswith("**"))or (cleaned.startswith("__") and cleaned.endswith("__"))):cleaned = cleaned.strip("\"'`").strip("*_").strip()# 5. liczby: usuń separatory tysięcy, zostaw kropkę dziesiętnąif re.fullmatch(r"-?\d{1,3}(?:,\d{3})+(?:\.\d+)?", cleaned):cleaned = cleaned.replace(",", "")# 6. usuń końcową kropkę (poza skrótami)if cleaned.endswith(".") and not re.search(r"\b(?:Mr|Mrs|Ms|Dr|St|Jr|Sr|Inc|Ltd|vs|etc)\.$", cleaned):cleaned = cleaned[:-1].strip()return cleaneddef __call__(self, question: str, task_id: str = "", file_name: str = "") -> str:2497 unmodified lines249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442497 unmodified linesprint("CodeAgent initialized with LiteLLMModel.")def _clean_exact_match_answer(self, text: str) -> str:import jsonimport reif text is None:return ""cleaned = str(text).strip()if not cleaned or cleaned.startswith("AGENT ERROR:"):return cleanedjson_block = re.search(r'\{\s*["\']tool["\']\s*:\s*["\'][^"\']+["\']\s*,\s*["\']arguments["\']\s*:\s*\{.*?\}\s*\}',cleaned,flags=re.DOTALL,)if json_block:cleaned = (cleaned[: json_block.start()] + cleaned[json_block.end() :]).strip()if not cleaned:return ""fa_call = re.search(r"final_answer\(\s*(?:answer\s*=\s*)?(['\"])(.*?)\1\s*\)",cleaned,flags=re.DOTALL,)if fa_call:cleaned = fa_call.group(2).strip()lines = [ln.strip(" \t\r\n-*•\"'") for ln in cleaned.splitlines() if ln.strip()]if not lines:return ""cleaned = lines[-1]if re.match(r"^(?: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|now i need to find|i found the)\b",cleaned,flags=re.IGNORECASE,):return ""if len(cleaned) > 160:return ""return cleaneddef __call__(self, question: str, task_id: str = "", file_name: str = "") -> str:
app.py+34-48