Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,10 @@ import pandas as pd
|
|
| 5 |
from smolagents import ToolCallingAgent, tool
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
import math
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# --- Tools ---
|
| 10 |
@tool
|
|
@@ -15,7 +19,7 @@ def web_search(query: str) -> str:
|
|
| 15 |
query: The search query string.
|
| 16 |
|
| 17 |
Returns:
|
| 18 |
-
A formatted string with search results
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
with DDGS() as ddgs:
|
|
@@ -29,7 +33,7 @@ def web_search(query: str) -> str:
|
|
| 29 |
|
| 30 |
@tool
|
| 31 |
def calculate(expression: str) -> str:
|
| 32 |
-
"""Evaluates mathematical expressions
|
| 33 |
|
| 34 |
Args:
|
| 35 |
expression: The math expression to evaluate.
|
|
@@ -40,36 +44,37 @@ def calculate(expression: str) -> str:
|
|
| 40 |
try:
|
| 41 |
safe_dict = {k: v for k, v in math.__dict__.items()
|
| 42 |
if not k.startswith("__")}
|
| 43 |
-
safe_dict.update({
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Calculation error: {str(e)}"
|
| 47 |
|
| 48 |
-
# --- Agent with
|
| 49 |
class GAIAAgent:
|
| 50 |
def __init__(self):
|
| 51 |
-
self.system_prompt = """You are an AI assistant
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
Guidelines:
|
| 57 |
-
- For factual questions, always use web_search
|
| 58 |
-
- For math problems, use calculate
|
| 59 |
-
- Be concise but thorough
|
| 60 |
-
- Verify information when possible
|
| 61 |
-
- If unsure, say so rather than guessing"""
|
| 62 |
|
| 63 |
try:
|
|
|
|
| 64 |
self.agent = ToolCallingAgent(
|
| 65 |
name="GAIA_Agent",
|
| 66 |
description=self.system_prompt,
|
| 67 |
tools=[web_search, calculate],
|
| 68 |
-
model=
|
|
|
|
| 69 |
planning_interval=3
|
| 70 |
)
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
-
raise RuntimeError(f"Agent
|
| 73 |
|
| 74 |
def __call__(self, question: str) -> str:
|
| 75 |
"""Process a question with proper error handling."""
|
|
@@ -95,11 +100,11 @@ def submit_answers(profile: gr.OAuthProfile | None):
|
|
| 95 |
answers = []
|
| 96 |
results = []
|
| 97 |
|
| 98 |
-
for item in questions[:15]:
|
| 99 |
task_id = item.get("task_id", "")
|
| 100 |
question = item.get("question", "")
|
| 101 |
-
|
| 102 |
answer = agent(question)
|
|
|
|
| 103 |
answers.append({
|
| 104 |
"task_id": task_id,
|
| 105 |
"submitted_answer": answer[:1000]
|
|
@@ -110,7 +115,6 @@ def submit_answers(profile: gr.OAuthProfile | None):
|
|
| 110 |
"Answer": answer[:200]
|
| 111 |
})
|
| 112 |
|
| 113 |
-
# Submit answers
|
| 114 |
submit_response = requests.post(
|
| 115 |
"https://agents-course-unit4-scoring.hf.space/submit",
|
| 116 |
json={
|
|
@@ -138,7 +142,6 @@ with gr.Blocks() as demo:
|
|
| 138 |
submit_btn = gr.Button("Run Evaluation", variant="primary")
|
| 139 |
output = gr.Textbox(label="Results")
|
| 140 |
table = gr.DataFrame(label="Details")
|
| 141 |
-
|
| 142 |
submit_btn.click(submit_answers, outputs=[output, table])
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from smolagents import ToolCallingAgent, tool
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
import math
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
|
| 10 |
+
# Initialize OpenAI client
|
| 11 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 12 |
|
| 13 |
# --- Tools ---
|
| 14 |
@tool
|
|
|
|
| 19 |
query: The search query string.
|
| 20 |
|
| 21 |
Returns:
|
| 22 |
+
A formatted string with search results.
|
| 23 |
"""
|
| 24 |
try:
|
| 25 |
with DDGS() as ddgs:
|
|
|
|
| 33 |
|
| 34 |
@tool
|
| 35 |
def calculate(expression: str) -> str:
|
| 36 |
+
"""Evaluates mathematical expressions.
|
| 37 |
|
| 38 |
Args:
|
| 39 |
expression: The math expression to evaluate.
|
|
|
|
| 44 |
try:
|
| 45 |
safe_dict = {k: v for k, v in math.__dict__.items()
|
| 46 |
if not k.startswith("__")}
|
| 47 |
+
safe_dict.update({
|
| 48 |
+
'__builtins__': None,
|
| 49 |
+
'abs': abs,
|
| 50 |
+
'round': round
|
| 51 |
+
})
|
| 52 |
+
result = eval(expression, {'__builtins__': None}, safe_dict)
|
| 53 |
+
return str(result)
|
| 54 |
except Exception as e:
|
| 55 |
return f"Calculation error: {str(e)}"
|
| 56 |
|
| 57 |
+
# --- Agent with Proper Model Initialization ---
|
| 58 |
class GAIAAgent:
|
| 59 |
def __init__(self):
|
| 60 |
+
self.system_prompt = """You are an AI assistant that answers questions using tools:
|
| 61 |
+
- Use web_search for factual queries
|
| 62 |
+
- Use calculate for math problems
|
| 63 |
+
- Be concise and accurate"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
try:
|
| 66 |
+
# Initialize with proper ChatModel instance
|
| 67 |
self.agent = ToolCallingAgent(
|
| 68 |
name="GAIA_Agent",
|
| 69 |
description=self.system_prompt,
|
| 70 |
tools=[web_search, calculate],
|
| 71 |
+
model=client.chat.completions, # Proper model instance
|
| 72 |
+
model_name="gpt-3.5-turbo",
|
| 73 |
planning_interval=3
|
| 74 |
)
|
| 75 |
+
print("✅ Agent initialized with OpenAI model")
|
| 76 |
except Exception as e:
|
| 77 |
+
raise RuntimeError(f"Agent init failed: {str(e)}")
|
| 78 |
|
| 79 |
def __call__(self, question: str) -> str:
|
| 80 |
"""Process a question with proper error handling."""
|
|
|
|
| 100 |
answers = []
|
| 101 |
results = []
|
| 102 |
|
| 103 |
+
for item in questions[:15]:
|
| 104 |
task_id = item.get("task_id", "")
|
| 105 |
question = item.get("question", "")
|
|
|
|
| 106 |
answer = agent(question)
|
| 107 |
+
|
| 108 |
answers.append({
|
| 109 |
"task_id": task_id,
|
| 110 |
"submitted_answer": answer[:1000]
|
|
|
|
| 115 |
"Answer": answer[:200]
|
| 116 |
})
|
| 117 |
|
|
|
|
| 118 |
submit_response = requests.post(
|
| 119 |
"https://agents-course-unit4-scoring.hf.space/submit",
|
| 120 |
json={
|
|
|
|
| 142 |
submit_btn = gr.Button("Run Evaluation", variant="primary")
|
| 143 |
output = gr.Textbox(label="Results")
|
| 144 |
table = gr.DataFrame(label="Details")
|
|
|
|
| 145 |
submit_btn.click(submit_answers, outputs=[output, table])
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|