Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ from smolagents.tools import Tool
|
|
| 8 |
import time
|
| 9 |
import openai
|
| 10 |
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
|
|
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
@@ -16,10 +17,9 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
# --- Retry Helper for Agent Call ---
|
| 19 |
-
def safe_agent_call(agent, question, retries=5, wait_time=
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
-
Retries up to `retries` times, waits `wait_time` seconds between attempts.
|
| 23 |
"""
|
| 24 |
for attempt in range(retries):
|
| 25 |
try:
|
|
@@ -34,6 +34,7 @@ def safe_agent_call(agent, question, retries=5, wait_time=60):
|
|
| 34 |
raise e
|
| 35 |
raise Exception(f"Failed after {retries} retries due to repeated rate limit errors.")
|
| 36 |
|
|
|
|
| 37 |
# --- Basic Agent Definition ---
|
| 38 |
class BasicAgent:
|
| 39 |
def __init__(self):
|
|
@@ -112,22 +113,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 112 |
continue
|
| 113 |
try:
|
| 114 |
submitted_answer = safe_agent_call(agent, question_text)
|
| 115 |
-
answers_payload.append({
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
"Submitted Answer": submitted_answer,
|
| 123 |
-
})
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Error running agent on task {task_id}: {e}")
|
| 126 |
-
results_log.append({
|
| 127 |
-
"Task ID": task_id,
|
| 128 |
-
"Question": question_text,
|
| 129 |
-
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 130 |
-
})
|
| 131 |
|
| 132 |
if not answers_payload:
|
| 133 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 8 |
import time
|
| 9 |
import openai
|
| 10 |
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
| 11 |
+
import random
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
|
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 19 |
# --- Retry Helper for Agent Call ---
|
| 20 |
+
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
| 21 |
"""
|
| 22 |
+
Helper function to safely call the agent with retry on rate limit errors (HTTP 429).
|
|
|
|
| 23 |
"""
|
| 24 |
for attempt in range(retries):
|
| 25 |
try:
|
|
|
|
| 34 |
raise e
|
| 35 |
raise Exception(f"Failed after {retries} retries due to repeated rate limit errors.")
|
| 36 |
|
| 37 |
+
|
| 38 |
# --- Basic Agent Definition ---
|
| 39 |
class BasicAgent:
|
| 40 |
def __init__(self):
|
|
|
|
| 113 |
continue
|
| 114 |
try:
|
| 115 |
submitted_answer = safe_agent_call(agent, question_text)
|
| 116 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 117 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 118 |
+
|
| 119 |
+
sleep_time = random.uniform(25, 40)
|
| 120 |
+
print(f"[Rate Limit Control] Sleeping {sleep_time:.1f} seconds after answering to avoid hitting rate limits...")
|
| 121 |
+
time.sleep(sleep_time)
|
| 122 |
+
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
print(f"Error running agent on task {task_id}: {e}")
|
| 125 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
if not answers_payload:
|
| 128 |
print("Agent did not produce any answers to submit.")
|