sirine1712 commited on
Commit
91f5922
Β·
verified Β·
1 Parent(s): 9925072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -65
app.py CHANGED
@@ -4,129 +4,147 @@ import requests
4
  import pandas as pd
5
  from smolagents import ToolCallingAgent, tool
6
  from duckduckgo_search import DDGS
 
 
7
 
8
-
9
- # Define a DuckDuckGo tool
10
  @tool
11
  def duck_search(query: str) -> str:
12
- """Searches the web using DuckDuckGo for the given query and returns the top results."""
13
- with DDGS() as ddgs:
14
- results = ddgs.text(query, max_results=3)
15
- return "\n".join([r["body"] for r in results])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
 
 
 
 
 
 
 
 
17
 
18
- # Custom Agent class
19
- class GAIAAgent:
20
  def __init__(self):
21
  self.agent = ToolCallingAgent(
22
  name="GAIAWebToolAgent",
23
  description="An agent using DuckDuckGo and calculator tools.",
24
  tools=[duck_search, calculator],
25
- model="gpt-3.5-turbo"
 
 
 
 
 
26
  )
 
27
 
28
  def __call__(self, question: str) -> str:
29
  print(f"πŸ” Question: {question}")
30
  try:
31
- return self.agent.run(
32
- question,
33
- step_limit=5,
34
- system_prompt=(
35
- "You are a helpful reasoning agent. You can answer questions using search and calculation tools. "
36
- "Be concise and accurate. Think step by step when needed. Use DuckDuckGo for recent or factual queries."
37
- )
38
- )
39
  except Exception as e:
40
  print(f"❌ Agent error: {e}")
41
  return f"Error: {e}"
42
 
43
-
44
- # Evaluation and submission code (Hugging Face GAIA integration)
45
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
46
 
 
47
  def run_and_submit_all(profile: gr.OAuthProfile | None):
48
  space_id = os.getenv("SPACE_ID")
49
- api_url = DEFAULT_API_URL
50
- questions_url = f"{api_url}/questions"
51
- submit_url = f"{api_url}/submit"
52
-
53
  if profile:
54
  username = profile.username
55
- print(f"User: {username}")
56
  else:
57
  return "Please login to Hugging Face.", None
58
 
 
 
 
 
59
  try:
60
- agent = GAIAAgent()
61
  except Exception as e:
62
  return f"Agent initialization error: {e}", None
63
 
64
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
65
-
66
  try:
67
  response = requests.get(questions_url, timeout=15)
68
  response.raise_for_status()
69
- questions_data = response.json()
 
 
70
  except Exception as e:
71
- return f"Error fetching questions: {e}", None
72
 
73
- answers_payload = []
74
  results_log = []
 
75
 
76
- for item in questions_data:
77
  task_id = item.get("task_id")
78
  question = item.get("question")
79
  if not task_id or not question:
80
  continue
81
  try:
82
  answer = agent(question)
83
- answers_payload.append({"task_id": task_id, "submitted_answer": answer})
84
  results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
 
85
  except Exception as e:
86
- results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"Error: {e}"})
 
87
 
88
  if not answers_payload:
89
- return "No answers generated.", pd.DataFrame(results_log)
90
-
91
- submission_data = {
92
- "username": username,
93
- "agent_code": agent_code,
94
- "answers": answers_payload
95
- }
96
 
97
  try:
98
- response = requests.post(submit_url, json=submission_data, timeout=60)
 
 
 
 
99
  response.raise_for_status()
100
- result_data = response.json()
101
- final_status = (
102
  f"βœ… Submission Successful!\n"
103
- f"User: {result_data.get('username')}\n"
104
- f"Score: {result_data.get('score')}% "
105
- f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\n"
106
- f"Message: {result_data.get('message', '')}"
107
  )
108
- return final_status, pd.DataFrame(results_log)
109
  except Exception as e:
110
- return f"Submission error: {e}", pd.DataFrame(results_log)
111
 
112
-
113
- # Gradio UI
114
  with gr.Blocks() as demo:
115
- gr.Markdown("## 🌍 GAIA Agent with Web Search & Calculator Tools")
116
- gr.Markdown(
117
- "1. Log in with your Hugging Face account.\n"
118
- "2. Click the button to evaluate the agent on GAIA questions.\n"
119
- "3. Results and score will appear below."
120
- )
121
-
122
  gr.LoginButton()
123
- run_button = gr.Button("πŸ” Run & Submit to GAIA")
124
- status_output = gr.Textbox(label="Status", lines=5)
125
- results_table = gr.DataFrame(label="Results")
126
-
127
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
128
 
 
129
 
130
  if __name__ == "__main__":
131
- print("πŸš€ Launching app...")
132
- demo.launch()
 
4
  import pandas as pd
5
  from smolagents import ToolCallingAgent, tool
6
  from duckduckgo_search import DDGS
7
+ import math
8
+ import openai
9
 
10
+ # --- Tools ---
 
11
  @tool
12
  def duck_search(query: str) -> str:
13
+ """
14
+ Searches the web using DuckDuckGo and returns a short summary.
15
+
16
+ Args:
17
+ query: The search query string.
18
+
19
+ Returns:
20
+ A string summarizing the top search results.
21
+ """
22
+ try:
23
+ with DDGS() as ddgs:
24
+ results = ddgs.text(query, max_results=3)
25
+ summaries = [f"{r['title']}: {r['body']}" for r in results]
26
+ return "\n\n".join(summaries) if summaries else "No results found."
27
+ except Exception as e:
28
+ return f"Search error: {e}"
29
+
30
+ @tool
31
+ def calculator(expression: str) -> str:
32
+ """
33
+ Evaluates basic math expressions using math module.
34
+
35
+ Args:
36
+ expression: A math expression as a string.
37
 
38
+ Returns:
39
+ The result or an error message.
40
+ """
41
+ try:
42
+ result = eval(expression, {"__builtins__": {}}, math.__dict__)
43
+ return str(result)
44
+ except Exception as e:
45
+ return f"Calculation error: {e}"
46
 
47
+ # --- Agent Wrapper ---
48
+ class WebSearchAgent:
49
  def __init__(self):
50
  self.agent = ToolCallingAgent(
51
  name="GAIAWebToolAgent",
52
  description="An agent using DuckDuckGo and calculator tools.",
53
  tools=[duck_search, calculator],
54
+ model="gpt-3.5-turbo", # You can use gpt-4o if you have access
55
+ step_limit=5,
56
+ system_prompt=(
57
+ "You're a helpful reasoning agent. Use the provided tools "
58
+ "to help answer the user's questions accurately."
59
+ ),
60
  )
61
+ print("βœ… Agent initialized.")
62
 
63
  def __call__(self, question: str) -> str:
64
  print(f"πŸ” Question: {question}")
65
  try:
66
+ return self.agent.run(question)
 
 
 
 
 
 
 
67
  except Exception as e:
68
  print(f"❌ Agent error: {e}")
69
  return f"Error: {e}"
70
 
71
+ # --- Constants ---
 
72
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
73
 
74
+ # --- Evaluation & Submission ---
75
  def run_and_submit_all(profile: gr.OAuthProfile | None):
76
  space_id = os.getenv("SPACE_ID")
 
 
 
 
77
  if profile:
78
  username = profile.username
79
+ print(f"πŸ‘€ User: {username}")
80
  else:
81
  return "Please login to Hugging Face.", None
82
 
83
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
84
+ questions_url = f"{DEFAULT_API_URL}/questions"
85
+ submit_url = f"{DEFAULT_API_URL}/submit"
86
+
87
  try:
88
+ agent = WebSearchAgent()
89
  except Exception as e:
90
  return f"Agent initialization error: {e}", None
91
 
 
 
92
  try:
93
  response = requests.get(questions_url, timeout=15)
94
  response.raise_for_status()
95
+ questions = response.json()
96
+ if not questions:
97
+ return "No questions received.", None
98
  except Exception as e:
99
+ return f"Failed to fetch questions: {e}", None
100
 
 
101
  results_log = []
102
+ answers_payload = []
103
 
104
+ for item in questions:
105
  task_id = item.get("task_id")
106
  question = item.get("question")
107
  if not task_id or not question:
108
  continue
109
  try:
110
  answer = agent(question)
 
111
  results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
112
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
113
  except Exception as e:
114
+ error_msg = f"Agent error: {e}"
115
+ results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": error_msg})
116
 
117
  if not answers_payload:
118
+ return "No answers were generated.", pd.DataFrame(results_log)
 
 
 
 
 
 
119
 
120
  try:
121
+ response = requests.post(submit_url, json={
122
+ "username": username.strip(),
123
+ "agent_code": agent_code,
124
+ "answers": answers_payload
125
+ }, timeout=60)
126
  response.raise_for_status()
127
+ result = response.json()
128
+ status = (
129
  f"βœ… Submission Successful!\n"
130
+ f"User: {result.get('username')}\n"
131
+ f"Score: {result.get('score', 'N/A')}% "
132
+ f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
133
+ f"Message: {result.get('message', 'No message')}"
134
  )
135
+ return status, pd.DataFrame(results_log)
136
  except Exception as e:
137
+ return f"❌ Submission failed: {e}", pd.DataFrame(results_log)
138
 
139
+ # --- UI ---
 
140
  with gr.Blocks() as demo:
141
+ gr.Markdown("# πŸ€– GAIA Agent with Web Search & Calculator")
 
 
 
 
 
 
142
  gr.LoginButton()
143
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
144
+ status_box = gr.Textbox(label="Status", lines=5)
145
+ result_table = gr.DataFrame(label="Agent Answers")
 
 
146
 
147
+ run_btn.click(fn=run_and_submit_all, outputs=[status_box, result_table])
148
 
149
  if __name__ == "__main__":
150
+ demo.launch(debug=True, share=False)