crazylemonade commited on
Commit
692af63
·
verified ·
1 Parent(s): 96388fe

Delete Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +0 -265
Dockerfile DELETED
@@ -1,265 +0,0 @@
1
- # ─────────────────────────────────────────────────────────────────────────────
2
- # MinerU OCR Service — Hugging Face Docker Space (CPU / pipeline backend)
3
- #
4
- # Optimized for FREE tier: 2 vCPU · 16 GB RAM · 50 GB Disk · No GPU
5
- #
6
- # ── FORENSIC ROOT CAUSE ANALYSIS ─────────────────────────────────────────────
7
- #
8
- # FAILURE 1: "No module named 'ultralytics'"
9
- #
10
- # ROOT CAUSE 1 — [full-cpu] is NOT a valid extra in magic-pdf 1.3.12
11
- # Valid extras: [full], [full_old_linux], [lite]
12
- # When pip receives an unknown extra name it silently installs only the BASE
13
- # package — omitting ultralytics, doclayout-yolo, rapid-table, and everything
14
- # else needed for layout detection. Container booted because torch IS in the
15
- # base deps, but doc_analyze crashed at the first layout detection call.
16
- #
17
- # ROOT CAUSE 2 — opencv conflict
18
- # doclayout-yolo==0.0.2b1, ultralytics>=8.3.48, and rapid-table>=1.0.5 all
19
- # declare opencv-python (non-headless) as a runtime dep. pip resolves and
20
- # installs the full X11-linked opencv-python, which overwrites the cv2 module
21
- # from our earlier opencv-python-headless install.
22
- # Fix: force-reinstall opencv-python-headless as the LAST pip step so the
23
- # headless cv2 module wins. Runtime import works; pip-check shows warnings
24
- # but they are harmless — both packages expose an identical cv2 API.
25
- #
26
- # WHY [lite] / paddlepaddle / paddleocr ARE NOT INSTALLED
27
- # 1. paddlepaddle==3.0.0b1 (pinned by [lite]) does not exist on PyPI.
28
- # Install would fail at the paddlepaddle resolution step.
29
- # 2. paddleocr==2.7.3 requires opencv-python <=4.6.0.66, conflicting with
30
- # ultralytics>=4.6.0 and doclayout-yolo>=4.6.0.
31
- # 3. The pipeline backend NEVER uses paddlepaddle. The OCR engine for the
32
- # pipeline (full) mode is paddleocr2pytorch — a self-contained PyTorch
33
- # reimplementation of the PaddleOCR architecture bundled inside the
34
- # magic-pdf wheel itself. No paddlepaddle C library is required.
35
- # Confirmed by reading magic_pdf/model/sub_modules/model_init.py:
36
- # ocr_model = PytorchPaddleOCR(...) # pure torch, NOT from paddleocr pkg
37
- # pp_structure_v2.py (which imports paddleocr) is only loaded in 'lite'
38
- # model mode, which is never used by MINERU_BACKEND=pipeline.
39
- #
40
- # ── FAILURE 3: OCR MODEL PATH MISMATCH (2025-06) ─────────────────────────────
41
- #
42
- # ERROR: "No such file or directory:
43
- # /app/models/PDF-Extract-Kit-1.0/models/OCR/paddleocr_torch/ch_PP-OCRv3_det_infer.pth"
44
- #
45
- # ROOT CAUSE:
46
- # magic-pdf 1.3.12 ships models_config.yml that references OCR weight files
47
- # which do NOT exist in the opendatalab/PDF-Extract-Kit-1.0 HF repo.
48
- # The HF repo was updated to v5 det models but the package config still
49
- # references v3 det models.
50
- #
51
- # FORENSIC PROOF (read from wheel magic_pdf-1.3.12-py3-none-any.whl):
52
- # models_config.yml says (for ch_lite, ch, ch_server, ch_server_v4, ch_lite_v4):
53
- # det: ch_PP-OCRv3_det_infer.pth ← NOT present in HF repo
54
- # models_config.yml says (for en, latin):
55
- # det: en_PP-OCRv3_det_infer.pth ← NOT present in HF repo
56
- #
57
- # HF repo (opendatalab/PDF-Extract-Kit-1.0 models/OCR/paddleocr_torch/) has:
58
- # ch_PP-OCRv5_det_infer.pth ← present, v5 arch in arch_config.yaml
59
- # Multilingual_PP-OCRv3_det_infer.pth ← present, identical arch to en v3 det
60
- #
61
- # PATH RESOLUTION (read from pytorch_paddle.py):
62
- # ocr_models_dir = os.path.join(get_local_models_dir(), 'OCR', 'paddleocr_torch')
63
- # det_model_path = os.path.join(ocr_models_dir, det_filename)
64
- # → /app/models/PDF-Extract-Kit-1.0/models/OCR/paddleocr_torch/<filename>
65
- #
66
- # ARCH LOOKUP (read from pytorchocr_utility.py):
67
- # get_arch_config(model_path) uses Path(model_path).stem as the key into
68
- # arch_config.yaml (bundled inside magic-pdf wheel).
69
- # Confirmed: 'ch_PP-OCRv5_det_infer' IS in arch_config.yaml (DB algorithm,
70
- # PPLCNetV3 backbone, RSEFPN neck — same algorithm family as v3).
71
- # Confirmed: 'Multilingual_PP-OCRv3_det_infer' IS in arch_config.yaml
72
- # (DB algorithm, MobileNetV3 — same arch as en_PP-OCRv3_det_infer).
73
- #
74
- # CPU DEFAULT PATH (read from pytorch_paddle.py):
75
- # self.lang = kwargs.get('lang', 'ch') # default when no lang given
76
- # if device == 'cpu' and lang in ['ch', 'ch_server']:
77
- # self.lang = 'ch_lite' # CPU forces ch_lite
78
- # ch_lite.det = ch_PP-OCRv3_det_infer.pth ← MISSING → patched to v5
79
- # ch_lite.rec = ch_PP-OCRv5_rec_infer.pth ← present in HF repo ✓
80
- #
81
- # FIX: Layer 3.5 patches the installed models_config.yml in-place, updating
82
- # every det filename that references a file absent from the HF repo to one
83
- # that is present AND has a matching arch entry in arch_config.yaml.
84
- #
85
- # ADDITIONAL FORENSIC FINDING — slanet-plus.onnx:
86
- # magic_pdf/resources/slanet_plus/slanet-plus.onnx is BUNDLED inside the
87
- # magic-pdf wheel itself. rapid_table.py reads it from the package resources
88
- # directory when table_sub_model_name is set. No separate HF download needed.
89
- #
90
- # COMPLETE RUNTIME DEPENDENCY GRAPH (pipeline backend, CPU)
91
- # magic-pdf base: torch, torchvision, PyMuPDF, numpy, pydantic, loguru, ...
92
- # magic-pdf[full]: ultralytics, doclayout-yolo==0.0.2b1, rapid-table,
93
- # shapely, pyclipper, omegaconf, matplotlib, ftfy, dill,
94
- # PyYAML, openai, albumentations
95
- # rapid-table: onnxruntime (auto-installed as transitive dep)
96
- # opencv: opencv-python pulled by doclayout-yolo/ultralytics/rapid-table;
97
- # overridden by force-reinstall of opencv-python-headless
98
- # paddleocr2pytorch (bundled in magic-pdf wheel):
99
- # torch, numpy, cv2, pyclipper, shapely, yaml — all covered
100
- #
101
- # ── System packages ────────────────────────────────────────────────────────────
102
- # libgl1 — OpenCV needs libGL.so.1 for ALL image operations (not just GUI).
103
- # libglib2.0-0 — GLib; required by OpenCV and many C extensions.
104
- # libgomp1 — OpenMP; required by ONNX Runtime and YOLO inference.
105
- # poppler-utils — pdfinfo/pdftoppm; used by MinerU PDF pre-processing.
106
- # ─────────────────────────────────────────────────────────────────────────────
107
-
108
- FROM python:3.10-slim
109
-
110
- ENV PYTHONUNBUFFERED=1
111
- ENV PYTHONDONTWRITEBYTECODE=1
112
- ENV PORT=7860
113
- ENV MINERU_DEVICE_MODE=cpu
114
- ENV MINERU_BACKEND=pipeline
115
-
116
- # ── System dependencies ────────────────────────────────────────────────────────
117
- RUN apt-get update \
118
- && apt-get install -y --no-install-recommends \
119
- libgl1 \
120
- libglib2.0-0 \
121
- libgomp1 \
122
- poppler-utils \
123
- && rm -rf /var/lib/apt/lists/*
124
-
125
- WORKDIR /app
126
-
127
- # ── Layer 1: FastAPI + lightweight runtime deps ───────────────────────────────
128
- # Install opencv-python-headless here as a placeholder. It will be overridden
129
- # in Layer 4 after magic-pdf[full] installs the non-headless opencv-python via
130
- # its transitive deps (doclayout-yolo, ultralytics, rapid-table).
131
- # The force-reinstall in Layer 4 restores the headless build as the winner.
132
- RUN pip install --no-cache-dir --timeout 300 \
133
- "fastapi>=0.115.0" \
134
- "uvicorn[standard]>=0.32.0" \
135
- "python-multipart>=0.0.12" \
136
- "Pillow>=10.0.0" \
137
- "pillow-heif>=0.18.0" \
138
- "huggingface_hub>=0.25.0" \
139
- "opencv-python-headless>=4.8.0"
140
-
141
- # ── Layer 2: CPU-only PyTorch — MUST precede magic-pdf ───────────────────────
142
- # PyPI serves the CUDA-enabled torch wheel by default (~2.5 GB).
143
- # Installing from the official CPU wheel index first causes pip to treat the
144
- # already-installed CPU build as satisfying magic-pdf's `torch` requirement,
145
- # skipping the larger CUDA wheel. Saves ~2.5 GB of image size.
146
- RUN pip install --no-cache-dir --timeout 600 \
147
- --index-url https://download.pytorch.org/whl/cpu \
148
- "torch>=2.2.2,!=2.5.0,!=2.5.1,<3" \
149
- "torchvision>=0.15.2"
150
-
151
- # ── Layer 3: magic-pdf with the CORRECT extras ────────────────────────────────
152
- # [full] provides:
153
- # - ultralytics >=8.3.48 YOLO framework (required by doclayout-yolo)
154
- # - doclayout-yolo ==0.0.2b1 layout detection (on myhloli index only)
155
- # - rapid-table >=1.0.5 table structure detection
156
- # - shapely, pyclipper geometry; used by paddleocr2pytorch OCR
157
- # - omegaconf, matplotlib, ftfy, dill, PyYAML, openai, albumentations
158
- #
159
- # Note: doclayout-yolo==0.0.2b1 is only on the myhloli custom index, not PyPI.
160
- # --extra-index-url must be present in this layer.
161
- #
162
- # Note: rapid-table declares onnxruntime>1.17.0 as a required dep; pip will
163
- # resolve and install it automatically here — no explicit pin needed.
164
- #
165
- # Note: [lite] is intentionally NOT used.
166
- # - paddlepaddle==3.0.0b1 (pinned by [lite]) does not exist on PyPI.
167
- # - paddleocr==2.7.3 requires opencv-python <=4.6.0.66, incompatible with
168
- # ultralytics and doclayout-yolo (both require >=4.6.0).
169
- # - PaddleOCR is not needed: the pipeline backend uses paddleocr2pytorch,
170
- # a pure-PyTorch OCR engine bundled inside the magic-pdf wheel itself.
171
- RUN pip install --no-cache-dir --timeout 600 \
172
- --extra-index-url https://myhloli.github.io/wheels/ \
173
- "magic-pdf[full]==1.3.12"
174
-
175
- # ── Layer 3.5: Patch OCR model config ────────���───────────────────────────────
176
- # PROBLEM: models_config.yml in magic-pdf 1.3.12 references OCR det weight
177
- # files that do NOT exist in the opendatalab/PDF-Extract-Kit-1.0 HF repo.
178
- # The repo was updated to v5 det models; the package config still says v3.
179
- #
180
- # Missing files: ch_PP-OCRv3_det_infer.pth (all ch* langs)
181
- # en_PP-OCRv3_det_infer.pth (en, latin langs)
182
- # Replacements: ch_PP-OCRv5_det_infer.pth (same arch family, in repo)
183
- # Multilingual_PP-OCRv3_det_infer.pth (same arch as en v3, in repo)
184
- #
185
- # Safety proof: pytorchocr_utility.py:get_arch_config() uses Path(model_path).stem
186
- # as the lookup key in arch_config.yaml. Both replacement stems ARE defined in
187
- # arch_config.yaml (bundled inside the magic-pdf wheel).
188
- #
189
- # This patch runs BEFORE download_models.py so the downloaded file list and the
190
- # expected file list stay in sync.
191
- RUN python3 - <<'PYEOF'
192
- import sys, yaml
193
- from pathlib import Path
194
- import magic_pdf
195
-
196
- pkg = Path(magic_pdf.__file__).parent
197
- cfg_path = pkg / 'model/sub_modules/ocr/paddleocr2pytorch/pytorchocr/utils/resources/models_config.yml'
198
- arch_path = pkg / 'model/sub_modules/ocr/paddleocr2pytorch/pytorchocr/utils/resources/arch_config.yaml'
199
-
200
- print(f"Patching: {cfg_path}")
201
-
202
- with open(cfg_path) as f:
203
- config = yaml.safe_load(f)
204
- with open(arch_path) as f:
205
- arch_text = f.read()
206
-
207
- # Maps: filename that models_config.yml references → filename actually in HF repo
208
- # Only det files need patching; rec files are already present in the repo.
209
- DET_MAP = {
210
- 'ch_PP-OCRv3_det_infer.pth': 'ch_PP-OCRv5_det_infer.pth',
211
- 'en_PP-OCRv3_det_infer.pth': 'Multilingual_PP-OCRv3_det_infer.pth',
212
- }
213
-
214
- patched = 0
215
- for lang, files in config['lang'].items():
216
- old = files.get('det', '')
217
- if old in DET_MAP:
218
- new = DET_MAP[old]
219
- arch_key = new[:-4] # strip .pth to get arch_config key
220
- if (arch_key + ':') not in arch_text:
221
- print(f"ERROR: arch key '{arch_key}' not found in arch_config.yaml", file=sys.stderr)
222
- sys.exit(1)
223
- files['det'] = new
224
- print(f" [{lang}] det: {old} -> {new}")
225
- patched += 1
226
-
227
- with open(cfg_path, 'w') as f:
228
- yaml.dump(config, f, default_flow_style=False, allow_unicode=True)
229
-
230
- print(f"Patched {patched} language entries. models_config.yml updated.")
231
- PYEOF
232
-
233
- # ── Layer 4: Restore headless OpenCV ─────────────────────────────────────────
234
- # Layer 3 pulled in opencv-python (non-headless) via doclayout-yolo, ultralytics,
235
- # and rapid-table — all of which declare `opencv-python` as a required dep.
236
- # The full opencv-python build links against libSM, libXext, libXrender which
237
- # are NOT installed (removed to reduce image size) and would cause ImportError.
238
- # Force-reinstalling opencv-python-headless replaces the cv2 shared object with
239
- # the headless build, which works on this slim container without X11 libs.
240
- # pip-check will show "ultralytics requires opencv-python" warnings — these are
241
- # harmless because both packages expose an identical cv2 Python API.
242
- RUN pip install --no-cache-dir --timeout 300 \
243
- --force-reinstall \
244
- "opencv-python-headless>=4.8.0"
245
-
246
- # ── Application code ──────────────────────────────────────────────────────────
247
- COPY download_models.py .
248
- COPY validate.py .
249
- COPY main.py .
250
- COPY entrypoint.sh .
251
- RUN chmod +x entrypoint.sh
252
-
253
- # ── Download models at build time ─────────────────────────────────────────────
254
- # Baked into image = zero cold-start download delay.
255
- # Skip-if-exists logic in download_models.py gives Docker layer-cache reuse:
256
- # code-only rebuilds skip the 15-minute model download entirely.
257
- # MFR (formula recognition, ~1-2 GB) is excluded — disabled in config.
258
- RUN python download_models.py
259
-
260
- # Persist config; entrypoint.sh restores it if /root is wiped on restart.
261
- RUN mkdir -p /app/config && cp /root/magic-pdf.json /app/config/magic-pdf.json
262
-
263
- # ── Runtime ───────────────────────────────────────────────────────────────────
264
- EXPOSE 7860
265
- ENTRYPOINT ["/app/entrypoint.sh"]