NgBaoAnn commited on
Commit ·
c88f3fa
1
Parent(s): 99f90fd
fix: retry with backoff for 429 rate-limit on question fetch
Browse files
app.py
CHANGED
|
@@ -629,14 +629,28 @@ 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
|
| 633 |
yield "📡 Fetching GAIA questions…", None
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
return
|
| 641 |
|
| 642 |
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 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)
|