eigbney commited on
Commit
e919265
·
verified ·
1 Parent(s): 6689564

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -43
app.py CHANGED
@@ -1,85 +1,78 @@
1
  import os
2
- import subprocess
3
- import sys
4
-
5
- try:
6
- import duckduckgo_search
7
- except ImportError:
8
- subprocess.check_call([sys.executable, "-m", "pip", "install", "duckduckgo-search==6.3.2"])
9
-
10
  import gradio as gr
11
  import requests
12
  import pandas as pd
13
  from smolagents import CodeAgent, HfApiModel
14
 
15
- try:
16
- from smolagents import DuckDuckGoSearchTool
17
- HAS_SEARCH = True
18
- except ImportError:
19
- HAS_SEARCH = False
20
-
21
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
22
 
23
  class BasicAgent:
24
  def __init__(self):
 
 
25
  self.model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct")
26
 
27
- tools = []
28
- if HAS_SEARCH:
29
- try:
30
- tools.append(DuckDuckGoSearchTool())
31
- except Exception as e:
32
- print(f"Could not init search tool: {e}")
33
-
34
  self.agent = CodeAgent(
35
- tools=tools,
36
  model=self.model,
37
- add_base_tools=True # Python interpreter for math/logic
38
  )
39
- print(f"Agent initialized. Search available: {HAS_SEARCH}")
40
 
41
  def __call__(self, question: str) -> str:
 
42
  clean_prompt = (
43
- f"Question: {question}\n\n"
44
- "Instructions: Solve the question above. Provide ONLY the final answer "
45
- "value. No explanation, no 'The answer is...', just the value."
 
46
  )
 
47
  try:
 
48
  result = self.agent.run(clean_prompt)
49
  return str(result).strip()
50
  except Exception as e:
51
- return f"Error: {e}"
 
52
 
53
  def run_and_submit_all(profile: gr.OAuthProfile | None):
54
  space_id = os.getenv("SPACE_ID")
 
55
  if not profile:
56
  return "Please Login to Hugging Face with the button.", None
57
 
58
- username = f"{profile.username}"
59
- # Define agent code link
60
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
61
 
62
- # 1. Fetch Questions
63
  try:
64
  response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
65
  questions_data = response.json()
66
  except Exception as e:
67
- return f"Error fetching questions: {e}", None
68
 
69
- # 2. Run Agent
70
  agent = BasicAgent()
71
  answers_payload = []
72
  results_log = []
73
 
74
- # Process all questions
 
75
  for item in questions_data:
76
  task_id = item.get("task_id")
77
  question_text = item.get("question")
 
78
  if not task_id: continue
79
 
 
80
  submitted_answer = agent(question_text)
 
81
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
82
- results_log.append({"Task ID": task_id, "Answer": submitted_answer})
83
 
84
  # 3. Submit
85
  submission_data = {
@@ -91,20 +84,31 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
91
  try:
92
  response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
93
  result_data = response.json()
94
- status = f"Success! Score: {result_data.get('score')}% ({result_data.get('correct_count')}/{result_data.get('total_attempted')})"
95
- return status, pd.DataFrame(results_log)
 
 
 
 
 
96
  except Exception as e:
97
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
98
 
99
  # --- Gradio Interface ---
100
  with gr.Blocks() as demo:
101
- gr.Markdown("# GAIA Final Solver")
 
 
102
  gr.LoginButton()
103
- run_button = gr.Button("Run Evaluation & Submit", variant="primary")
104
- status_output = gr.Textbox(label="Status")
105
- results_table = gr.DataFrame(label="Answers Generated")
 
106
 
107
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
108
 
109
  if __name__ == "__main__":
110
  demo.launch()
 
1
  import os
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
  from smolagents import CodeAgent, HfApiModel
6
 
7
+ # --- Constants ---
 
 
 
 
 
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  class BasicAgent:
11
  def __init__(self):
12
+ # HfApiModel is native and fast within the HF ecosystem
13
+ # It uses your HF_TOKEN automatically if set in Secrets
14
  self.model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct")
15
 
16
+ # We use CodeAgent WITHOUT external tools like Search.
17
+ # add_base_tools=True provides the Python Interpreter for logic/math.
 
 
 
 
 
18
  self.agent = CodeAgent(
19
+ tools=[],
20
  model=self.model,
21
+ add_base_tools=True
22
  )
23
+ print("Clean Agent initialized (Python Interpreter enabled).")
24
 
25
  def __call__(self, question: str) -> str:
26
+ # Prompt specifically designed for GAIA Exact Match scoring
27
  clean_prompt = (
28
+ f"Solve this task: {question}\n\n"
29
+ "Final Answer Requirement: Provide ONLY the numeric or text value. "
30
+ "Do not include units, symbols, or conversational filler. "
31
+ "No 'The answer is...', no 'FINAL ANSWER' text. Just the raw value."
32
  )
33
+
34
  try:
35
+ # The agent will write code to solve if the question is complex
36
  result = self.agent.run(clean_prompt)
37
  return str(result).strip()
38
  except Exception as e:
39
+ print(f"Agent Error: {e}")
40
+ return "Error"
41
 
42
  def run_and_submit_all(profile: gr.OAuthProfile | None):
43
  space_id = os.getenv("SPACE_ID")
44
+
45
  if not profile:
46
  return "Please Login to Hugging Face with the button.", None
47
 
48
+ username = profile.username
49
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
 
50
 
51
+ # 1. Fetch
52
  try:
53
  response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
54
  questions_data = response.json()
55
  except Exception as e:
56
+ return f"Fetch Error: {e}", None
57
 
58
+ # 2. Run
59
  agent = BasicAgent()
60
  answers_payload = []
61
  results_log = []
62
 
63
+ print(f"Starting evaluation for {len(questions_data)} questions...")
64
+
65
  for item in questions_data:
66
  task_id = item.get("task_id")
67
  question_text = item.get("question")
68
+
69
  if not task_id: continue
70
 
71
+ # Run agent
72
  submitted_answer = agent(question_text)
73
+
74
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
75
+ results_log.append({"Task ID": task_id, "Question": question_text[:100], "Answer": submitted_answer})
76
 
77
  # 3. Submit
78
  submission_data = {
 
84
  try:
85
  response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
86
  result_data = response.json()
87
+
88
+ final_status = (
89
+ f"Submission Successful!\n"
90
+ f"Score: {result_data.get('score')}% "
91
+ f"({result_data.get('correct_count')}/{result_data.get('total_attempted')} correct)"
92
+ )
93
+ return final_status, pd.DataFrame(results_log)
94
  except Exception as e:
95
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
96
 
97
  # --- Gradio Interface ---
98
  with gr.Blocks() as demo:
99
+ gr.Markdown("# GAIA Agent Evaluation Runner (Clean Version)")
100
+ gr.Markdown("Uses `smolagents` with a built-in Python Interpreter to solve tasks.")
101
+
102
  gr.LoginButton()
103
+ run_button = gr.Button("Run Evaluation & Submit All", variant="primary")
104
+
105
+ status_output = gr.Textbox(label="Status", lines=4)
106
+ results_table = gr.DataFrame(label="Generated Answers", wrap=True)
107
 
108
+ run_button.click(
109
+ fn=run_and_submit_all,
110
+ outputs=[status_output, results_table]
111
+ )
112
 
113
  if __name__ == "__main__":
114
  demo.launch()