Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files92 unmodified lines9394959696979899194 unmodified lines29429529629729729829930038 unmodified lines33934034134234234334434518 unmodified lines364365366367367368369370120 unmodified lines491492493494494495496497115 unmodified lines613614615616616617618619101 unmodified lines72172272372472572672772872472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876973273373477077177277377477577677773877877978078178278331 unmodified lines815816817776818819820821211 unmodified lines10331034103599410361037103810393 unmodified lines1043104410451004104610471048104939 unmodified lines1089109010911050109210931094109546 unmodified lines1142114311441103114511461147114892 unmodified lines try: response = requests.get( url, timeout=15, timeout=45, headers={"User-Agent": "Mozilla/5.0"}, ) response.raise_for_status()194 unmodified lines "https://html.duckduckgo.com/html/", data={"q": query}, headers={"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"}, timeout=15, timeout=45, ) resp.raise_for_status() links = re.findall(38 unmodified lines "srlimit": 5, }, headers=headers, timeout=15, timeout=45, ) search_resp.raise_for_status() results = search_resp.json().get("query", {}).get("search", [])18 unmodified lines "format": "json", }, headers=headers, timeout=15, timeout=45, ) parse_resp.raise_for_status() parse_data = parse_resp.json().get("parse", {})120 unmodified lines } ], max_tokens=2048, timeout=30, timeout=45, ) out = (response.choices[0].message.content or "").strip() if out:115 unmodified lines models_resp = requests.get( "https://generativelanguage.googleapis.com/v1beta/models", params={"key": gemini_key}, timeout=15, timeout=45, ) if models_resp.status_code == 200: for m in models_resp.json().get("models", []):101 unmodified lines
@classmethod def _validate_message(cls, msg): content = getattr(msg, "content", None) if msg is not None else None if content is None or not str(content).strip(): reasoning = getattr(msg, "reasoning_content", None) if msg is not None else None tool_calls = getattr(msg, "tool_calls", None) if msg is not None else None if not reasoning and not tool_calls: import re
if msg is None: raise RuntimeError( "Error in generating model output: empty model response (content=None)" )
raw_content = getattr(msg, "content", None) if isinstance(raw_content, list): text_parts = [] for part in raw_content: if isinstance(part, dict): if part.get("type") in ("text", "output_text", None) and part.get("text"): text_parts.append(str(part["text"])) else: p_type = getattr(part, "type", None) p_text = getattr(part, "text", None) if p_text and p_type in ("text", "output_text", None): text_parts.append(str(p_text)) content_str = "\n".join(text_parts).strip() elif raw_content is None: content_str = "" else: content_str = str(raw_content).strip()
reasoning = ( getattr(msg, "reasoning_content", None) or getattr(msg, "reasoning", None) or "" ) reasoning_str = str(reasoning).strip() if reasoning else "" tool_calls = getattr(msg, "tool_calls", None)
if not content_str: if reasoning_str: code_match = re.search( r"(<code>.*?</code>|```(?:py|python)?\s*\n.*?```)", reasoning_str, flags=re.DOTALL | re.IGNORECASE, ) if code_match: content_str = code_match.group(1).strip() if not content_str and not tool_calls: raise RuntimeError( "Error in generating model output: empty model response (content=None)" ) content = "" cleaned = cls._sanitize_code_tags(str(content)) if not cleaned.strip() and not getattr(msg, "tool_calls", None):
cleaned = cls._sanitize_code_tags(content_str) if content_str else "" if not cleaned.strip() and not tool_calls: raise RuntimeError( "Error in generating model output: empty model response after sanitization" )
try: msg.content = cleaned msg.content = cleaned except Exception: pass return msg
def _prepare_call_kwargs(self, kwargs: dict) -> dict:31 unmodified lines "api_key": self._get_api_key(model_name), "max_tokens": 8192, "num_retries": 0, "timeout": 20, "timeout": 45, } if model_name.startswith("gemini/"): kwargs["reasoning_effort"] = "low"211 unmodified lines "api_key": self._get_api_key(self.model_id), "max_tokens": 8192, "num_retries": 0, "timeout": 20, "timeout": 45, } if self.model_id.startswith("gemini/"): model_kwargs["reasoning_effort"] = "low"3 unmodified lines if switched_provider or any(m in err_lower for m in fast_retry_markers): wait_sec = 1 else: wait_sec = 8 wait_sec = 4 print(f"Sleeping {wait_sec}s before next attempt...") time.sleep(wait_sec)
39 unmodified lines # 2. Fetch Questions print(f"Fetching questions from: {questions_url}") try: response = requests.get(questions_url, timeout=15) response = requests.get(questions_url, timeout=45) response.raise_for_status() questions_data = response.json() if not questions_data:46 unmodified lines # 5. Submit print(f"Submitting {len(answers_payload)} answers to: {submit_url}") try: response = requests.post(submit_url, json=submission_data, timeout=60) response = requests.post(submit_url, json=submission_data, timeout=45) response.raise_for_status() result_data = response.json() final_status = (app.py+62-20