Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files1267 unmodified lines12681269127012711272127312 unmodified lines12861287128812891290129129 unmodified lines1321132213231324132513261327132811 unmodified lines1340134113421343134413459 unmodified lines1355135613571358135913601361136213631149 unmodified lines251325142515251625172518251912 unmodified lines253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325541267 unmodified lines)video_analyzed = Falsegemini_pool = self.provider_key_pools.get("gemini") or []if gemini_pool and needs_video_analysis:gemini_models = list(12 unmodified lineswhile True:g_key = self._get_api_key(g_model)if not g_key:breaktry:gm_resp = requests.post(29 unmodified linessections.append(f"Multimodal Video Analysis:\n{analysis_text}")video_analyzed = Truebreakbreakerr_low = gm_resp.text.lower()if gm_resp.status_code in (401, 402, 403):if hasattr(self, "_rotate_api_key") and self._rotate_api_key("gemini", permanent=True11 unmodified linesexcept Exception as gm_exc:if "task time budget exhausted" in str(gm_exc).lower():raisebreakif video_analyzed:break9 unmodified linesbreakif not has_transcript and not video_analyzed:sections.append("Transcript/Video Analysis Status: unavailable. ""Use web_search or search_wikipedia with the video title, channel, or quoted dialogue.")return _trim_tool_output("\n\n".join(sections), max_chars=12000)1149 unmodified lines# Usuwanie znacznikówcleaned = re.sub(r"</?code[^>]*>", "", cleaned, flags=re.IGNORECASE)cleaned = re.sub(r"final_answer\s*\(\s*", "", cleaned, flags=re.IGNORECASE)cleaned = re.sub(r"\)\s*$", "", cleaned)cleaned = re.sub(r"^final_answer\s*:\s*", "", cleaned, flags=re.IGNORECASE)cleaned = cleaned.strip()12 unmodified linescleaned = re.sub(r"(?im)^\s*(?:#\s*)?(?:thought|reasoning|plan)\s*:.*$", "", cleaned).strip()fa_call = re.search(r"final_answer\(\s*(?:answer\s*=\s*)?(['\"])(.*?)\1\s*\)",cleaned,flags=re.DOTALL,)if fa_call:cleaned = fa_call.group(2).strip()if "->" in cleaned:tail_part = cleaned.split("->")[-1].strip()quoted_match = re.findall(r'""([^"]+)""|"([^"]+)"', tail_part)if quoted_match:last_q = quoted_match[-1]cleaned = (last_q[0] or last_q[1]).strip()else:cleaned = tail_part.strip(" .\"'")lines = [ln.strip(" \t\r\n-*•\"'") for ln in cleaned.splitlines() if ln.strip()]if not lines:return ""cleaned = lines[-1]1267 unmodified lines126812691270127112721273127412 unmodified lines128712881289129012911292129329 unmodified lines132313241325132613271328132913301331133211 unmodified lines13441345134613471348134913509 unmodified lines1360136113621363136413651366136713681369137013711149 unmodified lines25212522252325242525252612 unmodified lines253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725881267 unmodified lines)video_analyzed = Falsevideo_analysis_error = ""gemini_pool = self.provider_key_pools.get("gemini") or []if gemini_pool and needs_video_analysis:gemini_models = list(12 unmodified lineswhile True:g_key = self._get_api_key(g_model)if not g_key:video_analysis_error = "no gemini api key"breaktry:gm_resp = requests.post(29 unmodified linessections.append(f"Multimodal Video Analysis:\n{analysis_text}")video_analyzed = Truebreakvideo_analysis_error = "gemini returned empty candidate"breakerr_low = gm_resp.text.lower()video_analysis_error = f"gemini status {gm_resp.status_code}: {gm_resp.text[:200]}"if gm_resp.status_code in (401, 402, 403):if hasattr(self, "_rotate_api_key") and self._rotate_api_key("gemini", permanent=True11 unmodified linesexcept Exception as gm_exc:if "task time budget exhausted" in str(gm_exc).lower():raisevideo_analysis_error = f"gemini exception: {type(gm_exc).__name__}: {gm_exc}"breakif video_analyzed:break9 unmodified linesbreakif not has_transcript and not video_analyzed:reason = f" ({video_analysis_error})" if video_analysis_error else ""sections.append("Transcript/Video Analysis Status: unavailable"f"{reason}. ""Use web_search or search_wikipedia with the video title, channel, or quoted dialogue. ""Do NOT guess visual counts from the question alone.")return _trim_tool_output("\n\n".join(sections), max_chars=12000)1149 unmodified lines# Usuwanie znacznikówcleaned = re.sub(r"</?code[^>]*>", "", cleaned, flags=re.IGNORECASE)cleaned = re.sub(r"final_answer\s*\(\s*", "", cleaned, flags=re.IGNORECASE)cleaned = re.sub(r"^final_answer\s*:\s*", "", cleaned, flags=re.IGNORECASE)cleaned = cleaned.strip()12 unmodified linescleaned = re.sub(r"(?im)^\s*(?:#\s*)?(?:thought|reasoning|plan)\s*:.*$", "", cleaned).strip()fa_call = re.search(r"final_answer\s*\(\s*(?:answer\s*=\s*)?"r"(?:(['\"])(.*?)\1|([^()]*?))"r"\s*\)",cleaned,flags=re.DOTALL,)if fa_call:cleaned = (fa_call.group(2) or fa_call.group(3) or "").strip(# Obsługa formatu "answer -> wartość" (tylko gdy -> jest w kontekście odpowiedzi)arrow_match = re.search(r"\b(?:answer|result|output|final\s+answer)\s*->\s*(.+)$",cleaned,flags=re.IGNORECASE | re.DOTALL,)if arrow_match:tail_part = arrow_match.group(1).strip()# Najpierw spróbuj wyciągnąć ostatni fragment w cudzysłowach (pojedynczych lub podwójnych)quoted_match = re.findall(r'"([^"]+)"|\'([^\']+)\'', tail_part)if quoted_match:last_q = quoted_match[-1]cleaned = (last_q[0] or last_q[1]).strip()else:cleaned = tail_part.strip(" .\"'")junk_pattern = re.compile(r"^(?:the wikipedia article|the youtube video|the search results show|this scene is from|"r"the page is quite long|the task doesn't have|the download failed|let me try|let me search|"r"let me check|now i need to find|i found the|the operation is not commutative|"r"the scene occurs|so the answer is|therefore|thus|hence|in conclusion|"r"i need to|i should|i will|i can|based on the|according to the)\b",re.IGNORECASE,)answer_line = ""for ln in reversed(lines):if not junk_pattern.match(ln):answer_line = lnbreakif not answer_line:return ""cleaned = answer_lineif not lines:return ""cleaned = lines[-1]
app.py+43-9