# Use Python 3.9 slim image for better performance FROM python:3.11-slim # Set environment variables ENV PYTHONUNBUFFERED=1 ENV PYTHONDONTWRITEBYTECODE=1 ENV HF_HOME=/tmp/huggingface ENV TRANSFORMERS_CACHE=/tmp/huggingface/transformers ENV TORCH_HOME=/tmp/torch # Create a non-root user (required for HF Spaces) RUN useradd -m -u 1000 user USER user ENV HOME=/home/user ENV PATH=/home/user/.local/bin:$PATH # Set working directory WORKDIR $HOME/app # Install system dependencies (as root) USER root RUN apt-get update && apt-get install -y \ build-essential \ curl \ git \ && rm -rf /var/lib/apt/lists/* # Switch back to user USER user # Copy requirements and install Python dependencies COPY --chown=user requirements.txt . RUN pip install --no-cache-dir --upgrade pip RUN pip install --no-cache-dir --upgrade -r requirements.txt # Download spaCy model RUN python -m spacy download en_core_web_sm # Copy application code COPY --chown=user . . # Create cache directories RUN mkdir -p /tmp/huggingface/transformers RUN mkdir -p /tmp/torch # Expose port EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:7860/ || exit 1 # Run the Flask application CMD ["python", "app.py"]