Spaces:
Sleeping
Sleeping
| # --------------------------------------------------------- | |
| # Stage 1: Build the frontend Next.js App | |
| # --------------------------------------------------------- | |
| FROM node:20-alpine AS frontend-builder | |
| WORKDIR /app | |
| # Install dependencies first for layer caching | |
| COPY frontend/package*.json ./ | |
| RUN npm ci | |
| # Copy the rest of the frontend source | |
| COPY frontend/ ./ | |
| # Generate the static HTML/CSS/JS export in /app/out | |
| RUN npm run build | |
| # --------------------------------------------------------- | |
| # Stage 2: Build the FastAPI Backend | |
| # --------------------------------------------------------- | |
| FROM python:3.12-slim | |
| # Install system packages including C++ compiler for PyTorch Inductor (torch.compile) | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| g++ \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Create a non-root user (UID 1000) as required by Hugging Face Spaces | |
| RUN useradd -m -u 1000 user | |
| WORKDIR /app | |
| # Copy backend requirements first for caching | |
| COPY --chown=user:user backend/requirements.txt /app/ | |
| # Install Python dependencies | |
| # CRITICAL: We force the installation of CPU-only PyTorch wheels. | |
| # This drastically reduces the image size (from ~2.5GB to ~500MB) | |
| # and fits nicely into Hugging Face Spaces free-tier constraints. | |
| RUN pip install --no-cache-dir -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu | |
| # Copy the rest of the backend files | |
| COPY --chown=user:user backend/ /app/ | |
| # Copy the built frontend static files from Stage 1 | |
| # FastAPI mounts this at '/' | |
| COPY --from=frontend-builder --chown=user:user /app/out /app/static | |
| # Ensure the database and current directory are writable by 'user' | |
| # (The PokemonService cache writes to poke_database.json) | |
| RUN chown -R user:user /app | |
| # Switch to the non-root user | |
| USER user | |
| # Set environment variables | |
| ENV HOME=/home/user \ | |
| PATH=/home/user/.local/bin:$PATH \ | |
| PYTHONUNBUFFERED=1 \ | |
| OMP_NUM_THREADS=1 \ | |
| MKL_NUM_THREADS=1 \ | |
| HF_HOME=/tmp/hf_home | |
| # Expose the default port used by Hugging Face Spaces | |
| EXPOSE 7860 | |
| # Start Uvicorn bound to 0.0.0.0 and port 7860 | |
| CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"] |