avi080704 commited on
Commit
42411d3
·
verified ·
1 Parent(s): 23b0362

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -47
app.py CHANGED
@@ -4,24 +4,29 @@ import requests
4
  import pandas as pd
5
 
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool
7
- from smolagents.models import ApiModel
8
 
9
- # -----------------------------
10
  # Constants
11
- # -----------------------------
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
- # -----------------------------
15
- # Build Agent
16
- # -----------------------------
17
  class BasicAgent:
 
18
  def __init__(self):
19
 
20
- # Uses your HF token automatically from Space secrets
21
- model = ApiModel(
22
- model_id="Qwen/Qwen2.5-72B-Instruct"
 
 
 
23
  )
24
 
 
25
  self.agent = CodeAgent(
26
  tools=[
27
  DuckDuckGoSearchTool()
@@ -31,79 +36,122 @@ class BasicAgent:
31
  verbosity_level=1
32
  )
33
 
34
- print("HF Agent initialized.")
35
 
36
  def __call__(self, question: str) -> str:
37
 
 
 
38
  prompt = f"""
39
- You are a highly accurate GAIA benchmark solving agent.
40
 
41
- Rules:
42
- - Think carefully step by step.
43
- - Use web search when needed.
 
44
  - Return ONLY the final answer.
45
- - Keep the answer concise.
46
- - Do not explain reasoning unless asked.
47
 
48
  Question:
49
  {question}
50
  """
51
 
52
  try:
53
- answer = self.agent.run(prompt)
54
 
55
- if answer is None:
56
- return "I could not determine the answer."
 
 
 
 
57
 
58
- return str(answer).strip()
 
 
59
 
60
  except Exception as e:
 
61
  print(f"Agent error: {e}")
62
- return f"Error: {e}"
63
 
64
- # -----------------------------
65
- # Main Evaluation Function
66
- # -----------------------------
 
 
 
67
  def run_and_submit_all(profile: gr.OAuthProfile | None):
68
 
 
69
  space_id = os.getenv("SPACE_ID")
70
 
 
71
  if profile:
72
- username = f"{profile.username}"
73
- print(f"User logged in: {username}")
74
  else:
75
- return "Please login to Hugging Face.", None
76
 
 
77
  api_url = DEFAULT_API_URL
78
  questions_url = f"{api_url}/questions"
79
  submit_url = f"{api_url}/submit"
80
 
 
81
  # Initialize Agent
 
82
  try:
83
  agent = BasicAgent()
 
84
  except Exception as e:
 
 
 
85
  return f"Error initializing agent: {e}", None
86
 
 
87
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
88
 
 
89
  # Fetch Questions
 
90
  try:
91
- response = requests.get(questions_url, timeout=30)
 
 
 
 
 
 
 
92
  response.raise_for_status()
 
93
  questions_data = response.json()
94
 
 
 
95
  except Exception as e:
 
 
 
96
  return f"Error fetching questions: {e}", None
97
 
 
98
  # Run Agent
99
- results_log = []
100
  answers_payload = []
 
101
 
102
  for item in questions_data:
103
 
104
  task_id = item.get("task_id")
105
  question_text = item.get("question")
106
 
 
 
 
 
 
107
  try:
108
 
109
  submitted_answer = agent(question_text)
@@ -121,19 +169,25 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
121
 
122
  except Exception as e:
123
 
 
 
124
  results_log.append({
125
  "Task ID": task_id,
126
  "Question": question_text,
127
  "Submitted Answer": f"ERROR: {e}"
128
  })
129
 
130
- # Submit
 
 
131
  submission_data = {
132
  "username": username.strip(),
133
  "agent_code": agent_code,
134
  "answers": answers_payload
135
  }
136
 
 
 
137
  try:
138
 
139
  response = requests.post(
@@ -147,45 +201,59 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
147
  result_data = response.json()
148
 
149
  final_status = (
150
- f"Submission Successful!\n"
151
  f"User: {result_data.get('username')}\n"
152
- f"Overall Score: {result_data.get('score', 'N/A')}%\n"
153
- f"Correct: {result_data.get('correct_count', '?')}/"
154
- f"{result_data.get('total_attempted', '?')}\n"
 
155
  f"Message: {result_data.get('message', '')}"
156
  )
157
 
 
 
158
  return final_status, pd.DataFrame(results_log)
159
 
160
  except Exception as e:
161
 
162
- return (
163
- f"Submission failed: {e}",
164
- pd.DataFrame(results_log)
165
- )
166
 
167
- # -----------------------------
168
- # Gradio UI
169
- # -----------------------------
 
 
 
 
 
170
  with gr.Blocks() as demo:
171
 
172
  gr.Markdown("# Hugging Face Agents Course - Final Assignment")
173
 
174
- gr.Markdown("""
 
175
  This agent uses:
 
176
  - Hugging Face Inference API
177
  - smolagents
178
  - DuckDuckGo web search
179
  - Qwen2.5-72B-Instruct
180
- """)
 
181
 
 
182
  gr.LoginButton()
183
 
184
- run_button = gr.Button("Run Evaluation & Submit")
 
 
 
185
 
 
186
  status_output = gr.Textbox(
187
  label="Submission Result",
188
- lines=6
 
189
  )
190
 
191
  results_table = gr.DataFrame(
@@ -193,10 +261,25 @@ This agent uses:
193
  wrap=True
194
  )
195
 
 
196
  run_button.click(
197
  fn=run_and_submit_all,
198
- outputs=[status_output, results_table]
 
 
 
199
  )
200
 
 
 
 
201
  if __name__ == "__main__":
202
- demo.launch(debug=True)
 
 
 
 
 
 
 
 
 
4
  import pandas as pd
5
 
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool
7
+ from smolagents.models import InferenceClientModel
8
 
9
+ # ---------------------------------------------------
10
  # Constants
11
+ # ---------------------------------------------------
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
+ # ---------------------------------------------------
15
+ # Agent Definition
16
+ # ---------------------------------------------------
17
  class BasicAgent:
18
+
19
  def __init__(self):
20
 
21
+ print("Initializing Hugging Face Agent...")
22
+
23
+ # Hugging Face Inference API model
24
+ model = InferenceClientModel(
25
+ model_id="Qwen/Qwen2.5-72B-Instruct",
26
+ token=os.getenv("HF_TOKEN")
27
  )
28
 
29
+ # Build agent
30
  self.agent = CodeAgent(
31
  tools=[
32
  DuckDuckGoSearchTool()
 
36
  verbosity_level=1
37
  )
38
 
39
+ print("Agent initialized successfully.")
40
 
41
  def __call__(self, question: str) -> str:
42
 
43
+ print(f"\nQuestion: {question[:100]}")
44
+
45
  prompt = f"""
46
+ You are an expert GAIA benchmark solving agent.
47
 
48
+ Your job:
49
+ - Think step-by-step.
50
+ - Use web search if needed.
51
+ - Solve the task accurately.
52
  - Return ONLY the final answer.
53
+ - Keep answers concise.
54
+ - No explanations unless necessary.
55
 
56
  Question:
57
  {question}
58
  """
59
 
60
  try:
 
61
 
62
+ result = self.agent.run(prompt)
63
+
64
+ if result is None:
65
+ return "Could not determine the answer."
66
+
67
+ final_answer = str(result).strip()
68
 
69
+ print(f"Answer: {final_answer}")
70
+
71
+ return final_answer
72
 
73
  except Exception as e:
74
+
75
  print(f"Agent error: {e}")
 
76
 
77
+ return f"Error: {str(e)}"
78
+
79
+
80
+ # ---------------------------------------------------
81
+ # Evaluation + Submission
82
+ # ---------------------------------------------------
83
  def run_and_submit_all(profile: gr.OAuthProfile | None):
84
 
85
+ # Get Space ID
86
  space_id = os.getenv("SPACE_ID")
87
 
88
+ # Check login
89
  if profile:
90
+ username = profile.username
91
+ print(f"Logged in as: {username}")
92
  else:
93
+ return "Please login with Hugging Face first.", None
94
 
95
+ # API URLs
96
  api_url = DEFAULT_API_URL
97
  questions_url = f"{api_url}/questions"
98
  submit_url = f"{api_url}/submit"
99
 
100
+ # ---------------------------------------------------
101
  # Initialize Agent
102
+ # ---------------------------------------------------
103
  try:
104
  agent = BasicAgent()
105
+
106
  except Exception as e:
107
+
108
+ print(f"Error initializing agent: {e}")
109
+
110
  return f"Error initializing agent: {e}", None
111
 
112
+ # Link to your Space code
113
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
114
 
115
+ # ---------------------------------------------------
116
  # Fetch Questions
117
+ # ---------------------------------------------------
118
  try:
119
+
120
+ print("Fetching questions...")
121
+
122
+ response = requests.get(
123
+ questions_url,
124
+ timeout=30
125
+ )
126
+
127
  response.raise_for_status()
128
+
129
  questions_data = response.json()
130
 
131
+ print(f"Fetched {len(questions_data)} questions.")
132
+
133
  except Exception as e:
134
+
135
+ print(f"Error fetching questions: {e}")
136
+
137
  return f"Error fetching questions: {e}", None
138
 
139
+ # ---------------------------------------------------
140
  # Run Agent
141
+ # ---------------------------------------------------
142
  answers_payload = []
143
+ results_log = []
144
 
145
  for item in questions_data:
146
 
147
  task_id = item.get("task_id")
148
  question_text = item.get("question")
149
 
150
+ if not task_id or question_text is None:
151
+ continue
152
+
153
+ print(f"\nRunning task: {task_id}")
154
+
155
  try:
156
 
157
  submitted_answer = agent(question_text)
 
169
 
170
  except Exception as e:
171
 
172
+ print(f"Task error: {e}")
173
+
174
  results_log.append({
175
  "Task ID": task_id,
176
  "Question": question_text,
177
  "Submitted Answer": f"ERROR: {e}"
178
  })
179
 
180
+ # ---------------------------------------------------
181
+ # Submit Answers
182
+ # ---------------------------------------------------
183
  submission_data = {
184
  "username": username.strip(),
185
  "agent_code": agent_code,
186
  "answers": answers_payload
187
  }
188
 
189
+ print("Submitting answers...")
190
+
191
  try:
192
 
193
  response = requests.post(
 
201
  result_data = response.json()
202
 
203
  final_status = (
204
+ f"Submission Successful!\n\n"
205
  f"User: {result_data.get('username')}\n"
206
+ f"Score: {result_data.get('score', 'N/A')}%\n"
207
+ f"Correct: "
208
+ f"{result_data.get('correct_count', '?')}/"
209
+ f"{result_data.get('total_attempted', '?')}\n\n"
210
  f"Message: {result_data.get('message', '')}"
211
  )
212
 
213
+ print(final_status)
214
+
215
  return final_status, pd.DataFrame(results_log)
216
 
217
  except Exception as e:
218
 
219
+ error_msg = f"Submission failed: {e}"
 
 
 
220
 
221
+ print(error_msg)
222
+
223
+ return error_msg, pd.DataFrame(results_log)
224
+
225
+
226
+ # ---------------------------------------------------
227
+ # Gradio Interface
228
+ # ---------------------------------------------------
229
  with gr.Blocks() as demo:
230
 
231
  gr.Markdown("# Hugging Face Agents Course - Final Assignment")
232
 
233
+ gr.Markdown(
234
+ """
235
  This agent uses:
236
+
237
  - Hugging Face Inference API
238
  - smolagents
239
  - DuckDuckGo web search
240
  - Qwen2.5-72B-Instruct
241
+ """
242
+ )
243
 
244
+ # HF Login
245
  gr.LoginButton()
246
 
247
+ # Run Button
248
+ run_button = gr.Button(
249
+ "Run Evaluation & Submit"
250
+ )
251
 
252
+ # Outputs
253
  status_output = gr.Textbox(
254
  label="Submission Result",
255
+ lines=8,
256
+ interactive=False
257
  )
258
 
259
  results_table = gr.DataFrame(
 
261
  wrap=True
262
  )
263
 
264
+ # Button Action
265
  run_button.click(
266
  fn=run_and_submit_all,
267
+ outputs=[
268
+ status_output,
269
+ results_table
270
+ ]
271
  )
272
 
273
+ # ---------------------------------------------------
274
+ # Launch App
275
+ # ---------------------------------------------------
276
  if __name__ == "__main__":
277
+
278
+ print("\n==============================")
279
+ print("Starting Hugging Face Agent...")
280
+ print("==============================\n")
281
+
282
+ demo.launch(
283
+ debug=True,
284
+ share=False
285
+ )