mobrobro commited on
Commit
519da76
·
verified ·
1 Parent(s): 11549b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -222,6 +222,8 @@ def extract_final_answer(agent_response):
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(
@@ -246,7 +248,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
246
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
247
 
248
  if profile:
249
- username= f"{profile.username}"
250
  print(f"User logged in: {username}")
251
  else:
252
  print("User not logged in.")
@@ -267,25 +269,49 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
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.")
277
- return "Fetched questions list is empty or invalid format.", None
278
- print(f"Fetched {len(questions_data)} questions.")
279
- except requests.exceptions.RequestException as e:
280
- print(f"Error fetching questions: {e}")
281
- return f"Error fetching questions: {e}", None
282
- except requests.exceptions.JSONDecodeError as e:
283
- print(f"Error decoding JSON response from questions endpoint: {e}")
284
- print(f"Response text: {response.text[:500]}")
285
- return f"Error decoding server response for questions: {e}", None
286
- except Exception as e:
287
- print(f"An unexpected error occurred fetching questions: {e}")
288
- return f"An unexpected error occurred fetching questions: {e}", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
  # 3. Run your Agent
291
  results_log = []
 
222
  # Replace BasicAgent with your SmolaAgent in the run_and_submit_all function
223
  import backoff
224
  import time
225
+ import json
226
+ import os
227
 
228
  # Add backoff decorator for API requests
229
  @backoff.on_exception(
 
248
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
249
 
250
  if profile:
251
+ username = f"{profile.username}"
252
  print(f"User logged in: {username}")
253
  else:
254
  print("User not logged in.")
 
269
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
270
  print(agent_code)
271
 
272
+ # 2. Try to load cached questions or fetch from API with rate limit handling
273
+ cached_questions_path = "cached_questions.json"
274
+
275
+ if os.path.exists(cached_questions_path) and os.path.getsize(cached_questions_path) > 2:
276
+ print(f"Loading cached questions from {cached_questions_path}")
277
+ try:
278
+ with open(cached_questions_path, "r") as f:
279
+ questions_data = json.load(f)
280
+ print(f"Loaded {len(questions_data)} questions from cache")
281
+ except Exception as e:
282
+ print(f"Error loading cached questions: {e}")
283
+ return f"Error loading cached questions: {e}", None
284
+ else:
285
+ # Fetch questions from API with rate limit handling
286
+ print(f"No cached questions found. Fetching from: {questions_url}")
287
+ try:
288
+ response = rate_limited_request("GET", questions_url, timeout=15)
289
+ questions_data = response.json()
290
+
291
+ # Cache the questions for future runs
292
+ if questions_data:
293
+ try:
294
+ with open(cached_questions_path, "w") as f:
295
+ json.dump(questions_data, f)
296
+ print(f"Cached {len(questions_data)} questions to {cached_questions_path}")
297
+ except Exception as e:
298
+ print(f"Warning: Failed to cache questions: {e}")
299
+ except requests.exceptions.RequestException as e:
300
+ print(f"Error fetching questions: {e}")
301
+ return f"Error fetching questions: {e}", None
302
+ except requests.exceptions.JSONDecodeError as e:
303
+ print(f"Error decoding JSON response from questions endpoint: {e}")
304
+ print(f"Response text: {response.text[:500]}")
305
+ return f"Error decoding server response for questions: {e}", None
306
+ except Exception as e:
307
+ print(f"An unexpected error occurred fetching questions: {e}")
308
+ return f"An unexpected error occurred fetching questions: {e}", None
309
+
310
+ if not questions_data:
311
+ print("Questions list is empty.")
312
+ return "Questions list is empty or invalid format.", None
313
+
314
+ print(f"Processing {len(questions_data)} questions...")
315
 
316
  # 3. Run your Agent
317
  results_log = []