tfrere commited on
Commit
7e5b577
·
1 Parent(s): 2b6f74b
Files changed (4) hide show
  1. Dockerfile +20 -12
  2. README.md +37 -37
  3. app/server.py +31 -35
  4. pyproject.toml +1 -1
Dockerfile CHANGED
@@ -43,17 +43,28 @@ RUN poetry config virtualenvs.create false \
43
  RUN mkdir -p static templates screenshots /home/user/.cache && \
44
  chown -R user:user /app /home/user/.cache
45
 
46
- # Set HOME for the following Playwright install step
47
  ENV HOME=/home/user \
48
  PYTHONPATH=/app
49
 
50
- # Switch to non-root user for browser installation
51
- USER user
52
-
53
- # Install Playwright browsers under the non-root user HOME directory
54
- RUN playwright install chromium
55
-
56
- # Switch back to root to copy files
 
 
 
 
 
 
 
 
 
 
 
57
  USER root
58
 
59
  # Copy application code
@@ -61,9 +72,6 @@ COPY app /app/app
61
  COPY templates /app/templates
62
  COPY static /app/static
63
 
64
- # Install system dependencies for Playwright
65
- RUN apt-get update && apt-get install -y fonts-noto-color-emoji fonts-freefont-ttf libharfbuzz-icu0
66
-
67
  # Make sure all files are owned by user
68
  RUN chown -R user:user /app
69
 
@@ -78,4 +86,4 @@ USER user
78
  EXPOSE 7860
79
 
80
  # Start command
81
- CMD ["uvicorn", "app.server:app", "--host", "0.0.0.0", "--port", "7860"]
 
43
  RUN mkdir -p static templates screenshots /home/user/.cache && \
44
  chown -R user:user /app /home/user/.cache
45
 
46
+ # Set HOME for the browser
47
  ENV HOME=/home/user \
48
  PYTHONPATH=/app
49
 
50
+ # Install additional Chrome dependencies
51
+ RUN apt-get update && apt-get install -y \
52
+ fonts-noto-color-emoji \
53
+ fonts-freefont-ttf \
54
+ libharfbuzz-icu0 \
55
+ # Chrome dependencies
56
+ libglib2.0-0 \
57
+ unzip \
58
+ xvfb
59
+
60
+ # Install Chrome - required for Helium
61
+ RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
62
+ && echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list \
63
+ && apt-get update \
64
+ && apt-get install -y google-chrome-stable \
65
+ && rm -rf /var/lib/apt/lists/*
66
+
67
+ # Switch to non-root user to copy files
68
  USER root
69
 
70
  # Copy application code
 
72
  COPY templates /app/templates
73
  COPY static /app/static
74
 
 
 
 
75
  # Make sure all files are owned by user
76
  RUN chown -R user:user /app
77
 
 
86
  EXPOSE 7860
87
 
88
  # Start command
89
+ CMD ["uvicorn", "app.server:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -8,72 +8,72 @@ app_file: app/server.py
8
  pinned: false
9
  ---
10
 
11
- # Minimal Browser Screenshot Experiment
12
 
13
- This web application allows taking screenshots of websites using a headless browser and displaying them directly in the user interface.
14
 
15
- ## Features
16
 
17
- - Simple and responsive web interface
18
- - URL input with default value pointing to Open LLM Leaderboard
19
- - Screenshot capture via Playwright (Chromium browser)
20
- - Display of results in the interface
21
 
22
- ## Technologies Used
23
 
24
- - **Backend**: FastAPI, Playwright
25
- - **Frontend**: HTML/JS with Tailwind CSS
26
- - **Dependency Management**: Poetry
27
- - **Deployment**: Docker, Hugging Face Spaces
28
 
29
- ## Local Installation
30
 
31
- ### Prerequisites
32
 
33
  - Python 3.10+
34
  - Poetry
35
- - Docker (optional)
36
 
37
- ### Installation with Poetry
38
 
39
  ```bash
40
- # Clone the repository
41
  git clone [repo-url]
42
  cd minimal-browser-screenshot-experiment
43
 
44
- # Install dependencies
45
  poetry install
46
 
47
- # Install Playwright browsers
48
- poetry run playwright install chromium
49
- poetry run playwright install-deps chromium
50
-
51
- # Launch the application
52
  poetry run uvicorn app.server:app --host 0.0.0.0 --port 7860
53
  ```
54
 
55
- ### Using Docker
56
 
57
  ```bash
58
- # Build the Docker image
59
  docker build -t minimal-browser-screenshot .
60
 
61
- # Run the container
62
  docker run -p 7860:7860 minimal-browser-screenshot
63
  ```
64
 
65
- ## Deployment on Hugging Face Spaces
 
 
 
 
 
 
66
 
67
- This application is designed to be easily deployed on a Hugging Face Space.
68
 
69
- 1. Create a new Docker-type Space
70
- 2. Associate this GitHub repository with your Space
71
- 3. The Space will automatically use the provided Dockerfile to build and deploy the application
 
 
72
 
73
- ## Usage
74
 
75
- 1. Access the application through your browser
76
- 2. Enter the URL of the site you want to screenshot (default: Open LLM Leaderboard)
77
- 3. Click "Take a screenshot"
78
- 4. Wait a few seconds for the headless browser to load the page and take the screenshot
79
- 5. The screenshot appears in the interface
 
8
  pinned: false
9
  ---
10
 
11
+ # Minimal Browser Screenshot Experiment avec Helium
12
 
13
+ Cette application web permet de prendre des captures d'écran de sites web en utilisant un navigateur headless et de les afficher directement dans l'interface utilisateur.
14
 
15
+ ## Fonctionnalités
16
 
17
+ - Interface web simple et responsive
18
+ - Saisie d'URL avec valeur par défaut pointant vers Open LLM Leaderboard
19
+ - Capture d'écran via Helium (navigateur Chrome)
20
+ - Affichage des résultats dans l'interface
21
 
22
+ ## Technologies utilisées
23
 
24
+ - **Backend**: FastAPI, Helium
25
+ - **Frontend**: HTML/JS avec Tailwind CSS
26
+ - **Gestion des dépendances**: Poetry
27
+ - **Déploiement**: Docker, Hugging Face Spaces
28
 
29
+ ## Installation locale
30
 
31
+ ### Prérequis
32
 
33
  - Python 3.10+
34
  - Poetry
35
+ - Docker (optionnel)
36
 
37
+ ### Installation avec Poetry
38
 
39
  ```bash
40
+ # Cloner le dépôt
41
  git clone [repo-url]
42
  cd minimal-browser-screenshot-experiment
43
 
44
+ # Installer les dépendances
45
  poetry install
46
 
47
+ # Lancer l'application
 
 
 
 
48
  poetry run uvicorn app.server:app --host 0.0.0.0 --port 7860
49
  ```
50
 
51
+ ### Utilisation de Docker
52
 
53
  ```bash
54
+ # Construire l'image Docker
55
  docker build -t minimal-browser-screenshot .
56
 
57
+ # Lancer le conteneur
58
  docker run -p 7860:7860 minimal-browser-screenshot
59
  ```
60
 
61
+ ## Déploiement sur Hugging Face Spaces
62
+
63
+ Cette application est conçue pour être facilement déployée sur un Hugging Face Space.
64
+
65
+ 1. Créer un nouveau Space de type Docker
66
+ 2. Associer ce dépôt GitHub à votre Space
67
+ 3. Le Space utilisera automatiquement le Dockerfile fourni pour construire et déployer l'application
68
 
69
+ ## Utilisation
70
 
71
+ 1. Accéder à l'application via votre navigateur
72
+ 2. Saisir l'URL du site dont vous souhaitez prendre une capture d'écran (par défaut: Open LLM Leaderboard)
73
+ 3. Cliquer sur "Take a screenshot"
74
+ 4. Attendre quelques secondes que le navigateur headless charge la page et prenne la capture d'écran
75
+ 5. La capture d'écran apparaît dans l'interface
76
 
77
+ ## À propos d'Helium
78
 
79
+ Helium est une bibliothèque Python qui simplifie l'automatisation des navigateurs. C'est une couche d'abstraction au-dessus de Selenium qui offre une API plus simple et plus intuitive. Helium a été choisi pour remplacer Playwright dans ce projet pour démontrer sa facilité d'utilisation.
 
 
 
 
app/server.py CHANGED
@@ -4,7 +4,7 @@ from fastapi import FastAPI, Request, Form
4
  from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
5
  from fastapi.staticfiles import StaticFiles
6
  from fastapi.templating import Jinja2Templates
7
- from playwright.async_api import async_playwright
8
  import uuid
9
  import logging
10
  import pathlib
@@ -46,43 +46,39 @@ async def take_screenshot(url: str = Form(...)):
46
  filename = f"{uuid.uuid4()}.png"
47
  filepath = str(screenshots_path / filename)
48
 
49
- # Log browser executable paths
50
  logger.info(f"HOME env: {os.environ.get('HOME')}")
51
  logger.info(f"Current working directory: {os.getcwd()}")
52
 
53
- # Take the screenshot with Playwright
54
- async with async_playwright() as p:
55
- # Configuration adapted for Docker environment
56
- browser = await p.chromium.launch(
57
- headless=True,
58
- args=[
59
- '--no-sandbox',
60
- '--disable-setuid-sandbox',
61
- '--disable-dev-shm-usage',
62
- '--disable-accelerated-2d-canvas',
63
- '--no-first-run',
64
- '--no-zygote',
65
- '--single-process',
66
- '--disable-gpu'
67
- ],
68
- executable_path=None # Use default path
69
- )
70
-
71
- logger.info("Browser launched successfully")
72
- page = await browser.new_page(viewport={"width": 1280, "height": 720})
73
-
74
- try:
75
- logger.info(f"Navigating to URL: {url}")
76
- await page.goto(url, wait_until="networkidle", timeout=60000)
77
- logger.info("Navigation complete, taking screenshot")
78
- await page.screenshot(path=filepath)
79
- logger.info(f"Screenshot saved to {filepath}")
80
- except Exception as e:
81
- logger.error(f"Error during page navigation or screenshot: {str(e)}")
82
- raise
83
- finally:
84
- await browser.close()
85
- logger.info("Browser closed")
86
 
87
  return JSONResponse({
88
  "success": True,
 
4
  from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
5
  from fastapi.staticfiles import StaticFiles
6
  from fastapi.templating import Jinja2Templates
7
+ from helium import start_chrome, kill_browser, screenshot
8
  import uuid
9
  import logging
10
  import pathlib
 
46
  filename = f"{uuid.uuid4()}.png"
47
  filepath = str(screenshots_path / filename)
48
 
49
+ # Log environment info
50
  logger.info(f"HOME env: {os.environ.get('HOME')}")
51
  logger.info(f"Current working directory: {os.getcwd()}")
52
 
53
+ # Configuration for Docker environment
54
+ chrome_options = {
55
+ "headless": True,
56
+ "options": [
57
+ '--no-sandbox',
58
+ '--disable-setuid-sandbox',
59
+ '--disable-dev-shm-usage',
60
+ '--disable-accelerated-2d-canvas',
61
+ '--no-first-run',
62
+ '--no-zygote',
63
+ '--single-process',
64
+ '--disable-gpu'
65
+ ]
66
+ }
67
+
68
+ # Take the screenshot with Helium
69
+ logger.info("Launching Chrome browser")
70
+ driver = start_chrome(url, **chrome_options)
71
+
72
+ logger.info("Browser launched successfully")
73
+
74
+ try:
75
+ logger.info(f"Taking screenshot at {filepath}")
76
+ screenshot(filepath)
77
+ logger.info(f"Screenshot saved to {filepath}")
78
+ finally:
79
+ # Always kill the browser to free resources
80
+ kill_browser()
81
+ logger.info("Browser closed")
 
 
 
 
82
 
83
  return JSONResponse({
84
  "success": True,
pyproject.toml CHANGED
@@ -11,7 +11,7 @@ fastapi = "^0.103.1"
11
  uvicorn = "^0.23.2"
12
  jinja2 = "^3.1.2"
13
  python-multipart = "^0.0.6"
14
- playwright = "^1.39.0"
15
  aiofiles = "^23.2.1"
16
 
17
  [build-system]
 
11
  uvicorn = "^0.23.2"
12
  jinja2 = "^3.1.2"
13
  python-multipart = "^0.0.6"
14
+ helium = "^3.2.7"
15
  aiofiles = "^23.2.1"
16
 
17
  [build-system]