Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files824 unmodified lines825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888824 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_starterr_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_idelse "gemini")other_provider_models = [mfor m in models_to_tryif m != self.model_idand ((m.split("/", 1)[0].lower() if "/" in m else "gemini")!= current_provider)]if other_provider_models and any(m in err_lowerfor 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_modelself.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 25print(f"Sleeping {wait_sec}s before next attempt...")time.sleep(wait_sec)return "AGENT ERROR: Max retries exceeded."824 unmodified lines825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901824 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_starterr_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_idelse "gemini")other_provider_models = [mfor m in models_to_tryif m != self.model_idand ((m.split("/", 1)[0].lower() if "/" in m else "gemini")!= current_provider)]if other_provider_models and any(m in err_lowerfor 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_modelbuild_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 25print(f"Sleeping {wait_sec}s before next attempt...")time.sleep(wait_sec)return "AGENT ERROR: Max retries exceeded."
app.py+70-57