| # --- Stage 1: Build the frontend --- | |
| FROM node:20-alpine AS frontend-builder | |
| WORKDIR /app/frontend | |
| COPY frontend/package*.json ./ | |
| RUN npm install | |
| COPY frontend/ ./ | |
| RUN npm run build | |
| # --- Stage 2: Build the backend & package the app --- | |
| FROM python:3.11-slim | |
| WORKDIR /app | |
| # Set environment variables | |
| ENV PYTHONDONTWRITEBYTECODE=1 | |
| ENV PYTHONUNBUFFERED=1 | |
| ENV HF_HOME=/tmp/hf_cache | |
| ENV PORT=7860 | |
| # Create writable cache directory for Hugging Face models | |
| RUN mkdir -p /tmp/hf_cache && chmod 777 /tmp/hf_cache | |
| # Copy backend requirements and install dependencies | |
| COPY backend/requirements.txt ./backend/ | |
| RUN pip install --no-cache-dir -r backend/requirements.txt | |
| # Pre-download the Hugging Face models during Docker build time | |
| # so the Space starts up instantly without timeout. | |
| RUN python -c "from transformers import pipeline; \ | |
| pipeline('text-classification', model='j-hartmann/emotion-english-distilroberta-base', top_k=None); \ | |
| pipeline('ner', model='dslim/distilbert-NER', aggregation_strategy='simple')" | |
| # Copy built frontend assets from stage 1 | |
| COPY --from=frontend-builder /app/frontend/dist ./frontend/dist | |
| # Copy backend source code | |
| COPY backend/ ./backend/ | |
| # Make the backend directory writable (needed for writing sentiment logs) | |
| RUN chmod -R 777 /app/backend | |
| # Expose port 7860 (Hugging Face Spaces default port) | |
| EXPOSE 7860 | |
| # Run uvicorn server, binding to the port specified by Hugging Face Spaces | |
| CMD ["sh", "-c", "uvicorn backend.main:app --host 0.0.0.0 --port ${PORT}"] | |