Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -73,43 +73,42 @@ async def process_question(agent, question_text: str, task_id: str, results_log:
|
|
73 |
"""Process a single question with global rate limiting."""
|
74 |
submitted_answer = None
|
75 |
max_retries = 3
|
76 |
-
retry_delay =
|
77 |
|
78 |
for attempt in range(max_retries):
|
79 |
try:
|
80 |
-
|
|
|
81 |
print(f"Rate limit reached for task {task_id}. Waiting to retry...")
|
82 |
await asyncio.sleep(retry_delay)
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
87 |
return {"task_id": task_id, "submitted_answer": submitted_answer}
|
88 |
-
|
89 |
except aiohttp.ClientResponseError as e:
|
90 |
if e.status == 429:
|
91 |
-
print(f"Rate limit hit for task {task_id}. Retrying after
|
92 |
retry_delay *= 2 # Exponential backoff
|
93 |
-
# retry_delay += random.uniform(0, 5) # Jitter
|
94 |
-
print(f"Retry #{attempt+1} in {retry_delay:.1f}s")
|
95 |
await asyncio.sleep(retry_delay)
|
96 |
-
|
97 |
-
await asyncio.sleep(60)
|
98 |
-
try:
|
99 |
-
submitted_answer = await agent(question_text)
|
100 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
101 |
-
return {"task_id": task_id, "submitted_answer": submitted_answer}
|
102 |
-
except Exception as retry_e:
|
103 |
-
submitted_answer = f"AGENT ERROR: {retry_e}"
|
104 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
105 |
-
return None
|
106 |
else:
|
107 |
submitted_answer = f"AGENT ERROR: {e}"
|
108 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
109 |
return None
|
|
|
|
|
|
|
|
|
|
|
110 |
except Exception as e:
|
111 |
submitted_answer = f"AGENT ERROR: {e}"
|
112 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
113 |
return None
|
114 |
|
115 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
73 |
"""Process a single question with global rate limiting."""
|
74 |
submitted_answer = None
|
75 |
max_retries = 3
|
76 |
+
retry_delay = 6 # 6 seconds for 10 RPM
|
77 |
|
78 |
for attempt in range(max_retries):
|
79 |
try:
|
80 |
+
# Non-blocking rate limit check
|
81 |
+
while not token_bucket.consume(1):
|
82 |
print(f"Rate limit reached for task {task_id}. Waiting to retry...")
|
83 |
await asyncio.sleep(retry_delay)
|
84 |
+
print(f"Processing task {task_id} (attempt {attempt + 1})...")
|
85 |
+
submitted_answer = await asyncio.wait_for(
|
86 |
+
agent(question_text, task_id),
|
87 |
+
timeout=60 # 60-second timeout per question
|
88 |
+
)
|
89 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
90 |
+
print(f"Completed task {task_id} with answer: {submitted_answer[:50]}...")
|
91 |
return {"task_id": task_id, "submitted_answer": submitted_answer}
|
|
|
92 |
except aiohttp.ClientResponseError as e:
|
93 |
if e.status == 429:
|
94 |
+
print(f"Rate limit hit for task {task_id}. Retrying after {retry_delay}s...")
|
95 |
retry_delay *= 2 # Exponential backoff
|
|
|
|
|
96 |
await asyncio.sleep(retry_delay)
|
97 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
else:
|
99 |
submitted_answer = f"AGENT ERROR: {e}"
|
100 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
101 |
+
print(f"Failed task {task_id}: {submitted_answer}")
|
102 |
return None
|
103 |
+
except asyncio.TimeoutError:
|
104 |
+
submitted_answer = f"AGENT ERROR: Timeout after 60 seconds"
|
105 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
106 |
+
print(f"Failed task {task_id}: {submitted_answer}")
|
107 |
+
return None
|
108 |
except Exception as e:
|
109 |
submitted_answer = f"AGENT ERROR: {e}"
|
110 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
111 |
+
print(f"Failed task {task_id}: {submitted_answer}")
|
112 |
return None
|
113 |
|
114 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|