# Multi-stage build for optimized image FROM python:3.9-slim as builder # Install build dependencies RUN apt-get update && apt-get install -y \ build-essential \ curl \ && rm -rf /var/lib/apt/lists/* # Copy requirements and install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Final stage FROM python:3.9-slim # Install runtime dependencies RUN apt-get update && apt-get install -y \ curl \ && rm -rf /var/lib/apt/lists/* \ && apt-get clean # Set working directory WORKDIR /app # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ TRANSFORMERS_CACHE=/app/model_cache \ HF_HOME=/app/model_cache \ HUGGINGFACE_HUB_CACHE=/app/model_cache \ MODEL_NAME=microsoft/DialoGPT-medium # Create cache directories with proper permissions RUN mkdir -p /app/model_cache && chmod 777 /app/model_cache # Copy installed packages from builder stage COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages COPY --from=builder /usr/local/bin /usr/local/bin # Copy application code COPY . . # Create non-root user and set permissions RUN useradd -m -u 1000 user && \ chown -R user:user /app && \ chmod 777 /app/model_cache USER user # Expose HuggingFace Spaces port (7860) EXPOSE 7860 # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=300s --retries=3 \ CMD curl -f https://cronjob-python-chatbot.hf.space/health || exit 1 # Run FastAPI application CMD ["python", "main.py"]