NgBaoAnn commited on
Commit Β·
d4c089e
1
Parent(s): e143dce
Add retry with backoff for submit endpoint (handles 429 rate limit)
Browse files
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 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 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 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|