# Use an official lightweight Python image FROM python:3.11-slim # Create a non-root user (Hugging Face Spaces recommends running as a non-root user) RUN useradd -m -u 1000 user # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 ENV PORT=7860 # MongoDB is required. If you use a cloud DB, you can inject MONGO_URI in HuggingFace Secrets. # ENV MONGO_URI=mongodb+srv://... # Switch to the non-root user and set the working directory USER user WORKDIR /home/user/app # Copy the requirements file and install dependencies COPY --chown=user requirements.txt . # Install dependencies into the user's local environment RUN pip install --no-cache-dir --user -r requirements.txt # Add the local bin to PATH ENV PATH="/home/user/.local/bin:${PATH}" # Copy the application code COPY --chown=user . . # Expose the Hugging Face Spaces default port EXPOSE 7860 # Run the FastAPI application using Uvicorn CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]