Spaces:
Sleeping
Sleeping
| # Use Python 3.11 slim image for better performance and smaller size | |
| FROM python:3.11-slim | |
| # Set working directory | |
| WORKDIR /app | |
| # Set environment variables | |
| ENV PYTHONDONTWRITEBYTECODE=1 \ | |
| PYTHONUNBUFFERED=1 \ | |
| FLASK_ENV=production \ | |
| PORT=7860 | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| build-essential \ | |
| curl \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy requirements first for better caching | |
| COPY req.txt . | |
| # Install Python dependencies | |
| RUN pip install --no-cache-dir --upgrade pip && \ | |
| pip install --no-cache-dir -r req.txt | |
| # Copy application code | |
| COPY . . | |
| # Create a non-root user for security | |
| RUN useradd --create-home --shell /bin/bash app && \ | |
| chown -R app:app /app | |
| # Switch to non-root user | |
| USER app | |
| # Expose the port (Hugging Face Spaces uses 7860 by default) | |
| EXPOSE 7860 | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ | |
| CMD curl -f http://localhost:7860/health || exit 1 | |
| # Run the application with Gunicorn for production | |
| CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "2", "--timeout", "120", "--keep-alive", "2", "--max-requests", "1000", "--max-requests-jitter", "100", "app:app"] |