File size: 2,689 Bytes
43b01f6
44df236
 
 
43b01f6
44df236
 
 
 
 
 
43b01f6
44df236
 
 
 
 
 
 
 
a49c5dc
 
 
44df236
 
 
 
5f5a1d2
 
 
 
a49c5dc
 
5f5a1d2
a49c5dc
5f5a1d2
a49c5dc
 
5f5a1d2
a49c5dc
43b01f6
a49c5dc
 
 
 
 
 
44df236
a49c5dc
 
 
0f77915
a49c5dc
 
 
 
 
 
 
41ee299
 
 
 
a49c5dc
 
dbf9b27
a49c5dc
 
 
41ee299
 
3c3eb16
 
 
8c58924
a49c5dc
e432715
 
a49c5dc
3c3eb16
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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"]