Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files378 unmodified lines37938038138238338438538638738838939039139239339439539686 unmodified lines4834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135146 unmodified lines521522523524525526527528529530531532533534378 unmodified linesexcept Exception as list_exc:print(f"Warning: could not list Gemini models via REST: {list_exc}")if available_api_models:matched = [m for m in preferred_models if m in available_api_models]extra = sorted(m for m in available_api_modelsif ("flash" in m or "pro" in m)and "embedding" not in mand "image" not in mand "tts" not in mand "lyria" not in mand "nano-banana" not in mand "2.0" not in mand "2.5" not in mand m not in matched)self.working_models = matched + extra86 unmodified lines)models_to_try = list(getattr(self, "working_models", []) or [self.model_id])max_attempts = max(3, len(models_to_try) + 1)for attempt in range(max_attempts):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).lower()is_limit_or_transient = any(token in err_strfor token in ("429","rate","quota","resource_exhausted","too many requests","limit","503","overloaded","temporarily unavailable","no response from model",))print(f"[AGENT ERROR] task_id={task_id} model={self.model_id} (attempt {attempt + 1}/{max_attempts}): {exc}")if not is_limit_or_transient or attempt == max_attempts - 1:return f"AGENT ERROR: {exc}"if len(models_to_try) > 1:6 unmodified linesself.agent.model = LiteLLMModel(model_id=self.model_id,api_key=self.gemini_key,max_tokens=4096,num_retries=2,timeout=45,)print(f"Switched model to {self.model_id}.")wait_sec = 20 if attempt == 0 else 45print(f"Sleeping {wait_sec}s before retry...")time.sleep(wait_sec)return "AGENT ERROR: Max retries exceeded."378 unmodified lines37938038138238338438538638738838939039139239339439539639739839940040140286 unmodified lines4894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255266 unmodified lines533534535536537538539540541542543544545546378 unmodified linesexcept Exception as list_exc:print(f"Warning: could not list Gemini models via REST: {list_exc}")excluded_tokens = ("embedding","image","tts","lyria","nano-banana","customtools","omni","2.0","2.5",)if available_api_models:matched = [m for m in preferred_models if m in available_api_models]extra = sorted(m for m in available_api_modelsif ("flash" in m or "pro" in m)and not any(tok in m for tok in excluded_tokens)and m not in matched)self.working_models = matched + extra86 unmodified lines)models_to_try = list(getattr(self, "working_models", []) or [self.model_id])max_attempts = max(3, len(models_to_try))error_markers = ("error in generating model output","429","404","notfounderror","ratelimiterror","resource_exhausted","quota","too many requests","503","overloaded","temporarily unavailable",)for attempt in range(max_attempts):try:result = self.agent.run(prompt)raw_text = "" if result is None else str(result).strip()raw_lower = raw_text.lower()if any(marker in raw_lower for marker in error_markers):raise RuntimeError(raw_text)return self._clean_exact_match_answer(raw_text)except Exception as exc:err_lower = str(exc).lower()is_recoverable = any(marker in err_lower for marker in error_markers)print(f"[AGENT ERROR] task_id={task_id} model={self.model_id} "f"(attempt {attempt + 1}/{max_attempts}): {exc}")if not is_recoverable or attempt == max_attempts - 1:return f"AGENT ERROR: {exc}"if len(models_to_try) > 1:6 unmodified linesself.agent.model = LiteLLMModel(model_id=self.model_id,api_key=self.gemini_key,max_tokens=2048,num_retries=1,timeout=25,)print(f"Switched model to {self.model_id}.")wait_sec = 5 if ("404" in err_lower or "notfounderror" in err_lower) else 25print(f"Sleeping {wait_sec}s before next attempt...")time.sleep(wait_sec)return "AGENT ERROR: Max retries exceeded."
app.py+42-30