Spaces:
Sleeping
Sleeping
arijit121 commited on
Commit ·
bc62a2f
1
Parent(s): 3b60dbf
update
Browse files
app.py
CHANGED
|
@@ -30,13 +30,16 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 30 |
def build_agent():
|
| 31 |
"""Build a smolagents CodeAgent equipped for GAIA benchmark tasks."""
|
| 32 |
|
| 33 |
-
# Model — Use the HF Inference API
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
model = ModelClass(
|
| 37 |
max_tokens=4096,
|
| 38 |
temperature=0.1,
|
| 39 |
-
model_id=
|
| 40 |
custom_role_conversions=None,
|
| 41 |
)
|
| 42 |
|
|
@@ -61,29 +64,52 @@ def build_agent():
|
|
| 61 |
return agent
|
| 62 |
|
| 63 |
|
| 64 |
-
def extract_answer(raw_answer
|
| 65 |
-
"""
|
| 66 |
if raw_answer is None:
|
| 67 |
return ""
|
| 68 |
answer = str(raw_answer).strip()
|
| 69 |
|
| 70 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
prefixes = [
|
| 72 |
-
"FINAL ANSWER:",
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"The answer is:",
|
| 77 |
-
"Answer:",
|
| 78 |
]
|
| 79 |
for prefix in prefixes:
|
| 80 |
-
if answer.startswith(prefix):
|
| 81 |
answer = answer[len(prefix):].strip()
|
| 82 |
|
| 83 |
# Remove surrounding quotes if present
|
| 84 |
-
if (answer
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
return answer
|
| 89 |
|
|
@@ -150,7 +176,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 150 |
print(f"\n{'='*60}")
|
| 151 |
print(f"Question {i+1}/{len(questions_data)} (task_id: {task_id})")
|
| 152 |
print(f"Q: {question_text[:100]}...")
|
| 153 |
-
raw_answer = agent.run(question_text)
|
| 154 |
submitted_answer = extract_answer(raw_answer)
|
| 155 |
print(f"A: {submitted_answer}")
|
| 156 |
answers_payload.append({
|
|
|
|
| 30 |
def build_agent():
|
| 31 |
"""Build a smolagents CodeAgent equipped for GAIA benchmark tasks."""
|
| 32 |
|
| 33 |
+
# Model — Use the HF Inference API
|
| 34 |
+
# Try multiple models in order of preference
|
| 35 |
+
model_id = os.getenv(
|
| 36 |
+
"MODEL_ID",
|
| 37 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 38 |
+
)
|
| 39 |
model = ModelClass(
|
| 40 |
max_tokens=4096,
|
| 41 |
temperature=0.1,
|
| 42 |
+
model_id=model_id,
|
| 43 |
custom_role_conversions=None,
|
| 44 |
)
|
| 45 |
|
|
|
|
| 64 |
return agent
|
| 65 |
|
| 66 |
|
| 67 |
+
def extract_answer(raw_answer) -> str:
|
| 68 |
+
"""Aggressively clean agent output to extract only the final answer value."""
|
| 69 |
if raw_answer is None:
|
| 70 |
return ""
|
| 71 |
answer = str(raw_answer).strip()
|
| 72 |
|
| 73 |
+
# If the answer contains final_answer("..."), extract the argument
|
| 74 |
+
fa_match = re.search(r'final_answer\(["\'](.+?)["\']\)', answer, re.DOTALL)
|
| 75 |
+
if fa_match:
|
| 76 |
+
answer = fa_match.group(1).strip()
|
| 77 |
+
|
| 78 |
+
# Remove code blocks (```py ... ```)
|
| 79 |
+
answer = re.sub(r'```[\s\S]*?```', '', answer).strip()
|
| 80 |
+
|
| 81 |
+
# Remove <end_code> tags and surrounding artifacts
|
| 82 |
+
answer = re.sub(r'<end_code>.*', '', answer, flags=re.DOTALL).strip()
|
| 83 |
+
|
| 84 |
+
# Remove Calling tools: [...] JSON metadata
|
| 85 |
+
answer = re.sub(r'Calling tools:.*', '', answer, flags=re.DOTALL).strip()
|
| 86 |
+
|
| 87 |
+
# Remove "Using the `final_answer` tool:" and similar
|
| 88 |
+
answer = re.sub(r'Using the `final_answer` tool:.*', '', answer, flags=re.DOTALL).strip()
|
| 89 |
+
|
| 90 |
+
# Remove Thought: / Code: sections if they leaked through
|
| 91 |
+
answer = re.sub(r'^Thought:.*?(?=\S)', '', answer, flags=re.DOTALL).strip()
|
| 92 |
+
|
| 93 |
+
# Remove common prefixes
|
| 94 |
prefixes = [
|
| 95 |
+
"FINAL ANSWER:", "Final Answer:", "final answer:",
|
| 96 |
+
"The final answer is:", "The final answer is ",
|
| 97 |
+
"The answer is:", "The answer is ",
|
| 98 |
+
"Answer:", "Final answer:",
|
|
|
|
|
|
|
| 99 |
]
|
| 100 |
for prefix in prefixes:
|
| 101 |
+
if answer.lower().startswith(prefix.lower()):
|
| 102 |
answer = answer[len(prefix):].strip()
|
| 103 |
|
| 104 |
# Remove surrounding quotes if present
|
| 105 |
+
if len(answer) >= 2:
|
| 106 |
+
if (answer[0] == '"' and answer[-1] == '"') or \
|
| 107 |
+
(answer[0] == "'" and answer[-1] == "'"):
|
| 108 |
+
answer = answer[1:-1].strip()
|
| 109 |
+
|
| 110 |
+
# Remove trailing periods (unless it's a decimal number)
|
| 111 |
+
if answer.endswith('.') and not re.match(r'^\d+\.$', answer):
|
| 112 |
+
answer = answer[:-1].strip()
|
| 113 |
|
| 114 |
return answer
|
| 115 |
|
|
|
|
| 176 |
print(f"\n{'='*60}")
|
| 177 |
print(f"Question {i+1}/{len(questions_data)} (task_id: {task_id})")
|
| 178 |
print(f"Q: {question_text[:100]}...")
|
| 179 |
+
raw_answer = agent.run(question_text, reset=True)
|
| 180 |
submitted_answer = extract_answer(raw_answer)
|
| 181 |
print(f"A: {submitted_answer}")
|
| 182 |
answers_payload.append({
|