Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files2497 unmodified lines249824992500250125022501250225032504250525062507250825082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725092510251125122513251425152516251725182519252025212522252325242525252625272528252925302539254025412542254325442531253225332534253525362537254625472548254925502551255225532554255525382539254025412542254325442497 unmodified lines print("CodeAgent initialized with LiteLLMModel.")
def _clean_exact_match_answer(self, text: str) -> str: import re, json import json import re
if text is None: return "" cleaned = str(text).strip() if not cleaned or cleaned.startswith("AGENT ERROR:"): return cleaned # 1. usuń tagi code i markdown cleaned = 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 niepusta lines = [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() break else: cleaned = lines[-1] elif lines: cleaned = lines[0] else:
json_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 "" # 4. zdejmij otaczające cudzysłowy / bold / backtick if 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 = 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, ): 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 ""
if len(cleaned) > 160: return "" return cleaned
def __call__(self, question: str, task_id: str = "", file_name: str = "") -> str:app.py+34-48