Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files26 unmodified lines272829303132413 unmodified lines44644744844945045145245345445545645745845946046146246346426 unmodified linesenv_ci = {k.upper(): v for k, v in os.environ.items() if v}raw_key = env_ci.get("GEMINI_API_KEY") or env_ci.get("GOOGLE_API_KEY") or ""gemini_key = raw_key.strip(" '\"\n\r\t")if not gemini_key:raise ValueError("Missing GEMINI_API_KEY in environment/secrets. "413 unmodified linesdef __call__(self, question: str, task_id: str = "") -> str:prompt = question.strip()if task_id:prompt += (f"\n\nTask ID: {task_id}. If the question references an attached file "f"or image, use download_task_file(task_id='{task_id}') to inspect it.")try:result = self.agent.run(prompt)raw_text = "" if result is None else str(result).strip()return self._clean_exact_match_answer(raw_text)except Exception as exc:print(f"[AGENT ERROR] task_id={task_id}: {exc}")return f"AGENT ERROR: {exc}"def run_and_submit_all( profile: gr.OAuthProfile | None):"""26 unmodified lines2728293031323334413 unmodified lines44844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649726 unmodified linesenv_ci = {k.upper(): v for k, v in os.environ.items() if v}raw_key = env_ci.get("GEMINI_API_KEY") or env_ci.get("GOOGLE_API_KEY") or ""gemini_key = raw_key.strip(" '\"\n\r\t")self.gemini_key = gemini_keyself.working_models = [m for m in candidate_models if m != "gemini/gemini-3.8-flash"]if not gemini_key:raise ValueError("Missing GEMINI_API_KEY in environment/secrets. "413 unmodified linesdef __call__(self, question: str, task_id: str = "") -> str:import timefrom smolagents import LiteLLMModelprompt = question.strip()if task_id:prompt += (f"\n\nTask ID: {task_id}. If the question references an attached file "f"or image, use download_task_file(task_id='{task_id}') to inspect it.")for attempt in range(3):try:result = self.agent.run(prompt)raw_text = "" if result is None else str(result).strip()return self._clean_exact_match_answer(raw_text)except Exception as exc:err_str = str(exc)is_rate_limit = any(token in err_str.lower()for token in ("429", "rate", "quota", "resource_exhausted", "too many requests"))print(f"[AGENT ERROR] task_id={task_id} (attempt {attempt + 1}/3): {exc}")if not is_rate_limit or attempt == 2:return f"AGENT ERROR: {exc}"# Przełącz na kolejny działający model z osobnej puli RPM/TPM i odczekaj okno minutoweif len(getattr(self, "working_models", [])) > 1:idx = self.working_models.index(self.model_id)next_model = self.working_models[(idx + 1) % len(self.working_models)]self.model_id = next_modelself.agent.model = LiteLLMModel(model_id=self.model_id,api_key=self.gemini_key,max_tokens=4096,num_retries=3,timeout=45,)print(f"Switched model to {self.model_id} after 429.")wait_sec = 35 * (attempt + 1)print(f"Sleeping {wait_sec}s to reset Gemini rate limit...")time.sleep(wait_sec)return "AGENT ERROR: Max retries exceeded."def run_and_submit_all( profile: gr.OAuthProfile | None):"""
app.py+40-7