Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ import aiohttp
|
|
15 |
import asyncio
|
16 |
import json
|
17 |
from agent import MagAgent
|
18 |
-
from token_bucket import Limiter
|
19 |
|
20 |
# (Keep Constants as is)
|
21 |
# --- Constants ---
|
@@ -26,8 +26,9 @@ RATE_LIMIT = 15 # Requests per minute
|
|
26 |
TOKEN_BUCKET_CAPACITY = RATE_LIMIT
|
27 |
TOKEN_BUCKET_REFILL_RATE = RATE_LIMIT / 60.0 # Tokens per second
|
28 |
|
29 |
-
# Initialize global token bucket
|
30 |
-
|
|
|
31 |
|
32 |
async def fetch_questions(session: aiohttp.ClientSession, questions_url: str) -> list:
|
33 |
"""Fetch questions asynchronously."""
|
@@ -72,7 +73,8 @@ async def process_question(agent, question_text: str, task_id: str, results_log:
|
|
72 |
await token_bucket.wait(1)
|
73 |
submitted_answer = await agent(question_text)
|
74 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
75 |
-
return {"task_id"
|
|
|
76 |
except aiohttp.ClientResponseError as e:
|
77 |
if e.status == 429:
|
78 |
print(f"Rate limit hit for task {task_id}. Retrying after delay...")
|
|
|
15 |
import asyncio
|
16 |
import json
|
17 |
from agent import MagAgent
|
18 |
+
from token_bucket import Limiter, MemoryStorage
|
19 |
|
20 |
# (Keep Constants as is)
|
21 |
# --- Constants ---
|
|
|
26 |
TOKEN_BUCKET_CAPACITY = RATE_LIMIT
|
27 |
TOKEN_BUCKET_REFILL_RATE = RATE_LIMIT / 60.0 # Tokens per second
|
28 |
|
29 |
+
# Initialize global token bucket with MemoryStorage
|
30 |
+
storage = MemoryStorage()
|
31 |
+
token_bucket = Limiter(rate=TOKEN_BUCKET_REFILL_RATE, capacity=TOKEN_BUCKET_CAPACITY, storage=storage)
|
32 |
|
33 |
async def fetch_questions(session: aiohttp.ClientSession, questions_url: str) -> list:
|
34 |
"""Fetch questions asynchronously."""
|
|
|
73 |
await token_bucket.wait(1)
|
74 |
submitted_answer = await agent(question_text)
|
75 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
76 |
+
return {"task_id"
|
77 |
+
: task_id, "submitted_answer": submitted_answer}
|
78 |
except aiohttp.ClientResponseError as e:
|
79 |
if e.status == 429:
|
80 |
print(f"Rate limit hit for task {task_id}. Retrying after delay...")
|