marcosremar2 commited on
Commit
eaa0dd6
·
1 Parent(s): ff60d55

Update Dockerfile and requirements for better installation of magic-pdf

Browse files
Files changed (2) hide show
  1. Dockerfile +38 -104
  2. requirements.txt +6 -8
Dockerfile CHANGED
@@ -1,120 +1,54 @@
1
- FROM nvidia/cuda:12.1.0-base-ubuntu22.04
2
-
3
- # Set environment variables
4
- ENV DEBIAN_FRONTEND=noninteractive
5
- ENV PYTHONUNBUFFERED=1
6
 
7
  # Install system dependencies
8
- RUN apt-get update && apt-get install -y \
9
- python3 \
10
- python3-pip \
11
- python3-venv \
12
- python3-dev \
13
- wget \
14
- git \
15
  build-essential \
 
16
  libgl1-mesa-glx \
17
  libglib2.0-0 \
18
- imagemagick \
19
- ghostscript \
20
- poppler-utils \
21
- libmagickwand-dev \
22
- fonts-freefont-ttf \
23
- ffmpeg \
24
- libsm6 \
25
- libxext6 \
26
- libxrender-dev \
27
- pkg-config \
28
- libcairo2-dev \
29
- sudo \
30
  && rm -rf /var/lib/apt/lists/*
31
 
32
- # Configure ImageMagick policy to allow PDF conversion (needed for sample PDF creation)
33
- RUN if [ -f "/etc/ImageMagick-6/policy.xml" ]; then \
34
- sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/g' /etc/ImageMagick-6/policy.xml; \
35
- fi
36
-
37
- # Create a virtual environment
38
- RUN python3 -m venv /opt/mineru_venv
39
- ENV PATH="/opt/mineru_venv/bin:$PATH"
40
-
41
- # Upgrade pip in the virtual environment
42
- RUN pip install --upgrade pip
43
-
44
- # Clone the MinerU repository
45
- RUN git clone https://github.com/opendatalab/MinerU.git /tmp/MinerU
46
-
47
- # Install required packages
48
- RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
49
-
50
- # Install MinerU with all features
51
- WORKDIR /tmp/MinerU
52
- RUN pip install --no-cache-dir -e ".[full]"
53
-
54
- # Install additional dependencies for the web application
55
- RUN pip install --no-cache-dir flask==2.3.3 flask-cors==4.0.0 werkzeug==2.3.7
56
-
57
- # Create a non-root user for Hugging Face Spaces
58
- RUN useradd -m -u 1000 user && \
59
- echo "user ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/user && \
60
- chmod 0440 /etc/sudoers.d/user
61
-
62
- # Create directories with proper permissions
63
- RUN mkdir -p /tmp/pdf_uploads /tmp/pdf_output && \
64
- chmod -R 777 /tmp/pdf_uploads && \
65
- chmod -R 777 /tmp/pdf_output && \
66
- mkdir -p /tmp/samples && \
67
- chmod -R 777 /tmp/samples
68
-
69
- # Create models directory structure with proper permissions
70
- RUN mkdir -p /tmp/models/MFD/YOLO && \
71
- mkdir -p /tmp/models/MFR/unimernet && \
72
- mkdir -p /tmp/models/table/rapid && \
73
- mkdir -p /tmp/models/layout/doclayout && \
74
- chmod -R 777 /tmp/models
75
 
76
- # Download model weights
77
- WORKDIR /tmp/models
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- # Download the YOLO model for formula detection (using curl with progress and retry)
80
- RUN apt-get update && apt-get install -y curl && \
81
- curl -L --retry 5 --retry-delay 5 -o /tmp/models/MFD/YOLO/yolo_v8_ft.pt https://huggingface.co/opendatalab/MinerU/resolve/main/models/mfd/yolo_v8_mfd.pt && \
82
- curl -L --retry 5 --retry-delay 5 -o /tmp/models/MFD/YOLO/yolo_v8_mfd.pt https://huggingface.co/opendatalab/MinerU/resolve/main/models/mfd/yolo_v8_mfd.pt && \
83
- curl -L --retry 5 --retry-delay 5 -o /tmp/models/MFR/unimernet/unimernet_small.pth https://huggingface.co/opendatalab/MinerU/resolve/main/models/mfr/unimernet_small.pth && \
84
- curl -L --retry 5 --retry-delay 5 -o /tmp/models/table/rapid/rapid_table.pt https://huggingface.co/opendatalab/MinerU/resolve/main/models/table/rapid_table.pt && \
85
- curl -L --retry 5 --retry-delay 5 -o /tmp/models/table/rapid/slanet_plus.pt https://huggingface.co/opendatalab/MinerU/resolve/main/models/table/slanet_plus.pt && \
86
- curl -L --retry 5 --retry-delay 5 -o /tmp/models/layout/doclayout/doclayout_yolo.pt https://huggingface.co/opendatalab/MinerU/resolve/main/models/layout/doclayout_yolo.pt && \
87
- ls -la /tmp/models/MFD/YOLO/ && \
88
- ls -la /tmp/models/MFR/unimernet/ && \
89
- ls -la /tmp/models/table/rapid/ && \
90
- ls -la /tmp/models/layout/doclayout/
91
 
92
- # Verify file sizes (files should not be empty)
93
- RUN find /tmp/models -type f -size 0 -exec echo "Warning: Empty file {}" \;
 
94
 
95
- # Create app directory
96
- RUN mkdir -p /app/samples && chown -R user:user /app
97
 
98
- # Copy the application files
99
- WORKDIR /app
100
- COPY . /app/
101
 
102
- # Fix permissions for the user
103
- RUN chown -R user:user /app && \
104
- mkdir -p /home/user/.config/magic_pdf && \
105
- chown -R user:user /home/user/.config && \
106
- chown -R user:user /tmp/pdf_uploads && \
107
- chown -R user:user /tmp/pdf_output && \
108
- chown -R user:user /tmp/models
109
 
110
- # Expose the port
111
  EXPOSE 7860
112
 
113
- # Set up entrypoint
114
- RUN chmod +x /app/entrypoint.sh
115
-
116
- # Switch to non-root user for running the app
117
- USER user
118
-
119
- # Start the application
120
- CMD ["/app/entrypoint.sh"]
 
1
+ FROM python:3.10-slim
 
 
 
 
2
 
3
  # Install system dependencies
4
+ RUN apt-get update && \
5
+ apt-get install -y --no-install-recommends \
 
 
 
 
 
6
  build-essential \
7
+ curl \
8
  libgl1-mesa-glx \
9
  libglib2.0-0 \
10
+ wget \
11
+ git \
 
 
 
 
 
 
 
 
 
 
12
  && rm -rf /var/lib/apt/lists/*
13
 
14
+ # Set working directory
15
+ WORKDIR /app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Set environment variables
18
+ ENV PYTHONUNBUFFERED=1 \
19
+ PYTHONDONTWRITEBYTECODE=1 \
20
+ UPLOAD_FOLDER=/tmp/pdf_uploads \
21
+ OUTPUT_FOLDER=/tmp/pdf_output
22
+
23
+ # Create necessary directories and set permissions
24
+ RUN mkdir -p /tmp/pdf_uploads /tmp/pdf_output /tmp/models \
25
+ && chmod -R 777 /tmp/pdf_uploads /tmp/pdf_output /tmp/models
26
+
27
+ # Copy requirements and app files
28
+ COPY requirements.txt /app/
29
+ COPY *.py /app/
30
+ COPY entrypoint.sh /app/
31
+ RUN chmod +x /app/entrypoint.sh
32
 
33
+ # Create a non-root user and change ownership
34
+ RUN useradd -m -u 1000 appuser && \
35
+ chown -R appuser:appuser /app /tmp/pdf_uploads /tmp/pdf_output /tmp/models
 
 
 
 
 
 
 
 
 
36
 
37
+ # Install Python dependencies
38
+ RUN pip install --no-cache-dir -r requirements.txt && \
39
+ pip install --no-cache-dir pymupdf
40
 
41
+ # Switch to non-root user
42
+ USER appuser
43
 
44
+ # Install MinerU and PyMuPDF (fallback)
45
+ RUN pip install --no-cache-dir --user minerupdf
 
46
 
47
+ # Test the installation
48
+ RUN ~/.local/bin/magic-pdf --version || echo "magic-pdf not working, will fallback to PyMuPDF"
 
 
 
 
 
49
 
50
+ # Expose port
51
  EXPOSE 7860
52
 
53
+ # Set the entrypoint
54
+ ENTRYPOINT ["/app/entrypoint.sh"]
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,10 +1,8 @@
1
  flask==2.3.3
2
- werkzeug==2.3.7
3
  flask-cors==4.0.0
4
- requests>=2.31.0
5
- pillow>=9.4.0
6
- numpy>=1.24.0
7
- wget>=3.2
8
- magic-pdf[full]>=1.3.0
9
- uuid>=1.30
10
- python-magic>=0.4.27
 
1
  flask==2.3.3
 
2
  flask-cors==4.0.0
3
+ werkzeug==2.3.7
4
+ Pillow>=9.0.0
5
+ numpy>=1.20.0
6
+ requests>=2.25.0
7
+ pymupdf>=1.20.0
8
+ minerupdf