Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,8 +22,8 @@ from token_bucket import Limiter, MemoryStorage
|
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
| 24 |
# Rate limiting configuration
|
| 25 |
-
|
| 26 |
-
|
| 27 |
TOKEN_BUCKET_REFILL_RATE = RATE_LIMIT / 60.0 # Tokens per second
|
| 28 |
|
| 29 |
# Initialize global token bucket with MemoryStorage
|
|
@@ -56,7 +56,8 @@ async def submit_answers(session: aiohttp.ClientSession, submit_url: str,
|
|
| 56 |
response.raise_for_status()
|
| 57 |
return await response.json()
|
| 58 |
except aiohttp.ClientResponseError as e:
|
| 59 |
-
print(f"Submission Failed: Server responded with status {e.status}. Detail: {e.message}"
|
|
|
|
| 60 |
return None
|
| 61 |
except aiohttp.ClientError as e:
|
| 62 |
print(f"Submission Failed: Network error - {e}")
|
|
@@ -65,40 +66,48 @@ async def submit_answers(session: aiohttp.ClientSession, submit_url: str,
|
|
| 65 |
print(f"An unexpected error occurred during submission: {e}")
|
| 66 |
return None
|
| 67 |
|
| 68 |
-
async def process_question(agent, question_text: str, task_id: str,
|
| 69 |
-
results_log: list):
|
| 70 |
"""Process a single question with global rate limiting."""
|
| 71 |
submitted_answer = None
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 93 |
return None
|
| 94 |
-
|
| 95 |
submitted_answer = f"AGENT ERROR: {e}"
|
| 96 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 97 |
return None
|
| 98 |
-
except Exception as e:
|
| 99 |
-
submitted_answer = f"AGENT ERROR: {e}"
|
| 100 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 101 |
-
return None
|
| 102 |
|
| 103 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 104 |
"""
|
|
@@ -121,7 +130,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 121 |
|
| 122 |
# 1. Instantiate Agent
|
| 123 |
try:
|
| 124 |
-
agent =MagAgent()
|
| 125 |
except Exception as e:
|
| 126 |
print(f"Error instantiating agent: {e}")
|
| 127 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
| 24 |
# Rate limiting configuration
|
| 25 |
+
MAX_MODEL_CALLS_PER_MINUTE = 12 # Conservative buffer below 15 RPM
|
| 26 |
+
RATE_LIMIT = MAX_MODEL_CALLS_PER_MINUTE
|
| 27 |
TOKEN_BUCKET_REFILL_RATE = RATE_LIMIT / 60.0 # Tokens per second
|
| 28 |
|
| 29 |
# Initialize global token bucket with MemoryStorage
|
|
|
|
| 56 |
response.raise_for_status()
|
| 57 |
return await response.json()
|
| 58 |
except aiohttp.ClientResponseError as e:
|
| 59 |
+
print(f"Submission Failed: Server responded with status {e.status}. Detail: {e.message}"
|
| 60 |
+
)
|
| 61 |
return None
|
| 62 |
except aiohttp.ClientError as e:
|
| 63 |
print(f"Submission Failed: Network error - {e}")
|
|
|
|
| 66 |
print(f"An unexpected error occurred during submission: {e}")
|
| 67 |
return None
|
| 68 |
|
| 69 |
+
async def process_question(agent, question_text: str, task_id: str, results_log: list):
|
|
|
|
| 70 |
"""Process a single question with global rate limiting."""
|
| 71 |
submitted_answer = None
|
| 72 |
+
max_retries = 3
|
| 73 |
+
retry_delay = 18 # Start with Gemini's recommended delay
|
| 74 |
+
|
| 75 |
+
for attempt in range(max_retries):
|
| 76 |
+
try:
|
| 77 |
+
if not token_bucket.consume(1):
|
| 78 |
+
print(f"Rate limit reached for task {task_id}. Waiting to retry...")
|
| 79 |
+
await asyncio.sleep(retry_delay)
|
| 80 |
+
continue
|
| 81 |
+
|
| 82 |
+
submitted_answer = await agent(question_text)
|
| 83 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 84 |
+
return {"task_id": task_id, "submitted_answer": submitted_answer}
|
| 85 |
+
|
| 86 |
+
except aiohttp.ClientResponseError as e:
|
| 87 |
+
if e.status == 429:
|
| 88 |
+
print(f"Rate limit hit for task {task_id}. Retrying after delay...")
|
| 89 |
+
retry_delay *= 2 # Exponential backoff
|
| 90 |
+
retry_delay += random.uniform(0, 5) # Jitter
|
| 91 |
+
print(f"Retry #{attempt+1} in {retry_delay:.1f}s")
|
| 92 |
+
await asyncio.sleep(retry_delay)
|
| 93 |
+
while not token_bucket.consume(1):
|
| 94 |
+
await asyncio.sleep(60 / RATE_LIMIT)
|
| 95 |
+
try:
|
| 96 |
+
submitted_answer = await agent(question_text)
|
| 97 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 98 |
+
return {"task_id": task_id, "submitted_answer": submitted_answer}
|
| 99 |
+
except Exception as retry_e:
|
| 100 |
+
submitted_answer = f"AGENT ERROR: {retry_e}"
|
| 101 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 102 |
+
return None
|
| 103 |
+
else:
|
| 104 |
+
submitted_answer = f"AGENT ERROR: {e}"
|
| 105 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 106 |
return None
|
| 107 |
+
except Exception as e:
|
| 108 |
submitted_answer = f"AGENT ERROR: {e}"
|
| 109 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 110 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 113 |
"""
|
|
|
|
| 130 |
|
| 131 |
# 1. Instantiate Agent
|
| 132 |
try:
|
| 133 |
+
agent =MagAgent(rate_limiter=token_bucket)
|
| 134 |
except Exception as e:
|
| 135 |
print(f"Error instantiating agent: {e}")
|
| 136 |
return f"Error initializing agent: {e}", None
|