Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files92 unmodified lines93949596979899194 unmodified lines29429529629729829930038 unmodified lines33934034134234334434518 unmodified lines364365366367368369370120 unmodified lines491492493494495496497115 unmodified lines613614615616617618619101 unmodified lines72172272372472572672772872973073173273373473573673773873974074131 unmodified lines773774775776777778779211 unmodified lines9919929939949959969973 unmodified lines100110021003100410051006100739 unmodified lines104710481049105010511052105346 unmodified lines110011011102110311041105110692 unmodified linestry:response = requests.get(url,timeout=15,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,)resp.raise_for_status()links = re.findall(38 unmodified lines"srlimit": 5,},headers=headers,timeout=15,)search_resp.raise_for_status()results = search_resp.json().get("query", {}).get("search", [])18 unmodified lines"format": "json",},headers=headers,timeout=15,)parse_resp.raise_for_status()parse_data = parse_resp.json().get("parse", {})120 unmodified lines}],max_tokens=2048,timeout=30,)out = (response.choices[0].message.content or "").strip()if out:115 unmodified linesmodels_resp = requests.get("https://generativelanguage.googleapis.com/v1beta/models",params={"key": gemini_key},timeout=15,)if models_resp.status_code == 200:for m in models_resp.json().get("models", []):101 unmodified lines@classmethoddef _validate_message(cls, msg):content = getattr(msg, "content", None) if msg is not None else Noneif content is None or not str(content).strip():reasoning = getattr(msg, "reasoning_content", None) if msg is not None else Nonetool_calls = getattr(msg, "tool_calls", None) if msg is not None else Noneif not reasoning 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):raise RuntimeError("Error in generating model output: empty model response after sanitization")msg.content = cleanedreturn msgdef _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,}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,}if self.model_id.startswith("gemini/"):model_kwargs["reasoning_effort"] = "low"3 unmodified linesif switched_provider or any(m in err_lower for m in fast_retry_markers):wait_sec = 1else:wait_sec = 8print(f"Sleeping {wait_sec}s before next attempt...")time.sleep(wait_sec)39 unmodified lines# 2. Fetch Questionsprint(f"Fetching questions from: {questions_url}")try:response = requests.get(questions_url, timeout=15)response.raise_for_status()questions_data = response.json()if not questions_data:46 unmodified lines# 5. Submitprint(f"Submitting {len(answers_payload)} answers to: {submit_url}")try:response = requests.post(submit_url, json=submission_data, timeout=60)response.raise_for_status()result_data = response.json()final_status = (92 unmodified lines93949596979899194 unmodified lines29429529629729829930038 unmodified lines33934034134234334434518 unmodified lines364365366367368369370120 unmodified lines491492493494495496497115 unmodified lines613614615616617618619101 unmodified lines72172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278331 unmodified lines815816817818819820821211 unmodified lines10331034103510361037103810393 unmodified lines104310441045104610471048104939 unmodified lines108910901091109210931094109546 unmodified lines114211431144114511461147114892 unmodified linestry:response = requests.get(url,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=45,)resp.raise_for_status()links = re.findall(38 unmodified lines"srlimit": 5,},headers=headers,timeout=45,)search_resp.raise_for_status()results = search_resp.json().get("query", {}).get("search", [])18 unmodified lines"format": "json",},headers=headers,timeout=45,)parse_resp.raise_for_status()parse_data = parse_resp.json().get("parse", {})120 unmodified lines}],max_tokens=2048,timeout=45,)out = (response.choices[0].message.content or "").strip()if out:115 unmodified linesmodels_resp = requests.get("https://generativelanguage.googleapis.com/v1beta/models",params={"key": gemini_key},timeout=45,)if models_resp.status_code == 200:for m in models_resp.json().get("models", []):101 unmodified lines@classmethoddef _validate_message(cls, msg):import reif 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)")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 = cleanedexcept Exception:passreturn msgdef _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": 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": 45,}if self.model_id.startswith("gemini/"):model_kwargs["reasoning_effort"] = "low"3 unmodified linesif switched_provider or any(m in err_lower for m in fast_retry_markers):wait_sec = 1else:wait_sec = 4print(f"Sleeping {wait_sec}s before next attempt...")time.sleep(wait_sec)39 unmodified lines# 2. Fetch Questionsprint(f"Fetching questions from: {questions_url}")try:response = requests.get(questions_url, timeout=45)response.raise_for_status()questions_data = response.json()if not questions_data:46 unmodified lines# 5. Submitprint(f"Submitting {len(answers_payload)} answers to: {submit_url}")try:response = requests.post(submit_url, json=submission_data, timeout=45)response.raise_for_status()result_data = response.json()final_status = (
app.py+62-20