Spaces:
Paused
Paused
Update app/lens_text_core.py
Browse files123451 unmodified line78910111213141516171813 unmodified lines32333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889119 unmodified lines209210211212213214215216import os, time, asyncio, base64, re, threading, hashlib, logging, shutilfrom io import BytesIOfrom typing import Any, Dict, List, Unionfrom urllib.parse import urlparse1 unmodified lineimport httpxfrom PIL import Imagefrom selenium import webdriverfrom selenium.webdriver.chrome.options import Options as ChromeOptionsfrom selenium.webdriver.chrome.service import Service as ChromeServicefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECCHROME_BINARY_PATH = os.getenv("CHROME_BINARY_PATH", "").strip()13 unmodified lines"--window-size=1920,1080 --headless=new",).split()_CACHE_TTL = 300_IDLE_TIMEOUT = int(os.getenv("CHROME_IDLE_SECONDS", "60"))_BROWSER_TTL = 900_COMMON_CHROME_PATHS = [# Linux"/usr/bin/google-chrome", "/usr/bin/chromium", "/usr/bin/chromium-browser","/snap/bin/chromium","/opt/google/chrome/google-chrome",# macOS"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","/Applications/Chromium.app/Contents/MacOS/Chromium",# Windowsr"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",r"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",]def _find_chrome_binary() -> str:if (env := os.getenv("CHROME_BINARY")):return envfor cmd in ("google-chrome", "chromium-browser", "chromium", "chrome"):path = shutil.which(cmd)if path:return pathfor path in _COMMON_CHROME_PATHS:if os.path.exists(path):return pathraise RuntimeError("Chrome binary not found; set CHROME_BINARY env var or install Chrome/Chromium")def _build_chrome(cookie_dict: Dict[str, str] | None = None):bin_loc = _find_chrome_binary()drv_path = os.getenv("CHROMEDRIVER", "/usr/bin/chromedriver")opts = ChromeOptions()opts.binary_location = bin_locfor flag in CHROME_EXTRA_ARGS:try:opts.add_argument(flag)except Exception:passservice = ChromeService(executable_path=drv_path)drv = webdriver.Chrome(service=service, options=opts)drv.execute_cdp_cmd("Network.enable", {})if cookie_dict:for name, val in cookie_dict.items():try:drv.execute_cdp_cmd("Network.setCookie", {"name": name, "value": val,"domain": ".google.com", "path": "/", "secure": True})119 unmodified linesreturn base - off if op == "-" else base + offdef _extract_boxes(drv, w: int, h: int) -> List[Dict[str, Any]]:WebDriverWait(drv, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.lv6PAb")))nodes = drv.find_elements(By.XPATH, "//div[contains(@class,'lv6PAb') and @aria-label]")out: List[Dict[str,Any]] = []for n in nodes:123451 unmodified line7891011121313 unmodified lines272829303132333435363738394041424344454647484950119 unmodified lines170171172173174175176177import os, time, asyncio, base64, re, threading, hashlib, loggingfrom io import BytesIOfrom typing import Any, Dict, List, Unionfrom urllib.parse import urlparse1 unmodified lineimport httpxfrom PIL import Imagefrom seleniumbase import DriverCHROME_BINARY_PATH = os.getenv("CHROME_BINARY_PATH", "").strip()13 unmodified lines"--window-size=1920,1080 --headless=new",).split()_CACHE_TTL = 600_BROWSER_TTL = 900_IDLE_TIMEOUT = int(os.getenv("CHROME_IDLE_SECONDS", "60"))def _build_chrome(cookie_dict: Dict[str, str] | None = None):drv = Driver(browser="chrome",uc=True,headless=True,incognito=True)drv.get("https://google.com/favicon.ico")if cookie_dict:for name, val in cookie_dict.items():try:drv.add_cookie({"name": name, "value": val,"domain": ".google.com", "path": "/", "secure": True})119 unmodified linesreturn base - off if op == "-" else base + offdef _extract_boxes(drv, w: int, h: int) -> List[Dict[str, Any]]:drv.wait_for_element_visible("div.lv6PAb", timeout=10)nodes = drv.find_elements("xpath", "//div[contains(@class, 'lv6PAb') and @aria-label]")out: List[Dict[str,Any]] = []for n in nodes:
app/lens_text_core.py+15-54