thinhbtt commited on
Commit
a39dddb
·
verified ·
1 Parent(s): 494a2fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py CHANGED
@@ -392,3 +392,76 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
392
  time.sleep(0.2) # polite pause to avoid hammering external services
393
  except Exception as e:
394
  results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
  time.sleep(0.2) # polite pause to avoid hammering external services
393
  except Exception as e:
394
  results
395
+ except Exception as e:
396
+ results_log.append({
397
+ "Task ID": task_id,
398
+ "Question": question_text,
399
+ "Submitted Answer": f"ERROR: {e}"
400
+ })
401
+
402
+ if not answers_payload:
403
+ return "Agent did not produce any answers.", pd.DataFrame(results_log)
404
+
405
+ submission_data = {
406
+ "username": username.strip(),
407
+ "agent_code": agent_code,
408
+ "answers": answers_payload
409
+ }
410
+
411
+ try:
412
+ resp = requests.post(
413
+ submit_url,
414
+ json=submission_data,
415
+ headers=USER_AGENT,
416
+ timeout=60
417
+ )
418
+ resp.raise_for_status()
419
+ result = resp.json()
420
+ final_status = (
421
+ f"Submission Successful!\n"
422
+ f"User: {result.get('username')}\n"
423
+ f"Overall Score: {result.get('score', 'N/A')}% "
424
+ f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
425
+ f"Message: {result.get('message', '')}"
426
+ )
427
+ return final_status, pd.DataFrame(results_log)
428
+
429
+ except requests.exceptions.HTTPError as e:
430
+ try:
431
+ body = e.response.json()
432
+ detail = body.get("detail") or json.dumps(body)[:400]
433
+ except Exception:
434
+ detail = e.response.text[:400]
435
+ return f"Submission Failed: HTTP {e.response.status_code} - {detail}", pd.DataFrame(results_log)
436
+ except Exception as e:
437
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
438
+
439
+
440
+
441
+ # ------------------------------
442
+ # Gradio UI
443
+ # ------------------------------
444
+ with gr.Blocks() as demo:
445
+ gr.Markdown("# Level-2 Agent (Rule-based + Wiki/File Tools)")
446
+ gr.Markdown("Duplicate this space, make it public, then login and press **Run Evaluation & Submit All Answers**.")
447
+
448
+ gr.LoginButton()
449
+
450
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
451
+
452
+ status_output = gr.Textbox(
453
+ label="Run Status / Submission Result",
454
+ lines=6,
455
+ interactive=False
456
+ )
457
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
458
+
459
+ run_button.click(
460
+ fn=run_and_submit_all,
461
+ inputs=[],
462
+ outputs=[status_output, results_table]
463
+ )
464
+
465
+
466
+ if __name__ == "__main__":
467
+ demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))