Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files26 unmodified lines2728293031323334413 unmodified lines44844945045145245345445545645745845946046145545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649726 unmodified lines env_ci = {k.upper(): v for k, v in os.environ.items() if v} raw_key = env_ci.get("GEMINI_API_KEY") or env_ci.get("GOOGLE_API_KEY") or "" gemini_key = raw_key.strip(" '\"\n\r\t") self.gemini_key = gemini_key self.working_models = [m for m in candidate_models if m != "gemini/gemini-3.8-flash"] if not gemini_key: raise ValueError( "Missing GEMINI_API_KEY in environment/secrets. "413 unmodified lines
def __call__(self, question: str, task_id: str = "") -> str: import time from smolagents import LiteLLMModel
prompt = question.strip() if task_id: prompt += ( f"\n\nTask ID: {task_id}. If the question references an attached file " f"or image, use download_task_file(task_id='{task_id}') to inspect it." )
for attempt in range(3): try: result = self.agent.run(prompt) raw_text = "" if result is None else str(result).strip() return self._clean_exact_match_answer(raw_text) except Exception as exc: print(f"[AGENT ERROR] task_id={task_id}: {exc}") return f"AGENT ERROR: {exc}" try: result = self.agent.run(prompt) raw_text = "" if result is None else str(result).strip() return self._clean_exact_match_answer(raw_text) except Exception as exc: err_str = str(exc) is_rate_limit = any( token in err_str.lower() for token in ("429", "rate", "quota", "resource_exhausted", "too many requests") ) print(f"[AGENT ERROR] task_id={task_id} (attempt {attempt + 1}/3): {exc}") if not is_rate_limit or attempt == 2: return f"AGENT ERROR: {exc}"
# Przełącz na kolejny działający model z osobnej puli RPM/TPM i odczekaj okno minutowe if len(getattr(self, "working_models", [])) > 1: idx = self.working_models.index(self.model_id) next_model = self.working_models[(idx + 1) % len(self.working_models)] self.model_id = next_model self.agent.model = LiteLLMModel( model_id=self.model_id, api_key=self.gemini_key, max_tokens=4096, num_retries=3, timeout=45, ) print(f"Switched model to {self.model_id} after 429.")
wait_sec = 35 * (attempt + 1) print(f"Sleeping {wait_sec}s to reset Gemini rate limit...") time.sleep(wait_sec)
return "AGENT ERROR: Max retries exceeded."
def run_and_submit_all( profile: gr.OAuthProfile | None): """app.py+40-7