File size: 2,581 Bytes
98feb93
 
 
 
8d3cb40
23a2d5f
668e6ec
8d3cb40
 
2cb9811
 
54381e2
98feb93
 
23a2d5f
2cb9811
98feb93
23a2d5f
98feb93
 
 
23a2d5f
2cb9811
98feb93
470bf5f
23a2d5f
98feb93
668e6ec
 
 
 
 
 
23a2d5f
668e6ec
 
 
 
 
54381e2
 
 
e47ad74
eed76a9
668e6ec
 
 
 
 
470bf5f
 
 
 
 
 
 
 
 
eed76a9
470bf5f
 
 
 
668e6ec
 
 
 
23a2d5f
e47ad74
 
23a2d5f
668e6ec
 
 
23a2d5f
668e6ec
 
eed76a9
668e6ec
 
23a2d5f
 
 
eed76a9
23a2d5f
 
668e6ec
 
 
 
 
 
 
23a2d5f
668e6ec
 
23a2d5f
 
668e6ec
23a2d5f
668e6ec
eed76a9
668e6ec
eed76a9
 
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
98
99
100
101
102
103
104
105
# ────────────────
# Stage 1: Builder
# ────────────────
FROM python:3.10-slim AS builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    gcc \
    g++ \
    pkg-config \
    libmupdf-dev \
    && rm -rf /var/lib/apt/lists/*

# Upgrade pip and install wheel
RUN pip install --upgrade pip setuptools wheel

# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Copy requirements and install dependencies
WORKDIR /build
COPY requirements.txt .

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

# ────────────────
# Stage 2: Production
# ────────────────
FROM python:3.10-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    poppler-utils \
    tesseract-ocr \
    tesseract-ocr-eng \
    tesseract-ocr-fas \
    mupdf-tools \
    libmupdf-dev \
    ffmpeg \
    libopencv-dev \
    libgl1-mesa-dri \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libgomp1 \
    libgbm1 \
    libxss1 \
    libgtk-3-0 \
    libxtst6 \
    libxrandr2 \
    libasound2 \
    libpangocairo-1.0-0 \
    libatk1.0-0 \
    libcairo-gobject2 \
    libgdk-pixbuf-2.0-0 \
    libffi-dev \
    libssl-dev \
    curl \
    wget \
    sqlite3 \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# Create non-root user
RUN groupadd -g 1000 appuser && useradd -r -u 1000 -g appuser appuser

# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Set working directory
WORKDIR /app

# Copy all files
COPY --chown=appuser:appuser . .

# Pre-download TrOCR model to speed up startup
RUN python -c "from transformers import TrOCRProcessor, VisionEncoderDecoderModel; \
    TrOCRProcessor.from_pretrained('microsoft/trocr-base-printed'); \
    VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-base-printed')" || echo "Model download failed, will download at runtime"

# Environment variables
ENV PYTHONPATH=/app
ENV TRANSFORMERS_CACHE=/app/cache
ENV HF_HOME=/app/cache
ENV LOG_LEVEL=INFO
ENV ENVIRONMENT=production
ENV PYTHONUNBUFFERED=1

# Switch to non-root user
USER appuser

# Expose port
EXPOSE 7860

# Healthcheck
HEALTHCHECK --interval=45s --timeout=30s --start-period=180s --retries=10 \
    CMD python -c "import requests; requests.get('http://localhost:7860')" || exit 1

# Start Gradio app (not FastAPI)
CMD ["python", "app.py"]