Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files23 unmodified lines2425262728293031323334353637141 unmodified lines17918018118218318418514 unmodified lines20020120220320420520620764 unmodified lines27227327427527627723 unmodified linesfrom smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, toollitellm.drop_params = Trueraw_key = os.getenv("GEMINI_API_KEY")gemini_key = raw_key.strip(" '\"\n\r\t") if raw_key else ""if not gemini_key:raise ValueError("Missing GEMINI_API_KEY in environment/secrets. ""Add GEMINI_API_KEY in Space Settings -> Variables and secrets and restart the Space.")os.environ["GEMINI_API_KEY"] = gemini_key@tooldef fetch_url_text(url: str) -> str:141 unmodified linesb64_data = base64.b64encode(f.read()).decode("utf-8")response = litellm.completion(model="gemini/gemini-2.5-flash",api_key=gemini_key,messages=[{14 unmodified linesexcept Exception as e:return f"Error inspecting image '{image_path}': {str(e)}"model = LiteLLMModel(model_id="gemini/gemini-3.8-flash",api_key=gemini_key,max_tokens=4096,num_retries=5,64 unmodified linesraw_text = "" if result is None else str(result).strip()return self._clean_exact_match_answer(raw_text)except Exception as exc:return f"AGENT ERROR: {exc}"def run_and_submit_all( profile: gr.OAuthProfile | None):23 unmodified lines242526272829303132333435363738394041424344141 unmodified lines18618718818919019119214 unmodified lines20720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124264 unmodified lines30730830931031131231323 unmodified linesfrom smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, toollitellm.drop_params = Trueenv_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. ""Add GEMINI_API_KEY in Space Settings -> Variables and secrets and restart the Space.")if gemini_key.startswith("sk-or-"):raise ValueError("GEMINI_API_KEY contains an OpenRouter key ('sk-or-...'). ""Replace it with a Google Gemini API key ('AIza...').")os.environ["GEMINI_API_KEY"] = gemini_keyos.environ["GOOGLE_API_KEY"] = gemini_key@tooldef fetch_url_text(url: str) -> str:141 unmodified linesb64_data = base64.b64encode(f.read()).decode("utf-8")response = litellm.completion(model=self.model_id,api_key=gemini_key,messages=[{14 unmodified linesexcept Exception as e:return f"Error inspecting image '{image_path}': {str(e)}"candidate_models = ["gemini/gemini-3.8-flash","gemini/gemini-3-flash-preview","gemini/gemini-2.5-flash-lite","gemini/gemini-2.0-flash",]selected_model = Nonelast_probe_error = ""for candidate in candidate_models:try:litellm.completion(model=candidate,api_key=gemini_key,messages=[{"role": "user", "content": "Reply with OK"}],max_tokens=2048,timeout=15,)selected_model = candidateprint(f"Selected working Gemini model: {selected_model}")breakexcept Exception as probe_exc:last_probe_error = f"{candidate}: {probe_exc}"print(f"Model probe failed for {last_probe_error}")if not selected_model:raise RuntimeError(f"No working Gemini model found. Last error -> {last_probe_error}")self.model_id = selected_modelmodel = LiteLLMModel(model_id=self.model_id,api_key=gemini_key,max_tokens=4096,num_retries=5,64 unmodified linesraw_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):
app.py+40-4