# ---------------------------- # Stage 1: Build the React Frontend # ---------------------------- FROM node:20-alpine AS builder RUN apk add --no-cache libc6-compat WORKDIR /app # Create writable directories for Hugging Face Spaces RUN mkdir -p /tmp/huggingface && \ chmod -R 777 /tmp/huggingface && \ mkdir -p /app/workspace && \ chmod -R 777 /app/workspace # Set cache environment variables at build time ENV HF_HOME=/tmp/huggingface \ TRANSFORMERS_CACHE=/tmp/huggingface \ XDG_CACHE_HOME=/tmp \ WRITABLE_DIR=/app/workspace # Copy the 'frontend' folder from the project root into the container COPY frontend ./frontend # Switch to the frontend directory WORKDIR /app/frontend # Install dependencies (using yarn, npm, or pnpm) RUN if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ elif [ -f package-lock.json ]; then npm ci; \ elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ else echo "No lockfile found. Exiting." && exit 1; \ fi # Build the React app (produces a production-ready build in the "build" folder) RUN npm run build # ---------------------------- # Stage 2: Set Up the FastAPI Backend and Serve the React App # ---------------------------- FROM python:3.12-slim AS backend WORKDIR /app # Install OS-level dependencies RUN apt-get update --fix-missing && \ apt-get install --no-install-recommends -y git curl && \ apt-get clean && rm -rf /var/lib/apt/lists/* # Install Node.js (if needed for any backend tasks) RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \ apt-get update --fix-missing && \ apt-get install --no-install-recommends -y nodejs && \ apt-get clean && rm -rf /var/lib/apt/lists/* # Copy requirements.txt and install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Install additional dependencies for torch and spaCy RUN pip install --no-cache-dir torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124 RUN python -m spacy download en_core_web_sm # Disable telemetry for deepeval ENV DEEPEVAL_TELEMETRY_OPT_OUT=YES # Copy the rest of your backend code and resources COPY . . # Copy the built React app from the builder stage into the same folder structure as used in your FastAPI code COPY --from=builder /app/frontend/build ./frontend/build # Run HuggingFace Spaces as a root user # Re-create writable directories in this stage RUN mkdir -p /tmp/huggingface /app/workspace && \ chmod -R 777 /tmp/huggingface /app/workspace && \ useradd -m spaces-user && \ chown -R spaces-user:spaces-user /tmp/huggingface /app/workspace # Set the user to spaces-user for running the application USER spaces-user # Expose the port EXPOSE ${PORT:-7860} # Start the FastAPI backend using Uvicorn, reading the PORT env variable CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}"]