| # ============================================================================ | |
| # AI API Dockerfile - Optimized for Hugging Face Spaces | |
| # ============================================================================ | |
| # This Dockerfile is configured to run on Hugging Face Spaces with: | |
| # - Port 7860 (HF Spaces default) | |
| # - Non-root user for security | |
| # - CUDA support for GPU acceleration | |
| # - 4-bit quantization for efficient memory usage | |
| # ============================================================================ | |
| FROM nvidia/cuda:12.1.0-cudnn8-runtime-ubuntu22.04 | |
| # Set environment variables | |
| ENV DEBIAN_FRONTEND=noninteractive \ | |
| PYTHONUNBUFFERED=1 \ | |
| PYTHONDONTWRITEBYTECODE=1 \ | |
| PIP_NO_CACHE_DIR=1 \ | |
| PIP_DISABLE_PIP_VERSION_CHECK=1 \ | |
| CUDA_HOME=/usr/local/cuda \ | |
| PATH=/usr/local/cuda/bin:${PATH} \ | |
| LD_LIBRARY_PATH=/usr/local/cuda/lib64:${LD_LIBRARY_PATH} | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| python3.10 \ | |
| python3-pip \ | |
| python3-dev \ | |
| build-essential \ | |
| git \ | |
| wget \ | |
| curl \ | |
| ca-certificates \ | |
| libgomp1 \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Create non-root user for security | |
| RUN useradd -m -u 1000 -s /bin/bash appuser | |
| # Set working directory | |
| WORKDIR /app | |
| # Copy requirements first (for better caching) | |
| COPY requirements.txt . | |
| # Upgrade pip and install Python dependencies | |
| RUN pip3 install --upgrade pip setuptools wheel && \ | |
| pip3 install --no-cache-dir -r requirements.txt | |
| # Copy application code | |
| COPY main.py . | |
| # Create necessary directories | |
| RUN mkdir -p /app/logs && \ | |
| chown -R appuser:appuser /app | |
| # Switch to non-root user | |
| USER appuser | |
| # Set default environment variables (can be overridden) | |
| ENV MODEL_NAME="mistralai/Mistral-7B-Instruct-v0.2" \ | |
| API_KEY="your-secret-api-key-here" \ | |
| MAX_LENGTH="2048" \ | |
| TEMPERATURE="0.7" \ | |
| TOP_P="0.95" \ | |
| CACHE_SIZE="100" \ | |
| PORT="7860" \ | |
| HOST="0.0.0.0" | |
| # Expose port 7860 (Hugging Face Spaces default) | |
| EXPOSE 7860 | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ | |
| CMD curl -f http://localhost:7860/health || exit 1 | |
| # Run the application | |
| CMD ["python3", "main.py"] | |