bakhil-aissa commited on
Commit
828f49f
·
verified ·
1 Parent(s): dbb95c8

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +353 -0
README.md ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # PDF Pipeline
2
+
3
+ A comprehensive PDF-to-Markdown extraction pipeline using state-of-the-art layout detection, OCR, and table extraction models.
4
+
5
+ ## Features
6
+
7
+ - **Layout Detection**: Uses PP-DocLayoutV3 (ONNX) for accurate document layout analysis
8
+ - **OCR Support**: Multiple OCR backends - PaddleOCR, RapidOCR, Pytesseract
9
+ - **Table Extraction**: TableFormerONNX with OTSL (Object Table Structure Language) output
10
+ - **Dual Input Support**: Process both PDFs and standalone images (PNG, JPG, etc.)
11
+ - **Streamlit UI**: Interactive web interface for easy document processing
12
+
13
+ ## Architecture
14
+
15
+ ```
16
+ PDF/Image Input
17
+
18
+
19
+ ┌─────────────────────────────────────────────────────────────┐
20
+ │ Layout Detection │
21
+ │ PP-DocLayoutV3 (ONNX) │
22
+ └─────────────────────────────────────────────────────────────┘
23
+
24
+
25
+ ┌─────────────────────────────────────────────────────────────┐
26
+ │ Text Extraction Strategy │
27
+ ├─────────────────────────────────────────────────────────────┤
28
+ │ Native PDF Text │ OCR Fallback (Paddle/Rapid/Tesseract) │
29
+ │ (pdfplumber) │ │
30
+ └─────────────────────────────────────────────────────────────┘
31
+
32
+
33
+ ┌─────────────────────────────────────────────────────────────┐
34
+ │ Region Classification │
35
+ │ • Text Blocks • Tables • Figures • Headers • Footers │
36
+ └─────────────────────────────────────────────────────────────┘
37
+
38
+
39
+ ┌─────────────────────────────────────────────────────────────┐
40
+ │ Table Extraction (if applicable) │
41
+ │ TableFormerONNX → OTSL → Markdown │
42
+ └─────────────────────────────────────────────────────────────┘
43
+
44
+
45
+ ┌─────────────────────────────────────────────────────────────┐
46
+ │ Markdown Output │
47
+ │ • Structured text • Extracted tables • Figure references │
48
+ └─────────────────────────────────────────────────────────────┘
49
+ ```
50
+
51
+ ## Project Structure
52
+
53
+ ```
54
+ pdf_pipeline_project/
55
+ ├── main.py # Streamlit web interface with HuggingFace Hub integration
56
+ ├── example_usage.py # Python API examples
57
+ ├── html_to_table.py # HTML table utilities
58
+ ├── requirements.txt # Python dependencies
59
+ ├── PP-DocLayout/ # Layout detection models (282MB) - auto-downloaded
60
+ ├── tableformerv1/ # Table extraction models (205MB) - auto-downloaded
61
+ ├── pp_ocr_small/ # Small OCR models (~35MB) - auto-downloaded
62
+ ├── pp_ocr_medium/ # Medium OCR models (~100MB) - auto-downloaded
63
+ ├── pdf_pipeline/ # Core Python package
64
+ │ ├── __init__.py # Public API exports
65
+ │ ├── layout.py # DocLayoutV3 ONNX wrapper
66
+ │ ├── ocr_backends.py # OCR implementations (RapidOCR, Pytesseract)
67
+ │ ├── pipeline.py # Main processing pipeline
68
+ │ ├── table_extraction.py # TableFormer integration
69
+ │ ├── logging_config.py # Logging utilities
70
+ │ └── ch_en_dict.txt # OCR character dictionary
71
+ ├── examples/ # Sample PDFs and outputs
72
+ └── README.md # This file
73
+ ```
74
+
75
+ ## Installation
76
+
77
+ ### Prerequisites
78
+
79
+ - Python 3.10+
80
+ - Tesseract OCR (for Pytesseract backend)
81
+ - HuggingFace account token (for model downloading)
82
+
83
+ ### Setup
84
+
85
+ 1. **Clone the repository**:
86
+ ```bash
87
+ git clone <repository-url>
88
+ cd pdf_pipeline_project
89
+ ```
90
+
91
+ 2. **Install Python dependencies**:
92
+ ```bash
93
+ pip install -r requirements.txt
94
+ ```
95
+
96
+ 3. **Install Tesseract** (Ubuntu/Debian):
97
+ ```bash
98
+ sudo apt-get update
99
+ sudo apt-get install -y tesseract-ocr tesseract-ocr-fra tesseract-ocr-eng
100
+ ```
101
+
102
+ For macOS:
103
+ ```bash
104
+ brew install tesseract
105
+ ```
106
+
107
+ ### Model Download (Automatic)
108
+
109
+ Models are automatically downloaded from HuggingFace Hub on first run:
110
+
111
+ | Model | Repository | Size |
112
+ |-------|------------|------|
113
+ | **PP-DocLayoutV3** | `PaddlePaddle/PP-DocLayoutV3_onnx` | ~282MB |
114
+ | **TableFormer** | `bakhil-aissa/tableformerv1` | ~205MB |
115
+ | **PaddleOCR Medium** | `PaddlePaddle/PP-OCRv6_medium_*_onnx` | ~100MB |
116
+ | **PaddleOCR Small** | `PaddlePaddle/PP-OCRv6_small_*_onnx` | ~35MB |
117
+
118
+ To pre-download models:
119
+ ```python
120
+ from huggingface_hub import snapshot_download
121
+
122
+ snapshot_download(repo_id="PaddlePaddle/PP-DocLayoutV3_onnx", local_dir="PP-DocLayout")
123
+ snapshot_download(repo_id="bakhil-aissa/tableformerv1", local_dir="tableformerv1")
124
+ ```
125
+
126
+ ## Usage
127
+
128
+ ### Streamlit Web Interface
129
+
130
+ Run the interactive web UI:
131
+
132
+ ```bash
133
+ streamlit run main.py
134
+ ```
135
+
136
+ Then open your browser to `http://localhost:8501`
137
+
138
+ Features:
139
+ - Upload PDF or image files
140
+ - Configure OCR backend (PaddleOCR, RapidOCR, Pytesseract)
141
+ - Adjust rendering resolution
142
+ - Preview extracted markdown
143
+ - Download results
144
+
145
+ ### Python API
146
+
147
+ #### Process a PDF:
148
+
149
+ ```python
150
+ from pdf_pipeline import (
151
+ DocLayoutV3,
152
+ TableFormerONNX,
153
+ get_ocr_backend,
154
+ process_document,
155
+ )
156
+ from huggingface_hub import snapshot_download
157
+ import os
158
+
159
+ # Model paths (auto-download from HuggingFace)
160
+ PP_DOCLAYOUT_PATH = ("PP-DocLayout/inference.onnx" if os.path.exists("PP-DocLayout/inference.onnx")
161
+ else os.path.join(snapshot_download(repo_id="PaddlePaddle/PP-DocLayoutV3_onnx", local_dir="PP-DocLayout"), "inference.onnx")
162
+ )
163
+
164
+ DET_PATH_MEDIUM = (
165
+ "pp_ocr_medium/det/inference.onnx" if os.path.exists("pp_ocr_medium/det/inference.onnx")
166
+ else os.path.join(snapshot_download(repo_id="PaddlePaddle/PP-OCRv6_medium_det_onnx", local_dir="pp_ocr_medium", subfolder="det"), "inference.onnx")
167
+ )
168
+
169
+ REC_PATH_MEDIUM = (
170
+ "pp_ocr_medium/rec/inference.onnx" if os.path.exists("pp_ocr_medium/rec/inference.onnx")
171
+ else snapshot_download(repo_id="PaddlePaddle/PP-OCRv6_medium_rec_onnx", local_dir="pp_ocr_medium", subfolder="rec")
172
+ )
173
+
174
+ REC_KEYS_PATH = "ch_en_dict.txt"
175
+
176
+ # Initialize components
177
+ layout_detector = DocLayoutV3(PP_DOCLAYOUT_PATH)
178
+ page_ocr_backend = get_ocr_backend("rapidocr", det_model_path=DET_PATH_MEDIUM, rec_model_path=REC_PATH_MEDIUM, rec_keys_path=REC_KEYS_PATH)
179
+ table_runner = TableFormerONNX(artifact_root="tableformerv1", variant="accurate")
180
+ table_ocr_backend = get_ocr_backend("rapidocr", det_model_path=DET_PATH_MEDIUM, rec_model_path=REC_PATH_MEDIUM, rec_keys_path=REC_KEYS_PATH)
181
+
182
+ # Process document
183
+ markdown_doc = process_document(
184
+ "document.pdf",
185
+ layout_detector,
186
+ page_ocr_backend=page_ocr_backend,
187
+ table_runner=table_runner,
188
+ table_ocr_backend=table_ocr_backend,
189
+ )
190
+
191
+ print(markdown_doc)
192
+ ```
193
+
194
+ #### Process an Image:
195
+
196
+ ```python
197
+ # Same setup as above...
198
+
199
+ # Process standalone image (always uses OCR)
200
+ markdown_doc = process_document(
201
+ "scanned_page.png",
202
+ layout_detector,
203
+ page_ocr_backend=page_ocr_backend,
204
+ table_runner=table_runner,
205
+ table_ocr_backend=table_ocr_backend,
206
+ )
207
+ ```
208
+
209
+ ## OCR Backends
210
+
211
+ Choose the best OCR backend for your needs:
212
+
213
+ | Backend | Speed | Accuracy | Languages | Notes |
214
+ |---------|-------|----------|-----------|-------|
215
+ | **RapidOCR** | Very Fast | Good | 10+ | ONNX-based, lightweight |
216
+ | **Pytesseract** | Medium | Good | 100+ | Tesseract wrapper, configurable |
217
+
218
+ ### Model Sizes
219
+
220
+ RapidOCR supports two model sizes:
221
+
222
+ | Model | Detection | Recognition | Size | Speed | Accuracy |
223
+ |-------|-----------|-------------|------|-------|----------|
224
+ | **Small** | PP-OCRv6_small | PP-OCRv6_small | ~35MB | Fast | Good |
225
+ | **Medium** | PP-OCRv6_medium | PP-OCRv6_medium | ~100MB | Medium | Better |
226
+
227
+ Switch backends and models:
228
+ ```python
229
+ # RapidOCR with small model (faster)
230
+ ocr = get_ocr_backend("rapidocr",
231
+ det_model_path=DET_PATH_SMALL,
232
+ rec_model_path=REC_PATH_SMALL,
233
+ rec_keys_path=REC_KEYS_PATH)
234
+
235
+ # RapidOCR with medium model (more accurate)
236
+ ocr = get_ocr_backend("rapidocr",
237
+ det_model_path=DET_PATH_MEDIUM,
238
+ rec_model_path=REC_PATH_MEDIUM,
239
+ rec_keys_path=REC_KEYS_PATH)
240
+
241
+ # Pytesseract (French + English)
242
+ ocr = get_ocr_backend("pytesseract", lang="fra+eng")
243
+ ```
244
+
245
+ ## Troubleshooting
246
+
247
+ ### Issue: `ModuleNotFoundError: No module named 'pdf_pipeline'`
248
+
249
+ **Solution**: Run from project root, or install as editable package:
250
+ ```bash
251
+ pip install -e .
252
+ ```
253
+
254
+ ### Issue: `TesseractNotFoundError: tesseract is not installed`
255
+
256
+ **Solution**: Install Tesseract system binary:
257
+ ```bash
258
+ # Ubuntu/Debian
259
+ sudo apt-get install tesseract-ocr
260
+
261
+ # macOS
262
+ brew install tesseract
263
+
264
+ # Windows: Download installer from https://github.com/UB-Mannheim/tesseract/wiki
265
+ ```
266
+
267
+ ### Issue: `onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument`
268
+
269
+ **Solution**: Check model files are not corrupted. Re-download if needed:
270
+ ```bash
271
+ # Check file sizes match expected
272
+ ls -lh PP-DocLayout/*.onnx
273
+ ls -lh tableformerv1/onnx/accurate/*.onnx
274
+ ```
275
+
276
+ ### Issue: Out of Memory (OOM) on large PDFs
277
+
278
+ **Solution**: Reduce rendering resolution:
279
+ ```python
280
+ # Lower DPI for memory-constrained environments
281
+ process_document(
282
+ "large.pdf",
283
+ layout_detector,
284
+ resolution=100, # Default is 150, try 100 or 72
285
+ )
286
+ ```
287
+
288
+ ### Issue: Slow OCR on CPU
289
+
290
+ **Solution**: Use RapidOCR for faster CPU inference:
291
+ ```python
292
+ ocr = get_ocr_backend("rapidocr") # ~2-3x faster than PaddleOCR on CPU
293
+ ```
294
+
295
+ ## Performance Benchmarks
296
+
297
+ Typical processing times (single page, Intel i7, 16GB RAM):
298
+
299
+ | Stage | Time | Notes |
300
+ |-------|------|-------|
301
+ | PDF Rendering | 200-500ms | Depends on resolution |
302
+ | Layout Detection | 300-800ms | ONNXRuntime, CPU |
303
+ | Text Extraction | 100-300ms | Native PDF text |
304
+ | OCR (fallback) | 1-3s | Only if no native text |
305
+ | Table Extraction | 2-5s | Per table region |
306
+
307
+ **Total**: ~1-5 seconds per page (depending on content density)
308
+
309
+ ## Contributing
310
+
311
+ Contributions are welcome! Please follow these steps:
312
+
313
+ 1. Fork the repository
314
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
315
+ 3. Commit your changes (`git commit -m 'Add amazing feature'`)
316
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
317
+ 5. Open a Pull Request
318
+
319
+ ### Development Setup
320
+
321
+ ```bash
322
+ # Install development dependencies
323
+ pip install -r requirements.txt
324
+ pip install pytest black flake8 mypy
325
+
326
+ # Run tests
327
+ pytest tests/
328
+
329
+ # Format code
330
+ black pdf_pipeline/ main.py
331
+
332
+ # Type checking
333
+ mypy pdf_pipeline/
334
+ ```
335
+
336
+ ## License
337
+
338
+ This project is licensed under the MIT License - see the LICENSE file for details.
339
+
340
+ ## Acknowledgments
341
+
342
+ - **PaddleOCR** - For OCR model implementations
343
+ - **PP-DocLayout** - For document layout detection
344
+ - **TableFormer** - For table structure recognition
345
+ - **Docling** - For OTSL to Markdown conversion
346
+
347
+ ## Contact
348
+
349
+ For questions or support, please open an issue on GitHub or contact the maintainers.
350
+
351
+ ---
352
+
353
+ **Made with ❤️ for document processing automation**