maytemuma commited on
Commit
dcf3d84
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +230 -54
app.py CHANGED
@@ -1,34 +1,207 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
 
 
 
 
 
 
 
 
6
 
7
- # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  class BasicAgent:
 
 
 
14
  def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
-
22
- def run_and_submit_all( profile: gr.OAuthProfile | None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  """
24
- Fetches all questions, runs the BasicAgent on them, submits all answers,
25
  and displays the results.
26
  """
27
- # --- Determine HF Space Runtime URL and Repo URL ---
28
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
29
 
30
  if profile:
31
- username= f"{profile.username}"
32
  print(f"User logged in: {username}")
33
  else:
34
  print("User not logged in.")
@@ -38,13 +211,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
38
  questions_url = f"{api_url}/questions"
39
  submit_url = f"{api_url}/submit"
40
 
41
- # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
  agent = BasicAgent()
44
  except Exception as e:
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
47
- # 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)
48
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
  print(agent_code)
50
 
@@ -55,51 +228,62 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
55
  response.raise_for_status()
56
  questions_data = response.json()
57
  if not questions_data:
58
- print("Fetched questions list is empty.")
59
- return "Fetched questions list is empty or invalid format.", None
60
  print(f"Fetched {len(questions_data)} questions.")
61
  except requests.exceptions.RequestException as e:
62
  print(f"Error fetching questions: {e}")
63
  return f"Error fetching questions: {e}", None
64
- except requests.exceptions.JSONDecodeError as e:
65
- print(f"Error decoding JSON response from questions endpoint: {e}")
66
- print(f"Response text: {response.text[:500]}")
67
- return f"Error decoding server response for questions: {e}", None
68
  except Exception as e:
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
71
 
72
- # 3. Run your Agent
73
  results_log = []
74
  answers_payload = []
75
  print(f"Running agent on {len(questions_data)} questions...")
76
- for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
  if not task_id or question_text is None:
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
 
 
 
82
  try:
83
- submitted_answer = agent(question_text)
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
86
  except Exception as e:
87
- print(f"Error running agent on task {task_id}: {e}")
88
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
89
 
90
  if not answers_payload:
91
  print("Agent did not produce any answers to submit.")
92
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
93
 
94
- # 4. Prepare Submission
95
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
96
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
97
  print(status_update)
98
 
99
  # 5. Submit
100
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
101
  try:
102
- response = requests.post(submit_url, json=submission_data, timeout=60)
103
  response.raise_for_status()
104
  result_data = response.json()
105
  final_status = (
@@ -142,28 +326,23 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
142
 
143
  # --- Build Gradio Interface using Blocks ---
144
  with gr.Blocks() as demo:
145
- gr.Markdown("# Basic Agent Evaluation Runner")
146
  gr.Markdown(
147
  """
148
- **Instructions:**
149
 
150
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
151
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
152
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
153
 
154
- ---
155
- **Disclaimers:**
156
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
157
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
158
  """
159
  )
160
 
161
  gr.LoginButton()
162
-
163
- run_button = gr.Button("Run Evaluation & Submit All Answers")
164
-
165
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
166
- # Removed max_rows=10 from DataFrame constructor
167
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
168
 
169
  run_button.click(
@@ -173,9 +352,8 @@ with gr.Blocks() as demo:
173
 
174
  if __name__ == "__main__":
175
  print("\n" + "-"*30 + " App Starting " + "-"*30)
176
- # Check for SPACE_HOST and SPACE_ID at startup for information
177
  space_host_startup = os.getenv("SPACE_HOST")
178
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
179
 
180
  if space_host_startup:
181
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -183,14 +361,12 @@ if __name__ == "__main__":
183
  else:
184
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
185
 
186
- if space_id_startup: # Print repo URLs if SPACE_ID is found
187
  print(f"✅ SPACE_ID found: {space_id_startup}")
188
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
189
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
190
  else:
191
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
192
-
193
- print("-"*(60 + len(" App Starting ")) + "\n")
194
 
195
- print("Launching Gradio Interface for Basic Agent Evaluation...")
 
196
  demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ from smolagents import (
6
+ CodeAgent,
7
+ DuckDuckGoSearchTool,
8
+ WebSearchTool,
9
+ VisitWebpageTool,
10
+ InferenceClientModel,
11
+ tool,
12
+ )
13
 
 
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+
18
+ # =============================================
19
+ # CUSTOM TOOLS
20
+ # =============================================
21
+
22
+ @tool
23
+ def download_file_from_api(task_id: str) -> str:
24
+ """Downloads a file associated with a GAIA task and returns its text content.
25
+ Use this tool when a question mentions an attached file, spreadsheet, image,
26
+ audio, or any document that you need to read.
27
+
28
+ Args:
29
+ task_id: The task_id string for the question that has an associated file.
30
+ """
31
+ import tempfile
32
+ api_url = "https://agents-course-unit4-scoring.hf.space"
33
+ url = f"{api_url}/files/{task_id}"
34
+ try:
35
+ response = requests.get(url, timeout=30)
36
+ response.raise_for_status()
37
+
38
+ content_type = response.headers.get("Content-Type", "")
39
+
40
+ # If it's a text-based file, return the text directly
41
+ if "text" in content_type or "json" in content_type or "csv" in content_type:
42
+ return response.text[:10000] # Limit to avoid context overflow
43
+
44
+ # For Excel files
45
+ if "spreadsheet" in content_type or "excel" in content_type:
46
+ import openpyxl
47
+ import io
48
+ wb = openpyxl.load_workbook(io.BytesIO(response.content))
49
+ result = []
50
+ for sheet_name in wb.sheetnames:
51
+ ws = wb[sheet_name]
52
+ result.append(f"--- Sheet: {sheet_name} ---")
53
+ for row in ws.iter_rows(values_only=True):
54
+ result.append("\t".join([str(c) if c is not None else "" for c in row]))
55
+ return "\n".join(result)[:10000]
56
+
57
+ # For PDF files
58
+ if "pdf" in content_type:
59
+ try:
60
+ import PyPDF2
61
+ import io
62
+ reader = PyPDF2.PdfReader(io.BytesIO(response.content))
63
+ text = ""
64
+ for page in reader.pages:
65
+ text += page.extract_text() or ""
66
+ return text[:10000] if text else "PDF found but could not extract text."
67
+ except ImportError:
68
+ return "PDF file detected but PyPDF2 not installed."
69
+
70
+ # For other binary files, save and report
71
+ suffix = ""
72
+ if "image" in content_type:
73
+ suffix = ".png"
74
+ elif "audio" in content_type:
75
+ suffix = ".mp3"
76
+
77
+ with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
78
+ f.write(response.content)
79
+ return f"File downloaded to {f.name} (type: {content_type}). File size: {len(response.content)} bytes."
80
+
81
+ except Exception as e:
82
+ return f"Error downloading file for task {task_id}: {str(e)}"
83
+
84
+
85
+ @tool
86
+ def read_local_file(file_path: str) -> str:
87
+ """Reads the content of a local file and returns it as a string.
88
+
89
+ Args:
90
+ file_path: The path to the file to read.
91
+ """
92
+ try:
93
+ with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
94
+ return f.read()[:10000]
95
+ except Exception as e:
96
+ return f"Error reading file: {str(e)}"
97
+
98
+
99
+ # =============================================
100
+ # AGENT CLASS
101
+ # =============================================
102
+
103
  class BasicAgent:
104
+ """An agent that uses smolagents CodeAgent with web search and file tools
105
+ to answer GAIA benchmark questions. Uses Hugging Face Inference API (no GPU needed)."""
106
+
107
  def __init__(self):
108
+ print("Initializing SmolAgent for GAIA benchmark...")
109
+
110
+ # Use HF Inference API - completely free, no GPU needed
111
+ model = InferenceClientModel(
112
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
113
+ token=os.getenv("HF_TOKEN"),
114
+ max_tokens=2096,
115
+ temperature=0.1, # Low temperature for more precise answers
116
+ )
117
+
118
+ # System prompt tailored for GAIA exact-match scoring
119
+ system_prompt = """You are a precise AI assistant solving GAIA benchmark questions.
120
+
121
+ CRITICAL RULES:
122
+ 1. Your final answer must be ONLY the answer itself - no explanations, no prefixes like "The answer is", no extra text.
123
+ 2. If the answer is a number, give just the number (e.g., "42" not "The answer is 42").
124
+ 3. If the answer is a name, give just the name (e.g., "Paris" not "The answer is Paris").
125
+ 4. If the answer is a list, use comma-separated values as requested.
126
+ 5. Be precise and factual. Use tools to verify information.
127
+ 6. If a question mentions a file or attachment, use the download_file_from_api tool with the task_id.
128
+ 7. Always search the web when you need current or specific factual information.
129
+ 8. Think step by step, but your FINAL output must be ONLY the answer.
130
+ """
131
+
132
+ self.agent = CodeAgent(
133
+ model=model,
134
+ tools=[
135
+ DuckDuckGoSearchTool(),
136
+ VisitWebpageTool(),
137
+ download_file_from_api,
138
+ read_local_file,
139
+ ],
140
+ max_steps=8,
141
+ verbosity_level=1,
142
+ additional_authorized_imports=[
143
+ "json", "re", "math", "datetime", "collections",
144
+ "csv", "io", "os", "tempfile",
145
+ ],
146
+ )
147
+
148
+ # Override system prompt
149
+ self.agent.system_prompt = system_prompt + "\n\n" + self.agent.system_prompt
150
+
151
+ print("SmolAgent initialized successfully!")
152
+
153
+ def __call__(self, question: str, task_id: str = None) -> str:
154
+ print(f"Agent processing question: {question[:80]}...")
155
+
156
+ # Build the prompt with task_id context if available
157
+ prompt = question
158
+ if task_id:
159
+ prompt = f"""Answer this question. If you need to download an attached file, use download_file_from_api with task_id="{task_id}".
160
+
161
+ Question: {question}
162
+
163
+ Remember: respond with ONLY the final answer, nothing else."""
164
+ else:
165
+ prompt = f"""Answer this question precisely.
166
+
167
+ Question: {question}
168
+
169
+ Remember: respond with ONLY the final answer, nothing else."""
170
+
171
+ try:
172
+ result = self.agent.run(prompt)
173
+ # Clean up the answer
174
+ answer = str(result).strip()
175
+ # Remove common prefixes that models add
176
+ for prefix in [
177
+ "The answer is ", "The answer is: ", "Answer: ", "FINAL ANSWER: ",
178
+ "Final answer: ", "The final answer is ", "The final answer is: ",
179
+ ]:
180
+ if answer.lower().startswith(prefix.lower()):
181
+ answer = answer[len(prefix):].strip()
182
+ # Remove trailing periods if the answer is not a sentence
183
+ if answer.endswith(".") and len(answer.split()) <= 5:
184
+ answer = answer[:-1].strip()
185
+ print(f"Agent answer: {answer}")
186
+ return answer
187
+ except Exception as e:
188
+ print(f"Agent error: {e}")
189
+ return "Unable to determine the answer."
190
+
191
+
192
+ # =============================================
193
+ # SUBMISSION LOGIC
194
+ # =============================================
195
+
196
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
197
  """
198
+ Fetches all questions, runs the agent on them, submits all answers,
199
  and displays the results.
200
  """
201
+ space_id = os.getenv("SPACE_ID")
 
202
 
203
  if profile:
204
+ username = f"{profile.username}"
205
  print(f"User logged in: {username}")
206
  else:
207
  print("User not logged in.")
 
211
  questions_url = f"{api_url}/questions"
212
  submit_url = f"{api_url}/submit"
213
 
214
+ # 1. Instantiate Agent
215
  try:
216
  agent = BasicAgent()
217
  except Exception as e:
218
  print(f"Error instantiating agent: {e}")
219
  return f"Error initializing agent: {e}", None
220
+
221
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
222
  print(agent_code)
223
 
 
228
  response.raise_for_status()
229
  questions_data = response.json()
230
  if not questions_data:
231
+ print("Fetched questions list is empty.")
232
+ return "Fetched questions list is empty or invalid format.", None
233
  print(f"Fetched {len(questions_data)} questions.")
234
  except requests.exceptions.RequestException as e:
235
  print(f"Error fetching questions: {e}")
236
  return f"Error fetching questions: {e}", None
 
 
 
 
237
  except Exception as e:
238
  print(f"An unexpected error occurred fetching questions: {e}")
239
  return f"An unexpected error occurred fetching questions: {e}", None
240
 
241
+ # 3. Run Agent on each question
242
  results_log = []
243
  answers_payload = []
244
  print(f"Running agent on {len(questions_data)} questions...")
245
+ for i, item in enumerate(questions_data):
246
  task_id = item.get("task_id")
247
  question_text = item.get("question")
248
  if not task_id or question_text is None:
249
  print(f"Skipping item with missing task_id or question: {item}")
250
  continue
251
+ print(f"\n{'='*50}")
252
+ print(f"Question {i+1}/{len(questions_data)} - Task: {task_id}")
253
+ print(f"{'='*50}")
254
  try:
255
+ submitted_answer = agent(question_text, task_id=task_id)
256
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
257
+ results_log.append({
258
+ "Task ID": task_id,
259
+ "Question": question_text,
260
+ "Submitted Answer": submitted_answer
261
+ })
262
  except Exception as e:
263
+ print(f"Error running agent on task {task_id}: {e}")
264
+ results_log.append({
265
+ "Task ID": task_id,
266
+ "Question": question_text,
267
+ "Submitted Answer": f"AGENT ERROR: {e}"
268
+ })
269
 
270
  if not answers_payload:
271
  print("Agent did not produce any answers to submit.")
272
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
273
 
274
+ # 4. Prepare Submission
275
+ submission_data = {
276
+ "username": username.strip(),
277
+ "agent_code": agent_code,
278
+ "answers": answers_payload
279
+ }
280
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
281
  print(status_update)
282
 
283
  # 5. Submit
284
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
285
  try:
286
+ response = requests.post(submit_url, json=submission_data, timeout=120)
287
  response.raise_for_status()
288
  result_data = response.json()
289
  final_status = (
 
326
 
327
  # --- Build Gradio Interface using Blocks ---
328
  with gr.Blocks() as demo:
329
+ gr.Markdown("# 🤖 GAIA Agent - Final Assignment")
330
  gr.Markdown(
331
  """
332
+ **Agent**: SmolAgent (CodeAgent) with Qwen2.5-Coder-32B via HF Inference API
333
 
334
+ **Tools**: Web Search, Webpage Visitor, File Downloader
 
 
335
 
336
+ **Instructions:**
337
+ 1. Log in to your Hugging Face account using the button below.
338
+ 2. Click 'Run Evaluation & Submit All Answers' to start.
339
+ 3. Wait for the agent to process all 20 questions (this may take several minutes).
340
  """
341
  )
342
 
343
  gr.LoginButton()
344
+ run_button = gr.Button("🚀 Run Evaluation & Submit All Answers")
 
 
345
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
346
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
347
 
348
  run_button.click(
 
352
 
353
  if __name__ == "__main__":
354
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
355
  space_host_startup = os.getenv("SPACE_HOST")
356
+ space_id_startup = os.getenv("SPACE_ID")
357
 
358
  if space_host_startup:
359
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
361
  else:
362
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
363
 
364
+ if space_id_startup:
365
  print(f"✅ SPACE_ID found: {space_id_startup}")
366
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
 
367
  else:
368
+ print("ℹ️ SPACE_ID environment variable not found (running locally?).")
 
 
369
 
370
+ print("-"*60 + "\n")
371
+ print("Launching Gradio Interface...")
372
  demo.launch(debug=True, share=False)