renwei2024 commited on
Commit
ebe6efc
·
1 Parent(s): 7d916b8

Create GaiaAgent and use it to answer the GAIA questions

Browse files
Files changed (1) hide show
  1. app.py +103 -9
app.py CHANGED
@@ -1,23 +1,113 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
- # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
- def __init__(self):
15
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -38,9 +128,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
38
  questions_url = f"{api_url}/questions"
39
  submit_url = f"{api_url}/submit"
40
 
 
 
 
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
- agent = BasicAgent()
44
  except Exception as e:
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
 
6
+ from smolagents import (
7
+ CodeAgent,
8
+ DuckDuckGoSearchTool,
9
+ LiteLLMModel,
10
+ ToolCallingAgent,
11
+ )
12
+
13
+ from tools.text_inspector_tool import TextInspectorTool
14
+ from tools.text_web_browser import (
15
+ ArchiveSearchTool,
16
+ FinderTool,
17
+ FindNextTool,
18
+ PageDownTool,
19
+ PageUpTool,
20
+ SimpleTextBrowser,
21
+ VisitTool,
22
+ )
23
+ from tools.visual_qa import visualizer
24
+
25
+
26
  # (Keep Constants as is)
27
  # --- Constants ---
28
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
29
 
30
+ # --- Gaia Agent Definition ---
31
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
32
+ class GaiaAgent:
33
+ def __init__(self, model_id: str = "o3"):
34
+ self._model = self._create_model(model_id)
35
+ self._text_limit = 100000
36
+ text_webbrowser_agent = self._create_text_webbrowser_agent()
37
+ self._manager_agent = CodeAgent(
38
+ model=self._model,
39
+ tools=[visualizer, TextInspectorTool(self._model, self._text_limit)],
40
+ max_steps=12,
41
+ verbosity_level=2,
42
+ additional_authorized_imports=["*"],
43
+ planning_interval=4,
44
+ managed_agents=[text_webbrowser_agent],
45
+ )
46
+
47
+ print("GaiaAgent initialized.")
48
+
49
+ def _create_model(self, model_id):
50
+ custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
51
+ model_params = {
52
+ "model_id": model_id,
53
+ "custom_role_conversions": custom_role_conversions,
54
+ "max_completion_tokens": 8192,
55
+ "reasoning_effort": "high",
56
+ }
57
+ return LiteLLMModel(**model_params)
58
+
59
+ def _create_text_webbrowser_agent(self):
60
+ user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
61
+
62
+ BROWSER_CONFIG = {
63
+ "viewport_size": 1024 * 5,
64
+ "downloads_folder": "downloads_folder",
65
+ "request_kwargs": {
66
+ "headers": {"User-Agent": user_agent},
67
+ "timeout": 300,
68
+ },
69
+ "serpapi_key": os.getenv("SERPAPI_API_KEY"),
70
+ }
71
+
72
+ os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
73
+
74
+ browser = SimpleTextBrowser(**BROWSER_CONFIG)
75
+ WEB_TOOLS = [
76
+ DuckDuckGoSearchTool(),
77
+ VisitTool(browser),
78
+ PageUpTool(browser),
79
+ PageDownTool(browser),
80
+ FinderTool(browser),
81
+ FindNextTool(browser),
82
+ ArchiveSearchTool(browser),
83
+ TextInspectorTool(self._model, self._text_limit),
84
+ ]
85
+ text_webbrowser_agent = ToolCallingAgent(
86
+ model=self._model,
87
+ tools=WEB_TOOLS,
88
+ max_steps=20,
89
+ verbosity_level=2,
90
+ planning_interval=4,
91
+ name="search_agent",
92
+ description="""A team member that will search the internet to answer your question.
93
+ Ask him for all your questions that require browsing the web.
94
+ Provide him as much context as possible, in particular if you need to search on a specific timeframe!
95
+ And don't hesitate to provide him with a complex search task, like finding a difference between two webpages.
96
+ Your request must be a real sentence, not a google search! Like "Find me this information (...)" rather than a few keywords.
97
+ """,
98
+ provide_run_summary=True,
99
+ )
100
+ text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to .txt online files.
101
+ If a non-html page is in another format, especially .pdf or a Youtube video, use tool 'inspect_file_as_text' to inspect it.
102
+ Additionally, if after some searching you find out that you need more information to answer the question, you can use `final_answer` with your request for clarification as argument to request for more information."""
103
+
104
+ return text_webbrowser_agent
105
+
106
  def __call__(self, question: str) -> str:
107
  print(f"Agent received question (first 50 chars): {question[:50]}...")
108
+ answer = self._manager_agent.run(question)
109
+ print(f"Agent returning fixed answer: {answer}")
110
+ return answer
111
 
112
  def run_and_submit_all( profile: gr.OAuthProfile | None):
113
  """
 
128
  questions_url = f"{api_url}/questions"
129
  submit_url = f"{api_url}/submit"
130
 
131
+ # 0. Create the model
132
+
133
+
134
+
135
  # 1. Instantiate Agent ( modify this part to create your agent)
136
  try:
137
+ agent = GaiaAgent()
138
  except Exception as e:
139
  print(f"Error instantiating agent: {e}")
140
  return f"Error initializing agent: {e}", None