Spaces:
Sleeping
Sleeping
Delete README.md
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213---title: MinerU OCR Serviceemoji: πcolorFrom: bluecolorTo: greensdk: dockerapp_port: 7860pinned: false---
# MinerU OCR & Document Extraction Service
Production-quality OCR and document extraction API powered by **MinerU** (`magic-pdf` package), running on Hugging Face Docker Spaces β free CPU tier.
---
## Complete Forensic Audit β All Root Causes Found and Fixed
### Root Cause 1 β `[full-cpu]` is NOT a valid extra (primary failure)
```OCR_PIPELINE_FAILED: No module named 'ultralytics'```
`magic-pdf[full-cpu]` does not exist. When pip receives an unknown extra name it **silently installs only the base package**, omitting:- `ultralytics` (YOLO framework for layout detection)- `doclayout-yolo==0.0.2b1` (layout detection model)- `rapid-table>=1.0.5` (table extraction)- `shapely`, `pyclipper`, `omegaconf`, `albumentations`, `ftfy`, and other deps
The container booted without error because `torch` **is** in the base deps, but `doc_analyze` crashed at the first layout detection call.
**Fix:** `magic-pdf[full]==1.3.12` (not `[full-cpu]`).
### Root Cause 2 β OpenCV non-headless conflict
`doclayout-yolo==0.0.2b1`, `ultralytics>=8.3.48`, and `rapid-table>=1.0.5` all declare `opencv-python` (non-headless) as a runtime dep. After Layer 3, pip has installed the full X11-linked `opencv-python` build, overwriting the headless cv2 module installed in Layer 1.
**Fix:** Layer 4 force-reinstalls `opencv-python-headless>=4.8.0` after all other packages. This overwrites the cv2 shared object with the headless build, which works on this slim container without X11 libs. pip-check shows "ultralytics requires opencv-python" warnings but they are harmless β both packages expose an identical cv2 API.
---
## Why `[lite]` / paddlepaddle / paddleocr Are NOT Installed
| Package | Status | Reason ||---|---|---|| `paddlepaddle==3.0.0b1` | β Not on PyPI | The `[lite]` extra pins this exact beta version. Available on PyPI: 2.6.2, 3.0.0, 3.1.x+ β NOT 3.0.0b1. || `paddleocr==2.7.3` | β Incompatible | Requires `opencv-python <=4.6.0.66`. Conflicts with `ultralytics>=4.6.0` and `doclayout-yolo>=4.6.0`. || PaddleOCR (any) | β Not needed | The pipeline (full) backend never uses PaddleOCR. See below. |
### The Actual OCR Engine: `paddleocr2pytorch`
The pipeline backend uses **`paddleocr2pytorch`** β a self-contained PyTorch reimplementation of the PaddleOCR architecture, **bundled directly inside the `magic-pdf` wheel** at:
```magic_pdf/model/sub_modules/ocr/paddleocr2pytorch/```
It is initialized via `PytorchPaddleOCR` in `model_init.py`. It requires only:- `torch` (CPU, already installed in Layer 2)- `cv2`, `numpy`, `pyclipper`, `shapely`, `yaml` (all in `magic-pdf[full]`)
`pp_structure_v2.py` (which imports `paddleocr`) is only loaded when `model_config.__model_mode__ == 'lite'`. The pipeline backend always uses `full` mode (`CustomPEKModel`). That file is **never imported at runtime**.
---
## Valid `magic-pdf` 1.3.12 Extras
| Extra | Provides ||---|---|| `[full]` | `ultralytics>=8.3.48`, `doclayout-yolo==0.0.2b1`, `rapid-table`, `shapely`, `pyclipper`, `omegaconf`, `matplotlib`, `ftfy`, `dill`, `PyYAML`, `openai`, `albumentations` || `[full_old_linux]` | Same as `[full]` but with tighter version pins for older Linux || `[lite]` | `paddleocr==2.7.3`, `paddlepaddle==3.0.0b1` (Linux) β **unusable**: `paddlepaddle==3.0.0b1` not on PyPI || ~~`[full-cpu]`~~ | **Does not exist** β pip silently ignores and installs base package only |
---
## Dockerfile Layer Strategy
```Layer 1: fastapi, uvicorn, pillow, pillow-heif, huggingface_hub opencv-python-headless (placeholder β overridden in Layer 4) βLayer 2: torch + torchvision from download.pytorch.org/whl/cpu (CPU-only) CPU wheel locked in β pip skips ~2.5 GB CUDA wheel from PyPI βLayer 3: magic-pdf[full]==1.3.12 --extra-index-url myhloli [full] β ultralytics, doclayout-yolo 0.0.2b1, rapid-table, shapely, pyclipper, omegaconf, matplotlib, ftfy, dill onnxruntime pulled automatically as transitive dep of rapid-table opencv-python (non-headless) also pulled as transitive dep (overridden in Layer 4) βLayer 4: pip install --force-reinstall opencv-python-headless>=4.8.0 Headless cv2 wins β container works without X11 libs```
Note: `--extra-index-url https://myhloli.github.io/wheels/` is required because `doclayout-yolo==0.0.2b1` only exists on the myhloli wheel server, not on PyPI.
---
## Previous Bug History
| Cause | Detail | Fix ||---|---|---|| `No module named magic_pdf` | Wrong install command | Fixed || `stat: path should be string ... not list` | Old `read_local_images([path])` API (removed in 1.x) | Fixed β use `FileBasedDataReader` || `No module named cv2` | `opencv-python-headless` missing | Fixed β Layer 1 || `No module named 'ultralytics'` | `[full-cpu]` is not a valid extra | Fixed β Layer 3 uses `[full]` || Memory showing `105 GB / 123 GB` | `/proc/meminfo` showing host RAM inside container | Fixed β cgroup-aware memory logic || `libreoffice` in apt-get | 1.5 GB; caused OOM / build timeout | Removed || X11 libs (`libsm6`, `libxrender-dev`) | Added 80 MB, not needed for headless cv2 | Removed || `python-magic` / `libmagic1` | Listed but never imported | Removed |
---
## Supported File Types
| Category | Extensions ||---|---|| PDF | `.pdf` (searchable, scanned, mixed) || Images | `.jpg` `.jpeg` `.png` `.webp` `.bmp` `.tiff` `.tif` `.gif` `.heic` `.heif` `.avif` |
---
## API Endpoints
### `GET /health````json{ "status": "healthy" }```
### `GET /status````json{ "status": "healthy", "provider": "mineru", "version": "1.3.12", "modelsLoaded": true, "uptimeSeconds": 3742, "memoryUsedMB": 5200, "memoryTotalMB": 16384, "activeRequests": 0, "cacheEntries": 3}```
### `POST /extract````json{ "success": true, "filename": "invoice.jpg", "docType": "invoice", "pageCount": 1, "confidence": 0.85, "markdown": "# Invoice\n\n...", "metadata": { "parseMethod": "ocr", "backend": "pipeline", "cached": false, "processingTimeMs": 4200, "docTypeClassification": "invoice", "imageCount": 0, "tableCount": 1, "formulaCount": 0 }}```
### `POST /batch````json{ "success": true, "processed": 3, "results": [...]}```
---
## Deployment
1. Create a new Hugging Face Space β **Docker** SDK2. Upload all files from this directory to the Space repo root3. Space builds automatically (~25β35 min for first build; code-only rebuilds are faster due to Docker layer cache)4. Test with curl:
```bashcurl https://<your-space>.hf.space/healthcurl -X POST https://<your-space>.hf.space/extract -F "file=@photo.jpg"curl -X POST https://<your-space>.hf.space/batch \ -F "files=@a.pdf" -F "files=@b.jpg" -F "files=@c.png"```
---
## Resource Budget (free CPU Basic)
| Resource | Budget | Usage ||---|---|---|| Disk | 50 GB | ~10β13 GB (packages ~5 GB + models ~5 GB) || RAM | 16 GB | ~6β8 GB at load; peaks ~10β12 GB during extraction || vCPU | 2 | Single uvicorn worker; sequential batch processing |
---
## Known Limitations
1. **No DOCX/PPTX/XLSX** β dropped to make the build reliable. Re-add with LibreOffice on paid hardware.2. **Formula OCR disabled** β MFR models excluded to save ~1-2 GB. Set `"enable": true` in `~/magic-pdf.json` and re-download MFR models to enable.3. **First-request latency** β models lazy-load into RAM on the first request (~30β60 s).4. **30 MB file size limit** β enforced on both `/extract` and `/batch`.5. **In-process cache** β SHA256 cache lives in RAM, cleared on container restart.6. **HEIC/HEIF** β requires `pillow-heif` (included); may fail on unusual encoder variants.README.mdDeleted-213