sirine1712's picture
Update app.py
1968b68 verified
raw
history blame
4.43 kB
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import ToolCallingAgent, tool
from duckduckgo_search import DDGS
from smolagents.schema import Tool
# Define a DuckDuckGo tool
@Tool
def duck_search(query: str) -> str:
"""Searches the web using DuckDuckGo for the given query and returns the top results."""
with DDGS() as ddgs:
results = ddgs.text(query, max_results=3)
return "\n".join([r["body"] for r in results])
# Custom Agent class
class GAIAAgent:
def __init__(self):
self.agent = ToolCallingAgent(
name="GAIAWebToolAgent",
description="An agent using DuckDuckGo and calculator tools.",
tools=[duck_search, calculator],
model="gpt-3.5-turbo"
)
def __call__(self, question: str) -> str:
print(f"πŸ” Question: {question}")
try:
return self.agent.run(
question,
step_limit=5,
system_prompt=(
"You are a helpful reasoning agent. You can answer questions using search and calculation tools. "
"Be concise and accurate. Think step by step when needed. Use DuckDuckGo for recent or factual queries."
)
)
except Exception as e:
print(f"❌ Agent error: {e}")
return f"Error: {e}"
# Evaluation and submission code (Hugging Face GAIA integration)
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
if profile:
username = profile.username
print(f"User: {username}")
else:
return "Please login to Hugging Face.", None
try:
agent = GAIAAgent()
except Exception as e:
return f"Agent initialization error: {e}", None
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
except Exception as e:
return f"Error fetching questions: {e}", None
answers_payload = []
results_log = []
for item in questions_data:
task_id = item.get("task_id")
question = item.get("question")
if not task_id or not question:
continue
try:
answer = agent(question)
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
except Exception as e:
results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"Error: {e}"})
if not answers_payload:
return "No answers generated.", pd.DataFrame(results_log)
submission_data = {
"username": username,
"agent_code": agent_code,
"answers": answers_payload
}
try:
response = requests.post(submit_url, json=submission_data, timeout=60)
response.raise_for_status()
result_data = response.json()
final_status = (
f"βœ… Submission Successful!\n"
f"User: {result_data.get('username')}\n"
f"Score: {result_data.get('score')}% "
f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\n"
f"Message: {result_data.get('message', '')}"
)
return final_status, pd.DataFrame(results_log)
except Exception as e:
return f"Submission error: {e}", pd.DataFrame(results_log)
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 🌍 GAIA Agent with Web Search & Calculator Tools")
gr.Markdown(
"1. Log in with your Hugging Face account.\n"
"2. Click the button to evaluate the agent on GAIA questions.\n"
"3. Results and score will appear below."
)
gr.LoginButton()
run_button = gr.Button("πŸ” Run & Submit to GAIA")
status_output = gr.Textbox(label="Status", lines=5)
results_table = gr.DataFrame(label="Results")
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
print("πŸš€ Launching app...")
demo.launch()