Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files455 unmodified lines45645745845946046146246346446546646746846947047147247347447547647747847946248048148246548348448548676 unmodified lines5635645655665675685695705715725735745755765 unmodified lines58258358458558658758858959059159259359459520 unmodified lines616617618588619620621591622623624625626627597628629630631455 unmodified lines if not self.working_models: raise RuntimeError("No generateContent Gemini models available for this API key.")
class SafeLiteLLMModel(LiteLLMModel): @staticmethod def _validate_message(msg): content = getattr(msg, "content", None) if msg is not None else None if content is None or not str(content).strip(): raise RuntimeError("Error in generating model output: empty model response (content=None)") if not isinstance(content, str): msg.content = str(content) return msg
if hasattr(LiteLLMModel, "generate"): def generate(self, *args, **kwargs): return self._validate_message(super().generate(*args, **kwargs)) else: def __call__(self, *args, **kwargs): return self._validate_message(super().__call__(*args, **kwargs))
self._model_cls = SafeLiteLLMModel self.model_id = self.working_models[0] print(f"Using primary Gemini model: {self.model_id} (fallbacks: {self.working_models[1:]})")
model = LiteLLMModel( model = self._model_cls( model_id=self.model_id, api_key=self.gemini_key, max_tokens=2048, max_tokens=8192, num_retries=1, timeout=5, )76 unmodified lines
models_to_try = list(getattr(self, "working_models", []) or [self.model_id]) max_attempts = max(3, len(models_to_try)) model_cls = getattr(self, "_model_cls", LiteLLMModel)
error_markers = ( "error in generating model output", "empty model response", "error in code parsing", "expected string or bytes-like object", "reached max steps", "429", "404", "notfounderror",5 unmodified lines "overloaded", "temporarily unavailable", ) fast_retry_markers = ( "404", "notfounderror", "empty model response", "error in code parsing", "expected string or bytes-like object", "reached max steps", )
for attempt in range(max_attempts): try:20 unmodified lines except ValueError: next_model = models_to_try[0] self.model_id = next_model self.agent.model = LiteLLMModel( self.agent.model = model_cls( model_id=self.model_id, api_key=self.gemini_key, max_tokens=2048, max_tokens=8192, 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 25 wait_sec = 3 if any(m in err_lower for m in fast_retry_markers) else 25 print(f"Sleeping {wait_sec}s before next attempt...") time.sleep(wait_sec)
app.py+36-5