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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -66
app.py CHANGED
@@ -1,132 +1,110 @@
 
1
  import subprocess
2
  import sys
3
 
4
- # Force install the missing dependency at runtime
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 os
11
  import gradio as gr
12
  import requests
13
  import pandas as pd
14
- from smolagents import CodeAgent, LiteLLMModel
 
 
 
 
 
 
15
 
16
- # --- Constants ---
17
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
 
19
  class BasicAgent:
20
  def __init__(self):
21
- # Using LiteLLM to connect to Qwen
22
- self.model = LiteLLMModel(model_id="huggingface/Qwen/Qwen2.5-72B-Instruct")
23
-
24
- # Initialize search tool
25
- search_tool = DuckDuckGoSearchTool()
26
 
 
 
 
 
 
 
 
27
  self.agent = CodeAgent(
28
- tools=[search_tool],
29
  model=self.model,
30
- add_base_tools=True
31
  )
32
- print("Advanced smolagent initialized with LiteLLM.")
33
 
34
  def __call__(self, question: str) -> str:
35
  clean_prompt = (
36
  f"Question: {question}\n\n"
37
  "Instructions: Solve the question above. Provide ONLY the final answer "
38
- "value without any explanation, units, or extra text. "
39
- "Do not include the phrase 'FINAL ANSWER' in your response."
40
  )
41
-
42
  try:
43
  result = self.agent.run(clean_prompt)
44
  return str(result).strip()
45
  except Exception as e:
46
- print(f"Error during agent execution: {e}")
47
- return "Error solving question"
48
 
49
  def run_and_submit_all(profile: gr.OAuthProfile | None):
50
  space_id = os.getenv("SPACE_ID")
51
-
52
  if not profile:
53
  return "Please Login to Hugging Face with the button.", None
54
 
55
  username = f"{profile.username}"
56
- api_url = DEFAULT_API_URL
57
- questions_url = f"{api_url}/questions"
58
- submit_url = f"{api_url}/submit"
59
-
60
- # Define agent_code link correctly
61
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "https://huggingface.co/spaces"
62
 
63
- # 1. Instantiate Agent
64
  try:
65
- agent = BasicAgent()
66
- except Exception as e:
67
- return f"Error initializing agent: {e}", None
68
-
69
- # 2. Fetch Questions
70
- try:
71
- response = requests.get(questions_url, timeout=15)
72
- response.raise_for_status()
73
  questions_data = response.json()
74
  except Exception as e:
75
  return f"Error fetching questions: {e}", None
76
 
77
- # 3. Run Agent on Questions
78
- results_log = []
79
  answers_payload = []
 
80
 
81
- # LIMITING TO FIRST 5 FOR TESTING - Remove [:5] to do all
82
  for item in questions_data:
83
  task_id = item.get("task_id")
84
  question_text = item.get("question")
85
-
86
- if not task_id or question_text is None:
87
- continue
88
 
89
- try:
90
- submitted_answer = agent(question_text)
91
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
92
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
93
- except Exception as e:
94
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
95
 
96
- # 4. Submit Results
97
  submission_data = {
98
- "username": username.strip(),
99
  "agent_code": agent_code,
100
  "answers": answers_payload
101
  }
102
 
103
  try:
104
- response = requests.post(submit_url, json=submission_data, timeout=60)
105
- response.raise_for_status()
106
  result_data = response.json()
107
-
108
- final_status = (
109
- f"Submission Successful!\n"
110
- f"User: {result_data.get('username')}\n"
111
- f"Score: {result_data.get('score', 0)}% "
112
- f"({result_data.get('correct_count', 0)}/{result_data.get('total_attempted', 0)} correct)"
113
- )
114
- return final_status, pd.DataFrame(results_log)
115
  except Exception as e:
116
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
117
 
118
  # --- Gradio Interface ---
119
  with gr.Blocks() as demo:
120
- gr.Markdown("# GAIA Agent Evaluation Runner")
121
  gr.LoginButton()
122
- run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
123
- status_output = gr.Textbox(label="Status", lines=4)
124
- results_table = gr.DataFrame(label="Detailed Results", wrap=True)
125
-
126
- run_button.click(
127
- fn=run_and_submit_all,
128
- outputs=[status_output, results_table]
129
- )
130
 
131
  if __name__ == "__main__":
132
  demo.launch()
 
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 = {
86
+ "username": username,
87
  "agent_code": agent_code,
88
  "answers": answers_payload
89
  }
90
 
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()