Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files2440 unmodified lines244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022440 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:import time2440 unmodified lines244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022440 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:import time
app.py+55-55