leofltt commited on
Commit
91b0aca
·
1 Parent(s): cc70c39

replying all qs

Browse files
Files changed (1) hide show
  1. app.py +32 -61
app.py CHANGED
@@ -226,40 +226,27 @@ class GaiaAgent:
226
  return f"Error during agent invocation: {e}"
227
 
228
 
229
- # In app.py
230
-
231
- # ... (keep all the code above this function)
232
-
233
-
234
  def run_and_submit_all(profile: gr.OAuthProfile | None):
235
  if not profile:
236
- return "Please Login to Hugging Face with the button.", None
237
  username = profile.username
238
  logging.info(f"User logged in: {username}")
239
 
240
  space_id = os.getenv("SPACE_ID")
241
  if not space_id:
242
- space_id = "leofltt/HF_Agents_Final_Assignment"
243
- logging.warning(f"SPACE_ID not found, using fallback for local run: {space_id}")
244
-
245
- if not space_id:
246
- return "CRITICAL ERROR: SPACE_ID environment variable is not set.", None
247
 
248
- api_url = DEFAULT_API_URL
249
- questions_url = f"{api_url}/questions"
250
- submit_url = f"{api_url}/submit"
251
 
252
  try:
253
  agent = GaiaAgent()
254
  except Exception as e:
255
- logging.critical(f"Fatal error instantiating agent: {e}", exc_info=True)
256
  return f"Fatal error initializing agent: {e}", None
257
 
258
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
259
-
260
- logging.info(f"Fetching questions from: {questions_url}")
261
  try:
262
- response = requests.get(questions_url, timeout=20)
263
  response.raise_for_status()
264
  questions_data = response.json()
265
  if not questions_data:
@@ -268,81 +255,65 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
268
  except Exception as e:
269
  return f"Error fetching questions: {e}", None
270
 
271
- # --- MODIFICATION FOR DEBUGGING ---
272
- # We will only process the first question from the list.
273
- questions_to_process = [questions_data[0]]
274
  logging.info(
275
- f"DEBUG MODE: Processing only the first question out of {len(questions_data)}."
276
  )
277
- # --- END OF MODIFICATION ---
278
 
279
  results_log = []
280
  answers_payload = []
281
-
282
- # The loop now runs only once.
283
- for i, item in enumerate(questions_to_process):
284
  task_id = item.get("task_id")
285
  question_text = item.get("question")
286
- logging.info(f"--- Processing question (Task ID: {task_id}) ---")
287
- if not task_id or question_text is None:
288
- continue
289
  try:
290
- submitted_answer = agent(question_text)
291
- answers_payload.append(
292
- {"task_id": task_id, "submitted_answer": submitted_answer}
293
- )
294
  results_log.append(
295
  {
296
  "Task ID": task_id,
297
  "Question": question_text,
298
- "Submitted Answer": submitted_answer,
299
  }
300
  )
301
  except Exception as e:
302
- logging.error(f"Error running agent on task {task_id}: {e}", exc_info=True)
 
303
  results_log.append(
304
  {
305
  "Task ID": task_id,
306
  "Question": question_text,
307
- "Submitted Answer": f"AGENT ERROR: {e}",
308
  }
309
  )
310
- # Also return the error in the status for immediate feedback
311
- return f"Agent failed on the first question with error: {e}", pd.DataFrame(
312
- results_log
313
- )
314
 
315
  if not answers_payload:
316
- return "Agent did not produce an answer for the first question.", pd.DataFrame(
317
- results_log
318
- )
319
-
320
- submission_data = {
321
- "username": username.strip(),
322
- "agent_code": agent_code,
323
- "answers": answers_payload,
324
- }
325
- logging.info(f"Submitting {len(answers_payload)} answer for user '{username}'...")
326
 
 
327
  try:
328
- response = requests.post(submit_url, json=submission_data, timeout=60)
 
 
 
 
 
 
 
329
  response.raise_for_status()
330
  result_data = response.json()
331
- final_status = (
332
- f"Submission Successful (for one question)!\n"
333
  f"User: {result_data.get('username')}\n"
334
  f"Overall Score: {result_data.get('score', 'N/A')}% "
335
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
336
  f"Message: {result_data.get('message', 'No message received.')}"
337
  )
338
- return final_status, pd.DataFrame(results_log)
339
- except requests.exceptions.HTTPError as e:
340
- error_detail = f"Server responded with status {e.response.status_code}. Detail: {e.response.text}"
341
- return f"Submission Failed: {error_detail}", pd.DataFrame(results_log)
342
  except Exception as e:
343
- return f"An unexpected error occurred during submission: {e}", pd.DataFrame(
344
- results_log
345
- )
346
 
347
 
348
  with gr.Blocks() as demo:
 
226
  return f"Error during agent invocation: {e}"
227
 
228
 
 
 
 
 
 
229
  def run_and_submit_all(profile: gr.OAuthProfile | None):
230
  if not profile:
231
+ return "Please Login to Hugging Face.", None
232
  username = profile.username
233
  logging.info(f"User logged in: {username}")
234
 
235
  space_id = os.getenv("SPACE_ID")
236
  if not space_id:
237
+ space_id = "leofltt/HF_Agents_Final_Assignment" # Your fallback
238
+ logging.warning(f"SPACE_ID not found, using fallback: {space_id}")
 
 
 
239
 
240
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
 
241
 
242
  try:
243
  agent = GaiaAgent()
244
  except Exception as e:
 
245
  return f"Fatal error initializing agent: {e}", None
246
 
247
+ logging.info("Fetching questions...")
 
 
248
  try:
249
+ response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=20)
250
  response.raise_for_status()
251
  questions_data = response.json()
252
  if not questions_data:
 
255
  except Exception as e:
256
  return f"Error fetching questions: {e}", None
257
 
258
+ # The loop will now process the full 'questions_data' list
 
 
259
  logging.info(
260
+ f"FULL EVALUATION MODE: Processing all {len(questions_data)} questions..."
261
  )
 
262
 
263
  results_log = []
264
  answers_payload = []
265
+ for i, item in enumerate(questions_data):
 
 
266
  task_id = item.get("task_id")
267
  question_text = item.get("question")
268
+ logging.info(
269
+ f"--- Processing question {i+1}/{len(questions_data)} (Task ID: {task_id}) ---"
270
+ )
271
  try:
272
+ answer = agent(question_text)
273
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
 
 
274
  results_log.append(
275
  {
276
  "Task ID": task_id,
277
  "Question": question_text,
278
+ "Submitted Answer": answer,
279
  }
280
  )
281
  except Exception as e:
282
+ error_message = f"AGENT ERROR on task {task_id}: {e}"
283
+ logging.error(error_message, exc_info=True)
284
  results_log.append(
285
  {
286
  "Task ID": task_id,
287
  "Question": question_text,
288
+ "Submitted Answer": error_message,
289
  }
290
  )
 
 
 
 
291
 
292
  if not answers_payload:
293
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
294
 
295
+ logging.info(f"Submitting {len(answers_payload)} answers...")
296
  try:
297
+ submission_data = {
298
+ "username": username,
299
+ "agent_code": agent_code,
300
+ "answers": answers_payload,
301
+ }
302
+ response = requests.post(
303
+ f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60
304
+ )
305
  response.raise_for_status()
306
  result_data = response.json()
307
+ status = (
308
+ f"Submission Successful!\n"
309
  f"User: {result_data.get('username')}\n"
310
  f"Overall Score: {result_data.get('score', 'N/A')}% "
311
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
312
  f"Message: {result_data.get('message', 'No message received.')}"
313
  )
314
+ return status, pd.DataFrame(results_log)
 
 
 
315
  except Exception as e:
316
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
317
 
318
 
319
  with gr.Blocks() as demo: