FROM python:3.9-slim | |
# Set environment variables | |
ENV PYTHONUNBUFFERED=1 | |
ENV TRANSFORMERS_CACHE=/app/cache | |
ENV HF_HOME=/app/cache | |
ENV PYTORCH_CUDA_ALLOC_CONF=garbage_collection_threshold:0.6,max_split_size_mb:128 | |
# Optimize for multiple large models | |
ENV TRANSFORMERS_OFFLINE=0 | |
ENV HF_HUB_ENABLE_HF_TRANSFER=1 | |
ENV TOKENIZERS_PARALLELISM=false | |
# Reduce memory fragmentation | |
ENV MALLOC_TRIM_THRESHOLD_=100000 | |
# Install system dependencies for better performance | |
RUN apt-get update && apt-get install -y \ | |
build-essential \ | |
&& rm -rf /var/lib/apt/lists/* | |
# Create non-root user | |
RUN useradd -m -u 1000 user | |
USER user | |
ENV HOME=/home/user \ | |
PATH=/home/user/.local/bin:$PATH | |
# Set working directory | |
WORKDIR /app | |
# Copy requirements and install dependencies | |
COPY --chown=user requirements.txt . | |
RUN pip install --no-cache-dir --upgrade pip && \ | |
pip install --no-cache-dir -r requirements.txt && \ | |
pip install --no-cache-dir hf_transfer | |
# Copy application code | |
COPY --chown=user . . | |
# Create cache directory | |
RUN mkdir -p /app/cache | |
# Expose port | |
EXPOSE 7860 | |
# Run the application | |
CMD ["python", "-u", "app.py"] |