Paperbag commited on
Commit
ab4e631
·
1 Parent(s): d20527e

slow down api call

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -140,16 +140,25 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
140
  }
141
 
142
  import concurrent.futures
143
- # Max workers = 10 -> Process 10 questions at the same time
144
- with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
145
- # Submit all items
146
- futures = {executor.submit(process_item, item): item for item in questions_data}
 
 
 
 
 
 
147
  for future in concurrent.futures.as_completed(futures):
148
  res = future.result()
149
  if res:
150
  answers_payload.append({"task_id": res["task_id"], "submitted_answer": res["submitted_answer"]})
151
  results_log.append({"Task ID": res["task_id"], "Question": res["question"], "Submitted Answer": res["submitted_answer"]})
152
 
 
 
 
153
  if not answers_payload:
154
  print("Agent did not produce any answers to submit.")
155
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
140
  }
141
 
142
  import concurrent.futures
143
+ import time
144
+
145
+ # Max workers = 2 -> Groq API has strict Token and Request Per Minute limits.
146
+ # 2 workers with a slight stagger prevents immediate bursting.
147
+ with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
148
+ futures = {}
149
+ for item in questions_data:
150
+ futures[executor.submit(process_item, item)] = item
151
+ time.sleep(3) # Stagger starting requests by 3 seconds to avoid bursting Rate Limits
152
+
153
  for future in concurrent.futures.as_completed(futures):
154
  res = future.result()
155
  if res:
156
  answers_payload.append({"task_id": res["task_id"], "submitted_answer": res["submitted_answer"]})
157
  results_log.append({"Task ID": res["task_id"], "Question": res["question"], "Submitted Answer": res["submitted_answer"]})
158
 
159
+ # Additional delay after finishing a question to let Token bucket refill
160
+ time.sleep(2)
161
+
162
  if not answers_payload:
163
  print("Agent did not produce any answers to submit.")
164
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)