Shivangsinha commited on
Commit
0dcb3b6
·
verified ·
1 Parent(s): 100a223

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -33,7 +33,7 @@ class BasicAgent:
33
  if not gemini_api_key:
34
  raise ValueError("GEMINI_API_KEY environment variable not set")
35
  self.model = LiteLLMModel(
36
- model_id="gemini/gemini-2.0-flash",
37
  api_key=gemini_api_key,
38
  )
39
  self.tools = [
@@ -48,17 +48,26 @@ class BasicAgent:
48
  max_steps=8,
49
  additional_authorized_imports=["datetime", "re", "json", "math", "collections"],
50
  )
51
- print("BasicAgent ready with Gemini 2.0 Flash (CodeAgent).")
52
 
53
  def __call__(self, question: str) -> str:
54
  print(f"Agent received question: {question[:80]}...")
55
- try:
56
- answer = self.agent.run(question)
57
- print(f"Agent answer: {str(answer)[:200]}")
58
- return str(answer)
59
- except Exception as e:
60
- print(f"Agent error: {e}")
61
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
62
 
63
 
64
  # --- The rest of the code (keep as-is) ---
@@ -106,7 +115,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
106
  except Exception as e:
107
  print(f"Error on task {task_id}: {e}")
108
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
109
- time.sleep(1)
110
  if not answers_payload:
111
  return "No answers.", pd.DataFrame(results_log)
112
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
33
  if not gemini_api_key:
34
  raise ValueError("GEMINI_API_KEY environment variable not set")
35
  self.model = LiteLLMModel(
36
+ model_id="gemini/gemini-1.5-flash",
37
  api_key=gemini_api_key,
38
  )
39
  self.tools = [
 
48
  max_steps=8,
49
  additional_authorized_imports=["datetime", "re", "json", "math", "collections"],
50
  )
51
+ print("BasicAgent ready with Gemini 1.5 Flash (CodeAgent).")
52
 
53
  def __call__(self, question: str) -> str:
54
  print(f"Agent received question: {question[:80]}...")
55
+ max_retries = 3
56
+ for attempt in range(max_retries):
57
+ try:
58
+ answer = self.agent.run(question)
59
+ print(f"Agent answer: {str(answer)[:200]}")
60
+ return str(answer)
61
+ except Exception as e:
62
+ err = str(e)
63
+ if "429" in err or "rate_limit" in err.lower() or "quota" in err.lower():
64
+ wait_time = 30 * (attempt + 1)
65
+ print(f"Rate limit hit, waiting {wait_time}s before retry {attempt+1}/{max_retries}...")
66
+ time.sleep(wait_time)
67
+ else:
68
+ print(f"Agent error: {e}")
69
+ return f"Error: {err}"
70
+ return "Error: Rate limit exceeded after retries"
71
 
72
 
73
  # --- The rest of the code (keep as-is) ---
 
115
  except Exception as e:
116
  print(f"Error on task {task_id}: {e}")
117
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
118
+ time.sleep(3)
119
  if not answers_payload:
120
  return "No answers.", pd.DataFrame(results_log)
121
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}