Files
MastersThesis/src/paddle_ocr/Dockerfile.gpu

100 lines
3.3 KiB
Docker
Raw Normal View History

2026-01-17 10:24:00 +01:00
# Dockerfile.gpu - CUDA-enabled PaddleOCR REST API
2026-01-17 10:46:36 +01:00
#
# Supports:
# - x86_64: Uses prebuilt paddlepaddle-gpu wheel from PyPI
# - ARM64: Uses locally compiled wheel from ./wheels/ directory
#
# For ARM64, first build the wheel:
# docker compose run build-paddle
# Then build this image:
# docker compose build ocr-gpu
#
# See README.md for detailed ARM64 GPU build instructions.
2026-01-17 10:24:00 +01:00
FROM nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04
LABEL maintainer="Sergio Jimenez"
LABEL description="PaddleOCR Tuning REST API - GPU/CUDA version"
WORKDIR /app
# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV CUDA_VISIBLE_DEVICES=0
# Install Python 3.11 and system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
python3.11 \
python3.11-venv \
python3-pip \
libgl1 \
libglib2.0-0 \
libsm6 \
libxext6 \
libxrender1 \
libgomp1 \
&& rm -rf /var/lib/apt/lists/* \
&& ln -sf /usr/bin/python3.11 /usr/bin/python
2026-01-17 10:46:36 +01:00
# Copy local wheels directory (may be empty or contain ARM64 wheel)
# The wheels/ directory is created by: docker compose run build-paddle
COPY wheels/ /tmp/wheels/
# Install Python dependencies
# Strategy:
# 1. If local paddlepaddle wheel exists (ARM64), install it first
# 2. Then install remaining dependencies (excluding paddlepaddle-gpu from requirements)
2026-01-17 10:24:00 +01:00
COPY requirements-gpu.txt .
2026-01-17 10:46:36 +01:00
# Install paddlepaddle: prefer local wheel, fallback to pip
RUN if ls /tmp/wheels/paddlepaddle*.whl 1>/dev/null 2>&1; then \
echo "=== Installing PaddlePaddle from local wheel (ARM64) ===" && \
pip install --no-cache-dir /tmp/wheels/paddlepaddle*.whl; \
else \
echo "=== Installing PaddlePaddle from PyPI (x86_64) ===" && \
pip install --no-cache-dir paddlepaddle-gpu==3.0.0; \
fi
# Install remaining dependencies (skip paddlepaddle-gpu line)
RUN grep -v "paddlepaddle-gpu" requirements-gpu.txt > /tmp/requirements-no-paddle.txt && \
pip install --no-cache-dir -r /tmp/requirements-no-paddle.txt
# Cleanup
RUN rm -rf /tmp/wheels /tmp/requirements-no-paddle.txt
2026-01-17 10:24:00 +01:00
# Copy application code
COPY paddle_ocr_tuning_rest.py .
COPY dataset_manager.py .
# Build arguments for models to bake into image
ARG DET_MODEL=PP-OCRv5_server_det
ARG REC_MODEL=PP-OCRv5_server_rec
# Set as environment variables (can be overridden at runtime)
ENV PADDLE_DET_MODEL=${DET_MODEL}
ENV PADDLE_REC_MODEL=${REC_MODEL}
# Download models during build (not at runtime)
RUN python -c "\
import os; \
from paddleocr import PaddleOCR; \
det = os.environ.get('PADDLE_DET_MODEL', 'PP-OCRv5_server_det'); \
rec = os.environ.get('PADDLE_REC_MODEL', 'PP-OCRv5_server_rec'); \
print(f'Downloading models: det={det}, rec={rec}'); \
ocr = PaddleOCR(text_detection_model_name=det, text_recognition_model_name=rec); \
print('Models downloaded successfully!')"
# Volume for dataset and optional additional model cache
VOLUME ["/app/dataset", "/root/.paddlex"]
# Expose API port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
# Run the API server
CMD ["uvicorn", "paddle_ocr_tuning_rest:app", "--host", "0.0.0.0", "--port", "8000"]