Spaces:
Sleeping
Sleeping
FROM nvidia/cuda:12.1.1-cudnn8-devel-ubuntu22.04 | |
ENV DEBIAN_FRONTEND=noninteractive | |
# Install Python and ML dependencies | |
RUN apt-get update && \ | |
apt-get install -y \ | |
python3.10 \ | |
python3.10-venv \ | |
python3.10-distutils \ | |
python3-pip \ | |
python3-dev \ | |
wget \ | |
git \ | |
libgl1 \ | |
fontconfig \ | |
libglib2.0-0 \ | |
libxrender1 \ | |
libsm6 \ | |
libxext6 \ | |
poppler-utils \ | |
libjpeg-dev \ | |
libpng-dev && \ | |
rm -rf /var/lib/apt/lists/* | |
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 | |
# Create user with UID 1000 (required for Hugging Face Spaces) | |
RUN useradd -m -u 1000 user | |
# Create necessary directories and set permissions | |
RUN mkdir -p /app /app/docker_mineru/output/images /home/user/.cache/huggingface /home/user/.cache/torch && \ | |
chown -R user:user /app /home/user | |
WORKDIR /app | |
# Copy requirements first | |
COPY --chown=user:user requirements.txt . | |
# Upgrade pip and install PyTorch dependencies first | |
# Use versions compatible with CUDA 12.1 for NVIDIA L4 support | |
RUN pip install --no-cache-dir --upgrade pip && \ | |
pip install --no-cache-dir \ | |
torch==2.1.2 \ | |
torchvision==0.16.2 \ | |
torchaudio==2.1.2 \ | |
--extra-index-url https://download.pytorch.org/whl/cu121 | |
# Install other requirements including gunicorn | |
RUN pip install --no-cache-dir -r requirements.txt && \ | |
pip install --no-cache-dir gunicorn | |
# Copy the rest of the application code | |
COPY --chown=user:user . . | |
# Ensure output directory exists and has correct permissions (redundant but safe) | |
RUN mkdir -p /app/docker_mineru/output/images && \ | |
chown -R user:user /app/docker_mineru/output | |
# Create marker static directory and set proper permissions (fix for font download error) | |
RUN mkdir -p /usr/local/lib/python3.10/dist-packages/static && \ | |
chmod -R 777 /usr/local/lib/python3.10/dist-packages/static | |
# Set the user | |
USER user | |
# Environment variables for caching (optional, might help with model downloads) | |
ENV HF_HOME=/home/user/.cache/huggingface | |
ENV TORCH_HOME=/home/user/.cache/torch | |
# Add environment variable for marker font path (alternative fix) | |
ENV MARKER_FONT_PATH=/home/user/.cache/marker_fonts | |
# Add PyTorch memory optimization environment variables | |
ENV PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True,max_split_size_mb:128 | |
ENV CUDA_VISIBLE_DEVICES=0 | |
# Expose the port | |
EXPOSE 7860 | |
# Command to run the application with Gunicorn and Uvicorn workers | |
# Reduced workers to 4 (from 16) to avoid OOM errors | |
CMD ["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "app.main:app", "--bind", "0.0.0.0:7860"] |