File size: 2,991 Bytes
211e423
 
80e4bb4
 
 
 
 
ab66d7a
0a7e5ec
211e423
 
 
 
 
1cd3063
 
211e423
e300623
0a7e5ec
3f0aec5
47aca17
0a7e5ec
 
 
47aca17
0a7e5ec
6a42f7a
 
 
 
 
 
 
 
 
 
 
0a7e5ec
 
e300623
 
 
1cd3063
 
 
 
e300623
 
 
 
 
 
 
 
 
 
80e4bb4
e300623
 
0a7e5ec
e300623
0a7e5ec
 
211e423
 
 
80e4bb4
6735521
80e4bb4
6735521
 
 
 
 
 
ab66d7a
6735521
80e4bb4
 
 
 
211e423
 
 
 
 
 
0a7e5ec
 
 
 
 
211e423
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
82
83
84
85
86
87
88
89
90
91
92
93
94
ARG BASE_IMAGE=pytorch/pytorch:2.7.0-cuda12.6-cudnn9-runtime
FROM ${BASE_IMAGE}

# Build args to optionally enable flash-attn installation and override wheel URL
# Enable by default for Hugging Face Spaces GPU builds; override locally with
#   --build-arg INSTALL_FLASH_ATTN=false
ARG INSTALL_FLASH_ATTN=true
ARG FLASH_ATTN_WHEEL_URL=https://github.com/mjun0812/flash-attention-prebuild-wheels/releases/download/v0.0.8/flash_attn-2.7.4.post1+cu126torch2.7-cp311-cp311-linux_x86_64.whl

# Persist caches and model storage in Spaces, and enable fast transfers
ENV HF_HUB_ENABLE_HF_TRANSFER=1 \
    HUGGINGFACE_HUB_CACHE=/data/.cache/huggingface \
    HF_HOME=/data/.cache/huggingface \
    DOTS_OCR_LOCAL_DIR=/data/models/dots-ocr \
    PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb=512 \
    OMP_NUM_THREADS=1

# Install system dependencies as root
RUN apt-get update && apt-get install -y \
    libgl1-mesa-dri \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender1 \
    libgomp1 \
    libgtk-3-0 \
    libavcodec-dev \
    libavformat-dev \
    libswscale-dev \
    libv4l-dev \
    libxvidcore-dev \
    libx264-dev \
    libjpeg-dev \
    libpng-dev \
    libtiff-dev \
    python3-dev \
    && rm -rf /var/lib/apt/lists/*

# Set up a new user named "user" with user ID 1000
RUN useradd -m -u 1000 user

# Create persistent data directories and grant write access to the user
RUN mkdir -p /data/.cache/huggingface /data/models/dots-ocr && \
    chown -R 1000:1000 /data

# Switch to the "user" user
USER user

# Set home to the user's home directory
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

# Set the working directory to the user's home directory
WORKDIR $HOME/app

# Upgrade pip in the CUDA-enabled base image
RUN pip install --no-cache-dir --upgrade pip

# Copy requirements and install Python dependencies
COPY --chown=user requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy pyproject.toml for package installation
COPY --chown=user pyproject.toml .

# Optionally install flash-attn wheel (requires Python/torch/CUDA compatibility)
# Will auto-skip if the wheel's Python tag does not match this image's Python.
RUN if [ "$INSTALL_FLASH_ATTN" = "true" ]; then \
      PYTAG=$(python -c "import sys; print(f'cp{sys.version_info.major}{sys.version_info.minor}')"); \
      echo "Detected Python tag: $PYTAG"; \
      if echo "$FLASH_ATTN_WHEEL_URL" | grep -q "$PYTAG"; then \
        echo "Installing flash-attn from $FLASH_ATTN_WHEEL_URL" && \
        pip install --no-cache-dir "$FLASH_ATTN_WHEEL_URL"; \
      else \
        echo "flash-attn wheel tag mismatch for $PYTAG. Skipping flash-attn install."; \
      fi; \
    else \
      echo "Skipping flash-attn installation"; \
    fi

# Copy source code
COPY --chown=user src/ ./src/
COPY --chown=user main.py .

# Install the package in development mode
RUN pip install --no-cache-dir -e .

# Expose port
EXPOSE 7860

# Run the application
CMD ["python", "main.py"]