Spaces:
Running
Running
Update app/lens_text_core.py
Browse files- app/lens_text_core.py +42 -51
app/lens_text_core.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
import os, time, asyncio, base64, re, threading, hashlib, logging, shutil
|
| 3 |
from io import BytesIO
|
| 4 |
from typing import Any, Dict, List, Union
|
| 5 |
from urllib.parse import urlparse
|
|
@@ -10,6 +10,7 @@ from PIL import Image
|
|
| 10 |
from selenium import webdriver
|
| 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
|
|
@@ -32,64 +33,54 @@ CHROME_EXTRA_ARGS = os.getenv(
|
|
| 32 |
"--window-size=1920,1080 --headless=new",
|
| 33 |
).split()
|
| 34 |
|
|
|
|
|
|
|
| 35 |
_CACHE_TTL = 300
|
| 36 |
_IDLE_TIMEOUT = int(os.getenv("CHROME_IDLE_SECONDS", "60"))
|
| 37 |
_BROWSER_TTL = 900
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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
|
| 70 |
-
|
| 71 |
-
drv_path = os.getenv("CHROMEDRIVER", "/usr/bin/chromedriver")
|
| 72 |
-
opts = ChromeOptions()
|
| 73 |
-
opts.binary_location = bin_loc
|
| 74 |
-
for flag in CHROME_EXTRA_ARGS:
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
except Exception:
|
| 78 |
pass
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
_cached_cookie, _cached_cookie_ts, _cookie_lock = None, 0.0, threading.Lock()
|
| 95 |
_global_driver, _driver_last_use, _driver_lock = None, 0.0, threading.Lock()
|
|
|
|
| 1 |
|
| 2 |
+
import os, time, asyncio, base64, re, threading, hashlib, logging, shutil, atexit, tempfile, uuid
|
| 3 |
from io import BytesIO
|
| 4 |
from typing import Any, Dict, List, Union
|
| 5 |
from urllib.parse import urlparse
|
|
|
|
| 10 |
from selenium import webdriver
|
| 11 |
from selenium.webdriver.chrome.options import Options as ChromeOptions
|
| 12 |
from selenium.webdriver.chrome.service import Service as ChromeService
|
| 13 |
+
from selenium.common.exceptions import SessionNotCreatedException
|
| 14 |
from selenium.webdriver.common.by import By
|
| 15 |
from selenium.webdriver.support.ui import WebDriverWait
|
| 16 |
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
| 33 |
"--window-size=1920,1080 --headless=new",
|
| 34 |
).split()
|
| 35 |
|
| 36 |
+
_PROFILE_DIRS = []
|
| 37 |
+
|
| 38 |
_CACHE_TTL = 300
|
| 39 |
_IDLE_TIMEOUT = int(os.getenv("CHROME_IDLE_SECONDS", "60"))
|
| 40 |
_BROWSER_TTL = 900
|
| 41 |
|
| 42 |
+
def _mk_profile_dir() -> str:
|
| 43 |
+
base = os.getenv("CHROME_PROFILE_BASE", tempfile.gettempdir())
|
| 44 |
+
p = os.path.join(base, f"chrome-profile-{os.getpid()}-{threading.get_ident()}-{uuid.uuid4().hex}")
|
| 45 |
+
os.makedirs(p, exist_ok=True)
|
| 46 |
+
_PROFILE_DIRS.append(p)
|
| 47 |
+
return p
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def _cleanup_profiles():
|
| 50 |
+
for p in _PROFILE_DIRS:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
| 52 |
+
shutil.rmtree(p, ignore_errors=True)
|
| 53 |
except Exception:
|
| 54 |
pass
|
| 55 |
+
|
| 56 |
+
atexit.register(_cleanup_profiles)
|
| 57 |
+
|
| 58 |
+
def _build_chrome() -> webdriver.Chrome:
|
| 59 |
+
opts = ChromeOptions()
|
| 60 |
+
|
| 61 |
+
extra = os.getenv(
|
| 62 |
+
"CHROME_EXTRA_ARGS",
|
| 63 |
+
"--disable-gpu --no-sandbox --disable-dev-shm-usage --window-size=1920,1080 --headless=new",
|
| 64 |
+
).split()
|
| 65 |
+
for a in extra:
|
| 66 |
+
if a:
|
| 67 |
+
opts.add_argument(a)
|
| 68 |
+
|
| 69 |
+
profile_dir = _mk_profile_dir()
|
| 70 |
+
opts.add_argument(f"--user-data-dir={profile_dir}")
|
| 71 |
+
opts.add_argument("--profile-directory=Default")
|
| 72 |
+
|
| 73 |
+
drv_path = os.getenv("CHROMEDRIVER")
|
| 74 |
+
try:
|
| 75 |
+
if drv_path and os.path.exists(drv_path):
|
| 76 |
+
return webdriver.Chrome(service=ChromeService(executable_path=drv_path), options=opts)
|
| 77 |
+
return webdriver.Chrome(options=opts)
|
| 78 |
+
except SessionNotCreatedException as e:
|
| 79 |
+
LOGGER.warning("SessionNotCreated: %s; retry with a fresh profile dir", e)
|
| 80 |
+
profile_dir2 = _mk_profile_dir()
|
| 81 |
+
opts.arguments = [a for a in opts.arguments if not a.startswith("--user-data-dir=")]
|
| 82 |
+
opts.add_argument(f"--user-data-dir={profile_dir2}")
|
| 83 |
+
return webdriver.Chrome(options=opts)
|
| 84 |
|
| 85 |
_cached_cookie, _cached_cookie_ts, _cookie_lock = None, 0.0, threading.Lock()
|
| 86 |
_global_driver, _driver_last_use, _driver_lock = None, 0.0, threading.Lock()
|