Francesco-A commited on
Commit
8cc2fb6
·
1 Parent(s): c0d7484

Fallback agent

Browse files

Added logic to switch Gemini agent model after RPD has been reached

Files changed (2) hide show
  1. agent.py +4 -2
  2. app.py +41 -11
agent.py CHANGED
@@ -151,6 +151,8 @@ class GeminiAgent:
151
  def __init__(self, native_multimodal: bool = True, model_id: str = "gemini/gemini-2.5-flash-lite"):
152
  # def __init__(self, native_multimodal: bool = True, model_id: str = "gemini/gemini-3-flash-preview"):
153
  self.native_multimodal = native_multimodal
 
 
154
  if self.native_multimodal:
155
  client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
156
 
@@ -178,7 +180,7 @@ class GeminiAgent:
178
  self.tools = AGENT_TOOLS
179
  self.gemini_agent = CodeAgent(
180
  name = "gemini_agent",
181
- description = "Gemini CodeAgent",
182
  model = self.model,
183
  tools = self.tools,
184
  add_base_tools = True, # probably redundant, but it does not hurt
@@ -188,7 +190,7 @@ class GeminiAgent:
188
  max_print_outputs_length=1_000_000
189
  )
190
 
191
- print("✅ Gemini agent initialized")
192
 
193
  def __call__(self, question: str, file_path: Optional[str] = None) -> str:
194
  prompt = f"{self.system_prompt}\n\nQuestion: {question}"
 
151
  def __init__(self, native_multimodal: bool = True, model_id: str = "gemini/gemini-2.5-flash-lite"):
152
  # def __init__(self, native_multimodal: bool = True, model_id: str = "gemini/gemini-3-flash-preview"):
153
  self.native_multimodal = native_multimodal
154
+ self.model_id = model_id
155
+
156
  if self.native_multimodal:
157
  client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))
158
 
 
180
  self.tools = AGENT_TOOLS
181
  self.gemini_agent = CodeAgent(
182
  name = "gemini_agent",
183
+ description = f"Gemini CodeAgent ({model_id})",
184
  model = self.model,
185
  tools = self.tools,
186
  add_base_tools = True, # probably redundant, but it does not hurt
 
190
  max_print_outputs_length=1_000_000
191
  )
192
 
193
+ print(f"✅ Gemini agent initialized with model: {model_id}")
194
 
195
  def __call__(self, question: str, file_path: Optional[str] = None) -> str:
196
  prompt = f"{self.system_prompt}\n\nQuestion: {question}"
app.py CHANGED
@@ -8,6 +8,23 @@ from agent import BasicAgent, GeminiAgent
8
  from typing import Optional
9
  from litellm.exceptions import RateLimitError, ContextWindowExceededError
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -29,6 +46,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
29
  global interrupt_flag
30
  interrupt_flag = False
31
 
 
 
 
 
32
  # --- Determine HF Space Runtime URL and Repo URL ---
33
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
34
 
@@ -45,7 +66,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
45
 
46
  # 1. Instantiate Agent (modify this part to create your agent)
47
  try:
48
- agent = GeminiAgent()
49
  agent_type = "GeminiAgent"
50
  except Exception as main_agent_error:
51
  print(f"{agent_type} failed to initialize: {main_agent_error}.")
@@ -55,7 +76,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
55
  print(f"Falling back to {agent_type}.")
56
  except Exception as secondary_agent_error:
57
  print(f"{agent_type} failed to initialize: {secondary_agent_error}.")
58
- agent_type = "None"
59
  return f"Error initializing agent: {e}", None
60
 
61
  # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
@@ -102,7 +123,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
102
  continue
103
 
104
  # CONTENT FILTER SKIP (using .lower() for case-insensitivity)
105
- filter_keywords = ["chess"]
106
  question_words = set(question_text.lower().split()) # Only matches if the exact word is used
107
  if any(word in question_words for word in filter_keywords):
108
  print(f"Skipping filtered question: {item}")
@@ -110,21 +131,30 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
110
  continue
111
 
112
  try:
 
 
 
113
  submitted_answer = agent(question_text)
 
 
114
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
115
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
116
 
117
- if interrupt_flag:
118
- time.sleep(1)
119
- else:
120
- time.sleep(30) # to not exceed free limits (if still not enough errors, try 60)
121
 
122
  except RateLimitError as e:
123
- print(f"🛑 TARGET HIT: Gemini Free Tier limit reached.")
124
  print(f"Details: {e}")
125
- # This is where we break so the space doesn't hang
126
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "STOPPED: API LIMIT REACHED"})
127
- break
 
 
 
 
 
 
 
128
 
129
  except Exception as e:
130
  error_msg = str(e)
 
8
  from typing import Optional
9
  from litellm.exceptions import RateLimitError, ContextWindowExceededError
10
 
11
+ # Gemini agent configs
12
+ MAX_TOTAL_REQUESTS = 20
13
+ request_count = 0
14
+ active_model_index = 0
15
+
16
+ GEMINI_MODELS = [
17
+ "gemini/gemini-2.5-flash-lite",
18
+ "gemini/gemini-2.5-flash",
19
+ ]
20
+
21
+ # Helper: (re)load Gemini agent with fallback
22
+ def load_gemini_agent(model_index: int) -> GeminiAgent:
23
+ model_id = GEMINI_MODELS[model_index]
24
+ print(f"Loading Gemini agent with model: {model_id}")
25
+ return GeminiAgent(model_id=model_id)
26
+
27
+
28
  # (Keep Constants as is)
29
  # --- Constants ---
30
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
46
  global interrupt_flag
47
  interrupt_flag = False
48
 
49
+ global request_count, active_model_index
50
+ request_count = 0
51
+ active_model_index = 0
52
+
53
  # --- Determine HF Space Runtime URL and Repo URL ---
54
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
55
 
 
66
 
67
  # 1. Instantiate Agent (modify this part to create your agent)
68
  try:
69
+ agent = load_gemini_agent(active_model_index)
70
  agent_type = "GeminiAgent"
71
  except Exception as main_agent_error:
72
  print(f"{agent_type} failed to initialize: {main_agent_error}.")
 
76
  print(f"Falling back to {agent_type}.")
77
  except Exception as secondary_agent_error:
78
  print(f"{agent_type} failed to initialize: {secondary_agent_error}.")
79
+ agent_type = "None" # replace with BasicAgent() if credits allow
80
  return f"Error initializing agent: {e}", None
81
 
82
  # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
 
123
  continue
124
 
125
  # CONTENT FILTER SKIP (using .lower() for case-insensitivity)
126
+ filter_keywords = ["chess", "video"]
127
  question_words = set(question_text.lower().split()) # Only matches if the exact word is used
128
  if any(word in question_words for word in filter_keywords):
129
  print(f"Skipping filtered question: {item}")
 
131
  continue
132
 
133
  try:
134
+ if agent_type == "GeminiAgent" and request_count >= MAX_TOTAL_REQUESTS:
135
+ raise RateLimitError("Global request cap reached")
136
+
137
  submitted_answer = agent(question_text)
138
+ request_count +=1
139
+
140
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
141
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
142
 
143
+ time.sleep(60 if not interrupt_flag else 1) # to not exceed RPM
 
 
 
144
 
145
  except RateLimitError as e:
146
+ print(f"🛑 TARGET HIT: Rate limit reached.")
147
  print(f"Details: {e}")
148
+
149
+ if agent_type == "GeminiAgent" and active_model_index + 1 < len(GEMINI_MODELS):
150
+ active_model_index += 1
151
+ agent = load_gemini_agent(active_model_index)
152
+ print("▶️ Switched to fallback Gemini model. Retrying question...")
153
+ else:
154
+ print("🛑 Stopping and submitting progress.")
155
+ # This is where we break so the space doesn't hang
156
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "STOPPED: API LIMIT REACHED"})
157
+ break
158
 
159
  except Exception as e:
160
  error_msg = str(e)