NgBaoAnn commited on
Commit
d4c089e
Β·
1 Parent(s): e143dce

Add retry with backoff for submit endpoint (handles 429 rate limit)

Browse files
Files changed (1) hide show
  1. app.py +44 -23
app.py CHANGED
@@ -772,36 +772,57 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
772
  pd.DataFrame(results_log),
773
  )
774
 
775
- # 4 β€” Submit
776
- yield f"πŸ“€ Submitting {len(answers_payload)} answers for **{username}**…", pd.DataFrame(results_log)
777
-
778
  submission = {
779
  "username": username,
780
  "agent_code": agent_code,
781
  "answers": answers_payload,
782
  }
783
- try:
784
- resp = requests.post(SUBMIT_URL, json=submission, timeout=120)
785
- resp.raise_for_status()
786
- data = resp.json()
787
- score = data.get("score", "N/A")
788
- correct = data.get("correct_count", "?")
789
- total_att = data.get("total_attempted", "?")
790
- msg = data.get("message", "")
791
- final_status = (
792
- f"πŸŽ‰ **Submission Successful!**\n\n"
793
- f"πŸ‘€ User: {data.get('username', username)}\n"
794
- f"πŸ“Š Score: **{score}%** ({correct}/{total_att} correct)\n"
795
- f"πŸ’¬ {msg}"
796
  )
797
- except requests.HTTPError as exc:
798
  try:
799
- detail = exc.response.json().get("detail", exc.response.text[:400])
800
- except Exception:
801
- detail = exc.response.text[:400]
802
- final_status = f"❌ Submission failed (HTTP {exc.response.status_code}):\n{detail}"
803
- except Exception as exc:
804
- final_status = f"❌ Submission error: {exc}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
805
 
806
  yield final_status, pd.DataFrame(results_log)
807
 
 
772
  pd.DataFrame(results_log),
773
  )
774
 
775
+ # 4 β€” Submit (with retry for 429 rate limits)
 
 
776
  submission = {
777
  "username": username,
778
  "agent_code": agent_code,
779
  "answers": answers_payload,
780
  }
781
+
782
+ final_status = "❌ Submission failed: unknown error"
783
+ for submit_attempt in range(1, 6):
784
+ yield (
785
+ f"πŸ“€ Submitting {len(answers_payload)} answers for **{username}**…"
786
+ + (f" (attempt {submit_attempt}/5)" if submit_attempt > 1 else ""),
787
+ pd.DataFrame(results_log),
 
 
 
 
 
 
788
  )
 
789
  try:
790
+ resp = requests.post(SUBMIT_URL, json=submission, timeout=120)
791
+ if resp.status_code == 429:
792
+ wait_sec = 30 * submit_attempt
793
+ if submit_attempt < 5:
794
+ yield f"⏳ Submit server busy (429). Waiting {wait_sec}s before retry {submit_attempt+1}/5…", pd.DataFrame(results_log)
795
+ time.sleep(wait_sec)
796
+ continue
797
+ else:
798
+ final_status = "❌ Submit server rate-limited after 5 attempts. Please try again in a few minutes."
799
+ break
800
+ resp.raise_for_status()
801
+ data = resp.json()
802
+ score = data.get("score", "N/A")
803
+ correct = data.get("correct_count", "?")
804
+ total_att = data.get("total_attempted", "?")
805
+ msg = data.get("message", "")
806
+ final_status = (
807
+ f"πŸŽ‰ **Submission Successful!**\n\n"
808
+ f"πŸ‘€ User: {data.get('username', username)}\n"
809
+ f"πŸ“Š Score: **{score}%** ({correct}/{total_att} correct)\n"
810
+ f"πŸ’¬ {msg}"
811
+ )
812
+ break
813
+ except requests.HTTPError as exc:
814
+ try:
815
+ detail = exc.response.json().get("detail", exc.response.text[:400])
816
+ except Exception:
817
+ detail = exc.response.text[:400]
818
+ final_status = f"❌ Submission failed (HTTP {exc.response.status_code}):\n{detail}"
819
+ if submit_attempt < 5:
820
+ time.sleep(15 * submit_attempt)
821
+ continue
822
+ break
823
+ except Exception as exc:
824
+ final_status = f"❌ Submission error: {exc}"
825
+ break
826
 
827
  yield final_status, pd.DataFrame(results_log)
828