File size: 4,428 Bytes
10e9b7d
 
4c934c3
 
1968b68
43ab812
16da5cd
4c934c3
0c36fa7
16da5cd
 
 
 
 
 
 
0c36fa7
94d642e
16da5cd
 
4c15dab
94d642e
 
326bc46
94d642e
16da5cd
94d642e
 
 
16da5cd
 
 
 
 
 
 
 
 
d7da87a
16da5cd
 
 
94d642e
16da5cd
 
326bc46
 
94d642e
 
16da5cd
 
 
 
94d642e
 
16da5cd
94d642e
0c36fa7
94d642e
 
16da5cd
94d642e
0c36fa7
94d642e
16da5cd
 
94d642e
 
 
16da5cd
94d642e
16da5cd
94d642e
0c36fa7
16da5cd
0c36fa7
16da5cd
94d642e
0c36fa7
 
94d642e
4c15dab
0c36fa7
326bc46
16da5cd
4c15dab
16da5cd
94d642e
 
16da5cd
 
 
 
 
 
 
94d642e
 
16da5cd
94d642e
16da5cd
 
94d642e
16da5cd
 
 
 
94d642e
16da5cd
94d642e
16da5cd
94d642e
16da5cd
 
94d642e
16da5cd
 
 
 
 
 
 
94d642e
16da5cd
 
 
 
 
94d642e
 
 
16da5cd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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()