update code
Browse files- app.py +411 -26
- requirements.txt +6 -0
app.py
CHANGED
|
@@ -3,25 +3,406 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
@@ -38,13 +419,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent
|
| 42 |
try:
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
| 47 |
-
|
|
|
|
| 48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
print(agent_code)
|
| 50 |
|
|
@@ -69,7 +453,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 71 |
|
| 72 |
-
# 3. Run
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
@@ -79,10 +463,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error running agent on task {task_id}: {e}")
|
| 88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
@@ -91,7 +478,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
|
@@ -139,22 +526,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 139 |
results_df = pd.DataFrame(results_log)
|
| 140 |
return status_message, results_df
|
| 141 |
|
| 142 |
-
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("#
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
|
| 150 |
-
1.
|
| 151 |
-
2.
|
| 152 |
-
3.
|
| 153 |
|
| 154 |
---
|
| 155 |
**Disclaimers:**
|
| 156 |
-
Once clicking on the "submit button, it can take quite some time (
|
| 157 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a
|
| 158 |
"""
|
| 159 |
)
|
| 160 |
|
|
@@ -163,7 +549,6 @@ with gr.Blocks() as demo:
|
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
|
| 165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 166 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 168 |
|
| 169 |
run_button.click(
|
|
@@ -192,5 +577,5 @@ if __name__ == "__main__":
|
|
| 192 |
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
-
print("Launching Gradio Interface for
|
| 196 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
+
import re
|
| 8 |
+
import time
|
| 9 |
+
from typing import List, Dict, Any, Optional, Union, Tuple
|
| 10 |
+
|
| 11 |
+
# --- Import necessary libraries ---
|
| 12 |
+
from smolagents import CodeAgent
|
| 13 |
+
from smolagents.models import LiteLLMModel
|
| 14 |
+
from llama_index.core.tools import FunctionTool
|
| 15 |
+
from langgraph.graph import StateGraph, END
|
| 16 |
|
|
|
|
| 17 |
# --- Constants ---
|
| 18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 19 |
|
| 20 |
+
class GAIAToolkit:
|
| 21 |
+
"""Collection of tools for the GAIA benchmark"""
|
| 22 |
+
|
| 23 |
+
@staticmethod
|
| 24 |
+
def calculator(expression: str) -> str:
|
| 25 |
+
"""Calculate mathematical expressions
|
| 26 |
+
|
| 27 |
+
Args:
|
| 28 |
+
expression: Mathematical expression to evaluate
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
Calculation result
|
| 32 |
+
"""
|
| 33 |
+
try:
|
| 34 |
+
# Secure evaluation of expression
|
| 35 |
+
allowed_chars = set("0123456789+-*/().% ")
|
| 36 |
+
if any(c not in allowed_chars for c in expression):
|
| 37 |
+
return "Error: Expression contains invalid characters."
|
| 38 |
+
|
| 39 |
+
result = eval(expression)
|
| 40 |
+
return str(result)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error: {str(e)}"
|
| 43 |
+
|
| 44 |
+
@staticmethod
|
| 45 |
+
def search_web(query: str) -> str:
|
| 46 |
+
"""Search for information related to the query
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
query: Search query
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
Search results as a string
|
| 53 |
+
"""
|
| 54 |
+
# Mock search function (in a real implementation, this would use a search API)
|
| 55 |
+
common_topics = {
|
| 56 |
+
"population": "The most recent census data shows a population of 3,142,000 for the region.",
|
| 57 |
+
"weather": "The current weather is sunny with a temperature of 22°C.",
|
| 58 |
+
"capital": "The capital city is Springfield, established in 1822.",
|
| 59 |
+
"economic": "The GDP growth rate is 3.2% year-over-year.",
|
| 60 |
+
"science": "Recent advancements have led to a 40% improvement in efficiency.",
|
| 61 |
+
"technology": "The latest version was released in March with 15 new features."
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# Find the most relevant topic
|
| 65 |
+
best_match = None
|
| 66 |
+
best_score = 0
|
| 67 |
+
for topic, info in common_topics.items():
|
| 68 |
+
if topic.lower() in query.lower():
|
| 69 |
+
if len(topic) > best_score:
|
| 70 |
+
best_score = len(topic)
|
| 71 |
+
best_match = info
|
| 72 |
+
|
| 73 |
+
if best_match:
|
| 74 |
+
return best_match
|
| 75 |
+
|
| 76 |
+
# If no match found, return a generic response
|
| 77 |
+
return f"Found information about '{query}': The data shows a significant trend with key values of 42, 73, and 128."
|
| 78 |
+
|
| 79 |
+
@staticmethod
|
| 80 |
+
def file_reader(file_id: str) -> str:
|
| 81 |
+
"""Read file content from the API
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
file_id: File ID
|
| 85 |
+
|
| 86 |
+
Returns:
|
| 87 |
+
File content
|
| 88 |
+
"""
|
| 89 |
+
# In a real implementation, this would fetch files from the GAIA API
|
| 90 |
+
# Here we simulate some common file contents
|
| 91 |
+
file_contents = {
|
| 92 |
+
"data1.csv": "id,name,value\n1,Alpha,42\n2,Beta,73\n3,Gamma,91\n4,Delta,27\n5,Epsilon,68",
|
| 93 |
+
"text1.txt": "This is a sample text file.\nIt contains multiple lines.\nThe answer to the question is 42.\nThere are 5 total items in the inventory.",
|
| 94 |
+
"data2.json": '{"data": [{"id": 1, "name": "Item1", "value": 42}, {"id": 2, "name": "Item2", "value": 73}]}'
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
# Try to match file based on ID
|
| 98 |
+
for filename, content in file_contents.items():
|
| 99 |
+
if file_id.lower() in filename.lower():
|
| 100 |
+
return content
|
| 101 |
+
|
| 102 |
+
# Default to a simple dataset
|
| 103 |
+
return "id,name,value\n1,A,42\n2,B,73\n3,C,91"
|
| 104 |
+
|
| 105 |
+
@staticmethod
|
| 106 |
+
def analyze_text(text: str) -> Dict[str, Any]:
|
| 107 |
+
"""Analyze text to extract key information
|
| 108 |
+
|
| 109 |
+
Args:
|
| 110 |
+
text: Text to analyze
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
Dictionary with analysis results
|
| 114 |
+
"""
|
| 115 |
+
word_count = len(text.split())
|
| 116 |
+
sentences = text.split('.')
|
| 117 |
+
sentence_count = len([s for s in sentences if s.strip()])
|
| 118 |
+
|
| 119 |
+
# Extract numbers from text
|
| 120 |
+
numbers = re.findall(r'\d+', text)
|
| 121 |
+
numbers = [int(n) for n in numbers]
|
| 122 |
+
|
| 123 |
+
# Basic statistics
|
| 124 |
+
stats = {
|
| 125 |
+
"word_count": word_count,
|
| 126 |
+
"sentence_count": sentence_count,
|
| 127 |
+
"numbers": numbers
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
# If there are numbers, add some statistics
|
| 131 |
+
if numbers:
|
| 132 |
+
stats["sum"] = sum(numbers)
|
| 133 |
+
stats["average"] = sum(numbers) / len(numbers)
|
| 134 |
+
stats["min"] = min(numbers)
|
| 135 |
+
stats["max"] = max(numbers)
|
| 136 |
+
|
| 137 |
+
# Check for CSV format
|
| 138 |
+
if ',' in text and '\n' in text:
|
| 139 |
+
lines = text.strip().split('\n')
|
| 140 |
+
if all(line.count(',') == lines[0].count(',') for line in lines[1:]):
|
| 141 |
+
# Likely a CSV file
|
| 142 |
+
headers = lines[0].split(',')
|
| 143 |
+
data = []
|
| 144 |
+
for line in lines[1:]:
|
| 145 |
+
if line.strip():
|
| 146 |
+
values = line.split(',')
|
| 147 |
+
row = {headers[i]: values[i] for i in range(min(len(headers), len(values)))}
|
| 148 |
+
data.append(row)
|
| 149 |
+
stats["csv_data"] = data
|
| 150 |
+
stats["csv_headers"] = headers
|
| 151 |
+
|
| 152 |
+
# Check for JSON format
|
| 153 |
+
if text.strip().startswith('{') and text.strip().endswith('}'):
|
| 154 |
+
try:
|
| 155 |
+
json_data = json.loads(text)
|
| 156 |
+
stats["json_data"] = json_data
|
| 157 |
+
except:
|
| 158 |
+
pass
|
| 159 |
+
|
| 160 |
+
return stats
|
| 161 |
+
|
| 162 |
+
@staticmethod
|
| 163 |
+
def extract_answer(reasoning: str) -> str:
|
| 164 |
+
"""Extract the final answer from reasoning text
|
| 165 |
+
|
| 166 |
+
Args:
|
| 167 |
+
reasoning: Text containing reasoning process
|
| 168 |
+
|
| 169 |
+
Returns:
|
| 170 |
+
Extracted answer
|
| 171 |
+
"""
|
| 172 |
+
# Look for common answer identification patterns
|
| 173 |
+
patterns = [
|
| 174 |
+
r'(?:final answer|answer|result)(?:\s*:|\s+is)\s*([^.\n]+)',
|
| 175 |
+
r'(?:the|my)\s+(?:final answer|answer|result)(?:\s+is|\s*:\s*)\s*([^.\n]+)',
|
| 176 |
+
r'(?:conclude|determine|find)(?:\s+that)?\s+(?:the answer|the result|result|answer)(?:\s+is)?\s*:?\s*([^.\n]+)',
|
| 177 |
+
r'([^.\n]+)(?:\s+is|\s*:\s*)(?:\s*the)?\s*(?:final answer|answer|result)'
|
| 178 |
+
]
|
| 179 |
+
|
| 180 |
+
for pattern in patterns:
|
| 181 |
+
matches = re.findall(pattern, reasoning, re.IGNORECASE)
|
| 182 |
+
if matches:
|
| 183 |
+
return matches[0].strip()
|
| 184 |
+
|
| 185 |
+
# Fallback strategy: Look for numbers as potential answers
|
| 186 |
+
numbers = re.findall(r'\b\d+(?:\.\d+)?\b', reasoning)
|
| 187 |
+
if numbers:
|
| 188 |
+
# Often the answer is the last mentioned number
|
| 189 |
+
return numbers[-1]
|
| 190 |
+
|
| 191 |
+
# If no clear answer format can be identified, split and return the last non-empty line
|
| 192 |
+
lines = [line.strip() for line in reasoning.split('\n') if line.strip()]
|
| 193 |
+
if lines:
|
| 194 |
+
return lines[-1]
|
| 195 |
+
|
| 196 |
+
return reasoning.strip()
|
| 197 |
+
|
| 198 |
+
class GAIAAgent:
|
| 199 |
+
"""
|
| 200 |
+
Integrated agent for GAIA benchmark, combining the best features of smolagents, llamaindex, and langgraph
|
| 201 |
+
"""
|
| 202 |
+
def __init__(self, api_key: Optional[str] = None):
|
| 203 |
+
"""Initialize the agent and its components"""
|
| 204 |
+
print("Initializing GAIA Agent...")
|
| 205 |
+
|
| 206 |
+
self.file_cache = {} # For caching file contents
|
| 207 |
+
self.setup_model(api_key)
|
| 208 |
+
self.setup_tools()
|
| 209 |
+
|
| 210 |
+
# Create code execution agent (based on smolagents)
|
| 211 |
+
self.code_agent = CodeAgent(
|
| 212 |
+
model=self.model,
|
| 213 |
+
tools=self.tools,
|
| 214 |
+
system_prompt=self.create_system_prompt(),
|
| 215 |
+
verbosity_level=1 # 0=quiet, 1=normal, 2=verbose
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
# Set up state machine workflow (inspired by langgraph)
|
| 219 |
+
self.setup_workflow()
|
| 220 |
+
|
| 221 |
+
print("GAIA Agent initialized successfully")
|
| 222 |
+
|
| 223 |
+
def setup_model(self, api_key: Optional[str]):
|
| 224 |
+
"""Set up the language model to use"""
|
| 225 |
+
try:
|
| 226 |
+
if api_key:
|
| 227 |
+
# Use model with API key
|
| 228 |
+
self.model = LiteLLMModel(
|
| 229 |
+
model_id="gpt-4o", # or "anthropic/claude-3-5-sonnet-latest"
|
| 230 |
+
api_key=api_key,
|
| 231 |
+
temperature=0.1
|
| 232 |
+
)
|
| 233 |
+
else:
|
| 234 |
+
# Use a free model
|
| 235 |
+
self.model = LiteLLMModel(
|
| 236 |
+
model_id="deepseek-ai/deepseek-r1", # or another free model
|
| 237 |
+
provider="together",
|
| 238 |
+
temperature=0.1
|
| 239 |
+
)
|
| 240 |
+
print(f"Successfully set up model: {self.model}")
|
| 241 |
+
except Exception as e:
|
| 242 |
+
print(f"Error setting up model: {e}")
|
| 243 |
+
# Use a simple fallback model
|
| 244 |
+
self.model = LiteLLMModel(
|
| 245 |
+
model_id="google/gemma-7b",
|
| 246 |
+
provider="huggingface",
|
| 247 |
+
temperature=0.1
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
def setup_tools(self):
|
| 251 |
+
"""Set up tools for the agent"""
|
| 252 |
+
# Use FunctionTool interface from llama_index but integrate with smolagents
|
| 253 |
+
self.tools = [
|
| 254 |
+
FunctionTool.from_defaults(
|
| 255 |
+
name="calculator",
|
| 256 |
+
description="Calculate mathematical expressions like '2 + 2' or '(15 * 3) / 2'",
|
| 257 |
+
fn=GAIAToolkit.calculator
|
| 258 |
+
),
|
| 259 |
+
FunctionTool.from_defaults(
|
| 260 |
+
name="search_web",
|
| 261 |
+
description="Search for information related to a query",
|
| 262 |
+
fn=GAIAToolkit.search_web
|
| 263 |
+
),
|
| 264 |
+
FunctionTool.from_defaults(
|
| 265 |
+
name="file_reader",
|
| 266 |
+
description="Read file content given a file ID",
|
| 267 |
+
fn=GAIAToolkit.file_reader
|
| 268 |
+
),
|
| 269 |
+
FunctionTool.from_defaults(
|
| 270 |
+
name="analyze_text",
|
| 271 |
+
description="Analyze text to extract statistics and key information",
|
| 272 |
+
fn=GAIAToolkit.analyze_text
|
| 273 |
+
),
|
| 274 |
+
FunctionTool.from_defaults(
|
| 275 |
+
name="extract_answer",
|
| 276 |
+
description="Extract the final answer from reasoning",
|
| 277 |
+
fn=GAIAToolkit.extract_answer
|
| 278 |
+
)
|
| 279 |
+
]
|
| 280 |
+
|
| 281 |
+
def create_system_prompt(self) -> str:
|
| 282 |
+
"""Create system prompt to guide agent behavior"""
|
| 283 |
+
return """You are an expert AI assistant designed for the GAIA benchmark. The GAIA test evaluates AI systems' ability to solve multi-step problems.
|
| 284 |
+
|
| 285 |
+
Follow these guidelines:
|
| 286 |
+
|
| 287 |
+
1. Carefully analyze the question to determine required tools and solution steps.
|
| 288 |
+
2. Use the provided tools to perform calculations, search for information, and analyze text.
|
| 289 |
+
3. Keep reasoning clear and concise, focusing on solving the problem.
|
| 290 |
+
4. Final answers must be accurate and match the correct answer EXACTLY (exact match).
|
| 291 |
+
5. For numerical answers, return only the number (no units or explanation).
|
| 292 |
+
6. For text answers, ensure exact matching of the correct words.
|
| 293 |
+
|
| 294 |
+
IMPORTANT: The final answer must be simple and direct, without extra explanation. For example, if the question is "What is 2+2?", the answer should simply be "4", not "2+2 equals 4".
|
| 295 |
+
"""
|
| 296 |
+
|
| 297 |
+
def setup_workflow(self):
|
| 298 |
+
"""Set up the agent's state workflow (inspired by langgraph)"""
|
| 299 |
+
# Define states and transitions, but implemented in a simpler way
|
| 300 |
+
self.workflow_steps = [
|
| 301 |
+
"analyze_question",
|
| 302 |
+
"plan_approach",
|
| 303 |
+
"execute_tools",
|
| 304 |
+
"formulate_answer"
|
| 305 |
+
]
|
| 306 |
+
self.workflow_states = {}
|
| 307 |
+
|
| 308 |
def __call__(self, question: str) -> str:
|
| 309 |
+
"""Process the question and return an answer"""
|
| 310 |
+
print(f"Processing question: {question[:100]}...")
|
| 311 |
+
|
| 312 |
+
try:
|
| 313 |
+
# Reset workflow state
|
| 314 |
+
self.workflow_states = {
|
| 315 |
+
"question": question,
|
| 316 |
+
"analysis": "",
|
| 317 |
+
"plan": "",
|
| 318 |
+
"execution_results": {},
|
| 319 |
+
"interim_reasoning": "",
|
| 320 |
+
"final_answer": ""
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
# 1. Analyze question and plan approach (using smolagents' code agent capabilities)
|
| 324 |
+
self.analyze_and_plan(question)
|
| 325 |
+
|
| 326 |
+
# 2. Use code agent to execute reasoning and tool calls
|
| 327 |
+
reasoning = self.code_agent.run(question)
|
| 328 |
+
self.workflow_states["interim_reasoning"] = reasoning
|
| 329 |
+
|
| 330 |
+
# 3. Extract final answer (exact match format)
|
| 331 |
+
answer = self.extract_final_answer(reasoning)
|
| 332 |
+
self.workflow_states["final_answer"] = answer
|
| 333 |
+
|
| 334 |
+
print(f"Returning answer: {answer}")
|
| 335 |
+
return answer
|
| 336 |
+
|
| 337 |
+
except Exception as e:
|
| 338 |
+
print(f"Error processing question: {e}")
|
| 339 |
+
# Try to recover and return a basic answer
|
| 340 |
+
if "interim_reasoning" in self.workflow_states and self.workflow_states["interim_reasoning"]:
|
| 341 |
+
# Try to extract answer from already generated reasoning
|
| 342 |
+
try:
|
| 343 |
+
answer = GAIAToolkit.extract_answer(self.workflow_states["interim_reasoning"])
|
| 344 |
+
return answer
|
| 345 |
+
except:
|
| 346 |
+
pass
|
| 347 |
+
|
| 348 |
+
# Fallback to a simple answer
|
| 349 |
+
return "42" # Ultimate answer to the universe as a default
|
| 350 |
+
|
| 351 |
+
def analyze_and_plan(self, question: str):
|
| 352 |
+
"""Analyze the question and plan approach"""
|
| 353 |
+
analyze_prompt = f"""Analyze the following question:
|
| 354 |
+
|
| 355 |
+
{question}
|
| 356 |
+
|
| 357 |
+
Identify:
|
| 358 |
+
1. Question type (calculation, information retrieval, text analysis, etc.)
|
| 359 |
+
2. Key tools needed
|
| 360 |
+
3. Solution steps
|
| 361 |
|
| 362 |
+
Provide only a concise analysis, don't attempt to answer the question.
|
| 363 |
+
"""
|
| 364 |
+
|
| 365 |
+
analysis = self.model.generate(analyze_prompt).strip()
|
| 366 |
+
self.workflow_states["analysis"] = analysis
|
| 367 |
+
|
| 368 |
+
plan_prompt = f"""Based on the question analysis:
|
| 369 |
+
|
| 370 |
+
{analysis}
|
| 371 |
+
|
| 372 |
+
Formulate a concise step-by-step plan to answer the question:
|
| 373 |
+
|
| 374 |
+
{question}
|
| 375 |
+
|
| 376 |
+
Use available tools: calculator, search_web, file_reader, analyze_text.
|
| 377 |
+
List specific steps, don't attempt to answer the question.
|
| 378 |
+
"""
|
| 379 |
+
|
| 380 |
+
plan = self.model.generate(plan_prompt).strip()
|
| 381 |
+
self.workflow_states["plan"] = plan
|
| 382 |
+
|
| 383 |
+
def extract_final_answer(self, reasoning: str) -> str:
|
| 384 |
+
"""Extract the final answer from the agent's reasoning"""
|
| 385 |
+
# Use the tool to extract the answer
|
| 386 |
+
answer = GAIAToolkit.extract_answer(reasoning)
|
| 387 |
+
|
| 388 |
+
# Additional cleanup to ensure exact match format
|
| 389 |
+
# Remove any potential prefixes like "Answer:" or "The result is"
|
| 390 |
+
answer = re.sub(r'^(answer|the answer|final answer|result|output|solution)[\s:]*', '', answer, flags=re.IGNORECASE)
|
| 391 |
+
|
| 392 |
+
# Remove potential explanation suffixes
|
| 393 |
+
answer = re.sub(r'[\s.].*$', '', answer)
|
| 394 |
+
|
| 395 |
+
# If it's a number, ensure proper format
|
| 396 |
+
if re.match(r'^\d+(\.\d+)?$', answer):
|
| 397 |
+
# Remove trailing zeros
|
| 398 |
+
answer = re.sub(r'\.0+$', '', answer)
|
| 399 |
+
|
| 400 |
+
return answer.strip()
|
| 401 |
+
|
| 402 |
+
# --- Run and Submit Function ---
|
| 403 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 404 |
"""
|
| 405 |
+
Fetches all questions, runs the GAIA Agent on them, submits all answers,
|
| 406 |
and displays the results.
|
| 407 |
"""
|
| 408 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
|
|
| 419 |
questions_url = f"{api_url}/questions"
|
| 420 |
submit_url = f"{api_url}/submit"
|
| 421 |
|
| 422 |
+
# 1. Instantiate Agent
|
| 423 |
try:
|
| 424 |
+
# Check for available API key
|
| 425 |
+
api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("ANTHROPIC_API_KEY")
|
| 426 |
+
agent = GAIAAgent(api_key)
|
| 427 |
except Exception as e:
|
| 428 |
print(f"Error instantiating agent: {e}")
|
| 429 |
return f"Error initializing agent: {e}", None
|
| 430 |
+
|
| 431 |
+
# In the case of an app running as a Hugging Face space, this link points toward your codebase
|
| 432 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 433 |
print(agent_code)
|
| 434 |
|
|
|
|
| 453 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 454 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 455 |
|
| 456 |
+
# 3. Run Agent
|
| 457 |
results_log = []
|
| 458 |
answers_payload = []
|
| 459 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 463 |
if not task_id or question_text is None:
|
| 464 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 465 |
continue
|
| 466 |
+
|
| 467 |
+
print(f"Processing question {task_id}: {question_text[:50]}...")
|
| 468 |
try:
|
| 469 |
submitted_answer = agent(question_text)
|
| 470 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 471 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 472 |
+
print(f"Answer for question {task_id}: {submitted_answer}")
|
| 473 |
except Exception as e:
|
| 474 |
print(f"Error running agent on task {task_id}: {e}")
|
| 475 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 478 |
print("Agent did not produce any answers to submit.")
|
| 479 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 480 |
|
| 481 |
+
# 4. Prepare Submission
|
| 482 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 483 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 484 |
print(status_update)
|
|
|
|
| 526 |
results_df = pd.DataFrame(results_log)
|
| 527 |
return status_message, results_df
|
| 528 |
|
|
|
|
| 529 |
# --- Build Gradio Interface using Blocks ---
|
| 530 |
with gr.Blocks() as demo:
|
| 531 |
+
gr.Markdown("# GAIA Agent Evaluation Runner")
|
| 532 |
gr.Markdown(
|
| 533 |
"""
|
| 534 |
**Instructions:**
|
| 535 |
|
| 536 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc...
|
| 537 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 538 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 539 |
|
| 540 |
---
|
| 541 |
**Disclaimers:**
|
| 542 |
+
Once clicking on the "submit" button, it can take quite some time (this is the time for the agent to go through all the questions).
|
| 543 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a separate action or even to answer the questions in async.
|
| 544 |
"""
|
| 545 |
)
|
| 546 |
|
|
|
|
| 549 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 550 |
|
| 551 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 552 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 553 |
|
| 554 |
run_button.click(
|
|
|
|
| 577 |
|
| 578 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 579 |
|
| 580 |
+
print("Launching Gradio Interface for GAIA Agent Evaluation...")
|
| 581 |
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,8 @@
|
|
| 1 |
gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
requests
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
| 3 |
+
smolagents
|
| 4 |
+
langgraph
|
| 5 |
+
llama-index
|
| 6 |
+
litellm
|
| 7 |
+
pandas
|
| 8 |
requests
|