Yago Bolivar
commited on
Commit
·
2abc50d
1
Parent(s):
87aad23
feat: add GAIA Agent and local testing scripts, including setup and requirements for development
Browse files- app2.py +617 -0
- app_local.py +192 -0
- quick_setup.sh +28 -0
- requirements.txt +14 -5
- run_local.sh +8 -0
- setup.sh +39 -0
- test_agent.py +92 -0
- test_question.py +49 -0
- update_files.py +46 -0
app2.py
ADDED
@@ -0,0 +1,617 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /Users/yagoairm2/Desktop/agents/final project/HF_Agents_Final_Project/app2.py
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
import pandas as pd
|
6 |
+
import json
|
7 |
+
from typing import Dict, List, Optional, Union, Any
|
8 |
+
import re
|
9 |
+
from dataclasses import dataclass
|
10 |
+
from abc import ABC, abstractmethod
|
11 |
+
import time
|
12 |
+
import logging
|
13 |
+
from dotenv import load_dotenv
|
14 |
+
import tempfile
|
15 |
+
import io
|
16 |
+
import sys
|
17 |
+
import contextlib
|
18 |
+
from urllib.parse import urlparse
|
19 |
+
from pathlib import Path
|
20 |
+
|
21 |
+
# Configure logging
|
22 |
+
logging.basicConfig(
|
23 |
+
level=logging.INFO,
|
24 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
25 |
+
handlers=[logging.StreamHandler()]
|
26 |
+
)
|
27 |
+
logger = logging.getLogger(__name__)
|
28 |
+
|
29 |
+
# --- Constants ---
|
30 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
31 |
+
DEFAULT_FILES_DIR = "dataset"
|
32 |
+
SYSTEM_PROMPT = """
|
33 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
34 |
+
"""
|
35 |
+
|
36 |
+
# --- Tool Interface ---
|
37 |
+
class Tool(ABC):
|
38 |
+
"""Base class for all tools that agent can use."""
|
39 |
+
name: str
|
40 |
+
description: str
|
41 |
+
|
42 |
+
@abstractmethod
|
43 |
+
def run(self, **kwargs) -> Dict[str, Any]:
|
44 |
+
"""Execute the tool with the provided arguments."""
|
45 |
+
pass
|
46 |
+
|
47 |
+
# --- Tools Implementation ---
|
48 |
+
class WebSearchTool(Tool):
|
49 |
+
"""Tool for performing web searches."""
|
50 |
+
name = "web_search"
|
51 |
+
description = "Search the web for information about a topic."
|
52 |
+
|
53 |
+
def __init__(self):
|
54 |
+
# Initialize any search API clients or session objects here
|
55 |
+
pass
|
56 |
+
|
57 |
+
def run(self, query: str) -> Dict[str, Any]:
|
58 |
+
"""
|
59 |
+
Perform a web search with the given query.
|
60 |
+
|
61 |
+
Args:
|
62 |
+
query: The search query
|
63 |
+
|
64 |
+
Returns:
|
65 |
+
Dict with search results
|
66 |
+
"""
|
67 |
+
# In a real implementation, this would use a search API
|
68 |
+
logger.info(f"WebSearchTool: Searching for '{query}'")
|
69 |
+
|
70 |
+
# Mock implementation - would be replaced with real search API
|
71 |
+
# You'd implement this with a proper search API like SerpAPI, Google Custom Search, etc.
|
72 |
+
time.sleep(1) # Simulate network delay
|
73 |
+
|
74 |
+
return {
|
75 |
+
"status": "success",
|
76 |
+
"results": [
|
77 |
+
{"title": f"Mock result for {query}", "snippet": "This is a placeholder for search results.", "url": "https://example.com"}
|
78 |
+
]
|
79 |
+
}
|
80 |
+
|
81 |
+
class FileReaderTool(Tool):
|
82 |
+
"""Tool for reading and processing different types of files."""
|
83 |
+
name = "file_reader"
|
84 |
+
description = "Read and process files of various formats."
|
85 |
+
|
86 |
+
def __init__(self, files_dir: str = DEFAULT_FILES_DIR):
|
87 |
+
self.files_dir = files_dir
|
88 |
+
|
89 |
+
def run(self, task_id: str, file_name: str) -> Dict[str, Any]:
|
90 |
+
"""
|
91 |
+
Read and process a file associated with a task.
|
92 |
+
|
93 |
+
Args:
|
94 |
+
task_id: The task identifier
|
95 |
+
file_name: Name of the file to process
|
96 |
+
|
97 |
+
Returns:
|
98 |
+
Dict with file content or error message
|
99 |
+
"""
|
100 |
+
try:
|
101 |
+
# First, try to find the file locally
|
102 |
+
file_path = os.path.join(self.files_dir, task_id, file_name)
|
103 |
+
|
104 |
+
if not os.path.exists(file_path):
|
105 |
+
# If file doesn't exist locally, try to download it
|
106 |
+
file_path = self._download_file(task_id, file_name)
|
107 |
+
|
108 |
+
# Process the file based on its extension
|
109 |
+
file_ext = os.path.splitext(file_name)[1].lower()
|
110 |
+
|
111 |
+
if file_ext in ['.txt', '.md', '.py', '.json', '.csv']:
|
112 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
113 |
+
content = f.read()
|
114 |
+
return {"status": "success", "content": content, "file_type": "text"}
|
115 |
+
|
116 |
+
elif file_ext in ['.png', '.jpg', '.jpeg']:
|
117 |
+
# For images, we'd use a vision model in the full implementation
|
118 |
+
return {"status": "success", "content": f"Image file: {file_path}", "file_type": "image"}
|
119 |
+
|
120 |
+
elif file_ext in ['.mp3', '.wav', '.ogg']:
|
121 |
+
# For audio, we'd use a speech-to-text model in the full implementation
|
122 |
+
return {"status": "success", "content": f"Audio file: {file_path}", "file_type": "audio"}
|
123 |
+
|
124 |
+
elif file_ext in ['.xlsx', '.xls']:
|
125 |
+
# For Excel files, we'd use pandas in the full implementation
|
126 |
+
return {"status": "success", "content": f"Excel file: {file_path}", "file_type": "spreadsheet"}
|
127 |
+
|
128 |
+
else:
|
129 |
+
return {"status": "error", "error": f"Unsupported file type: {file_ext}"}
|
130 |
+
|
131 |
+
except Exception as e:
|
132 |
+
logger.error(f"Error processing file {file_name}: {e}")
|
133 |
+
return {"status": "error", "error": str(e)}
|
134 |
+
|
135 |
+
def _download_file(self, task_id: str, file_name: str) -> str:
|
136 |
+
"""Download a file from the API and save it locally."""
|
137 |
+
api_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
138 |
+
|
139 |
+
logger.info(f"Downloading file for task {task_id}")
|
140 |
+
response = requests.get(api_url, timeout=30)
|
141 |
+
|
142 |
+
if response.status_code != 200:
|
143 |
+
raise Exception(f"Failed to download file: {response.status_code}")
|
144 |
+
|
145 |
+
# Create directory if it doesn't exist
|
146 |
+
os.makedirs(os.path.join(self.files_dir, task_id), exist_ok=True)
|
147 |
+
|
148 |
+
# Save file
|
149 |
+
file_path = os.path.join(self.files_dir, task_id, file_name)
|
150 |
+
with open(file_path, 'wb') as f:
|
151 |
+
f.write(response.content)
|
152 |
+
|
153 |
+
logger.info(f"File saved to {file_path}")
|
154 |
+
return file_path
|
155 |
+
|
156 |
+
class CodeInterpreterTool(Tool):
|
157 |
+
"""Tool for executing Python code safely."""
|
158 |
+
name = "code_interpreter"
|
159 |
+
description = "Execute Python code and return the result."
|
160 |
+
|
161 |
+
def run(self, code: str) -> Dict[str, Any]:
|
162 |
+
"""
|
163 |
+
Execute Python code and capture output.
|
164 |
+
|
165 |
+
Args:
|
166 |
+
code: The Python code to execute
|
167 |
+
|
168 |
+
Returns:
|
169 |
+
Dict with execution results
|
170 |
+
"""
|
171 |
+
logger.info("Running code interpreter")
|
172 |
+
|
173 |
+
output = io.StringIO()
|
174 |
+
error = io.StringIO()
|
175 |
+
|
176 |
+
try:
|
177 |
+
# Capture stdout and stderr
|
178 |
+
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(error):
|
179 |
+
# Execute the code in a restricted environment
|
180 |
+
exec_globals = {"__builtins__": {}}
|
181 |
+
|
182 |
+
# Add safe modules to globals
|
183 |
+
for safe_module in ["math", "random", "datetime", "re"]:
|
184 |
+
try:
|
185 |
+
exec_globals[safe_module] = __import__(safe_module)
|
186 |
+
except ImportError:
|
187 |
+
pass
|
188 |
+
|
189 |
+
# Execute the code
|
190 |
+
exec(code, exec_globals)
|
191 |
+
|
192 |
+
return {
|
193 |
+
"status": "success",
|
194 |
+
"stdout": output.getvalue(),
|
195 |
+
"stderr": error.getvalue()
|
196 |
+
}
|
197 |
+
|
198 |
+
except Exception as e:
|
199 |
+
return {
|
200 |
+
"status": "error",
|
201 |
+
"error": str(e),
|
202 |
+
"stdout": output.getvalue(),
|
203 |
+
"stderr": error.getvalue()
|
204 |
+
}
|
205 |
+
|
206 |
+
# --- LLM Interaction Module ---
|
207 |
+
class LLMModule:
|
208 |
+
"""Module for interacting with an LLM."""
|
209 |
+
|
210 |
+
def __init__(self, model_name: str = "Meta-Llama-3-8B-Instruct.Q4_0.gguf"):
|
211 |
+
"""Initialize the LLM module with a specified model."""
|
212 |
+
self.model_name = model_name
|
213 |
+
try:
|
214 |
+
from gpt4all import GPT4All
|
215 |
+
logger.info(f"Initializing GPT4All model: {model_name}")
|
216 |
+
self.model = GPT4All(model_name, allow_download=True)
|
217 |
+
logger.info("GPT4All model initialized successfully")
|
218 |
+
self.use_mock = False
|
219 |
+
except Exception as e:
|
220 |
+
logger.warning(f"Failed to initialize GPT4All model: {e}")
|
221 |
+
logger.warning("Using mock responses instead")
|
222 |
+
self.use_mock = True
|
223 |
+
|
224 |
+
def generate(self, prompt: str, system_prompt: str = None) -> str:
|
225 |
+
"""
|
226 |
+
Generate text using the LLM.
|
227 |
+
|
228 |
+
Args:
|
229 |
+
prompt: The user prompt
|
230 |
+
system_prompt: Optional system prompt
|
231 |
+
|
232 |
+
Returns:
|
233 |
+
Generated text
|
234 |
+
"""
|
235 |
+
logger.info(f"LLM: Generating response for prompt (first 50 chars): {prompt[:50]}...")
|
236 |
+
|
237 |
+
if self.use_mock:
|
238 |
+
# Fall back to mock response if model initialization failed
|
239 |
+
logger.warning("Using mock response")
|
240 |
+
response = f"This is a mock LLM response. I'm simulating thinking about: {prompt[:30]}...\n\nFINAL ANSWER: Mock answer"
|
241 |
+
return response
|
242 |
+
|
243 |
+
try:
|
244 |
+
# Combine system prompt and user prompt if system prompt is provided
|
245 |
+
full_prompt = prompt
|
246 |
+
if system_prompt:
|
247 |
+
full_prompt = f"{system_prompt}\n\n{prompt}"
|
248 |
+
|
249 |
+
# Generate response using GPT4All
|
250 |
+
with self.model.chat_session():
|
251 |
+
response = self.model.generate(full_prompt, max_tokens=1024, temp=0.7)
|
252 |
+
|
253 |
+
logger.info(f"LLM response (first 50 chars): {response[:50]}...")
|
254 |
+
return response
|
255 |
+
|
256 |
+
except Exception as e:
|
257 |
+
logger.error(f"Error generating response: {e}")
|
258 |
+
# Fall back to mock response if generation fails
|
259 |
+
response = f"Error generating LLM response. Falling back to mock response.\n\nFINAL ANSWER: Error occurred"
|
260 |
+
return response
|
261 |
+
|
262 |
+
def extract_final_answer(self, text: str) -> str:
|
263 |
+
"""Extract the final answer from LLM output using regex."""
|
264 |
+
match = re.search(r"FINAL ANSWER:\s*(.*?)(?:\n|$)", text, re.IGNORECASE)
|
265 |
+
if match:
|
266 |
+
return match.group(1).strip()
|
267 |
+
return text.strip()
|
268 |
+
|
269 |
+
# --- GAIA Agent Implementation ---
|
270 |
+
class GAIAAgent:
|
271 |
+
"""
|
272 |
+
Agent designed to answer questions from the GAIA benchmark.
|
273 |
+
|
274 |
+
This agent analyzes questions, selects appropriate tools, and generates answers.
|
275 |
+
"""
|
276 |
+
|
277 |
+
def __init__(self):
|
278 |
+
"""Initialize the GAIA agent with its tools and LLM."""
|
279 |
+
logger.info("Initializing GAIA Agent")
|
280 |
+
|
281 |
+
# Initialize LLM
|
282 |
+
self.llm = LLMModule()
|
283 |
+
|
284 |
+
# Initialize tools
|
285 |
+
self.tools = {
|
286 |
+
"web_search": WebSearchTool(),
|
287 |
+
"file_reader": FileReaderTool(),
|
288 |
+
"code_interpreter": CodeInterpreterTool()
|
289 |
+
}
|
290 |
+
|
291 |
+
def __call__(self, question: str) -> str:
|
292 |
+
"""
|
293 |
+
Answer a question using the agent's tools and reasoning capabilities.
|
294 |
+
|
295 |
+
Args:
|
296 |
+
question: The question to answer
|
297 |
+
|
298 |
+
Returns:
|
299 |
+
The agent's answer
|
300 |
+
"""
|
301 |
+
logger.info(f"Agent received question: {question[:100]}...")
|
302 |
+
|
303 |
+
# Step 1: Analyze the question to determine the approach
|
304 |
+
plan = self._plan_approach(question)
|
305 |
+
|
306 |
+
# Step 2: Execute the plan using tools if needed
|
307 |
+
tool_results = self._execute_plan(plan, question)
|
308 |
+
|
309 |
+
# Step 3: Generate the final answer
|
310 |
+
answer = self._generate_answer(question, plan, tool_results)
|
311 |
+
|
312 |
+
logger.info(f"Agent returning answer: {answer}")
|
313 |
+
return answer
|
314 |
+
|
315 |
+
def _plan_approach(self, question: str) -> Dict[str, Any]:
|
316 |
+
"""
|
317 |
+
Analyze the question and plan how to answer it.
|
318 |
+
|
319 |
+
Args:
|
320 |
+
question: The question to analyze
|
321 |
+
|
322 |
+
Returns:
|
323 |
+
Dict with the plan details
|
324 |
+
"""
|
325 |
+
# In a full implementation, this would use the LLM to analyze the question
|
326 |
+
# and determine what tools are needed
|
327 |
+
|
328 |
+
# For now, using a simple keyword-based approach
|
329 |
+
plan = {
|
330 |
+
"tools_needed": [],
|
331 |
+
"reasoning": "Determining how to approach this question..."
|
332 |
+
}
|
333 |
+
|
334 |
+
# Check for mentions of files
|
335 |
+
file_pattern = r"file[:\s]+([^\s.,?!]+)"
|
336 |
+
file_match = re.search(file_pattern, question, re.IGNORECASE)
|
337 |
+
if file_match:
|
338 |
+
plan["tools_needed"].append("file_reader")
|
339 |
+
plan["file_name"] = file_match.group(1)
|
340 |
+
|
341 |
+
# Check for mentions of websites, URLs, or internet searches
|
342 |
+
if any(term in question.lower() for term in ["website", "url", "search", "internet", "online", "web", "wikipedia"]):
|
343 |
+
plan["tools_needed"].append("web_search")
|
344 |
+
|
345 |
+
# Check for code execution needs
|
346 |
+
if any(term in question.lower() for term in ["code", "python", "execute", "run", "script", "program"]):
|
347 |
+
plan["tools_needed"].append("code_interpreter")
|
348 |
+
|
349 |
+
return plan
|
350 |
+
|
351 |
+
def _execute_plan(self, plan: Dict[str, Any], question: str) -> Dict[str, Any]:
|
352 |
+
"""
|
353 |
+
Execute the plan using the appropriate tools.
|
354 |
+
|
355 |
+
Args:
|
356 |
+
plan: The plan created by _plan_approach
|
357 |
+
question: The original question
|
358 |
+
|
359 |
+
Returns:
|
360 |
+
Dict with results from tool executions
|
361 |
+
"""
|
362 |
+
results = {}
|
363 |
+
|
364 |
+
for tool_name in plan.get("tools_needed", []):
|
365 |
+
if tool_name in self.tools:
|
366 |
+
tool = self.tools[tool_name]
|
367 |
+
|
368 |
+
if tool_name == "web_search":
|
369 |
+
# Extract search terms from the question
|
370 |
+
search_query = question # In a full implementation, you'd extract key terms
|
371 |
+
results[tool_name] = tool.run(query=search_query)
|
372 |
+
|
373 |
+
elif tool_name == "file_reader" and "file_name" in plan:
|
374 |
+
# In a full implementation, you'd extract task_id from context
|
375 |
+
task_id = "sample_task_id"
|
376 |
+
file_name = plan["file_name"]
|
377 |
+
results[tool_name] = tool.run(task_id=task_id, file_name=file_name)
|
378 |
+
|
379 |
+
elif tool_name == "code_interpreter" and "code" in plan:
|
380 |
+
code = plan["code"]
|
381 |
+
results[tool_name] = tool.run(code=code)
|
382 |
+
|
383 |
+
return results
|
384 |
+
|
385 |
+
def _generate_answer(self, question: str, plan: Dict[str, Any], tool_results: Dict[str, Any]) -> str:
|
386 |
+
"""
|
387 |
+
Generate the final answer based on the question, plan, and tool results.
|
388 |
+
|
389 |
+
Args:
|
390 |
+
question: The original question
|
391 |
+
plan: The plan that was executed
|
392 |
+
tool_results: Results from tool executions
|
393 |
+
|
394 |
+
Returns:
|
395 |
+
The final answer
|
396 |
+
"""
|
397 |
+
# Construct a prompt for the LLM that includes the question, tool results, and
|
398 |
+
# instructions to format the answer properly
|
399 |
+
|
400 |
+
prompt_parts = [
|
401 |
+
f"Question: {question}\n\n",
|
402 |
+
"I need to answer this question. Here's what I know:\n\n"
|
403 |
+
]
|
404 |
+
|
405 |
+
# Add tool results to the prompt
|
406 |
+
for tool_name, result in tool_results.items():
|
407 |
+
prompt_parts.append(f"Results from {tool_name}:\n{json.dumps(result, indent=2)}\n\n")
|
408 |
+
|
409 |
+
prompt_parts.append(
|
410 |
+
"Based on the above information, answer the question. "
|
411 |
+
"Remember to provide your reasoning first, then clearly state your final answer "
|
412 |
+
"in the format: FINAL ANSWER: [your concise answer]"
|
413 |
+
)
|
414 |
+
|
415 |
+
prompt = "".join(prompt_parts)
|
416 |
+
|
417 |
+
# Get response from LLM
|
418 |
+
llm_response = self.llm.generate(prompt, system_prompt=SYSTEM_PROMPT)
|
419 |
+
|
420 |
+
# Extract the final answer
|
421 |
+
final_answer = self.llm.extract_final_answer(llm_response)
|
422 |
+
|
423 |
+
return final_answer
|
424 |
+
|
425 |
+
# --- Runner Function for Gradio Interface ---
|
426 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None, test_username: str = ""):
|
427 |
+
"""
|
428 |
+
Fetches all questions, runs the GAIA Agent on them, submits all answers,
|
429 |
+
and displays the results.
|
430 |
+
"""
|
431 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
432 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
433 |
+
|
434 |
+
# Check if we're using a test username (for local development)
|
435 |
+
if test_username:
|
436 |
+
username = test_username
|
437 |
+
print(f"Using test username: {username}")
|
438 |
+
elif profile:
|
439 |
+
username = f"{profile.username}"
|
440 |
+
print(f"User logged in: {username}")
|
441 |
+
else:
|
442 |
+
print("User not logged in.")
|
443 |
+
return "Please Login to Hugging Face with the button or provide a test username.", None
|
444 |
+
|
445 |
+
api_url = DEFAULT_API_URL
|
446 |
+
questions_url = f"{api_url}/questions"
|
447 |
+
submit_url = f"{api_url}/submit"
|
448 |
+
|
449 |
+
# 1. Instantiate Agent
|
450 |
+
try:
|
451 |
+
agent = GAIAAgent()
|
452 |
+
except Exception as e:
|
453 |
+
print(f"Error instantiating agent: {e}")
|
454 |
+
return f"Error initializing agent: {e}", None
|
455 |
+
|
456 |
+
# In the case of an app running as a Hugging Face space, this link points toward your codebase
|
457 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
458 |
+
print(agent_code)
|
459 |
+
|
460 |
+
# 2. Fetch Questions
|
461 |
+
print(f"Fetching questions from: {questions_url}")
|
462 |
+
try:
|
463 |
+
response = requests.get(questions_url, timeout=15)
|
464 |
+
response.raise_for_status()
|
465 |
+
questions_data = response.json()
|
466 |
+
if not questions_data:
|
467 |
+
print("Fetched questions list is empty.")
|
468 |
+
return "Fetched questions list is empty or invalid format.", None
|
469 |
+
print(f"Fetched {len(questions_data)} questions.")
|
470 |
+
except requests.exceptions.RequestException as e:
|
471 |
+
print(f"Error fetching questions: {e}")
|
472 |
+
return f"Error fetching questions: {e}", None
|
473 |
+
except requests.exceptions.JSONDecodeError as e:
|
474 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
475 |
+
print(f"Response text: {response.text[:500]}")
|
476 |
+
return f"Error decoding server response for questions: {e}", None
|
477 |
+
except Exception as e:
|
478 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
479 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
480 |
+
|
481 |
+
# 3. Run your Agent
|
482 |
+
results_log = []
|
483 |
+
answers_payload = []
|
484 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
485 |
+
for item in questions_data:
|
486 |
+
task_id = item.get("task_id")
|
487 |
+
question_text = item.get("Question") # Note: Capital 'Q' in the JSON file
|
488 |
+
if not task_id or question_text is None:
|
489 |
+
print(f"Skipping item with missing task_id or Question: {item}")
|
490 |
+
continue
|
491 |
+
try:
|
492 |
+
submitted_answer = agent(question_text)
|
493 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
494 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
495 |
+
except Exception as e:
|
496 |
+
print(f"Error running agent on task {task_id}: {e}")
|
497 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
498 |
+
|
499 |
+
if not answers_payload:
|
500 |
+
print("Agent did not produce any answers to submit.")
|
501 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
502 |
+
|
503 |
+
# 4. Prepare Submission
|
504 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
505 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
506 |
+
print(status_update)
|
507 |
+
|
508 |
+
# 5. Submit
|
509 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
510 |
+
try:
|
511 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
512 |
+
response.raise_for_status()
|
513 |
+
result_data = response.json()
|
514 |
+
final_status = (
|
515 |
+
f"Submission Successful!\n"
|
516 |
+
f"User: {result_data.get('username')}\n"
|
517 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
518 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
519 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
520 |
+
)
|
521 |
+
print("Submission successful.")
|
522 |
+
results_df = pd.DataFrame(results_log)
|
523 |
+
return final_status, results_df
|
524 |
+
except requests.exceptions.HTTPError as e:
|
525 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
526 |
+
try:
|
527 |
+
error_json = e.response.json()
|
528 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
529 |
+
except requests.exceptions.JSONDecodeError:
|
530 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
531 |
+
status_message = f"Submission Failed: {error_detail}"
|
532 |
+
print(status_message)
|
533 |
+
results_df = pd.DataFrame(results_log)
|
534 |
+
return status_message, results_df
|
535 |
+
except requests.exceptions.Timeout:
|
536 |
+
status_message = "Submission Failed: The request timed out."
|
537 |
+
print(status_message)
|
538 |
+
results_df = pd.DataFrame(results_log)
|
539 |
+
return status_message, results_df
|
540 |
+
except requests.exceptions.RequestException as e:
|
541 |
+
status_message = f"Submission Failed: Network error - {e}"
|
542 |
+
print(status_message)
|
543 |
+
results_df = pd.DataFrame(results_log)
|
544 |
+
return status_message, results_df
|
545 |
+
except Exception as e:
|
546 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
547 |
+
print(status_message)
|
548 |
+
results_df = pd.DataFrame(results_log)
|
549 |
+
return status_message, results_df
|
550 |
+
|
551 |
+
# --- Build Gradio Interface using Blocks ---
|
552 |
+
with gr.Blocks() as demo:
|
553 |
+
gr.Markdown("# GAIA Agent Evaluation Runner")
|
554 |
+
gr.Markdown(
|
555 |
+
"""
|
556 |
+
**Instructions:**
|
557 |
+
|
558 |
+
1. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
559 |
+
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the GAIA agent, submit answers, and see the score.
|
560 |
+
|
561 |
+
This agent is capable of:
|
562 |
+
- Performing web searches for information
|
563 |
+
- Processing various file types (text, code, images, audio, etc.)
|
564 |
+
- Executing code safely for computational questions
|
565 |
+
- Reasoning through complex multi-step problems
|
566 |
+
|
567 |
+
The agent will automatically select the appropriate tools based on the question.
|
568 |
+
"""
|
569 |
+
)
|
570 |
+
|
571 |
+
with gr.Row():
|
572 |
+
login_button = gr.LoginButton()
|
573 |
+
test_username = gr.Textbox(label="Or enter test username for local development", placeholder="test_user")
|
574 |
+
|
575 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
576 |
+
|
577 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
578 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
579 |
+
|
580 |
+
run_button.click(
|
581 |
+
fn=run_and_submit_all,
|
582 |
+
inputs=[login_button, test_username],
|
583 |
+
outputs=[status_output, results_table]
|
584 |
+
)
|
585 |
+
|
586 |
+
if __name__ == "__main__":
|
587 |
+
print("\n" + "-"*30 + " GAIA Agent Starting " + "-"*30)
|
588 |
+
|
589 |
+
# Check for environment variables
|
590 |
+
load_dotenv() # Load environment variables from .env file if it exists
|
591 |
+
|
592 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
593 |
+
space_id_startup = os.getenv("SPACE_ID")
|
594 |
+
|
595 |
+
if space_host_startup:
|
596 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
597 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
598 |
+
else:
|
599 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
600 |
+
|
601 |
+
if space_id_startup:
|
602 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
603 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
604 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
605 |
+
else:
|
606 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
607 |
+
|
608 |
+
print("-"*(60 + len(" GAIA Agent Starting ")) + "\n")
|
609 |
+
|
610 |
+
print("Launching Gradio Interface for GAIA Agent Evaluation...")
|
611 |
+
# When running locally, disable OAuth to avoid login issues
|
612 |
+
is_local = not (space_host_startup or space_id_startup)
|
613 |
+
if is_local:
|
614 |
+
print("⚠️ Running in local mode - OAuth features will be disabled")
|
615 |
+
demo.launch(debug=True, share=False, auth=None)
|
616 |
+
else:
|
617 |
+
demo.launch(debug=True, share=False)
|
app_local.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /Users/yagoairm2/Desktop/agents/final project/HF_Agents_Final_Project/app_local.py
|
2 |
+
"""
|
3 |
+
A simplified version of app2.py that works better for local development.
|
4 |
+
This version doesn't require OAuth authentication and uses a test username instead.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import os
|
8 |
+
import sys
|
9 |
+
import gradio as gr
|
10 |
+
import requests
|
11 |
+
import pandas as pd
|
12 |
+
import json
|
13 |
+
import re
|
14 |
+
import time
|
15 |
+
import logging
|
16 |
+
import io
|
17 |
+
import contextlib
|
18 |
+
from typing import Dict, List, Optional, Union, Any
|
19 |
+
from pathlib import Path
|
20 |
+
try:
|
21 |
+
from dotenv import load_dotenv
|
22 |
+
except ImportError:
|
23 |
+
print("dotenv not found. Using os.environ only.")
|
24 |
+
def load_dotenv():
|
25 |
+
pass
|
26 |
+
|
27 |
+
# Configure logging
|
28 |
+
logging.basicConfig(
|
29 |
+
level=logging.INFO,
|
30 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
31 |
+
handlers=[logging.StreamHandler()]
|
32 |
+
)
|
33 |
+
logger = logging.getLogger(__name__)
|
34 |
+
|
35 |
+
# --- Constants ---
|
36 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
37 |
+
DEFAULT_FILES_DIR = "dataset"
|
38 |
+
SYSTEM_PROMPT = """
|
39 |
+
You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
40 |
+
"""
|
41 |
+
|
42 |
+
# --- Mock Agent Implementation ---
|
43 |
+
class MockAgent:
|
44 |
+
"""A simple agent that returns mock answers for testing purposes."""
|
45 |
+
|
46 |
+
def __init__(self):
|
47 |
+
logger.info("Initializing Mock Agent")
|
48 |
+
|
49 |
+
def __call__(self, question: str) -> str:
|
50 |
+
"""Return a mock answer based on the question content."""
|
51 |
+
logger.info(f"Mock Agent received question: {question[:50]}...")
|
52 |
+
|
53 |
+
# Return different mock answers based on question content
|
54 |
+
if "how many" in question.lower():
|
55 |
+
answer = "42"
|
56 |
+
elif "what is" in question.lower():
|
57 |
+
answer = "Example answer for a what-is question"
|
58 |
+
elif "?" in question:
|
59 |
+
answer = "Yes, that is correct."
|
60 |
+
else:
|
61 |
+
answer = "This is a mock answer for testing purposes."
|
62 |
+
|
63 |
+
logger.info(f"Mock Agent returning answer: {answer}")
|
64 |
+
return answer
|
65 |
+
|
66 |
+
# --- Runner Function for Gradio Interface ---
|
67 |
+
def run_and_submit_all(test_username: str = "test_user"):
|
68 |
+
"""
|
69 |
+
Fetches all questions, runs the agent on them, submits answers,
|
70 |
+
and displays the results.
|
71 |
+
"""
|
72 |
+
if not test_username:
|
73 |
+
test_username = "test_user"
|
74 |
+
|
75 |
+
print(f"Using test username: {test_username}")
|
76 |
+
|
77 |
+
api_url = DEFAULT_API_URL
|
78 |
+
questions_url = f"{api_url}/questions"
|
79 |
+
submit_url = f"{api_url}/submit"
|
80 |
+
|
81 |
+
# 1. Instantiate Agent
|
82 |
+
try:
|
83 |
+
agent = MockAgent() # Use the mock agent for testing
|
84 |
+
except Exception as e:
|
85 |
+
print(f"Error instantiating agent: {e}")
|
86 |
+
return f"Error initializing agent: {e}", None
|
87 |
+
|
88 |
+
agent_code = "https://huggingface.co/spaces/test/test/tree/main" # Mock URL
|
89 |
+
|
90 |
+
# 2. Fetch Questions (or use local file for faster testing)
|
91 |
+
questions_file = "question_set/common_questions.json"
|
92 |
+
if os.path.exists(questions_file):
|
93 |
+
print(f"Using local questions file: {questions_file}")
|
94 |
+
try:
|
95 |
+
with open(questions_file, 'r') as f:
|
96 |
+
questions_data = json.load(f)
|
97 |
+
print(f"Loaded {len(questions_data)} questions from local file.")
|
98 |
+
# For testing, limit to just a few questions
|
99 |
+
questions_data = questions_data[:3]
|
100 |
+
print(f"Limited to first {len(questions_data)} questions for testing.")
|
101 |
+
except Exception as e:
|
102 |
+
print(f"Error loading questions from local file: {e}")
|
103 |
+
return f"Error loading questions from local file: {e}", None
|
104 |
+
else:
|
105 |
+
print(f"Fetching questions from: {questions_url}")
|
106 |
+
try:
|
107 |
+
response = requests.get(questions_url, timeout=15)
|
108 |
+
response.raise_for_status()
|
109 |
+
questions_data = response.json()
|
110 |
+
if not questions_data:
|
111 |
+
print("Fetched questions list is empty.")
|
112 |
+
return "Fetched questions list is empty or invalid format.", None
|
113 |
+
print(f"Fetched {len(questions_data)} questions.")
|
114 |
+
# For testing, limit to just a few questions
|
115 |
+
questions_data = questions_data[:3]
|
116 |
+
print(f"Limited to first {len(questions_data)} questions for testing.")
|
117 |
+
except Exception as e:
|
118 |
+
print(f"Error fetching questions: {e}")
|
119 |
+
return f"Error fetching questions: {e}", None
|
120 |
+
|
121 |
+
# 3. Run Agent
|
122 |
+
results_log = []
|
123 |
+
answers_payload = []
|
124 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
125 |
+
for item in questions_data:
|
126 |
+
task_id = item.get("task_id")
|
127 |
+
question_text = item.get("Question")
|
128 |
+
if not task_id or question_text is None:
|
129 |
+
print(f"Skipping item with missing task_id or question")
|
130 |
+
continue
|
131 |
+
try:
|
132 |
+
submitted_answer = agent(question_text)
|
133 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
134 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
135 |
+
except Exception as e:
|
136 |
+
print(f"Error running agent on task {task_id}: {e}")
|
137 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
138 |
+
|
139 |
+
if not answers_payload:
|
140 |
+
print("Agent did not produce any answers to submit.")
|
141 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
142 |
+
|
143 |
+
# 4. Prepare Submission
|
144 |
+
submission_data = {"username": test_username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
145 |
+
status_update = f"Agent finished. Prepared {len(answers_payload)} answers for user '{test_username}'..."
|
146 |
+
print(status_update)
|
147 |
+
|
148 |
+
# 5. Show Results (but don't submit in local testing mode)
|
149 |
+
print("In local development mode - showing results without submitting")
|
150 |
+
final_status = (
|
151 |
+
f"Local Testing Complete!\n"
|
152 |
+
f"User: {test_username}\n"
|
153 |
+
f"Generated {len(answers_payload)} answers\n"
|
154 |
+
f"Message: This is a local test - no answers were submitted to the API"
|
155 |
+
)
|
156 |
+
results_df = pd.DataFrame(results_log)
|
157 |
+
return final_status, results_df
|
158 |
+
|
159 |
+
# --- Simple Gradio Interface ---
|
160 |
+
with gr.Blocks() as demo:
|
161 |
+
gr.Markdown("# GAIA Agent Local Testing Interface")
|
162 |
+
gr.Markdown(
|
163 |
+
"""
|
164 |
+
**Local Development Version**
|
165 |
+
|
166 |
+
This is a simplified version of the agent interface for local testing.
|
167 |
+
It uses a mock agent implementation that returns test answers.
|
168 |
+
|
169 |
+
Enter a username below and click the button to run the agent on a few sample questions.
|
170 |
+
"""
|
171 |
+
)
|
172 |
+
|
173 |
+
test_username = gr.Textbox(label="Test Username", value="test_user")
|
174 |
+
run_button = gr.Button("Run Test Evaluation")
|
175 |
+
|
176 |
+
status_output = gr.Textbox(label="Run Status", lines=5, interactive=False)
|
177 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
178 |
+
|
179 |
+
run_button.click(
|
180 |
+
fn=run_and_submit_all,
|
181 |
+
inputs=[test_username],
|
182 |
+
outputs=[status_output, results_table]
|
183 |
+
)
|
184 |
+
|
185 |
+
if __name__ == "__main__":
|
186 |
+
print("\n" + "-"*30 + " GAIA Agent Local Testing " + "-"*30)
|
187 |
+
|
188 |
+
# Try to load environment variables (optional)
|
189 |
+
load_dotenv()
|
190 |
+
|
191 |
+
print("Launching Gradio Interface for local testing...")
|
192 |
+
demo.launch(debug=True, share=False)
|
quick_setup.sh
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/zsh
|
2 |
+
# filepath: /Users/yagoairm2/Desktop/agents/final project/HF_Agents_Final_Project/quick_setup.sh
|
3 |
+
|
4 |
+
echo "===== GAIA Agent Quick Setup ====="
|
5 |
+
|
6 |
+
# Activate the virtual environment
|
7 |
+
echo "Activating virtual environment..."
|
8 |
+
source .venv/bin/activate
|
9 |
+
|
10 |
+
# Install dependencies
|
11 |
+
echo "Installing dependencies..."
|
12 |
+
pip install -r requirements.txt
|
13 |
+
|
14 |
+
# Create dataset directory if it doesn't exist
|
15 |
+
echo "Setting up directories..."
|
16 |
+
mkdir -p dataset
|
17 |
+
|
18 |
+
echo "Setup complete!"
|
19 |
+
echo ""
|
20 |
+
echo "Available commands:"
|
21 |
+
echo "- python app_local.py # Run the local testing app"
|
22 |
+
echo "- python test_agent.py -t TASK_ID # Test agent with a specific question"
|
23 |
+
echo ""
|
24 |
+
echo "Examples:"
|
25 |
+
echo "- python test_agent.py -t 8e867cd7-cff9-4e6c-867a-ff5ddc2550be"
|
26 |
+
echo "- python test_agent.py -q 'How many studio albums were published by Mercedes Sosa?'"
|
27 |
+
echo ""
|
28 |
+
echo "Note: For the first run, the system will download the Llama 3 model which may take some time."
|
requirements.txt
CHANGED
@@ -1,5 +1,14 @@
|
|
1 |
-
gradio
|
2 |
-
requests
|
3 |
-
|
4 |
-
python-dotenv
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio[oauth]>=5.0.0
|
2 |
+
requests>=2.31.0
|
3 |
+
pandas>=2.0.0
|
4 |
+
python-dotenv>=1.0.0
|
5 |
+
huggingface-hub>=0.19.0
|
6 |
+
itsdangerous>=2.1.2 # Required for gradio oauth
|
7 |
+
aider-install>=0.1.3
|
8 |
+
uv>=0.6.6
|
9 |
+
|
10 |
+
# Dependencies for GAIA Agent
|
11 |
+
gpt4all>=2.0.0 # For local LLM integration
|
12 |
+
beautifulsoup4>=4.12.0 # For web scraping
|
13 |
+
pillow>=10.0.0 # For image processing
|
14 |
+
google-api-python-client>=2.100.0 # For Google search API
|
run_local.sh
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
# Install dependencies and run the local version of the app
|
3 |
+
|
4 |
+
echo "Installing required packages..."
|
5 |
+
pip install -r requirements.txt
|
6 |
+
|
7 |
+
echo "\nStarting local version of the app..."
|
8 |
+
python app_local.py
|
setup.sh
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
# setup.sh - Setup script for GAIA Agent development
|
3 |
+
|
4 |
+
echo "Setting up the development environment for GAIA Agent..."
|
5 |
+
|
6 |
+
# Create a virtual environment if it doesn't exist
|
7 |
+
if [ ! -d "venv" ]; then
|
8 |
+
echo "Creating virtual environment..."
|
9 |
+
python3 -m venv venv
|
10 |
+
echo "Virtual environment created."
|
11 |
+
else
|
12 |
+
echo "Virtual environment already exists."
|
13 |
+
fi
|
14 |
+
|
15 |
+
# Activate the virtual environment
|
16 |
+
echo "Activating virtual environment..."
|
17 |
+
source venv/bin/activate
|
18 |
+
|
19 |
+
# Install dependencies
|
20 |
+
echo "Installing dependencies..."
|
21 |
+
pip install --upgrade pip
|
22 |
+
pip install -r requirements.txt
|
23 |
+
|
24 |
+
# Check if GPT4All is properly installed
|
25 |
+
echo "Checking GPT4All installation..."
|
26 |
+
python utilities/check_gpt4all.py
|
27 |
+
|
28 |
+
# Create dataset directory if it doesn't exist
|
29 |
+
if [ ! -d "dataset" ]; then
|
30 |
+
echo "Creating dataset directory..."
|
31 |
+
mkdir -p dataset
|
32 |
+
echo "Dataset directory created."
|
33 |
+
fi
|
34 |
+
|
35 |
+
echo ""
|
36 |
+
echo "Setup complete! You can now run the local testing app with:"
|
37 |
+
echo "python app_local.py"
|
38 |
+
echo ""
|
39 |
+
echo "For development, refer to the NEXT_STEPS.md file for guidance."
|
test_agent.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Script to test the GAIA agent with a specific question.
|
3 |
+
This is useful for testing the agent's response to a specific question
|
4 |
+
without having to run the full Gradio interface.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import sys
|
8 |
+
import json
|
9 |
+
from pathlib import Path
|
10 |
+
import argparse
|
11 |
+
import logging
|
12 |
+
import os
|
13 |
+
|
14 |
+
# Configure logging
|
15 |
+
logging.basicConfig(
|
16 |
+
level=logging.INFO,
|
17 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
18 |
+
)
|
19 |
+
logger = logging.getLogger(__name__)
|
20 |
+
|
21 |
+
# Import the agent class from app2.py
|
22 |
+
try:
|
23 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
24 |
+
from app2 import GAIAAgent
|
25 |
+
except ImportError:
|
26 |
+
logger.error("Failed to import GAIAAgent from app2.py")
|
27 |
+
sys.exit(1)
|
28 |
+
|
29 |
+
def load_questions(file_path):
|
30 |
+
"""Load questions from a JSON file."""
|
31 |
+
try:
|
32 |
+
with open(file_path, 'r') as f:
|
33 |
+
return json.load(f)
|
34 |
+
except Exception as e:
|
35 |
+
logger.error(f"Error loading questions from {file_path}: {e}")
|
36 |
+
return []
|
37 |
+
|
38 |
+
def find_question_by_id(questions, task_id):
|
39 |
+
"""Find a question by its task_id."""
|
40 |
+
for q in questions:
|
41 |
+
if q.get("task_id") == task_id:
|
42 |
+
return q
|
43 |
+
return None
|
44 |
+
|
45 |
+
def main():
|
46 |
+
parser = argparse.ArgumentParser(description='Test the GAIA agent with a specific question')
|
47 |
+
parser.add_argument('--question', '-q', type=str, help='The question to ask the agent')
|
48 |
+
parser.add_argument('--task-id', '-t', type=str, help='Task ID to look up in common_questions.json')
|
49 |
+
parser.add_argument('--file', '-f', type=str, default='question_set/common_questions.json',
|
50 |
+
help='Path to questions file (default: question_set/common_questions.json)')
|
51 |
+
|
52 |
+
args = parser.parse_args()
|
53 |
+
|
54 |
+
# Initialize the agent
|
55 |
+
logger.info("Initializing GAIA Agent...")
|
56 |
+
agent = GAIAAgent()
|
57 |
+
logger.info("Agent initialized")
|
58 |
+
|
59 |
+
question = args.question
|
60 |
+
|
61 |
+
# If task_id is provided, look up the question in the file
|
62 |
+
if not question and args.task_id:
|
63 |
+
questions = load_questions(args.file)
|
64 |
+
question_obj = find_question_by_id(questions, args.task_id)
|
65 |
+
|
66 |
+
if question_obj:
|
67 |
+
question = question_obj.get("Question")
|
68 |
+
expected_answer = question_obj.get("Final answer", "Not provided")
|
69 |
+
logger.info(f"Found question for task_id {args.task_id}")
|
70 |
+
logger.info(f"Expected answer: {expected_answer}")
|
71 |
+
else:
|
72 |
+
logger.error(f"Could not find question with task_id {args.task_id}")
|
73 |
+
sys.exit(1)
|
74 |
+
|
75 |
+
# Check if we have a question to answer
|
76 |
+
if not question:
|
77 |
+
logger.error("No question provided. Use --question or --task-id")
|
78 |
+
sys.exit(1)
|
79 |
+
|
80 |
+
logger.info(f"Question: {question}")
|
81 |
+
|
82 |
+
# Get the agent's answer
|
83 |
+
logger.info("Asking agent...")
|
84 |
+
try:
|
85 |
+
answer = agent(question)
|
86 |
+
logger.info(f"Agent's answer: {answer}")
|
87 |
+
except Exception as e:
|
88 |
+
logger.error(f"Error getting answer from agent: {e}")
|
89 |
+
sys.exit(1)
|
90 |
+
|
91 |
+
if __name__ == "__main__":
|
92 |
+
main()
|
test_question.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# /Users/yagoairm2/Desktop/agents/final project/HF_Agents_Final_Project/test_question.py
|
3 |
+
"""
|
4 |
+
Script to test GAIA agent with a single question
|
5 |
+
Usage:
|
6 |
+
python test_question.py "Your question here"
|
7 |
+
"""
|
8 |
+
|
9 |
+
import sys
|
10 |
+
import json
|
11 |
+
import logging
|
12 |
+
from app2 import GAIAAgent # Import the agent from app2.py
|
13 |
+
|
14 |
+
# Configure logging
|
15 |
+
logging.basicConfig(
|
16 |
+
level=logging.INFO,
|
17 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
18 |
+
handlers=[logging.StreamHandler()]
|
19 |
+
)
|
20 |
+
logger = logging.getLogger(__name__)
|
21 |
+
|
22 |
+
def main():
|
23 |
+
"""Run the agent on a single question from command line"""
|
24 |
+
if len(sys.argv) < 2:
|
25 |
+
print("Usage: python test_question.py \"Your question here\"")
|
26 |
+
return
|
27 |
+
|
28 |
+
# Get question from command line
|
29 |
+
question = sys.argv[1]
|
30 |
+
print(f"\n=== Testing GAIA Agent with question ===\n{question}\n")
|
31 |
+
|
32 |
+
# Initialize agent
|
33 |
+
try:
|
34 |
+
agent = GAIAAgent()
|
35 |
+
print("\n=== Agent initialized successfully ===\n")
|
36 |
+
except Exception as e:
|
37 |
+
print(f"\n!!! Error initializing agent: {e}")
|
38 |
+
return
|
39 |
+
|
40 |
+
# Run agent on question
|
41 |
+
try:
|
42 |
+
print("\n=== Running agent... ===\n")
|
43 |
+
answer = agent(question)
|
44 |
+
print(f"\n=== Agent response ===\n{answer}\n")
|
45 |
+
except Exception as e:
|
46 |
+
print(f"\n!!! Error running agent: {e}")
|
47 |
+
|
48 |
+
if __name__ == "__main__":
|
49 |
+
main()
|
update_files.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
def update_app_local():
|
7 |
+
"""Update app_local.py to fix the Question field case."""
|
8 |
+
print("Updating app_local.py...")
|
9 |
+
with open("app_local.py", "r") as f:
|
10 |
+
content = f.read()
|
11 |
+
|
12 |
+
# Replace the lowercase 'question' with uppercase 'Question'
|
13 |
+
updated_content = content.replace('item.get("question")', 'item.get("Question")')
|
14 |
+
|
15 |
+
with open("app_local.py", "w") as f:
|
16 |
+
f.write(updated_content)
|
17 |
+
|
18 |
+
print("Successfully updated app_local.py")
|
19 |
+
|
20 |
+
def update_app2():
|
21 |
+
"""Update app2.py to fix the Question field case."""
|
22 |
+
print("Updating app2.py...")
|
23 |
+
with open("app2.py", "r") as f:
|
24 |
+
content = f.read()
|
25 |
+
|
26 |
+
# Replace the lowercase 'question' with uppercase 'Question'
|
27 |
+
updated_content = content.replace('item.get("question")', 'item.get("Question")')
|
28 |
+
|
29 |
+
with open("app2.py", "w") as f:
|
30 |
+
f.write(updated_content)
|
31 |
+
|
32 |
+
print("Successfully updated app2.py")
|
33 |
+
|
34 |
+
def main():
|
35 |
+
print("Starting file updates...")
|
36 |
+
try:
|
37 |
+
update_app_local()
|
38 |
+
update_app2()
|
39 |
+
print("All files updated successfully!")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error updating files: {e}")
|
42 |
+
return 1
|
43 |
+
return 0
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
sys.exit(main())
|