Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files455 unmodified lines45645745845946046146246346446546646746876 unmodified lines5455465475485495505515525535 unmodified lines55956056156256356420 unmodified lines585586587588589590591592593594595596597598599600455 unmodified linesif not self.working_models:raise RuntimeError("No generateContent Gemini models available for this API key.")self.model_id = self.working_models[0]print(f"Using primary Gemini model: {self.model_id} (fallbacks: {self.working_models[1:]})")model = LiteLLMModel(model_id=self.model_id,api_key=self.gemini_key,max_tokens=2048,num_retries=1,timeout=5,)76 unmodified linesmodels_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",5 unmodified lines"overloaded","temporarily unavailable",)for attempt in range(max_attempts):try:20 unmodified linesexcept ValueError:next_model = models_to_try[0]self.model_id = next_modelself.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)455 unmodified lines45645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548676 unmodified lines5635645655665675685695705715725735745755765 unmodified lines58258358458558658758858959059159259359459520 unmodified lines616617618619620621622623624625626627628629630631455 unmodified linesif not self.working_models:raise RuntimeError("No generateContent Gemini models available for this API key.")class SafeLiteLLMModel(LiteLLMModel):@staticmethoddef _validate_message(msg):content = getattr(msg, "content", None) if msg is not None else Noneif 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 msgif 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 = SafeLiteLLMModelself.model_id = self.working_models[0]print(f"Using primary Gemini model: {self.model_id} (fallbacks: {self.working_models[1:]})")model = self._model_cls(model_id=self.model_id,api_key=self.gemini_key,max_tokens=8192,num_retries=1,timeout=5,)76 unmodified linesmodels_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 linesexcept ValueError:next_model = models_to_try[0]self.model_id = next_modelself.agent.model = model_cls(model_id=self.model_id,api_key=self.gemini_key,max_tokens=8192,num_retries=1,timeout=25,)print(f"Switched model to {self.model_id}.")wait_sec = 3 if any(m in err_lower for m in fast_retry_markers) else 25print(f"Sleeping {wait_sec}s before next attempt...")time.sleep(wait_sec)
app.py+36-5