NgBaoAnn commited on
Commit
f324d5f
·
1 Parent(s): c88f3fa

fix: robust 429 retry (10 attempts, 60s max wait, clear error messages)

Browse files
Files changed (1) hide show
  1. app.py +28 -11
app.py CHANGED
@@ -629,28 +629,45 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
629
  space_id = os.getenv("SPACE_ID", "ngbaoan/Final_Assignment_AI_agents_course")
630
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
631
 
632
- # 1 — Fetch questions (retry up to 5× with backoff for 429)
633
- yield "📡 Fetching GAIA questions…", None
 
634
  questions_data = None
635
- for attempt in range(1, 6):
 
636
  try:
637
  resp = requests.get(QUESTIONS_URL, timeout=30)
638
  if resp.status_code == 429:
639
- wait_sec = 10 * attempt # 10s, 20s, 30s, 40s, 50s
640
- yield f"⏳ Rate-limited (429). Waiting {wait_sec}s before retry {attempt}/5…", None
641
- import time; time.sleep(wait_sec)
 
 
 
 
 
 
 
642
  continue
643
  resp.raise_for_status()
644
  questions_data = resp.json()
645
  break
646
  except Exception as exc:
647
- if attempt == 5:
648
- yield f"❌ Failed to fetch questions after 5 attempts: {exc}", None
649
- return
650
- import time; time.sleep(10 * attempt)
 
 
651
 
652
  if not questions_data:
653
- yield "❌ No questions received.", None
 
 
 
 
 
 
654
  return
655
 
656
  total = len(questions_data)
 
629
  space_id = os.getenv("SPACE_ID", "ngbaoan/Final_Assignment_AI_agents_course")
630
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
631
 
632
+ # 1 — Fetch questions (retry up to 10× with backoff for 429)
633
+ import time
634
+ yield "📡 Fetching GAIA questions… (server may be busy, will retry automatically)", None
635
  questions_data = None
636
+ last_error = None
637
+ for attempt in range(1, 11):
638
  try:
639
  resp = requests.get(QUESTIONS_URL, timeout=30)
640
  if resp.status_code == 429:
641
+ wait_sec = min(15 * attempt, 60) # 15s, 30s, 45s, 60s max
642
+ if attempt == 10:
643
+ last_error = "Server still rate-limiting after 10 attempts (429)."
644
+ break
645
+ yield (
646
+ f"⏳ Server busy (429). Waiting {wait_sec}s… "
647
+ f"(attempt {attempt}/10 — this is normal, please wait)",
648
+ None,
649
+ )
650
+ time.sleep(wait_sec)
651
  continue
652
  resp.raise_for_status()
653
  questions_data = resp.json()
654
  break
655
  except Exception as exc:
656
+ last_error = str(exc)
657
+ if attempt == 10:
658
+ break
659
+ wait_sec = min(15 * attempt, 60)
660
+ yield f"⚠️ Attempt {attempt}/10 failed: {exc}. Retrying in {wait_sec}s…", None
661
+ time.sleep(wait_sec)
662
 
663
  if not questions_data:
664
+ yield (
665
+ f"❌ Could not fetch questions from the scoring server.\n"
666
+ f"Reason: {last_error}\n\n"
667
+ f"💡 This is a server-side rate limit — NOT a bug in your code.\n"
668
+ f"Please wait a few minutes and click Run again.",
669
+ None,
670
+ )
671
  return
672
 
673
  total = len(questions_data)