File size: 3,516 Bytes
7065d8d 72de53b 7065d8d 72de53b 6f32872 7065d8d 72de53b 7065d8d 72de53b 7065d8d 72de53b 6f32872 de8b168 7065d8d 72de53b 7065d8d 72de53b 7065d8d 72de53b 7065d8d 72de53b 6f32872 5f100b4 | 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 | # PaddleOCR sidecar for the FoodNode onboarding pipeline.
#
# Deployed as a Hugging Face Space (Docker SDK -- see README.md frontmatter).
# Auths every request via the X-Internal-API-Key header, since HF Spaces
# are publicly reachable by URL.
#
# PaddlePaddle is heavy (~800MB resident set, ~1.5GB image). Pin to
# Python 3.11 -- paddlepaddle 2.6.2's pre-built wheel is built against
# CPython 3.10/3.11 and segfaults on import under 3.12. Slim base +
# system libs needed for libGL / OpenCV.
FROM python:3.11-slim
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1
# System deps for PaddlePaddle + OpenCV (libgomp, libGL, libglib).
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates curl \
libgomp1 libglib2.0-0 libsm6 libxext6 libxrender1 libgl1 \
&& rm -rf /var/lib/apt/lists/*
# Hugging Face Spaces runs containers as uid 1000 -- files baked at
# build time must be owned by that uid or the runtime can't read them.
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
WORKDIR $HOME/app
COPY --chown=user:user requirements.txt .
RUN pip install --user --upgrade pip \
&& pip install --user -r requirements.txt \
# paddleocr pulls in BOTH opencv-python and opencv-contrib-python as
# transitive deps, on top of the opencv-python-headless we want for
# server-side use. All three install side-by-side and ship overlapping
# libcv symbols, which corrupts the heap on first use ("double free
# or corruption" / segfault). Drop the non-headless variants...
&& pip uninstall -y opencv-python opencv-contrib-python \
# ...then FORCE-REINSTALL the headless variant. The three opencv
# wheels share files under cv2/; uninstalling two of them deletes
# files that headless RECORD also references, leaving cv2 importable
# but missing core constants (e.g. cv2.INTER_LINEAR -> AttributeError
# when paddleocr tries to use it). --force-reinstall makes pip
# rewrite every file headless owns.
&& pip install --user --force-reinstall --no-deps opencv-python-headless==4.10.0.84
# Pre-download the English PaddleOCR detection + recognition models so the
# first request after a deploy isn't a ~10s download. Cache lands in
# $HOME/.paddleocr/, owned by uid 1000 and writable on subsequent boots.
RUN python -c "from paddleocr import PaddleOCR; PaddleOCR(use_angle_cls=True, lang='en', show_log=False)" \
|| echo "model preload failed; will pull on first request"
COPY --chown=user:user main.py .
# HF Spaces convention: routes incoming HTTP to the port declared in
# README.md frontmatter (`app_port: 7860`). Local docker-compose sets
# PORT=8001 explicitly so the existing in-network URL `http://card-ocr:8001`
# continues to resolve without compose-side rewires.
EXPOSE 7860
# Single worker -- PaddleOCR holds model state per-process and we expect
# single-digit cards/day, so concurrency isn't worth the RAM.
#
# Bind explicitly to 0.0.0.0 (IPv4). HF Spaces' edge proxy connects to
# containers over IPv4 -- `--host ::` would only work if the container's
# net.ipv6.bindv6only=0 sysctl is set (Linux default but not guaranteed
# inside HF's container runtime). When `--host ::` was in use the
# container ran fine internally but HF's edge couldn't reach it, leaving
# the Space publicly unreachable despite a healthy uvicorn process.
CMD ["sh", "-c", "exec uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860} --workers 1"]
|