TaoZewen commited on
Commit
17754f7
·
1 Parent(s): 14dce5d

run_and_submit_all add

Browse files
Files changed (1) hide show
  1. app.py +47 -14
app.py CHANGED
@@ -27,6 +27,28 @@ class BasicAgent:
27
  print(f"Agent returning fixed answer: {final_answer}")
28
  return final_answer
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  def run_and_submit_all(profile: gr.OAuthProfile | None):
31
  """
32
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -57,25 +79,36 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
57
  print(agent_code)
58
 
59
  # 2. Fetch Questions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  print(f"Fetching questions from: {questions_url}")
61
  try:
62
- response = requests.get(questions_url, timeout=15)
63
- response.raise_for_status()
64
- questions_data = response.json()
65
  if not questions_data:
66
- print("Fetched questions list is empty.")
67
- return "Fetched questions list is empty or invalid format.", None
68
  print(f"Fetched {len(questions_data)} questions.")
69
- except requests.exceptions.RequestException as e:
70
- print(f"Error fetching questions: {e}")
71
- return f"Error fetching questions: {e}", None
72
- except requests.exceptions.JSONDecodeError as e:
73
- print(f"Error decoding JSON response from questions endpoint: {e}")
74
- print(f"Response text: {response.text[:500]}")
75
- return f"Error decoding server response for questions: {e}", None
76
  except Exception as e:
77
- print(f"An unexpected error occurred fetching questions: {e}")
78
- return f"An unexpected error occurred fetching questions: {e}", None
79
 
80
  # 3. Run your Agent
81
  results_log = []
 
27
  print(f"Agent returning fixed answer: {final_answer}")
28
  return final_answer
29
 
30
+ def fetch_questions_with_retry(url: str, retries: int = 5, backoff_factor: float = 1.0):
31
+ """
32
+ 尝试 GET url,遇到 429 时指数退避重试,直到成功或超过重试次数。
33
+ """
34
+ for i in range(retries):
35
+ try:
36
+ resp = requests.get(url, timeout=15)
37
+ resp.raise_for_status()
38
+ return resp.json()
39
+ except requests.exceptions.HTTPError as e:
40
+ if resp.status_code == 429 and i < retries - 1:
41
+ wait = backoff_factor * (2 ** i)
42
+ print(f"Received 429, retrying after {wait:.1f}s (attempt {i+1}/{retries})…")
43
+ time.sleep(wait)
44
+ continue
45
+ else:
46
+ # 其余 HTTPError 或最后一次 429 都抛出
47
+ raise
48
+ except Exception:
49
+ # 网络等其它错误,直接抛
50
+ raise
51
+
52
  def run_and_submit_all(profile: gr.OAuthProfile | None):
53
  """
54
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
79
  print(agent_code)
80
 
81
  # 2. Fetch Questions
82
+ # print(f"Fetching questions from: {questions_url}")
83
+ # try:
84
+ # response = requests.get(questions_url, timeout=15)
85
+ # response.raise_for_status()
86
+ # questions_data = response.json()
87
+ # if not questions_data:
88
+ # print("Fetched questions list is empty.")
89
+ # return "Fetched questions list is empty or invalid format.", None
90
+ # print(f"Fetched {len(questions_data)} questions.")
91
+ # except requests.exceptions.RequestException as e:
92
+ # print(f"Error fetching questions: {e}")
93
+ # return f"Error fetching questions: {e}", None
94
+ # except requests.exceptions.JSONDecodeError as e:
95
+ # print(f"Error decoding JSON response from questions endpoint: {e}")
96
+ # print(f"Response text: {response.text[:500]}")
97
+ # return f"Error decoding server response for questions: {e}", None
98
+ # except Exception as e:
99
+ # print(f"An unexpected error occurred fetching questions: {e}")
100
+ # return f"An unexpected error occurred fetching questions: {e}", None
101
+
102
+ questions_url = f"{api_url}/questions"
103
  print(f"Fetching questions from: {questions_url}")
104
  try:
105
+ questions_data = fetch_questions_with_retry(questions_url, retries=5, backoff_factor=1.0)
 
 
106
  if not questions_data:
107
+ return "Fetched questions list is empty or invalid format.", None
 
108
  print(f"Fetched {len(questions_data)} questions.")
 
 
 
 
 
 
 
109
  except Exception as e:
110
+ print(f"Error fetching questions after retries: {e}")
111
+ return f"Error fetching questions: {e}", None
112
 
113
  # 3. Run your Agent
114
  results_log = []