naman1102 commited on
Commit
4ae17c7
·
1 Parent(s): f717af9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -552,26 +552,47 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
552
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
553
  print(f"Agent code location: {agent_code}")
554
 
555
- # 2. Fetch Questions
556
  print(f"Fetching questions from: {questions_url}")
557
- try:
558
- response = requests.get(questions_url, timeout=15)
559
- response.raise_for_status()
560
- questions_data = response.json()
561
- if not questions_data:
562
- print("Fetched questions list is empty.")
563
- return "Fetched questions list is empty or invalid format.", None
564
- print(f"Fetched {len(questions_data)} questions.")
565
- except requests.exceptions.RequestException as e:
566
- print(f"Error fetching questions: {e}")
567
- return f"Error fetching questions: {e}", None
568
- except requests.exceptions.JSONDecodeError as e:
569
- print(f"Error decoding JSON response from questions endpoint: {e}")
570
- print(f"Response text: {response.text[:500]}")
571
- return f"Error decoding server response for questions: {e}", None
572
- except Exception as e:
573
- print(f"An unexpected error occurred fetching questions: {e}")
574
- return f"An unexpected error occurred fetching questions: {e}", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575
 
576
  # 3. Run your Agent
577
  results_log = []
 
552
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
553
  print(f"Agent code location: {agent_code}")
554
 
555
+ # 2. Fetch Questions with retry logic
556
  print(f"Fetching questions from: {questions_url}")
557
+ max_retries = 3
558
+ base_timeout = 30 # Increased from 15 to 30 seconds
559
+
560
+ for attempt in range(max_retries):
561
+ try:
562
+ response = requests.get(
563
+ questions_url,
564
+ timeout=base_timeout * (attempt + 1), # Increase timeout with each retry
565
+ headers={'User-Agent': 'Mozilla/5.0'} # Add user agent to avoid potential blocking
566
+ )
567
+ response.raise_for_status()
568
+ questions_data = response.json()
569
+ if not questions_data:
570
+ print("Fetched questions list is empty.")
571
+ return "Fetched questions list is empty or invalid format.", None
572
+ print(f"Fetched {len(questions_data)} questions.")
573
+ break # Success, exit retry loop
574
+ except requests.exceptions.Timeout:
575
+ if attempt < max_retries - 1:
576
+ print(f"Timeout on attempt {attempt + 1}/{max_retries}. Retrying with longer timeout...")
577
+ time.sleep(2 * (attempt + 1)) # Exponential backoff
578
+ continue
579
+ else:
580
+ print("All retry attempts timed out.")
581
+ return "Error: All attempts to fetch questions timed out. Please try again later.", None
582
+ except requests.exceptions.RequestException as e:
583
+ print(f"Error fetching questions: {e}")
584
+ if attempt < max_retries - 1:
585
+ print(f"Retrying... (attempt {attempt + 1}/{max_retries})")
586
+ time.sleep(2 * (attempt + 1))
587
+ continue
588
+ return f"Error fetching questions after {max_retries} attempts: {e}", None
589
+ except requests.exceptions.JSONDecodeError as e:
590
+ print(f"Error decoding JSON response from questions endpoint: {e}")
591
+ print(f"Response text: {response.text[:500]}")
592
+ return f"Error decoding server response for questions: {e}", None
593
+ except Exception as e:
594
+ print(f"An unexpected error occurred fetching questions: {e}")
595
+ return f"An unexpected error occurred fetching questions: {e}", None
596
 
597
  # 3. Run your Agent
598
  results_log = []