avfranco commited on
Commit
bc2210a
·
1 Parent(s): aa923a7

final assignment gaia agent tools v0

Browse files
Files changed (3) hide show
  1. agents/gaia.py +38 -6
  2. app.py +13 -36
  3. tools/gaiatool.py +33 -0
agents/gaia.py CHANGED
@@ -1,10 +1,42 @@
1
- # --- Basic Agent Definition ---
 
 
 
2
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
3
- class BasicAgent:
 
 
 
 
 
 
4
  def __init__(self):
5
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def __call__(self, question: str) -> str:
7
  print(f"Agent received question (first 50 chars): {question[:50]}...")
8
- fixed_answer = "This is a default answer."
9
- print(f"Agent returning fixed answer: {fixed_answer}")
10
- return fixed_answer
 
1
+ # --- Gaia Agent Definition ---
2
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
3
+ from tools.gaiatool import fetch_questions
4
+
5
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
6
+ # (Keep Constants as is)
7
+ # --- Constants ---
8
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
+
10
+ api_url = DEFAULT_API_URL
11
+
12
+ class GaiaAgent:
13
  def __init__(self):
14
+ print("GaiaAgent initialized.")
15
+
16
+ self.questions_url = f"{api_url}/questions"
17
+
18
+ # Initialize the model
19
+ self.model = HfApiModel()
20
+
21
+ # Initialize the DuckDuckGo search tool
22
+ self.search_tool = DuckDuckGoSearchTool(
23
+ name="DuckDuckGoSearch",
24
+ description="Use this tool to search the web using DuckDuckGo.",
25
+ model=self.model,
26
+ )
27
+
28
+ self.agent = CodeAgent(
29
+ model=self.model,
30
+ tools=[self.search_tool],
31
+ )
32
+
33
+ self.questions_data = fetch_questions(self.questions_url)
34
+
35
+ def get_questions(self):
36
+ return self.questions_data
37
+
38
  def __call__(self, question: str) -> str:
39
  print(f"Agent received question (first 50 chars): {question[:50]}...")
40
+ answer = self.agent.run(self.questions_data)
41
+ print(f"Agent returning fixed answer: {answer}")
42
+ return answer
app.py CHANGED
@@ -3,17 +3,14 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from agents.gaia import BasicAgent
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  def run_and_submit_all( profile: gr.OAuthProfile | None):
13
- """
14
- Fetches all questions, runs the BasicAgent on them, submits all answers,
15
- and displays the results.
16
- """
17
  # --- Determine HF Space Runtime URL and Repo URL ---
18
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
19
 
@@ -24,45 +21,25 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
24
  print("User not logged in.")
25
  return "Please Login to Hugging Face with the button.", None
26
 
27
- api_url = DEFAULT_API_URL
28
- questions_url = f"{api_url}/questions"
29
- submit_url = f"{api_url}/submit"
30
 
31
- # 1. Instantiate Agent ( modify this part to create your agent)
32
  try:
33
- agent = BasicAgent()
34
  except Exception as e:
35
  print(f"Error instantiating agent: {e}")
36
  return f"Error initializing agent: {e}", None
 
37
  # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
38
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
39
  print(agent_code)
40
 
41
- # 2. Fetch Questions
42
- print(f"Fetching questions from: {questions_url}")
43
- try:
44
- response = requests.get(questions_url, timeout=15)
45
- response.raise_for_status()
46
- questions_data = response.json()
47
- if not questions_data:
48
- print("Fetched questions list is empty.")
49
- return "Fetched questions list is empty or invalid format.", None
50
- print(f"Fetched {len(questions_data)} questions.")
51
- except requests.exceptions.RequestException as e:
52
- print(f"Error fetching questions: {e}")
53
- return f"Error fetching questions: {e}", None
54
- except requests.exceptions.JSONDecodeError as e:
55
- print(f"Error decoding JSON response from questions endpoint: {e}")
56
- print(f"Response text: {response.text[:500]}")
57
- return f"Error decoding server response for questions: {e}", None
58
- except Exception as e:
59
- print(f"An unexpected error occurred fetching questions: {e}")
60
- return f"An unexpected error occurred fetching questions: {e}", None
61
-
62
- # 3. Run your Agent
63
  results_log = []
64
  answers_payload = []
 
65
  print(f"Running agent on {len(questions_data)} questions...")
 
66
  for item in questions_data:
67
  task_id = item.get("task_id")
68
  question_text = item.get("question")
@@ -169,10 +146,10 @@ with gr.Blocks() as demo:
169
  if __name__ == "__main__":
170
  print("\n" + "-"*30 + " App Starting " + "-"*30)
171
  # Check for SPACE_HOST and SPACE_ID at startup for information
172
- #space_host_startup = "https://agents-course-unit4-scoring.hf.space" #os.getenv("SPACE_HOST")
173
- #space_id_startup = "avfranco/final_assignment_template" #os.getenv("SPACE_ID") # Get SPACE_ID at startup
174
- space_host_startup = os.getenv("SPACE_HOST")
175
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
176
 
177
  if space_host_startup:
178
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from agents.gaia import GaiaAgent
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  def run_and_submit_all( profile: gr.OAuthProfile | None):
13
+
 
 
 
14
  # --- Determine HF Space Runtime URL and Repo URL ---
15
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
16
 
 
21
  print("User not logged in.")
22
  return "Please Login to Hugging Face with the button.", None
23
 
24
+ submit_url = f"{DEFAULT_API_URL}/submit"
 
 
25
 
26
+ # 1. Instantiate Agent
27
  try:
28
+ agent = GaiaAgent()
29
  except Exception as e:
30
  print(f"Error instantiating agent: {e}")
31
  return f"Error initializing agent: {e}", None
32
+
33
  # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
34
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
35
  print(agent_code)
36
 
37
+ # 2. Run your Agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  results_log = []
39
  answers_payload = []
40
+ questions_data = agent.get_questions()
41
  print(f"Running agent on {len(questions_data)} questions...")
42
+
43
  for item in questions_data:
44
  task_id = item.get("task_id")
45
  question_text = item.get("question")
 
146
  if __name__ == "__main__":
147
  print("\n" + "-"*30 + " App Starting " + "-"*30)
148
  # Check for SPACE_HOST and SPACE_ID at startup for information
149
+ space_host_startup = "https://avfranco-final-assignment-template.hf.space" #os.getenv("SPACE_HOST")
150
+ space_id_startup = "avfranco/final_assignment_template" #os.getenv("SPACE_ID") # Get SPACE_ID at startup
151
+ #space_host_startup = os.getenv("SPACE_HOST")
152
+ #space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
153
 
154
  if space_host_startup:
155
  print(f"✅ SPACE_HOST found: {space_host_startup}")
tools/gaiatool.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import requests
3
+
4
+ # (Keep Constants as is)
5
+ # --- Constants ---
6
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
7
+
8
+ def fetch_questions(questions_url: str) -> list:
9
+ """
10
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
11
+ and displays the results.
12
+ """
13
+
14
+ try:
15
+ response = requests.get(questions_url, timeout=15)
16
+ response.raise_for_status()
17
+ questions_data = response.json()
18
+ if not questions_data:
19
+ print("Fetched questions list is empty.")
20
+ return "Fetched questions list is empty or invalid format.", None
21
+ print(f"Fetched {len(questions_data)} questions.")
22
+ except requests.exceptions.RequestException as e:
23
+ print(f"Error fetching questions: {e}")
24
+ return f"Error fetching questions: {e}", None
25
+ except requests.exceptions.JSONDecodeError as e:
26
+ print(f"Error decoding JSON response from questions endpoint: {e}")
27
+ print(f"Response text: {response.text[:500]}")
28
+ return f"Error decoding server response for questions: {e}", None
29
+ except Exception as e:
30
+ print(f"An unexpected error occurred fetching questions: {e}")
31
+ return f"An unexpected error occurred fetching questions: {e}", None
32
+
33
+ return questions_data