Update app.py
Browse files
app.py
CHANGED
@@ -220,10 +220,27 @@ def extract_final_answer(agent_response):
|
|
220 |
return "Unable to determine"
|
221 |
|
222 |
# Replace BasicAgent with your SmolaAgent in the run_and_submit_all function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
224 |
"""
|
225 |
Fetches all questions, runs the SmolaAgent on them, submits all answers,
|
226 |
-
and displays the results.
|
227 |
"""
|
228 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
229 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
@@ -250,11 +267,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
250 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
251 |
print(agent_code)
|
252 |
|
253 |
-
# 2. Fetch Questions
|
254 |
print(f"Fetching questions from: {questions_url}")
|
255 |
try:
|
256 |
-
response =
|
257 |
-
response.raise_for_status()
|
258 |
questions_data = response.json()
|
259 |
if not questions_data:
|
260 |
print("Fetched questions list is empty.")
|
@@ -279,15 +295,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
279 |
task_id = item.get("task_id")
|
280 |
question_text = item.get("question")
|
281 |
|
282 |
-
# Check if there are files associated with this task
|
283 |
try:
|
284 |
files_url = f"{api_url}/files/{task_id}"
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
|
|
|
|
291 |
except Exception as e:
|
292 |
print(f"Error checking for files for task {task_id}: {e}")
|
293 |
# Continue even if file check fails
|
@@ -333,11 +351,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
333 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
334 |
print(status_update)
|
335 |
|
336 |
-
# 5. Submit
|
337 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
338 |
try:
|
339 |
-
response =
|
340 |
-
response.raise_for_status()
|
341 |
result_data = response.json()
|
342 |
final_status = (
|
343 |
f"Submission Successful!\n"
|
|
|
220 |
return "Unable to determine"
|
221 |
|
222 |
# Replace BasicAgent with your SmolaAgent in the run_and_submit_all function
|
223 |
+
import backoff
|
224 |
+
import time
|
225 |
+
|
226 |
+
# Add backoff decorator for API requests
|
227 |
+
@backoff.on_exception(
|
228 |
+
backoff.expo,
|
229 |
+
requests.exceptions.HTTPError,
|
230 |
+
max_tries=5,
|
231 |
+
giveup=lambda e: e.response.status_code != 429,
|
232 |
+
factor=2
|
233 |
+
)
|
234 |
+
def rate_limited_request(method, url, **kwargs):
|
235 |
+
"""Make a request with automatic backoff for rate limited requests"""
|
236 |
+
response = requests.request(method, url, **kwargs)
|
237 |
+
response.raise_for_status()
|
238 |
+
return response
|
239 |
+
|
240 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
241 |
"""
|
242 |
Fetches all questions, runs the SmolaAgent on them, submits all answers,
|
243 |
+
and displays the results with rate limit handling.
|
244 |
"""
|
245 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
246 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
|
267 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
268 |
print(agent_code)
|
269 |
|
270 |
+
# 2. Fetch Questions with rate limit handling
|
271 |
print(f"Fetching questions from: {questions_url}")
|
272 |
try:
|
273 |
+
response = rate_limited_request("GET", questions_url, timeout=15)
|
|
|
274 |
questions_data = response.json()
|
275 |
if not questions_data:
|
276 |
print("Fetched questions list is empty.")
|
|
|
295 |
task_id = item.get("task_id")
|
296 |
question_text = item.get("question")
|
297 |
|
298 |
+
# Check if there are files associated with this task with rate limit handling
|
299 |
try:
|
300 |
files_url = f"{api_url}/files/{task_id}"
|
301 |
+
try:
|
302 |
+
files_response = rate_limited_request("GET", files_url, timeout=15)
|
303 |
+
if files_response.status_code == 200:
|
304 |
+
print(f"Task {task_id} has associated files")
|
305 |
+
# Handle files if needed
|
306 |
+
except Exception as e:
|
307 |
+
print(f"Error checking for files for task {task_id}: {e}")
|
308 |
+
# Continue even if file check fails
|
309 |
except Exception as e:
|
310 |
print(f"Error checking for files for task {task_id}: {e}")
|
311 |
# Continue even if file check fails
|
|
|
351 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
352 |
print(status_update)
|
353 |
|
354 |
+
# 5. Submit with rate limit handling
|
355 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
356 |
try:
|
357 |
+
response = rate_limited_request("POST", submit_url, json=submission_data, timeout=60)
|
|
|
358 |
result_data = response.json()
|
359 |
final_status = (
|
360 |
f"Submission Successful!\n"
|