iQuentin commited on
Commit
7ea26eb
·
verified ·
1 Parent(s): 23d47f2

adding run_simuGAIA_all with no submission by default

Browse files
Files changed (1) hide show
  1. simuGAIA.py +127 -0
simuGAIA.py CHANGED
@@ -13,6 +13,10 @@ import traceback
13
  # Import our agent
14
  from agent import QAgent
15
 
 
 
 
 
16
  # Simulation of GAIA benchmark questions
17
  SAMPLE_QUESTIONS = [
18
  {
@@ -171,3 +175,126 @@ def run_GAIA_questions_simu():
171
 
172
  return results
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Import our agent
14
  from agent import QAgent
15
 
16
+ # (Keep Constants as is)
17
+ # --- Constants ---
18
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
+
20
  # Simulation of GAIA benchmark questions
21
  SAMPLE_QUESTIONS = [
22
  {
 
175
 
176
  return results
177
 
178
+
179
+
180
+ def run_simuGAIA_all( profile: gr.OAuthProfile | None, submit: Optional[bool] = False):
181
+ """
182
+ Fetches all questions, runs the QAgent on them,
183
+ and displays the results.
184
+ """
185
+ # --- Determine HF Space Runtime URL and Repo URL for submission ---
186
+ # space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
187
+
188
+ if profile:
189
+ username= f"{profile.username}"
190
+ print(f"User logged in: {username}")
191
+ else:
192
+ print("User not logged in.")
193
+ return "Please Login to Hugging Face with the button.", None
194
+
195
+ api_url = DEFAULT_API_URL
196
+ questions_url = f"{api_url}/questions"
197
+ # submit_url = f"{api_url}/submit"
198
+
199
+ # 1. Instantiate and init Agent ( modify this part to create your agent)
200
+ agent = init_agent()
201
+
202
+
203
+ # 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)
204
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
205
+ print(agent_code)
206
+
207
+ # 2. Fetch Questions
208
+ print(f"Fetching questions from: {questions_url}")
209
+ try:
210
+ response = requests.get(questions_url, timeout=15)
211
+ response.raise_for_status()
212
+ questions_data = response.json()
213
+ if not questions_data:
214
+ print("Fetched questions list is empty.")
215
+ return "Fetched questions list is empty or invalid format.", None
216
+ print(f"Fetched {len(questions_data)} questions.")
217
+ except requests.exceptions.RequestException as e:
218
+ print(f"Error fetching questions: {e}")
219
+ return f"Error fetching questions: {e}", None
220
+ except requests.exceptions.JSONDecodeError as e:
221
+ print(f"Error decoding JSON response from questions endpoint: {e}")
222
+ print(f"Response text: {response.text[:500]}")
223
+ return f"Error decoding server response for questions: {e}", None
224
+ except Exception as e:
225
+ print(f"An unexpected error occurred fetching questions: {e}")
226
+ return f"An unexpected error occurred fetching questions: {e}", None
227
+
228
+ # 3. Run your Agent
229
+ results_log = []
230
+ answers_payload = []
231
+ print(f"Running agent on {len(questions_data)} questions...")
232
+ for item in questions_data:
233
+ task_id = item.get("task_id")
234
+ question_text = item.get("question")
235
+ if not task_id or question_text is None:
236
+ print(f"Skipping item with missing task_id or question: {item}")
237
+ continue
238
+ try:
239
+ # submitted_answer = agent(question_text)
240
+ # answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
241
+ results_log.append({"Task ID": task_id, "Question": question_text}) # , "Submitted Answer": submitted_answer})
242
+ except Exception as e:
243
+ print(f"Error running agent on task {task_id}: {e}")
244
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
245
+
246
+ if not answers_payload:
247
+ print("Agent did not produce any answers to submit.")
248
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
249
+
250
+ if not submit:
251
+ return "Run finished. No submission done, as asked.", pd.DataFrame(results_log)
252
+
253
+ # 4. Prepare Submission
254
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
255
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
256
+ print(status_update)
257
+
258
+ # 5. Submit
259
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
260
+ try:
261
+ response = requests.post(submit_url, json=submission_data, timeout=60)
262
+ response.raise_for_status()
263
+ result_data = response.json()
264
+ final_status = (
265
+ f"Submission Successful!\n"
266
+ f"User: {result_data.get('username')}\n"
267
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
268
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
269
+ f"Message: {result_data.get('message', 'No message received.')}"
270
+ )
271
+ print("Submission successful.")
272
+ results_df = pd.DataFrame(results_log)
273
+ return final_status, results_df
274
+ except requests.exceptions.HTTPError as e:
275
+ error_detail = f"Server responded with status {e.response.status_code}."
276
+ try:
277
+ error_json = e.response.json()
278
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
279
+ except requests.exceptions.JSONDecodeError:
280
+ error_detail += f" Response: {e.response.text[:500]}"
281
+ status_message = f"Submission Failed: {error_detail}"
282
+ print(status_message)
283
+ results_df = pd.DataFrame(results_log)
284
+ return status_message, results_df
285
+ except requests.exceptions.Timeout:
286
+ status_message = "Submission Failed: The request timed out."
287
+ print(status_message)
288
+ results_df = pd.DataFrame(results_log)
289
+ return status_message, results_df
290
+ except requests.exceptions.RequestException as e:
291
+ status_message = f"Submission Failed: Network error - {e}"
292
+ print(status_message)
293
+ results_df = pd.DataFrame(results_log)
294
+ return status_message, results_df
295
+ except Exception as e:
296
+ status_message = f"An unexpected error occurred during submission: {e}"
297
+ print(status_message)
298
+ results_df = pd.DataFrame(results_log)
299
+ return status_message, results_df
300
+