FASTAPI / Dockerfile
tejani's picture
Update Dockerfile
1b3b60b verified
raw
history blame
1.97 kB
# Use Python 3.10 slim as the base image for compatibility with FastSD CPU
FROM python:3.10-slim
# Set working directory
WORKDIR /app
# Create a non-root user to avoid permission issues
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
# Install system dependencies required for FastSD CPU
RUN apt-get update && apt-get install -y \
git \
libgl1 \
libglib2.0-0 \
wget \
&& rm -rf /var/lib/apt/lists/*
# Clone the FastSD CPU repository
RUN git clone https://github.com/rupeshs/fastsdcpu.git .
# Create a virtual environment
RUN python -m venv /app/env
ENV PATH="/app/env/bin:$PATH"
# Upgrade pip and install Python dependencies
RUN pip install --no-cache-dir --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Install OpenVINO for performance optimization
RUN pip install --no-cache-dir openvino-dev
# Set cache directories for Hugging Face and Transformers
ENV HF_HOME=/app/cache/huggingface
ENV TRANSFORMERS_CACHE=/app/cache/huggingface/hub
ENV HUGGINGFACE_HUB_CACHE=/app/cache/huggingface/hub
# Create cache directory and set permissions
RUN mkdir -p /app/cache/huggingface/hub && chown -R appuser:appuser /app/cache
# Pre-download the default model (stabilityai/sd-turbo) to the cache
RUN python -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='stabilityai/sd-turbo', local_dir='/app/cache/huggingface/hub/models--stabilityai--sd-turbo', local_dir_use_symlinks=False)"
# Suppress OpenVINO telemetry by setting a writable telemetry directory
ENV OPENVINO_TELEMETRY_DIR=/app/cache/openvino
# Create OpenVINO telemetry directory and set permissions
RUN mkdir -p /app/cache/openvino && chown -R appuser:appuser /app/cache/openvino
# Switch to non-root user
USER appuser
# Expose port 8000 for the API server
EXPOSE 8000
# Set environment variables for Hugging Face Spaces
ENV PYTHONUNBUFFERED=1
ENV DEVICE=cpu
# Command to run the API server
CMD ["python", "src/app.py", "--api"]