Commit ·
9e5b439
1
Parent(s): ca73bee
first version
Browse files
agent.py
CHANGED
|
@@ -1,8 +1,127 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from smolagents import (
|
| 4 |
+
CodeAgent,
|
| 5 |
+
DuckDuckGoSearchTool,
|
| 6 |
+
PythonInterpreterTool,
|
| 7 |
+
VisitWebpageTool,
|
| 8 |
+
LiteLLMModel,
|
| 9 |
+
)
|
| 10 |
+
from tools import (
|
| 11 |
+
multiply,
|
| 12 |
+
add,
|
| 13 |
+
subtract,
|
| 14 |
+
divide,
|
| 15 |
+
modulus,
|
| 16 |
+
get_wikipedia_summary
|
| 17 |
+
)
|
| 18 |
+
# Load environment variables
|
| 19 |
+
load_dotenv()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def create_gaia_agent():
|
| 23 |
+
"""
|
| 24 |
+
Create a GAIA question-answering agent using Gemini API and smolagents tools
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# Initialize the Gemini model
|
| 28 |
+
model = LiteLLMModel(
|
| 29 |
+
model_id="gemini/gemini-2.0-flash",
|
| 30 |
+
api_key=os.getenv("GEMINI_API_KEY")
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Initialize tools for the agent
|
| 34 |
+
tools = [
|
| 35 |
+
DuckDuckGoSearchTool(),
|
| 36 |
+
PythonInterpreterTool(),
|
| 37 |
+
VisitWebpageTool(),
|
| 38 |
+
get_wikipedia_summary,
|
| 39 |
+
multiply,
|
| 40 |
+
add,
|
| 41 |
+
subtract,
|
| 42 |
+
divide,
|
| 43 |
+
modulus
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Create the agent with iterative decision-making capabilities
|
| 47 |
+
agent = CodeAgent(
|
| 48 |
+
tools=tools,
|
| 49 |
+
model=model
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return agent
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def answer_gaia_question(agent: CodeAgent, question: str):
|
| 56 |
+
"""
|
| 57 |
+
Answer a GAIA question using the agent
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
question (str): The GAIA question to answer
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
str: The agent's answer
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
# Enhanced prompt for GAIA questions
|
| 67 |
+
enhanced_prompt = f"""
|
| 68 |
+
You are an expert AI assistant designed to answer complex GAIA (General AI Assistant) questions.
|
| 69 |
+
These questions often require:
|
| 70 |
+
- Multi-step reasoning
|
| 71 |
+
- Web searches for current information
|
| 72 |
+
- Mathematical calculations
|
| 73 |
+
- Data analysis
|
| 74 |
+
- Combining information from multiple sources
|
| 75 |
+
|
| 76 |
+
Question: {question}
|
| 77 |
+
|
| 78 |
+
Please approach this systematically:
|
| 79 |
+
1. Break down the question into components
|
| 80 |
+
2. Identify what information you need
|
| 81 |
+
3. Use appropriate tools to gather information
|
| 82 |
+
4. Perform any necessary calculations
|
| 83 |
+
5. Synthesize your findings into a clear answer
|
| 84 |
+
|
| 85 |
+
Make sure to:
|
| 86 |
+
- Use web search when you need current or specific information
|
| 87 |
+
- Use Python for any calculations or data processing
|
| 88 |
+
- Visit specific webpages if you need detailed information from particular sources
|
| 89 |
+
- Think step by step and show your reasoning
|
| 90 |
+
|
| 91 |
+
Provide a final, clear answer at the end.
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
# Run the agent
|
| 96 |
+
result = agent.run(enhanced_prompt)
|
| 97 |
+
return result
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
return f"Error occurred while processing the question: {str(e)}"
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# Example usage for specific GAIA questions
|
| 106 |
+
def example_usage():
|
| 107 |
+
"""
|
| 108 |
+
Example of how to use the agent with specific GAIA questions
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
# Example GAIA questions (you can replace with actual ones)
|
| 112 |
+
example_questions = [
|
| 113 |
+
"What is the current population of Tokyo and how does it compare to New York City?",
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
print("Running example GAIA questions:")
|
| 117 |
+
print("=" * 40)
|
| 118 |
+
|
| 119 |
+
for i, question in enumerate(example_questions, 1):
|
| 120 |
+
print(f"\nExample {i}: {question}")
|
| 121 |
+
answer = answer_gaia_question(question)
|
| 122 |
+
print(f"Answer: {answer}")
|
| 123 |
+
print("-" * 40)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
example_usage()
|
app.py
CHANGED
|
@@ -1,24 +1,27 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from agent import
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
"""
|
| 14 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 15 |
and displays the results.
|
| 16 |
"""
|
| 17 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 18 |
-
space_id = os.getenv("SPACE_ID")
|
| 19 |
|
| 20 |
if profile:
|
| 21 |
-
username= f"{profile.username}"
|
| 22 |
print(f"User logged in: {username}")
|
| 23 |
else:
|
| 24 |
print("User not logged in.")
|
|
@@ -30,7 +33,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 30 |
|
| 31 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 32 |
try:
|
| 33 |
-
agent =
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error instantiating agent: {e}")
|
| 36 |
return f"Error initializing agent: {e}", None
|
|
@@ -45,16 +48,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 45 |
response.raise_for_status()
|
| 46 |
questions_data = response.json()
|
| 47 |
if not questions_data:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
print(f"Fetched {len(questions_data)} questions.")
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
print(f"Error fetching questions: {e}")
|
| 53 |
return f"Error fetching questions: {e}", None
|
| 54 |
except requests.exceptions.JSONDecodeError as e:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
except Exception as e:
|
| 59 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 60 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -64,18 +67,22 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 64 |
answers_payload = []
|
| 65 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 66 |
for item in questions_data:
|
|
|
|
| 67 |
task_id = item.get("task_id")
|
| 68 |
question_text = item.get("question")
|
| 69 |
if not task_id or question_text is None:
|
| 70 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 71 |
continue
|
| 72 |
try:
|
| 73 |
-
submitted_answer =
|
| 74 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 75 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
if not answers_payload:
|
| 81 |
print("Agent did not produce any answers to submit.")
|
|
@@ -162,10 +169,10 @@ with gr.Blocks() as demo:
|
|
| 162 |
)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|
| 165 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 166 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 167 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 168 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 169 |
|
| 170 |
if space_host_startup:
|
| 171 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
@@ -173,14 +180,14 @@ if __name__ == "__main__":
|
|
| 173 |
else:
|
| 174 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 175 |
|
| 176 |
-
if space_id_startup:
|
| 177 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 178 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 179 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 180 |
else:
|
| 181 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 182 |
|
| 183 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 184 |
|
| 185 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 186 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import inspect
|
| 7 |
import pandas as pd
|
| 8 |
+
from agent import create_gaia_agent, answer_gaia_question
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
+
|
| 15 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 16 |
"""
|
| 17 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 18 |
and displays the results.
|
| 19 |
"""
|
| 20 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 21 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 22 |
|
| 23 |
if profile:
|
| 24 |
+
username = f"{profile.username}"
|
| 25 |
print(f"User logged in: {username}")
|
| 26 |
else:
|
| 27 |
print("User not logged in.")
|
|
|
|
| 33 |
|
| 34 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 35 |
try:
|
| 36 |
+
agent = create_gaia_agent()
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Error instantiating agent: {e}")
|
| 39 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 48 |
response.raise_for_status()
|
| 49 |
questions_data = response.json()
|
| 50 |
if not questions_data:
|
| 51 |
+
print("Fetched questions list is empty.")
|
| 52 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 53 |
print(f"Fetched {len(questions_data)} questions.")
|
| 54 |
except requests.exceptions.RequestException as e:
|
| 55 |
print(f"Error fetching questions: {e}")
|
| 56 |
return f"Error fetching questions: {e}", None
|
| 57 |
except requests.exceptions.JSONDecodeError as e:
|
| 58 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 59 |
+
print(f"Response text: {response.text[:500]}")
|
| 60 |
+
return f"Error decoding server response for questions: {e}", None
|
| 61 |
except Exception as e:
|
| 62 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 63 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 67 |
answers_payload = []
|
| 68 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 69 |
for item in questions_data:
|
| 70 |
+
print("item:", item) # Debugging: print each item
|
| 71 |
task_id = item.get("task_id")
|
| 72 |
question_text = item.get("question")
|
| 73 |
if not task_id or question_text is None:
|
| 74 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 75 |
continue
|
| 76 |
try:
|
| 77 |
+
submitted_answer = answer_gaia_question(agent, question_text)
|
| 78 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 79 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 80 |
+
# timeout to avoid getting rate limited by the API
|
| 81 |
+
wait_time = 60 # seconds
|
| 82 |
+
time.sleep(wait_time)
|
| 83 |
except Exception as e:
|
| 84 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 85 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 86 |
|
| 87 |
if not answers_payload:
|
| 88 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 173 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 174 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 175 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 176 |
|
| 177 |
if space_host_startup:
|
| 178 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 180 |
else:
|
| 181 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 182 |
|
| 183 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 184 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 185 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 186 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 187 |
else:
|
| 188 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 189 |
|
| 190 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 191 |
|
| 192 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 193 |
+
demo.launch(debug=True, share=False)
|
tools.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
import wikipedia
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def multiply(a: int, b: int) -> int:
|
| 7 |
+
"""Multiply two numbers.
|
| 8 |
+
Args:
|
| 9 |
+
a: first int
|
| 10 |
+
b: second int
|
| 11 |
+
"""
|
| 12 |
+
return a * b
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def add(a: int, b: int) -> int:
|
| 17 |
+
"""Add two numbers.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
a: first int
|
| 21 |
+
b: second int
|
| 22 |
+
"""
|
| 23 |
+
return a + b
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@tool
|
| 27 |
+
def subtract(a: int, b: int) -> int:
|
| 28 |
+
"""Subtract two numbers.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
a: first int
|
| 32 |
+
b: second int
|
| 33 |
+
"""
|
| 34 |
+
return a - b
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def divide(a: int, b: int) -> int:
|
| 39 |
+
"""Divide two numbers.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
a: first int
|
| 43 |
+
b: second int
|
| 44 |
+
"""
|
| 45 |
+
if b == 0:
|
| 46 |
+
raise ValueError("Cannot divide by zero.")
|
| 47 |
+
return a / b
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@tool
|
| 51 |
+
def modulus(a: int, b: int) -> int:
|
| 52 |
+
"""Get the modulus of two numbers.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
a: first int
|
| 56 |
+
b: second int
|
| 57 |
+
"""
|
| 58 |
+
return a % b
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@tool
|
| 63 |
+
def get_wikipedia_summary(query: str, sentences: int = 2) -> str:
|
| 64 |
+
"""
|
| 65 |
+
Get a summary from Wikipedia for a given query.
|
| 66 |
+
|
| 67 |
+
Args:
|
| 68 |
+
query: The search term.
|
| 69 |
+
sentences: Number of sentences for the summary.
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
The summary as a string.
|
| 73 |
+
"""
|
| 74 |
+
try:
|
| 75 |
+
summary = wikipedia.summary(query, sentences=sentences)
|
| 76 |
+
return summary
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return f"Error retrieving Wikipedia summary: {e}"
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|