Spaces:
Sleeping
Sleeping
Delete README.md
Browse files
README.md
DELETED
|
@@ -1,267 +0,0 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: MinerU OCR Service
|
| 3 |
-
emoji: 📄
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo: green
|
| 6 |
-
sdk: docker
|
| 7 |
-
app_port: 7860
|
| 8 |
-
pinned: false
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
# MinerU OCR & Document Extraction Service — v2.0
|
| 12 |
-
|
| 13 |
-
Production OCR API with dual-path routing. Powered by **RapidOCR** (fast path) and **MinerU** (full pipeline), running on Hugging Face Docker Spaces — free CPU tier.
|
| 14 |
-
|
| 15 |
-
---
|
| 16 |
-
|
| 17 |
-
## Architecture
|
| 18 |
-
|
| 19 |
-
```
|
| 20 |
-
Image file (jpg/png/webp/bmp/heic/heif/avif)
|
| 21 |
-
│
|
| 22 |
-
▼
|
| 23 |
-
Path A — RapidOCR (target: 1–5 s)
|
| 24 |
-
ONNX inference, CPU-optimised
|
| 25 |
-
Models bundled in pip wheel (~50 MB), no separate download
|
| 26 |
-
│
|
| 27 |
-
├── confidence ≥ 0.65 → return result
|
| 28 |
-
│
|
| 29 |
-
└── confidence < 0.65 → MinerU fallback (Pass 2)
|
| 30 |
-
|
| 31 |
-
PDF file (or explicit escalation)
|
| 32 |
-
│
|
| 33 |
-
▼
|
| 34 |
-
Path B — MinerU pipeline (target: 5–20 s)
|
| 35 |
-
Layout detection (doclayout_yolo)
|
| 36 |
-
OCR (paddleocr2pytorch — PyTorch, bundled in magic-pdf wheel)
|
| 37 |
-
Markdown reconstruction
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
---
|
| 41 |
-
|
| 42 |
-
## Response Format (v2.0)
|
| 43 |
-
|
| 44 |
-
All successful responses include:
|
| 45 |
-
|
| 46 |
-
```json
|
| 47 |
-
{
|
| 48 |
-
"success": true,
|
| 49 |
-
"filename": "marksheet.jpg",
|
| 50 |
-
"docType": "marksheet",
|
| 51 |
-
"pageCount": 1,
|
| 52 |
-
"confidence": 0.92,
|
| 53 |
-
"ocrEngine": "rapidocr",
|
| 54 |
-
"markdown": "...",
|
| 55 |
-
"plainText": "...",
|
| 56 |
-
"entities": {
|
| 57 |
-
"studentName": "Rahul Sharma",
|
| 58 |
-
"rollNumber": "23456789",
|
| 59 |
-
"dob": "15/03/2008",
|
| 60 |
-
"subjects": {
|
| 61 |
-
"English": 93,
|
| 62 |
-
"Hindi": 95,
|
| 63 |
-
"Mathematics": 83,
|
| 64 |
-
"Science": 85,
|
| 65 |
-
"Sst": 77
|
| 66 |
-
},
|
| 67 |
-
"totalMarks": 433,
|
| 68 |
-
"maxMarks": 500,
|
| 69 |
-
"percentage": 86.6,
|
| 70 |
-
"overallGrade": "A"
|
| 71 |
-
},
|
| 72 |
-
"timings": {
|
| 73 |
-
"uploadMs": 12,
|
| 74 |
-
"decodeMs": 18,
|
| 75 |
-
"classifyMs": 3,
|
| 76 |
-
"ocrDetMs": 180,
|
| 77 |
-
"ocrRecMs": 620,
|
| 78 |
-
"ocrTotalMs": 820,
|
| 79 |
-
"markdownMs": 45,
|
| 80 |
-
"totalMs": 898
|
| 81 |
-
},
|
| 82 |
-
"metadata": {
|
| 83 |
-
"parseMethod": "fast_ocr",
|
| 84 |
-
"backend": "rapidocr",
|
| 85 |
-
"cached": false,
|
| 86 |
-
"processingTimeMs": 898,
|
| 87 |
-
"passesUsed": 1,
|
| 88 |
-
"docTypeClassification": "marksheet",
|
| 89 |
-
"textBlocks": 24,
|
| 90 |
-
"tableCount": 1
|
| 91 |
-
}
|
| 92 |
-
}
|
| 93 |
-
```
|
| 94 |
-
|
| 95 |
-
Error responses always include `errorCode`, `rootCause`, and `recommendation`:
|
| 96 |
-
|
| 97 |
-
```json
|
| 98 |
-
{
|
| 99 |
-
"success": false,
|
| 100 |
-
"stage": "ocr",
|
| 101 |
-
"errorCode": "OCR_ENGINE_FAILED",
|
| 102 |
-
"rootCause": "...",
|
| 103 |
-
"recommendation": "...",
|
| 104 |
-
"message": "..."
|
| 105 |
-
}
|
| 106 |
-
```
|
| 107 |
-
|
| 108 |
-
---
|
| 109 |
-
|
| 110 |
-
## Entity Extraction by Document Type
|
| 111 |
-
|
| 112 |
-
| Doc Type | Extracted Fields |
|
| 113 |
-
|---|---|
|
| 114 |
-
| `marksheet` | studentName, rollNumber, dob, school, year, subjects{}, totalMarks, maxMarks, percentage, overallGrade |
|
| 115 |
-
| `invoice` | invoiceNumber, invoiceDate, dueDate, billTo, totalAmount, gstin |
|
| 116 |
-
| `form` | name, dob, gender, phone, email |
|
| 117 |
-
| `certificate` | recipientName, certificateId, issueDate, courseOrEvent |
|
| 118 |
-
| `document` | key-value pairs (up to 20 fields, generic extraction) |
|
| 119 |
-
|
| 120 |
-
---
|
| 121 |
-
|
| 122 |
-
## Confidence Scoring
|
| 123 |
-
|
| 124 |
-
RapidOCR path — composite of three signals:
|
| 125 |
-
|
| 126 |
-
| Signal | Weight | Source |
|
| 127 |
-
|---|---|---|
|
| 128 |
-
| OCR engine block confidence | 40% | Mean of per-text-block scores from RapidOCR |
|
| 129 |
-
| Text density | 40% | Non-whitespace chars / total chars |
|
| 130 |
-
| Text coherence | 20% | Alpha chars / non-whitespace chars |
|
| 131 |
-
|
| 132 |
-
MinerU path — based on parse method + markdown quality:
|
| 133 |
-
- Base: `txt` mode = 0.95, `ocr` mode = 0.85
|
| 134 |
-
- Penalty: markdown < 100 chars → −0.15
|
| 135 |
-
- Bonus: markdown contains tables → +0.03
|
| 136 |
-
|
| 137 |
-
---
|
| 138 |
-
|
| 139 |
-
## Endpoints
|
| 140 |
-
|
| 141 |
-
| Method | Path | Purpose |
|
| 142 |
-
|---|---|---|
|
| 143 |
-
| `GET` | `/health` | Liveness — always 200 |
|
| 144 |
-
| `GET` | `/status` | Engine readiness, memory, cache, uptime |
|
| 145 |
-
| `GET` | `/warmup` | Pre-load both engines into RAM (call after deploy) |
|
| 146 |
-
| `GET` | `/diagnostics` | Model inventory, package versions, OCR config |
|
| 147 |
-
| `POST` | `/benchmark` | Benchmark RapidOCR on synthetic test image |
|
| 148 |
-
| `POST` | `/extract` | Single file extraction |
|
| 149 |
-
| `POST` | `/batch` | Up to 8 files, sequential |
|
| 150 |
-
|
| 151 |
-
---
|
| 152 |
-
|
| 153 |
-
## Performance Targets
|
| 154 |
-
|
| 155 |
-
| File type | Target | Acceptable | Unacceptable |
|
| 156 |
-
|---|---|---|---|
|
| 157 |
-
| Image (RapidOCR) | 1–5 s | < 10 s | > 15 s |
|
| 158 |
-
| PDF (MinerU, TXT) | 5–15 s | < 25 s | > 35 s |
|
| 159 |
-
| PDF (MinerU, scanned OCR) | 10–25 s | < 35 s | > 45 s |
|
| 160 |
-
|
| 161 |
-
---
|
| 162 |
-
|
| 163 |
-
## Dockerfile Layer Strategy
|
| 164 |
-
|
| 165 |
-
```
|
| 166 |
-
Layer 1: fastapi, uvicorn, pillow, pillow-heif, huggingface_hub,
|
| 167 |
-
opencv-python-headless (placeholder),
|
| 168 |
-
rapidocr-onnxruntime ← NEW: bundled ONNX models, no extra download
|
| 169 |
-
↓
|
| 170 |
-
Layer 2: torch + torchvision (CPU wheel — skips 2.5 GB CUDA default)
|
| 171 |
-
↓
|
| 172 |
-
Layer 3: magic-pdf[full]==1.3.12 (doclayout-yolo from myhloli index)
|
| 173 |
-
↓
|
| 174 |
-
Layer 3.5: patch models_config.yml in magic_pdf package
|
| 175 |
-
ch_PP-OCRv3_det → ch_PP-OCRv5_det (all ch* langs)
|
| 176 |
-
en_PP-OCRv3_det → Multilingual_PP-OCRv3_det (en, latin)
|
| 177 |
-
↓
|
| 178 |
-
Layer 4: pip install --force-reinstall opencv-python-headless
|
| 179 |
-
↓
|
| 180 |
-
App code + MinerU model download (MFR excluded, ~5 GB total)
|
| 181 |
-
```
|
| 182 |
-
|
| 183 |
-
---
|
| 184 |
-
|
| 185 |
-
## OCR Root Cause History
|
| 186 |
-
|
| 187 |
-
| # | Error | Root Cause | Fix |
|
| 188 |
-
|---|---|---|---|
|
| 189 |
-
| 1 | `No module named 'ultralytics'` | `[full-cpu]` is not a valid extra; pip installs base only | Changed to `magic-pdf[full]==1.3.12` |
|
| 190 |
-
| 2 | `ImportError: cv2` at runtime | `doclayout-yolo` pulls non-headless `opencv-python`, breaks slim container | Layer 4 `--force-reinstall opencv-python-headless` |
|
| 191 |
-
| 3 | `ch_PP-OCRv3_det_infer.pth not found` | HF repo updated to v5 det; `models_config.yml` in wheel still says v3 | Layer 3.5 patches config; arch safety verified in `arch_config.yaml` |
|
| 192 |
-
| 4 | Quality unacceptable for images | MinerU full pipeline used for all files; 40+ s for a simple marksheet JPG | RapidOCR fast path for images (1–5 s), MinerU only for PDFs |
|
| 193 |
-
|
| 194 |
-
---
|
| 195 |
-
|
| 196 |
-
## Deployment
|
| 197 |
-
|
| 198 |
-
1. Create a new Hugging Face Space → **Docker** SDK
|
| 199 |
-
2. Upload all files in this directory to the Space repo root
|
| 200 |
-
3. First build: ~25–35 min (model download included)
|
| 201 |
-
4. Call `/warmup` once after deploy to pre-load both engines
|
| 202 |
-
|
| 203 |
-
```bash
|
| 204 |
-
# Health check
|
| 205 |
-
curl https://<your-space>.hf.space/health
|
| 206 |
-
|
| 207 |
-
# Warmup (do this once after deploy)
|
| 208 |
-
curl https://<your-space>.hf.space/warmup
|
| 209 |
-
|
| 210 |
-
# Benchmark (verify RapidOCR timing)
|
| 211 |
-
curl -X POST https://<your-space>.hf.space/benchmark
|
| 212 |
-
|
| 213 |
-
# Extract a marksheet
|
| 214 |
-
curl -X POST https://<your-space>.hf.space/extract -F "file=@marksheet.jpg"
|
| 215 |
-
|
| 216 |
-
# Diagnostics (model inventory + config)
|
| 217 |
-
curl https://<your-space>.hf.space/diagnostics
|
| 218 |
-
```
|
| 219 |
-
|
| 220 |
-
---
|
| 221 |
-
|
| 222 |
-
## Validation Steps (post-deploy)
|
| 223 |
-
|
| 224 |
-
```bash
|
| 225 |
-
# 1. Confirm RapidOCR is ready
|
| 226 |
-
curl https://<your-space>.hf.space/status | python3 -m json.tool
|
| 227 |
-
|
| 228 |
-
# 2. Run benchmark and check timing
|
| 229 |
-
curl -X POST https://<your-space>.hf.space/benchmark | python3 -m json.tool
|
| 230 |
-
# Expected: rapidocr.inferenceMs < 5000, status: "ok"
|
| 231 |
-
|
| 232 |
-
# 3. Extract a marksheet — verify entities and timing
|
| 233 |
-
curl -X POST https://<your-space>.hf.space/extract \
|
| 234 |
-
-F "file=@marksheet.jpg" | python3 -m json.tool
|
| 235 |
-
# Expected: ocrEngine="rapidocr", timings.totalMs < 10000
|
| 236 |
-
# Expected: entities.subjects populated, entities.percentage present
|
| 237 |
-
|
| 238 |
-
# 4. Extract a PDF
|
| 239 |
-
curl -X POST https://<your-space>.hf.space/extract \
|
| 240 |
-
-F "file=@document.pdf" | python3 -m json.tool
|
| 241 |
-
# Expected: ocrEngine="mineru", metadata.parseMethod in ["txt","ocr"]
|
| 242 |
-
|
| 243 |
-
# 5. Repeat upload (cache hit)
|
| 244 |
-
curl -X POST https://<your-space>.hf.space/extract \
|
| 245 |
-
-F "file=@marksheet.jpg" | python3 -m json.tool
|
| 246 |
-
# Expected: metadata.cached=true, timings.totalMs=0
|
| 247 |
-
```
|
| 248 |
-
|
| 249 |
-
---
|
| 250 |
-
|
| 251 |
-
## Resource Budget (free CPU Basic)
|
| 252 |
-
|
| 253 |
-
| Resource | Budget | Usage |
|
| 254 |
-
|---|---|---|
|
| 255 |
-
| Disk | 50 GB | ~10–13 GB (packages ~5 GB + models ~5 GB) |
|
| 256 |
-
| RAM | 16 GB | RapidOCR: ~200 MB; MinerU models: ~4–6 GB at load |
|
| 257 |
-
| vCPU | 2 | Single uvicorn worker; sequential batch processing |
|
| 258 |
-
|
| 259 |
-
---
|
| 260 |
-
|
| 261 |
-
## Known Limitations
|
| 262 |
-
|
| 263 |
-
1. **`en` language OCR** — `en_PP-OCRv4_rec_infer.pth` absent from HF repo. MinerU falls back to ch_lite which handles printed English adequately. RapidOCR handles English natively.
|
| 264 |
-
2. **Formula OCR** — disabled. MFR models excluded (~1-2 GB saved). Set `"enable": true` in `~/magic-pdf.json` and re-download MFR models to enable.
|
| 265 |
-
3. **First-request latency** — MinerU models lazy-load on first PDF request (~30–60 s). Call `/warmup` to pre-load.
|
| 266 |
-
4. **In-process cache** — SHA256 cache lives in RAM, cleared on container restart.
|
| 267 |
-
5. **Batch is sequential** — 8-file max; parallel processing would OOM on 16 GB.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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