eigbney commited on
Commit
2a268c4
·
verified ·
1 Parent(s): d4417d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -4,33 +4,34 @@ import requests
4
  import pandas as pd
5
  from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
6
 
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
  class BasicAgent:
10
  def __init__(self):
11
-
12
  self.model = LiteLLMModel(model_id="huggingface/Qwen/Qwen2.5-72B-Instruct")
13
 
 
 
 
14
  self.agent = CodeAgent(
15
- tools=[DuckDuckGoSearchTool()],
16
  model=self.model,
17
  add_base_tools=True
18
  )
19
  print("Advanced smolagent initialized with LiteLLM.")
20
 
21
  def __call__(self, question: str) -> str:
22
- # Prompting for Exact Match scoring
23
  clean_prompt = (
24
  f"Question: {question}\n\n"
25
  "Instructions: Solve the question above. Provide ONLY the final answer "
26
  "value without any explanation, units, or extra text. "
27
- "Do not include 'FINAL ANSWER' in your response."
28
  )
29
 
30
  try:
31
- # The agent will reason and use tools if necessary
32
  result = self.agent.run(clean_prompt)
33
- # Ensure we return a clean string
34
  return str(result).strip()
35
  except Exception as e:
36
  print(f"Error during agent execution: {e}")
@@ -46,14 +47,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
46
  api_url = DEFAULT_API_URL
47
  questions_url = f"{api_url}/questions"
48
  submit_url = f"{api_url}/submit"
 
 
 
49
 
50
  # 1. Instantiate Agent
51
  try:
52
  agent = BasicAgent()
53
  except Exception as e:
54
  return f"Error initializing agent: {e}", None
55
-
56
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
57
 
58
  # 2. Fetch Questions
59
  try:
@@ -67,6 +69,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
67
  results_log = []
68
  answers_payload = []
69
 
 
70
  for item in questions_data:
71
  task_id = item.get("task_id")
72
  question_text = item.get("question")
@@ -74,9 +77,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
74
  if not task_id or question_text is None:
75
  continue
76
 
77
- # Optional: Handle file downloads for specific tasks
78
- # You could add logic here to download from f"{api_url}/files/{task_id}"
79
-
80
  try:
81
  submitted_answer = agent(question_text)
82
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
@@ -99,8 +99,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
99
  final_status = (
100
  f"Submission Successful!\n"
101
  f"User: {result_data.get('username')}\n"
102
- f"Score: {result_data.get('score')}% "
103
- f"({result_data.get('correct_count')}/{result_data.get('total_attempted')} correct)"
104
  )
105
  return final_status, pd.DataFrame(results_log)
106
  except Exception as e:
@@ -109,11 +109,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
109
  # --- Gradio Interface ---
110
  with gr.Blocks() as demo:
111
  gr.Markdown("# GAIA Agent Evaluation Runner")
112
- gr.Markdown("This agent uses `smolagents` with a Python interpreter and Web Search to solve tasks.")
113
-
114
  gr.LoginButton()
115
  run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
116
-
117
  status_output = gr.Textbox(label="Status", lines=4)
118
  results_table = gr.DataFrame(label="Detailed Results", wrap=True)
119
 
 
4
  import pandas as pd
5
  from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
6
 
7
+ # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  class BasicAgent:
11
  def __init__(self):
12
+ # Using LiteLLM to connect to Qwen
13
  self.model = LiteLLMModel(model_id="huggingface/Qwen/Qwen2.5-72B-Instruct")
14
 
15
+ # Initialize search tool
16
+ search_tool = DuckDuckGoSearchTool()
17
+
18
  self.agent = CodeAgent(
19
+ tools=[search_tool],
20
  model=self.model,
21
  add_base_tools=True
22
  )
23
  print("Advanced smolagent initialized with LiteLLM.")
24
 
25
  def __call__(self, question: str) -> str:
 
26
  clean_prompt = (
27
  f"Question: {question}\n\n"
28
  "Instructions: Solve the question above. Provide ONLY the final answer "
29
  "value without any explanation, units, or extra text. "
30
+ "Do not include the phrase 'FINAL ANSWER' in your response."
31
  )
32
 
33
  try:
 
34
  result = self.agent.run(clean_prompt)
 
35
  return str(result).strip()
36
  except Exception as e:
37
  print(f"Error during agent execution: {e}")
 
47
  api_url = DEFAULT_API_URL
48
  questions_url = f"{api_url}/questions"
49
  submit_url = f"{api_url}/submit"
50
+
51
+ # Define agent_code link correctly
52
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "https://huggingface.co/spaces"
53
 
54
  # 1. Instantiate Agent
55
  try:
56
  agent = BasicAgent()
57
  except Exception as e:
58
  return f"Error initializing agent: {e}", None
 
 
59
 
60
  # 2. Fetch Questions
61
  try:
 
69
  results_log = []
70
  answers_payload = []
71
 
72
+ # LIMITING TO FIRST 5 FOR TESTING - Remove [:5] to do all
73
  for item in questions_data:
74
  task_id = item.get("task_id")
75
  question_text = item.get("question")
 
77
  if not task_id or question_text is None:
78
  continue
79
 
 
 
 
80
  try:
81
  submitted_answer = agent(question_text)
82
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
99
  final_status = (
100
  f"Submission Successful!\n"
101
  f"User: {result_data.get('username')}\n"
102
+ f"Score: {result_data.get('score', 0)}% "
103
+ f"({result_data.get('correct_count', 0)}/{result_data.get('total_attempted', 0)} correct)"
104
  )
105
  return final_status, pd.DataFrame(results_log)
106
  except Exception as e:
 
109
  # --- Gradio Interface ---
110
  with gr.Blocks() as demo:
111
  gr.Markdown("# GAIA Agent Evaluation Runner")
 
 
112
  gr.LoginButton()
113
  run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
 
114
  status_output = gr.Textbox(label="Status", lines=4)
115
  results_table = gr.DataFrame(label="Detailed Results", wrap=True)
116