Spaces:
Running
Running
Update app/lens_text_core.py
Browse files- app/lens_text_core.py +15 -54
app/lens_text_core.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
import os, time, asyncio, base64, re, threading, hashlib, logging
|
| 3 |
from io import BytesIO
|
| 4 |
from typing import Any, Dict, List, Union
|
| 5 |
from urllib.parse import urlparse
|
|
@@ -7,12 +7,7 @@ from urllib.parse import urlparse
|
|
| 7 |
import httpx
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
-
from
|
| 11 |
-
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
| 12 |
-
from selenium.webdriver.chrome.service import Service as ChromeService
|
| 13 |
-
from selenium.webdriver.common.by import By
|
| 14 |
-
from selenium.webdriver.support.ui import WebDriverWait
|
| 15 |
-
from selenium.webdriver.support import expected_conditions as EC
|
| 16 |
|
| 17 |
CHROME_BINARY_PATH = os.getenv("CHROME_BINARY_PATH", "").strip()
|
| 18 |
|
|
@@ -32,58 +27,24 @@ CHROME_EXTRA_ARGS = os.getenv(
|
|
| 32 |
"--window-size=1920,1080 --headless=new",
|
| 33 |
).split()
|
| 34 |
|
| 35 |
-
_CACHE_TTL =
|
| 36 |
-
_IDLE_TIMEOUT = int(os.getenv("CHROME_IDLE_SECONDS", "60"))
|
| 37 |
_BROWSER_TTL = 900
|
| 38 |
-
|
| 39 |
-
_COMMON_CHROME_PATHS = [
|
| 40 |
-
# Linux
|
| 41 |
-
"/usr/bin/google-chrome", "/usr/bin/chromium", "/usr/bin/chromium-browser",
|
| 42 |
-
"/snap/bin/chromium",
|
| 43 |
-
"/opt/google/chrome/google-chrome",
|
| 44 |
-
# macOS
|
| 45 |
-
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
| 46 |
-
"/Applications/Chromium.app/Contents/MacOS/Chromium",
|
| 47 |
-
# Windows
|
| 48 |
-
r"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
|
| 49 |
-
r"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
|
| 50 |
-
]
|
| 51 |
-
|
| 52 |
-
def _find_chrome_binary() -> str:
|
| 53 |
-
if (env := os.getenv("CHROME_BINARY")):
|
| 54 |
-
return env
|
| 55 |
-
|
| 56 |
-
for cmd in ("google-chrome", "chromium-browser", "chromium", "chrome"):
|
| 57 |
-
path = shutil.which(cmd)
|
| 58 |
-
if path:
|
| 59 |
-
return path
|
| 60 |
-
|
| 61 |
-
for path in _COMMON_CHROME_PATHS:
|
| 62 |
-
if os.path.exists(path):
|
| 63 |
-
return path
|
| 64 |
-
|
| 65 |
-
raise RuntimeError(
|
| 66 |
-
"Chrome binary not found; set CHROME_BINARY env var or install Chrome/Chromium"
|
| 67 |
-
)
|
| 68 |
|
| 69 |
def _build_chrome(cookie_dict: Dict[str, str] | None = None):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
opts.add_argument(flag)
|
| 77 |
-
except Exception:
|
| 78 |
-
pass
|
| 79 |
-
service = ChromeService(executable_path=drv_path)
|
| 80 |
-
drv = webdriver.Chrome(service=service, options=opts)
|
| 81 |
|
| 82 |
-
drv.
|
|
|
|
| 83 |
if cookie_dict:
|
| 84 |
for name, val in cookie_dict.items():
|
| 85 |
try:
|
| 86 |
-
drv.
|
| 87 |
"name": name, "value": val,
|
| 88 |
"domain": ".google.com", "path": "/", "secure": True
|
| 89 |
})
|
|
@@ -209,8 +170,8 @@ def _parse_calc_value(calc: str, dim: float) -> float:
|
|
| 209 |
return base - off if op == "-" else base + off
|
| 210 |
|
| 211 |
def _extract_boxes(drv, w: int, h: int) -> List[Dict[str, Any]]:
|
| 212 |
-
|
| 213 |
-
nodes = drv.find_elements(
|
| 214 |
|
| 215 |
out: List[Dict[str,Any]] = []
|
| 216 |
for n in nodes:
|
|
|
|
| 1 |
|
| 2 |
+
import os, time, asyncio, base64, re, threading, hashlib, logging
|
| 3 |
from io import BytesIO
|
| 4 |
from typing import Any, Dict, List, Union
|
| 5 |
from urllib.parse import urlparse
|
|
|
|
| 7 |
import httpx
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
+
from seleniumbase import Driver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
CHROME_BINARY_PATH = os.getenv("CHROME_BINARY_PATH", "").strip()
|
| 13 |
|
|
|
|
| 27 |
"--window-size=1920,1080 --headless=new",
|
| 28 |
).split()
|
| 29 |
|
| 30 |
+
_CACHE_TTL = 600
|
|
|
|
| 31 |
_BROWSER_TTL = 900
|
| 32 |
+
_IDLE_TIMEOUT = int(os.getenv("CHROME_IDLE_SECONDS", "60"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def _build_chrome(cookie_dict: Dict[str, str] | None = None):
|
| 35 |
+
drv = Driver(
|
| 36 |
+
browser="chrome",
|
| 37 |
+
uc=True,
|
| 38 |
+
headless=True,
|
| 39 |
+
incognito=True
|
| 40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
drv.get("https://google.com/favicon.ico")
|
| 43 |
+
|
| 44 |
if cookie_dict:
|
| 45 |
for name, val in cookie_dict.items():
|
| 46 |
try:
|
| 47 |
+
drv.add_cookie({
|
| 48 |
"name": name, "value": val,
|
| 49 |
"domain": ".google.com", "path": "/", "secure": True
|
| 50 |
})
|
|
|
|
| 170 |
return base - off if op == "-" else base + off
|
| 171 |
|
| 172 |
def _extract_boxes(drv, w: int, h: int) -> List[Dict[str, Any]]:
|
| 173 |
+
drv.wait_for_element_visible("div.lv6PAb", timeout=10)
|
| 174 |
+
nodes = drv.find_elements("xpath", "//div[contains(@class, 'lv6PAb') and @aria-label]")
|
| 175 |
|
| 176 |
out: List[Dict[str,Any]] = []
|
| 177 |
for n in nodes:
|