FROM node:20-bookworm-slim # Install Python, pip, git (for segment-anything), and opencv dependencies RUN apt-get update && apt-get install -y \ python3 \ python3-pip \ python3-venv \ libgl1 \ libglib2.0-0 \ wget \ git \ && rm -rf /var/lib/apt/lists/* WORKDIR /app # Download the SAM Model weights into the image RUN mkdir -p /app/models && \ wget -O /app/models/sam_vit_b_01ec64.pth https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth # Set up Python virtual environment and install backend requirements # We do this first to cache the heavy PyTorch installation COPY backend/requirements.txt ./backend/ RUN python3 -m venv /app/.venv ENV PATH="/app/.venv/bin:$PATH" RUN .venv/bin/pip install --no-cache-dir -r backend/requirements.txt # Pre-download the Segformer model to avoid wasting 20+ seconds on the first request RUN .venv/bin/python3 -c "from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor; SegformerImageProcessor.from_pretrained('nvidia/segformer-b5-finetuned-ade-640-640'); SegformerForSemanticSegmentation.from_pretrained('nvidia/segformer-b5-finetuned-ade-640-640')" # Copy package.json and install frontend dependencies COPY package.json package-lock.json ./ RUN npm install # Copy the rest of the application COPY . . # Set environment variables for the Next.js app to find the model ENV SAM_CHECKPOINT=/app/models/sam_vit_b_01ec64.pth ENV SAM_MODEL_TYPE=vit_b ENV PORT=7860 # Build the Next.js app ENV NEXT_TELEMETRY_DISABLED=1 ENV NODE_OPTIONS="--max-old-space-size=4096" RUN npm run build # Expose Hugging Face default port ENV HOSTNAME="0.0.0.0" EXPOSE 7860 # Start the application CMD ["npm", "run", "start", "--", "-H", "0.0.0.0", "-p", "7860"]