# Stage 1: Build the React Frontend FROM node:20-alpine AS builder RUN apk add --no-cache libc6-compat WORKDIR /app # Create writable directories RUN mkdir -p /tmp/huggingface && \ chmod -R 777 /tmp/huggingface && \ mkdir -p /app/workspace && \ chmod -R 777 /app/workspace # Set cache environment variables ENV HF_HOME=/tmp/huggingface \ TRANSFORMERS_CACHE=/tmp/huggingface \ XDG_CACHE_HOME=/tmp \ WRITABLE_DIR=/app/workspace # Copy and build frontend COPY frontend ./frontend WORKDIR /app/frontend 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 RUN npm run build # Stage 2: FastAPI Backend FROM python:3.12-slim AS backend WORKDIR /app # Install OS dependencies and set up locales RUN apt-get update --fix-missing && \ apt-get install --no-install-recommends -y git curl locales && \ sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \ locale-gen en_US.UTF-8 && \ apt-get clean && rm -rf /var/lib/apt/lists/* # Set locale environment variables ENV LANG=en_US.UTF-8 \ LANGUAGE=en_US:en \ LC_ALL=en_US.UTF-8 \ PYTHONIOENCODING=utf-8 # Install Node.js 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 and install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt 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 ENV DEEPEVAL_TELEMETRY_OPT_OUT=YES # Copy backend code and frontend build COPY . . COPY --from=builder /app/frontend/build ./frontend/build # Set up user and permissions 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 USER spaces-user EXPOSE ${PORT:-7860} CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}"]