# Use an official Python runtime as a parent image FROM python:3.9-slim # Install git and git-lfs RUN apt-get update && apt-get install -y git git-lfs && git lfs install # Create a non-root user 'appuser' RUN useradd -ms /bin/bash appuser # Set the working directory WORKDIR /home/appuser/app # Copy requirements file COPY requirements.txt . # Install required packages RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY . . # Set environment variables for cache directories ENV HF_HOME=/home/appuser/app/.cache ENV HF_DATASETS_CACHE=/home/appuser/app/.cache # Create the cache directory RUN mkdir -p /home/appuser/app/.cache # Change ownership of the application files RUN chown -R appuser:appuser /home/appuser/app # Switch to non-root user USER appuser # Pre-download models and datasets RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('all-MiniLM-L6-v2')" RUN python -c "from datasets import load_dataset; load_dataset('Gustavosta/Stable-Diffusion-Prompts')" # Expose port 7860 EXPOSE 7860 # Command to run the API CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]