prmsu / Dockerfile
essay003's picture
Deploy PRMSU Essay Grader
2342b21 verified
Raw History Blame Contribute Delete
2.75 kB
# One container runs the whole system on a Hugging Face Space:
# scorer (Python, the trained models) on 127.0.0.1:8000, not reachable from outside
# server (Node, logins, GPT, database) on 7860, which also serves the built website
#
# Both base images are pinned to Debian bookworm so the Node binary copied from the
# official Node image runs against the same system libraries it was built for. No
# third-party install scripts: everything comes from official images and PyPI.
# ---- stage 1: install the server's Node dependencies with the official Node image ----
FROM node:22-bookworm-slim AS backend
WORKDIR /backend
COPY backend/package.json backend/package-lock.json ./
RUN npm ci --omit=dev
# ---- stage 2: the runtime ----
FROM python:3.11-slim-bookworm
# Node itself: a single self-contained binary.
COPY --from=backend /usr/local/bin/node /usr/local/bin/node
# Spaces run containers as uid 1000.
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH PYTHONUNBUFFERED=1
WORKDIR /home/user/app
# Python dependencies first, so editing code does not reinstall them on every rebuild.
COPY --chown=user scorer/requirements.txt scorer/requirements.txt
# torch 2.7.1+cpu, not the 2.5.1 used for training: PyTorch no longer publishes 2.5.1 CPU
# builds for Linux. Scores were checked identical between the two on 20 held-out essays.
# The extra index is for torch's own dependencies, which live on PyPI; the "+cpu" pin
# keeps pip from picking the multi-gigabyte CUDA build from PyPI instead.
RUN pip install --no-cache-dir --user --upgrade pip \
&& pip install --no-cache-dir --user "torch==2.7.1+cpu" \
--index-url https://download.pytorch.org/whl/cpu --extra-index-url https://pypi.org/simple \
&& pip install --no-cache-dir --user -r scorer/requirements.txt
# The models (2.5 GB) come from their own model repository, because a Space may hold only
# 1 GB of files. Downloaded at build time, so a restart does not fetch them again.
RUN python -c "from huggingface_hub import snapshot_download; snapshot_download('essay003/prmsu-essay-models', local_dir='scorer/outputs', allow_patterns=['en-only/*', 'detector-mitigated/*', 'detector-tl/*'])" && ls scorer/outputs/en-only/model.safetensors scorer/outputs/detector-mitigated/model.safetensors scorer/outputs/detector-tl/model.safetensors
COPY --chown=user . .
COPY --chown=user --from=backend /backend/node_modules backend/node_modules
ENV PORT=7860 \
BERT_API_URL=http://127.0.0.1:8000 \
BERT_TIMEOUT_MS=60000 \
FRONTEND_DIST=/home/user/app/web \
SCORER_MODEL=/home/user/app/scorer/outputs/en-only \
SCORER_MAX_LENGTH=1024
EXPOSE 7860
CMD ["bash", "start.sh"]