Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files824 unmodified lines825826827828829830831832833834835836837838839840841842828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901824 unmodified lines )
for attempt in range(max_attempts): step_start = time.monotonic() 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: elapsed = time.monotonic() - step_start 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"elapsed={elapsed:.1f}s (attempt {attempt + 1}/{max_attempts}): {exc}" step_start = time.monotonic() 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: elapsed = time.monotonic() - step_start 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"elapsed={elapsed:.1f}s (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: current_provider = ( self.model_id.split("/", 1)[0].lower() if "/" in self.model_id else "gemini" ) other_provider_models = [ m for m in models_to_try if m != self.model_id and ( (m.split("/", 1)[0].lower() if "/" in m else "gemini") != current_provider ) if not is_recoverable or attempt == max_attempts - 1: return f"AGENT ERROR: {exc}"
if len(models_to_try) > 1: current_provider = ( self.model_id.split("/", 1)[0].lower() if "/" in self.model_id else "gemini" ) other_provider_models = [ m for m in models_to_try if m != self.model_id and ( (m.split("/", 1)[0].lower() if "/" in m else "gemini") != current_provider ) ] if other_provider_models and any( m in err_lower for m in ( "401", "402", "404", "authenticationerror", "notfounderror", ) ): next_model = other_provider_models[0] else: try: idx = models_to_try.index(self.model_id) next_model = models_to_try[(idx + 1) % len(models_to_try)] except ValueError: next_model = models_to_try[0] self.model_id = next_model self.agent.model = self._build_model(self.model_id) print(f"Switched model to {self.model_id}.")
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) ] if other_provider_models and any( m in err_lower for m in ( "401", "402", "404", "authenticationerror", "notfounderror", ) ): next_model = other_provider_models[0] else: try: idx = models_to_try.index(self.model_id) next_model = models_to_try[(idx + 1) % len(models_to_try)] except ValueError: next_model = models_to_try[0] self.model_id = next_model build_fn = getattr(self, "_build_model", None) if callable(build_fn): self.agent.model = build_fn(self.model_id) else: model_kwargs = { "model_id": self.model_id, "api_key": self._get_api_key(self.model_id), "max_tokens": 8192, "num_retries": 1, "timeout": 25, } if self.model_id.startswith("gemini/"): model_kwargs["reasoning_effort"] = "low" self.agent.model = model_cls(**model_kwargs) print(f"Switched model to {self.model_id}.")
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)
return "AGENT ERROR: Max retries exceeded."
app.py+70-57