Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files378 unmodified lines37938038138238338438538638738838939039139239339439539639739838738838939039139239339940040140286 unmodified lines4894904914864924934944954964974984995005015025035045055065075085095105115125135145155164944954964974984995005015025035045055065075085175185195205215225105115235245255266 unmodified lines533534535524525526536537538539540541530531542543544545546378 unmodified lines except 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_models if ("flash" in m or "pro" in m) and "embedding" not in m and "image" not in m and "tts" not in m and "lyria" not in m and "nano-banana" not in m and "2.0" not in m and "2.5" not 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) + 1) 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_str = str(exc).lower() is_limit_or_transient = any( token in err_str for token in ( "429", "rate", "quota", "resource_exhausted", "too many requests", "limit", "503", "overloaded", "temporarily unavailable", "no response from model", ) 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}" ) 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: if not is_recoverable or attempt == max_attempts - 1: return f"AGENT ERROR: {exc}"
if len(models_to_try) > 1:6 unmodified lines self.agent.model = LiteLLMModel( model_id=self.model_id, api_key=self.gemini_key, max_tokens=4096, num_retries=2, timeout=45, max_tokens=2048, num_retries=1, timeout=25, ) print(f"Switched model to {self.model_id}.")
wait_sec = 20 if attempt == 0 else 45 print(f"Sleeping {wait_sec}s before retry...") wait_sec = 5 if ("404" in err_lower or "notfounderror" in err_lower) else 25 print(f"Sleeping {wait_sec}s before next attempt...") time.sleep(wait_sec)
return "AGENT ERROR: Max retries exceeded."app.py+42-30