LamiaYT commited on
Commit
70fa272
·
1 Parent(s): a39e119

Deploy GAIA agent

Browse files
Files changed (1) hide show
  1. app.py +71 -84
app.py CHANGED
@@ -3,126 +3,113 @@
3
  import os
4
  import gradio as gr
5
  import requests
6
- import inspect
7
  import pandas as pd
 
8
 
9
- # SmolAgents imports
10
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
11
 
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
- # --- Enhanced Agent Definition ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  class BasicAgent:
17
  def __init__(self):
18
- print("BasicAgent initialized with real agentic capabilities.")
19
-
20
- # Initialize tools and model
21
- self.search_tool = DuckDuckGoSearchTool()
22
  self.model = InferenceClientModel()
23
  self.agent = CodeAgent(
24
  model=self.model,
25
- tools=[self.search_tool]
26
  )
27
 
28
  def __call__(self, question: str) -> str:
29
- print(f"Agent received question (first 50 chars): {question[:50]}...")
30
  try:
31
- response = self.agent.run(question)
32
- print(f"Agent response (first 50 chars): {response[:50]}...")
33
- return response
34
  except Exception as e:
35
- print(f"Agent error during run: {e}")
36
- return f"Error in agent: {e}"
37
 
38
  def run_and_submit_all(profile: gr.OAuthProfile | None):
39
- """
40
- Fetches all questions, runs the BasicAgent on them, submits all answers,
41
- and displays the results.
42
- """
43
  space_id = os.getenv("SPACE_ID")
44
-
45
- if profile:
46
- username = profile.username
47
- print(f"User logged in: {username}")
48
- else:
49
- print("User not logged in.")
50
- return "Please login to Hugging Face to submit answers.", None
51
-
52
  questions_url = f"{DEFAULT_API_URL}/questions"
53
  submit_url = f"{DEFAULT_API_URL}/submit"
54
 
55
- try:
56
- agent = BasicAgent()
57
  except Exception as e:
58
- print(f"Error instantiating agent: {e}")
59
- return f"Error initializing agent: {e}", None
60
 
61
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
62
- print(agent_code)
63
-
64
- # Fetch questions
65
  try:
66
- resp = requests.get(questions_url, timeout=15)
67
- resp.raise_for_status()
68
- questions_data = resp.json()
69
- if not questions_data:
70
- return "Empty or invalid question list.", None
71
- print(f"Fetched {len(questions_data)} questions.")
72
  except Exception as e:
73
- print(f"Error fetching questions: {e}")
74
- return f"Error fetching questions: {e}", None
75
-
76
- # Run agent on questions
77
- results_log = []
78
- answers_payload = []
79
- for item in questions_data:
80
- task_id = item.get("task_id")
81
- question_text = item.get("question")
82
- if not task_id or question_text is None:
83
- continue
84
- try:
85
- submitted = agent(question_text)
86
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted})
87
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted})
88
- except Exception as e:
89
- print(f"Error on task {task_id}: {e}")
90
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
91
 
92
- if not answers_payload:
93
- return "Agent did not produce any answers.", pd.DataFrame(results_log)
94
 
95
- # Prepare & submit
96
- payload = {"username": username, "agent_code": agent_code, "answers": answers_payload}
97
  try:
98
- submit_resp = requests.post(submit_url, json=payload, timeout=60)
99
- submit_resp.raise_for_status()
100
- result_json = submit_resp.json()
101
- final_status = (
102
- f"Submission Successful!\n"
103
- f"User: {result_json.get('username')}\n"
104
- f"Score: {result_json.get('score', 'N/A')}% "
105
- f"({result_json.get('correct_count', '?')}/{result_json.get('total_attempted', '?')} correct)\n"
106
- f"Message: {result_json.get('message', '')}"
107
  )
108
- return final_status, pd.DataFrame(results_log)
109
  except Exception as e:
110
- return f"Submission failed: {e}", pd.DataFrame(results_log)
111
 
112
  # --- Gradio UI ---
113
  with gr.Blocks() as demo:
114
- gr.Markdown("# Basic Agent Evaluation Runner")
115
- gr.Markdown("""
116
- Modify `BasicAgent` to add more tools or logic.
117
- Log in, click **Run Evaluation & Submit All Answers**, and watch it process automatically.
118
- """)
119
  gr.LoginButton()
120
- run_btn = gr.Button("Run Evaluation & Submit All Answers")
121
- status = gr.Textbox(label="Status / Submission Result", lines=5, interactive=False)
122
- results = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
123
-
124
- run_btn.click(fn=run_and_submit_all, outputs=[status, results])
125
 
126
  if __name__ == "__main__":
127
- print("Launching app...")
128
  demo.launch(debug=True, share=False)
 
3
  import os
4
  import gradio as gr
5
  import requests
 
6
  import pandas as pd
7
+ from bs4 import BeautifulSoup
8
 
9
+ from smolagents import CodeAgent, tool, InferenceClientModel
 
10
 
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
+ # --- Custom Web Search Tool (avoiding proxies) ---
15
+ @tool
16
+ def simple_search(query: str) -> str:
17
+ """
18
+ Search DuckDuckGo via HTML parsing.
19
+
20
+ Args:
21
+ query (str): search query string.
22
+
23
+ Returns:
24
+ str: top 3 titles and URLs.
25
+ """
26
+ try:
27
+ resp = requests.get(
28
+ "https://html.duckduckgo.com/html/",
29
+ params={"q": query},
30
+ timeout=10
31
+ )
32
+ resp.raise_for_status()
33
+ soup = BeautifulSoup(resp.text, "html.parser")
34
+ results = []
35
+ for a in soup.select("a.result__a")[:3]:
36
+ title = a.get_text()
37
+ url = a["href"]
38
+ results.append(f"- {title}\n {url}")
39
+ return "\n".join(results) if results else "No results."
40
+ except Exception as e:
41
+ return f"Search error: {e}"
42
+
43
+ # --- Enhanced Agent ---
44
  class BasicAgent:
45
  def __init__(self):
46
+ print("BasicAgent initialized.")
 
 
 
47
  self.model = InferenceClientModel()
48
  self.agent = CodeAgent(
49
  model=self.model,
50
+ tools=[simple_search]
51
  )
52
 
53
  def __call__(self, question: str) -> str:
54
+ print("Received:", question[:50], "...")
55
  try:
56
+ return self.agent.run(question)
 
 
57
  except Exception as e:
58
+ return f"Agent run error: {e}"
 
59
 
60
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
 
61
  space_id = os.getenv("SPACE_ID")
62
+ if not profile:
63
+ return "Please login to Hugging Face", None
64
+ username = profile.username
 
 
 
 
 
65
  questions_url = f"{DEFAULT_API_URL}/questions"
66
  submit_url = f"{DEFAULT_API_URL}/submit"
67
 
68
+ try: agent = BasicAgent()
 
69
  except Exception as e:
70
+ return f"Agent init error: {e}", None
 
71
 
72
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
 
 
73
  try:
74
+ r = requests.get(questions_url, timeout=15)
75
+ r.raise_for_status()
76
+ qs = r.json()
 
 
 
77
  except Exception as e:
78
+ return f"Fetch questions error: {e}", None
79
+
80
+ logs, payload = [], []
81
+ for item in qs:
82
+ tid, q = item.get("task_id"), item.get("question")
83
+ if not tid or q is None: continue
84
+ ans = agent(q)
85
+ payload.append({"task_id": tid, "submitted_answer": ans})
86
+ logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
 
 
 
 
 
 
 
 
 
87
 
88
+ if not payload:
89
+ return "No answers generated.", pd.DataFrame(logs)
90
 
91
+ submit = {"username": username, "agent_code": agent_code, "answers": payload}
 
92
  try:
93
+ resp = requests.post(submit_url, json=submit, timeout=60)
94
+ resp.raise_for_status()
95
+ data = resp.json()
96
+ status = (
97
+ f" Score: {data.get('score','N/A')}% "
98
+ f"({data.get('correct_count','?')}/{data.get('total_attempted','?')} correct)\n"
99
+ f"{data.get('message','No message')}"
 
 
100
  )
101
+ return status, pd.DataFrame(logs)
102
  except Exception as e:
103
+ return f"Submit error: {e}", pd.DataFrame(logs)
104
 
105
  # --- Gradio UI ---
106
  with gr.Blocks() as demo:
107
+ gr.Markdown("# GAIA Evaluation Runner")
 
 
 
 
108
  gr.LoginButton()
109
+ run_btn = gr.Button("Run Evaluation & Submit")
110
+ status = gr.Textbox(lines=5, interactive=False)
111
+ results = gr.DataFrame(wrap=True)
112
+ run_btn.click(run_and_submit_all, outputs=[status, results])
 
113
 
114
  if __name__ == "__main__":
 
115
  demo.launch(debug=True, share=False)