| # Unified Dockerfile for Hugging Face Spaces | |
| # Stage 1: Build the frontend | |
| FROM node:25-slim AS build-ui | |
| WORKDIR /app/ui | |
| COPY ui/package*.json ./ | |
| RUN npm install | |
| COPY ui/ ./ | |
| RUN npm run build | |
| # Stage 2: Final Image | |
| FROM python:3.12-slim | |
| WORKDIR /app | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| build-essential \ | |
| curl \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Create a non-root user for Hugging Face (UID 1000) | |
| RUN useradd -m -u 1000 user | |
| USER user | |
| ENV PATH="/home/user/.local/bin:$PATH" | |
| # Install Python dependencies | |
| COPY --chown=user api/requirements.txt . | |
| RUN pip install --no-cache-dir --user -r requirements.txt | |
| # Copy backend code | |
| COPY --chown=user api/ ./ | |
| # Copy built frontend from Stage 1 to the 'dist' folder | |
| COPY --from=build-ui --chown=user /app/ui/dist ./dist | |
| # Hugging Face Spaces uses port 7860 | |
| EXPOSE 7860 | |
| # Run setup and start the API | |
| # We ensure models are installed in user space | |
| CMD ["sh", "-c", "python setup_models.py && uvicorn main:app --host 0.0.0.0 --port 7860"] | |