Spaces:
Sleeping
Sleeping
Add Chrome and ChromeDriver installation to Dockerfile; enhance screenshot functionality with improved options and logging
Browse files- Dockerfile +22 -2
- src/modules/apps/__init__.py +23 -5
Dockerfile
CHANGED
|
@@ -1,6 +1,28 @@
|
|
| 1 |
# Use a Python image with uv pre-installed
|
| 2 |
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Create a non-root user
|
| 5 |
RUN useradd -m -u 1000 appuser
|
| 6 |
|
|
@@ -50,6 +72,4 @@ USER appuser
|
|
| 50 |
|
| 51 |
EXPOSE 7860
|
| 52 |
# Run the FastAPI application by default
|
| 53 |
-
# Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs
|
| 54 |
-
# Uses `--host 0.0.0.0` to allow access from outside the container
|
| 55 |
CMD ["python", "main.py"]
|
|
|
|
| 1 |
# Use a Python image with uv pre-installed
|
| 2 |
FROM ghcr.io/astral-sh/uv:python3.11-bookworm
|
| 3 |
|
| 4 |
+
# Install Chrome and dependencies
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
wget \
|
| 7 |
+
gnupg \
|
| 8 |
+
curl \
|
| 9 |
+
unzip \
|
| 10 |
+
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
|
| 11 |
+
&& echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
|
| 12 |
+
&& apt-get update \
|
| 13 |
+
&& apt-get install -y \
|
| 14 |
+
google-chrome-stable \
|
| 15 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
+
|
| 17 |
+
# Install ChromeDriver
|
| 18 |
+
RUN CHROME_VERSION=$(google-chrome --version | awk '{print $3}' | awk -F'.' '{print $1}') \
|
| 19 |
+
&& CHROMEDRIVER_VERSION=$(curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_VERSION") \
|
| 20 |
+
&& wget -q "https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" \
|
| 21 |
+
&& unzip chromedriver_linux64.zip \
|
| 22 |
+
&& mv chromedriver /usr/local/bin/ \
|
| 23 |
+
&& chmod +x /usr/local/bin/chromedriver \
|
| 24 |
+
&& rm chromedriver_linux64.zip
|
| 25 |
+
|
| 26 |
# Create a non-root user
|
| 27 |
RUN useradd -m -u 1000 appuser
|
| 28 |
|
|
|
|
| 72 |
|
| 73 |
EXPOSE 7860
|
| 74 |
# Run the FastAPI application by default
|
|
|
|
|
|
|
| 75 |
CMD ["python", "main.py"]
|
src/modules/apps/__init__.py
CHANGED
|
@@ -4,6 +4,7 @@ import os
|
|
| 4 |
from selenium import webdriver
|
| 5 |
from selenium.webdriver.chrome.options import Options
|
| 6 |
from selenium.webdriver.chrome.service import Service
|
|
|
|
| 7 |
print("Gradio app loaded.")
|
| 8 |
|
| 9 |
def capture_page(url: str, output_file: str = "screenshot.png"):
|
|
@@ -14,15 +15,31 @@ def capture_page(url: str, output_file: str = "screenshot.png"):
|
|
| 14 |
:param output_file: The filename to save the screenshot.
|
| 15 |
"""
|
| 16 |
options = Options()
|
| 17 |
-
options.add_argument(
|
| 18 |
-
options.add_argument(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
service = Service(
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
driver.get(url)
|
|
|
|
|
|
|
| 26 |
driver.save_screenshot(output_file)
|
| 27 |
print(f"Screenshot saved: {output_file}")
|
| 28 |
finally:
|
|
@@ -44,6 +61,7 @@ def capture_and_show(url: str):
|
|
| 44 |
# Return the image path
|
| 45 |
return temp_path
|
| 46 |
except Exception as e:
|
|
|
|
| 47 |
return f"Error capturing page: {str(e)}"
|
| 48 |
|
| 49 |
def create_gradio_app():
|
|
|
|
| 4 |
from selenium import webdriver
|
| 5 |
from selenium.webdriver.chrome.options import Options
|
| 6 |
from selenium.webdriver.chrome.service import Service
|
| 7 |
+
from subprocess import PIPE, STDOUT
|
| 8 |
print("Gradio app loaded.")
|
| 9 |
|
| 10 |
def capture_page(url: str, output_file: str = "screenshot.png"):
|
|
|
|
| 15 |
:param output_file: The filename to save the screenshot.
|
| 16 |
"""
|
| 17 |
options = Options()
|
| 18 |
+
options.add_argument('--headless')
|
| 19 |
+
options.add_argument('--no-sandbox') # Required in Docker
|
| 20 |
+
options.add_argument('--disable-dev-shm-usage') # Required in Docker
|
| 21 |
+
options.add_argument('--disable-gpu') # Required in Docker
|
| 22 |
+
options.add_argument('--disable-software-rasterizer')
|
| 23 |
+
options.add_argument('--window-size=1920,1080')
|
| 24 |
+
options.add_argument('--disable-extensions')
|
| 25 |
+
options.add_argument('--disable-infobars')
|
| 26 |
|
| 27 |
+
# Set up Chrome service with explicit path to chromedriver and logging
|
| 28 |
+
service = Service(
|
| 29 |
+
executable_path='/usr/local/bin/chromedriver',
|
| 30 |
+
log_output=PIPE # Redirect logs to pipe
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Initialize Chrome with the service and options
|
| 34 |
+
driver = webdriver.Chrome(
|
| 35 |
+
service=service,
|
| 36 |
+
options=options
|
| 37 |
+
)
|
| 38 |
|
| 39 |
try:
|
| 40 |
driver.get(url)
|
| 41 |
+
# Add a small delay to ensure page loads completely
|
| 42 |
+
driver.implicitly_wait(5)
|
| 43 |
driver.save_screenshot(output_file)
|
| 44 |
print(f"Screenshot saved: {output_file}")
|
| 45 |
finally:
|
|
|
|
| 61 |
# Return the image path
|
| 62 |
return temp_path
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"Error in capture_and_show: {str(e)}") # Add detailed logging
|
| 65 |
return f"Error capturing page: {str(e)}"
|
| 66 |
|
| 67 |
def create_gradio_app():
|