# Use Python 3.9 slim image for better performance FROM python:3.9-slim # Set working directory WORKDIR /app # Set environment variables ENV PYTHONUNBUFFERED=1 ENV PYTHONDONTWRITEBYTECODE=1 ENV TRANSFORMERS_CACHE=/app/cache ENV HF_HOME=/app/cache # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ curl \ git \ && rm -rf /var/lib/apt/lists/* # Copy requirements first for better caching COPY requirements.txt . # Create a non-root user early RUN useradd --create-home --shell /bin/bash app # Install Python dependencies as app user RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Create cache directory and set permissions RUN mkdir -p /app/cache && \ chown -R app:app /app # Copy application code and set ownership COPY --chown=app:app app.py . # Switch to non-root user USER app # Pre-download models at build time (optional - comment out if you want faster builds) # RUN python -c " # from transformers import AutoTokenizer, AutoModel; # models = ['Lyon28/Albert-Base-V2', 'Lyon28/GPT-2', 'Lyon28/Tinny-Llama']; # [AutoTokenizer.from_pretrained(m) for m in models[:3]]; # [AutoModel.from_pretrained(m) for m in models[:3]]; # print('Sample models cached') # " # Expose port EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \ CMD curl -f http://localhost:7860/health || exit 1 # Run the application CMD ["python", "app.py"]