JetzeJ commited on
Commit
95ee6d8
·
1 Parent(s): f866a28

Add retry with backoff for 429 rate limit

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -134,17 +134,36 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
134
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
135
  print(agent_code)
136
 
137
- # 2. Fetch Questions
 
138
  print(f"Fetching questions from: {questions_url}")
139
- try:
140
- response = requests.get(questions_url, timeout=15)
141
- response.raise_for_status()
142
- questions_data = response.json()
143
- if not questions_data:
144
- return "Fetched questions list is empty or invalid format.", None
145
- print(f"Fetched {len(questions_data)} questions.")
146
- except Exception as e:
147
- return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  # 3. Run Agent
150
  results_log = []
 
134
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
135
  print(agent_code)
136
 
137
+ # 2. Fetch Questions (with retry + backoff for rate limits)
138
+ import time
139
  print(f"Fetching questions from: {questions_url}")
140
+ questions_data = None
141
+ for attempt in range(5):
142
+ try:
143
+ response = requests.get(questions_url, timeout=15)
144
+ if response.status_code == 429:
145
+ wait = 30 * (attempt + 1)
146
+ print(f"Rate limited (429). Waiting {wait}s before retry {attempt+1}/5...")
147
+ time.sleep(wait)
148
+ continue
149
+ response.raise_for_status()
150
+ questions_data = response.json()
151
+ break
152
+ except requests.exceptions.HTTPError as e:
153
+ if e.response.status_code == 429:
154
+ wait = 30 * (attempt + 1)
155
+ print(f"Rate limited (429). Waiting {wait}s before retry {attempt+1}/5...")
156
+ time.sleep(wait)
157
+ else:
158
+ return f"Error fetching questions: {e}", None
159
+ except Exception as e:
160
+ return f"Error fetching questions: {e}", None
161
+
162
+ if questions_data is None:
163
+ return "Error: Still rate limited after multiple retries. Please wait a few minutes and try again.", None
164
+ if not questions_data:
165
+ return "Fetched questions list is empty or invalid format.", None
166
+ print(f"Fetched {len(questions_data)} questions.")
167
 
168
  # 3. Run Agent
169
  results_log = []