File size: 2,963 Bytes
dc4e6da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# ============================================
# DocGenie API + Worker - Dockerfile (Minimal)
# ============================================
# Adapted for Hugging Face Spaces (Docker SDK):
#   - Non-root user (UID 1000) β€” HF Spaces requirement
#   - Port 7860                β€” HF Spaces default
#   - Playwright browsers in user-owned path

FROM python:3.11-slim

WORKDIR /app

# Install runtime system dependencies
RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    poppler-utils \
    tesseract-ocr \
    tesseract-ocr-eng \
    libglib2.0-0 \
    libnss3 \
    libnspr4 \
    libdbus-1-3 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libasound2 \
    libpango-1.0-0 \
    libcairo2 \
    && rm -rf /var/lib/apt/lists/*

# Install pip packages (no uv needed - simpler)
COPY api/requirements.txt ./api/requirements.txt
RUN pip install --no-cache-dir -r api/requirements.txt

# Copy ONLY the docgenie modules needed by API (not the full package)
COPY docgenie/__init__.py ./docgenie/__init__.py
COPY docgenie/logging.py ./docgenie/logging.py
COPY docgenie/generation ./docgenie/generation
COPY data/prompt_templates ./data/prompt_templates
COPY data/visual_element_prefabs ./data/visual_element_prefabs

# Copy API code
COPY api ./api

# Copy startup script
COPY start.sh ./start.sh
RUN chmod +x start.sh

# Clean up Python cache
RUN find /usr/local/lib/python3.11/site-packages -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true && \
    find /usr/local/lib/python3.11/site-packages -name "*.pyc" -delete

# -------------------------------------------------------
# Non-root user setup β€” required by Hugging Face Spaces
# -------------------------------------------------------
RUN useradd -m -u 1000 user

# Install Playwright system dependencies as root (requires apt β€” must run before USER switch)
RUN playwright install-deps chromium

# Create writable directories and hand ownership to user
RUN mkdir -p /tmp/docgenie /home/user/.cache/playwright && \
    chown -R user:user /app /tmp/docgenie /home/user

# Switch to non-root user for all runtime operations
USER user

# Set environment variables
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH \
    PYTHONUNBUFFERED=1 \
    PYTHONPATH=/app \
    PORT=7860 \
    PLAYWRIGHT_BROWSERS_PATH=/home/user/.cache/playwright

# Download Playwright Chromium browser binary into user-owned cache directory
# (browser download only β€” system deps already installed above as root)
RUN playwright install chromium

# Expose port 7860 (Hugging Face Spaces default)
EXPOSE 7860

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD python -c "import requests; requests.get('http://localhost:7860/health')"

# Start command β€” shell script handles API + RQ worker
CMD ["./start.sh"]