CoolFace
Apppublic

ykumar2020/gaia-final-assignment

sourceHugging Faceupdated 1mo agoView on Hugging Face
0likes
answer_formatter.py68 linesDownload Raw Back to root
1"""Conservative, exact-match-safe final answer formatting."""
2
3from __future__ import annotations
4
5import re
6import unicodedata
7from decimal import Decimal, InvalidOperation
8
9_PREFIX = re.compile(
10    r"^\s*(?:final\s+answer|answer|submitted\s+answer)\s*:\s*", re.IGNORECASE
11)
12
13
14def format_answer(value: object) -> str:
15    """Remove presentation noise without changing answer semantics or case."""
16    text = unicodedata.normalize("NFC", str(value or ""))
17    text = text.replace("\r\n", "\n").replace("\r", "\n").strip()
18    had_prefix = bool(_PREFIX.match(text))
19    text = _PREFIX.sub("", text).strip()
20    if text.startswith("```") and text.endswith("```"):
21        lines = text.splitlines()
22        if len(lines) >= 3:
23            text = "\n".join(lines[1:-1]).strip()
24    if len(text) >= 2 and text[0] == text[-1] and text[0] in {'"', "'", "`"}:
25        text = text[1:-1].strip()
26    text = re.split(
27        r"\n+(?:explanation|reasoning|evidence|source)s?\s*:",
28        text,
29        maxsplit=1,
30        flags=re.IGNORECASE,
31    )[0]
32    if had_prefix and "\n" in text:
33        text = next((line.strip() for line in text.splitlines() if line.strip()), "")
34    if had_prefix:
35        text = re.split(
36            r"\s+(?:because|since)\s+", text, maxsplit=1, flags=re.IGNORECASE
37        )[0].strip()
38    # GAIA submissions are scalar strings; collapse accidental wrapping while
39    # preserving meaningful punctuation, casing, currency symbols, and commas.
40    text = re.sub(r"\s+", " ", text).strip()
41    if not text:
42        raise ValueError("Agent returned an empty answer")
43    return text
44
45
46def apply_requested_format(question: str, answer: object) -> str:
47    """Apply only explicit output-format constraints from the question."""
48    text = format_answer(answer)
49    lowered = question.lower()
50    if "usd" in lowered and "two decimal" in lowered:
51        numeric = re.sub(r"[^0-9.\-]", "", text)
52        try:
53            return f"${Decimal(numeric):,.2f}"
54        except InvalidOperation:
55            return text
56    if "ascending order" in lowered and (
57        "comma-delimited" in lowered or "comma separated" in lowered
58    ):
59        values = re.findall(r"-?\d+(?:\.\d+)?", text)
60        if values:
61            values.sort(key=Decimal)
62            return ", ".join(values)
63    if "alphabet" in lowered and ("comma" in lowered or "list" in lowered):
64        values = [item.strip() for item in text.split(",") if item.strip()]
65        if len(values) > 1:
66            return ", ".join(sorted(values, key=str.casefold))
67    return text
68