File size: 1,743 Bytes
906aac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# =============== Hugging Face Spaces Official Base (2025) ===============
# This is the exact base image HF recommends for Playwright apps
FROM python:3.10-slim-bookworm

# Set environment variables (critical for headless + no buffering)
ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PLAYWRIGHT_BROWSERS_PATH=/home/appuser/.cache/ms-playwright

# Create non-root user (required by Hugging Face security policy)
RUN useradd -m -s /bin/bash appuser

# Install system basics + Playwright dependencies in one layer
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    wget \
    gnupg \
    && rm -rf /var/lib/apt/lists/*

# Set workdir
WORKDIR /home/appuser/app

# Copy requirements first (maximizes cache)
COPY requirements.txt .

# Install Python packages
RUN pip install --upgrade --no-cache-dir pip && \
    pip install --no-cache-dir -r requirements.txt

# ========= THIS IS THE MAGIC LINE FOR HUGGING FACE =========
# Installs Playwright + browsers + ALL system dependencies automatically
RUN pip install playwright && \
    playwright install --with-deps chromium
# ===========================================================
# (We only need Chromium — saves ~300MB vs installing all browsers)

# Copy your app code
COPY . .

# Fix permissions
RUN chown -R appuser:appuser /home/appuser

# Switch to non-root user (mandatory on HF)
USER appuser

# Expose Gradio default port
EXPOSE 7860

# Healthcheck (HF loves this)
HEALTHCHECK --interval=30s --timeout=5s --start-period=90s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:7860 || exit 1

# Start the app (standard for Gradio on HF Spaces)
CMD ["python", "app.py"]