Transformers
File size: 27,355 Bytes
2aa7f88
 
 
 
5e88111
 
2aa7f88
5e88111
 
 
 
 
 
2aa7f88
 
5e88111
 
 
 
 
 
 
 
2aa7f88
 
 
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808ef5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e88111
808ef5b
5e88111
 
 
1d60d8a
5e88111
 
 
1d60d8a
5e88111
2aa7f88
5e88111
 
 
d277ae4
2aa7f88
5e88111
 
 
d277ae4
5e88111
 
 
 
 
 
 
d277ae4
2aa7f88
5e88111
c1a9148
5e88111
 
 
 
 
 
 
 
 
c1a9148
5e88111
c1a9148
5e88111
 
 
2aa7f88
5e88111
2aa7f88
808ef5b
5e88111
808ef5b
976ee24
 
 
 
 
 
 
 
 
 
d277ae4
 
976ee24
 
 
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808ef5b
5e88111
d277ae4
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d277ae4
5e88111
 
 
 
 
 
 
 
d277ae4
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a9148
5e88111
c1a9148
5e88111
 
 
 
 
 
 
c1a9148
5e88111
c1a9148
5e88111
 
 
 
 
 
d277ae4
5e88111
d277ae4
5e88111
 
 
 
 
 
d277ae4
5e88111
 
 
 
d277ae4
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808ef5b
 
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808ef5b
5e88111
 
 
 
 
 
 
808ef5b
5e88111
 
 
 
808ef5b
5e88111
808ef5b
5e88111
c1a9148
5e88111
 
 
 
c1a9148
5e88111
808ef5b
5e88111
 
 
 
 
 
 
 
808ef5b
5e88111
808ef5b
5e88111
808ef5b
5e88111
808ef5b
5e88111
808ef5b
5e88111
808ef5b
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
808ef5b
5e88111
808ef5b
5e88111
 
 
 
808ef5b
5e88111
808ef5b
5e88111
808ef5b
5e88111
808ef5b
5e88111
 
 
 
 
2aa7f88
5e88111
 
 
 
2aa7f88
5e88111
 
 
 
 
ffc6bc1
5e88111
 
 
 
ffc6bc1
5e88111
ffc6bc1
5e88111
 
 
 
ffc6bc1
5e88111
 
 
 
ffc6bc1
5e88111
ffc6bc1
5e88111
 
 
 
2aa7f88
5e88111
 
 
 
 
2aa7f88
5e88111
2aa7f88
5e88111
 
 
 
2aa7f88
5e88111
 
 
808ef5b
5e88111
 
 
 
808ef5b
5e88111
 
 
26f2535
5e88111
26f2535
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9335bf9
5e88111
9335bf9
5e88111
 
 
9335bf9
5e88111
 
9335bf9
5e88111
 
 
9335bf9
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
9335bf9
5e88111
 
 
 
9335bf9
5e88111
 
 
 
9335bf9
 
5e88111
9335bf9
5e88111
9335bf9
5e88111
9335bf9
5e88111
 
 
 
 
 
26f2535
5e88111
26f2535
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f2535
5e88111
e441581
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e441581
5e88111
 
e441581
5e88111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e441581
5e88111
e441581
16bce23
e441581
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
---
license: openrail
---

<h1 align="center">PDF Document Layout Analysis</h1>
<p align="center">A Docker-powered microservice for intelligent PDF document layout analysis, OCR, and content extraction</p>

<p align="center">
  <img src="https://img.shields.io/badge/Python-3.10+-blue.svg" alt="Python Version">
  <img src="https://img.shields.io/badge/FastAPI-0.111.1-green.svg" alt="FastAPI">
  <img src="https://img.shields.io/badge/Docker-Ready-blue.svg" alt="Docker">
  <img src="https://img.shields.io/badge/GPU-Supported-orange.svg" alt="GPU Support">
</p>


<div align="center">
  <p><strong>Built with ❀️ by <a href="https://huridocs.org">HURIDOCS</a></strong></p>
  <p>
    <a href="https://github.com/huridocs/pdf-document-layout-analysis">⭐ Star us on GitHub</a> β€’
    <a href="https://hub.docker.com/r/huridocs/pdf-document-layout-analysis">🐳 Pull from Docker Hub</a> β€’
    <a href="https://huggingface.co/HURIDOCS/pdf-document-layout-analysis">πŸ€— View on Hugging Face</a>
  </p>
</div>



---

## πŸš€ Overview

This project provides a powerful and flexible PDF analysis microservice built with **Clean Architecture** principles. The service enables OCR, segmentation, and classification of different parts of PDF pages, identifying elements such as texts, titles, pictures, tables, formulas, and more. Additionally, it determines the correct reading order of these identified elements and can convert PDFs to various formats including Markdown and HTML.

### ✨ Key Features

- πŸ” **Advanced PDF Layout Analysis** - Segment and classify PDF content with high accuracy
- πŸ–ΌοΈ **Visual & Fast Models** - Choose between VGT (Vision Grid Transformer) for accuracy or LightGBM for speed
- πŸ“ **Multi-format Output** - Export to JSON, Markdown, HTML, and visualize PDF segmentations
- 🌐 **OCR Support** - 150+ language support with Tesseract OCR
- πŸ“Š **Table & Formula Extraction** - Extract tables as HTML and formulas as LaTeX
- πŸ—οΈ **Clean Architecture** - Modular, testable, and maintainable codebase
- 🐳 **Docker-Ready** - Easy deployment with GPU support
- ⚑ **RESTful API** - Comprehensive API with 10+ endpoints

<table>
  <tr>
    <td>
      <img src="https://raw.githubusercontent.com/huridocs/pdf-document-layout-analysis/main/images/vgtexample1.png"/>
    </td>
    <td>
      <img src="https://raw.githubusercontent.com/huridocs/pdf-document-layout-analysis/main/images/vgtexample2.png"/>
    </td>
    <td>
      <img src="https://raw.githubusercontent.com/huridocs/pdf-document-layout-analysis/main/images/vgtexample3.png"/>
    </td>
    <td>
      <img src="https://raw.githubusercontent.com/huridocs/pdf-document-layout-analysis/main/images/vgtexample4.png"/>
    </td>
  </tr>
</table>

### πŸ”— Project Links

- **GitHub**: [pdf-document-layout-analysis](https://github.com/huridocs/pdf-document-layout-analysis)
- **HuggingFace**: [pdf-document-layout-analysis](https://huggingface.co/HURIDOCS/pdf-document-layout-analysis)
- **DockerHub**: [pdf-document-layout-analysis](https://hub.docker.com/r/huridocs/pdf-document-layout-analysis/)

---

## πŸš€ Quick Start

### 1. Start the Service

**With GPU support (recommended for better performance):**
```bash
make start
```

**Without GPU support:**
```bash
make start_no_gpu
```

The service will be available at `http://localhost:5060`

**Check service status:**

```bash
curl http://localhost:5060/info
```

### 2. Basic PDF Analysis

**Analyze a PDF document (VGT model - high accuracy):**
```bash
curl -X POST -F 'file=@/path/to/your/document.pdf' http://localhost:5060
```

**Fast analysis (LightGBM models - faster processing):**
```bash
curl -X POST -F 'file=@/path/to/your/document.pdf' -F "fast=true" http://localhost:5060
```

### 3. Stop the Service

```bash
make stop
```

> πŸ’‘ **Tip**: Replace `/path/to/your/document.pdf` with the actual path to your PDF file. The service will return a JSON response with segmented content and metadata.


## πŸ“‹ Table of Contents

- [πŸš€ Quick Start](#πŸš€-quick-start)
- [βš™οΈ Dependencies](#βš™οΈ-dependencies)
- [πŸ“‹ Requirements](#πŸ“‹-requirements)
- [πŸ“š API Reference](#πŸ“š-api-reference)
- [πŸ’‘ Usage Examples](#πŸ’‘-usage-examples)
- [πŸ—οΈ Architecture](#πŸ—οΈ-architecture)
- [πŸ€– Models](#πŸ€–-models)
- [πŸ“Š Data](#πŸ“Š-data)
- [πŸ”§ Development](#πŸ”§-development)
- [πŸ“ˆ Benchmarks](#πŸ“ˆ-benchmarks)
  - [Performance](#performance)
  - [Speed](#speed)
- [🌐 Installation of More Languages for OCR](#🌐-installation-of-more-languages-for-ocr)
- [πŸ”— Related Services](#πŸ”—-related-services)
- [🀝 Contributing](#🀝-contributing)



## βš™οΈ Dependencies

### Required
- **Docker Desktop 4.25.0+** - [Installation Guide](https://www.docker.com/products/docker-desktop/)
- **Python 3.10+** (for local development)

### Optional
- **NVIDIA Container Toolkit** - [Installation Guide](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html) (for GPU support)

## πŸ“‹ Requirements

### System Requirements
- **RAM**: 2 GB minimum
- **GPU Memory**: 5 GB (optional, will fallback to CPU if unavailable)
- **Disk Space**: 10 GB for models and dependencies
- **CPU**: Multi-core recommended for better performance

### Docker Requirements
- Docker Engine 20.10+
- Docker Compose 2.0+

## πŸ“š API Reference

The service provides a comprehensive RESTful API with the following endpoints:

### Core Analysis Endpoints

| Endpoint | Method | Description | Parameters |
|----------|--------|-------------|------------|
| `/` | POST | Analyze PDF layout and extract segments | `file`, `fast`, `ocr_tables` |
| `/save_xml/{filename}` | POST | Analyze PDF and save XML output | `file`, `xml_file_name`, `fast` |
| `/get_xml/{filename}` | GET | Retrieve saved XML analysis | `xml_file_name` |

### Content Extraction Endpoints

| Endpoint | Method | Description | Parameters |
|----------|--------|-------------|------------|
| `/text` | POST | Extract text by content types | `file`, `fast`, `types` |
| `/toc` | POST | Extract table of contents | `file`, `fast` |
| `/toc_legacy_uwazi_compatible` | POST | Extract TOC (Uwazi compatible) | `file` |

### Format Conversion Endpoints

| Endpoint | Method | Description | Parameters |
|----------|--------|-------------|------------|
| `/markdown` | POST | Convert PDF to Markdown (includes segmentation data in zip) | `file`, `fast`, `extract_toc`, `dpi`, `output_file` |
| `/html` | POST | Convert PDF to HTML (includes segmentation data in zip) | `file`, `fast`, `extract_toc`, `dpi`, `output_file` |
| `/visualize` | POST | Visualize segmentation results on the PDF | `file`, `fast` |

### OCR & Utility Endpoints

| Endpoint | Method | Description | Parameters |
|----------|--------|-------------|------------|
| `/ocr` | POST | Apply OCR to PDF | `file`, `language` |
| `/info` | GET | Get service information | - |
| `/` | GET | Health check and system info | - |
| `/error` | GET | Test error handling | - |

### Common Parameters

- **`file`**: PDF file to process (multipart/form-data)
- **`fast`**: Use LightGBM models instead of VGT (boolean, default: false)
- **`ocr_tables`**: Apply OCR to table regions (boolean, default: false)
- **`language`**: OCR language code (string, default: "en")
- **`types`**: Comma-separated content types to extract (string, default: "all")
- **`extract_toc`**: Include table of contents at the beginning of the output (boolean, default: false)
- **`dpi`**: Image resolution for conversion (integer, default: 120)

## πŸ’‘ Usage Examples

### Basic PDF Analysis

**Standard analysis with VGT model:**
```bash
curl -X POST \
  -F '[email protected]' \
  http://localhost:5060
```

**Fast analysis with LightGBM models:**
```bash
curl -X POST \
  -F '[email protected]' \
  -F 'fast=true' \
  http://localhost:5060
```

**Analysis with table OCR:**
```bash
curl -X POST \
  -F '[email protected]' \
  -F 'ocr_tables=true' \
  http://localhost:5060
```

### Text Extraction

**Extract all text:**
```bash
curl -X POST \
  -F '[email protected]' \
  -F 'types=all' \
  http://localhost:5060/text
```

**Extract specific content types:**
```bash
curl -X POST \
  -F '[email protected]' \
  -F 'types=title,text,table' \
  http://localhost:5060/text
```

### Format Conversion

**Convert to Markdown:**
```bash
curl -X POST http://localhost:5060/markdown \
  -F '[email protected]' \
  -F 'extract_toc=true' \
  -F 'output_file=document.md' \
  --output 'document.zip'
```

**Convert to HTML:**
```bash
curl -X POST http://localhost:5060/html \
  -F '[email protected]' \
  -F 'extract_toc=true' \
  -F 'output_file=document.html' \
  --output 'document.zip'
```

> **πŸ“‹ Segmentation Data**: Format conversion endpoints automatically include detailed segmentation data in the zip output. The resulting zip file contains a `{filename}_segmentation.json` file with information about each detected document segment including:
> - **Coordinates**: `left`, `top`, `width`, `height`
> - **Page information**: `page_number`, `page_width`, `page_height` 
> - **Content**: `text` content and segment `type` (e.g., "Title", "Text", "Table", "Picture")


### OCR Processing

**OCR in English:**
```bash
curl -X POST \
  -F 'file=@scanned_document.pdf' \
  -F 'language=en' \
  http://localhost:5060/ocr \
  --output ocr_processed.pdf
```

**OCR in other languages:**
```bash
# French
curl -X POST \
  -F 'file=@document_french.pdf' \
  -F 'language=fr' \
  http://localhost:5060/ocr \
  --output ocr_french.pdf

# Spanish
curl -X POST \
  -F 'file=@document_spanish.pdf' \
  -F 'language=es' \
  http://localhost:5060/ocr \
  --output ocr_spanish.pdf
```

### Visualization

**Generate visualization PDF:**
```bash
curl -X POST \
  -F '[email protected]' \
  http://localhost:5060/visualize \
  --output visualization.pdf
```

### Table of Contents Extraction

**Extract structured TOC:**
```bash
curl -X POST \
  -F '[email protected]' \
  http://localhost:5060/toc
```

### XML Storage and Retrieval

**Analyze and save XML:**
```bash
curl -X POST \
  -F '[email protected]' \
  http://localhost:5060/save_xml/my_analysis
```

**Retrieve saved XML:**
```bash
curl http://localhost:5060/get_xml/my_analysis.xml
```

### Service Information

**Get service info and supported languages:**
```bash
curl http://localhost:5060/info
```

**Health check:**
```bash
curl http://localhost:5060/
```

### Response Format

Most endpoints return JSON with segment information:

```json
[
  {
    "left": 72.0,
    "top": 84.0,
    "width": 451.2,
    "height": 23.04,
    "page_number": 1,
    "page_width": 595.32,
    "page_height": 841.92,
    "text": "Document Title",
    "type": "Title"
  },
  {
    "left": 72.0,
    "top": 120.0,
    "width": 451.2,
    "height": 200.0,
    "page_number": 1,
    "page_width": 595.32,
    "page_height": 841.92,
    "text": "This is the main text content...",
    "type": "Text"
  }
]
```

### Supported Content Types

- `Caption` - Image and table captions
- `Footnote` - Footnote text
- `Formula` - Mathematical formulas
- `List item` - List items and bullet points
- `Page footer` - Footer content
- `Page header` - Header content
- `Picture` - Images and figures
- `Section header` - Section headings
- `Table` - Table content
- `Text` - Regular text paragraphs
- `Title` - Document and section titles


## πŸ—οΈ Architecture

This project follows **Clean Architecture** principles, ensuring separation of concerns, testability, and maintainability. The codebase is organized into distinct layers:

### Directory Structure

```
src/
β”œβ”€β”€ domain/                 # Enterprise Business Rules
β”‚   β”œβ”€β”€ PdfImages.py       # PDF image handling domain logic
β”‚   β”œβ”€β”€ PdfSegment.py      # PDF segment entity
β”‚   β”œβ”€β”€ Prediction.py      # ML prediction entity
β”‚   └── SegmentBox.py      # Core segment box entity
β”œβ”€β”€ use_cases/             # Application Business Rules
β”‚   β”œβ”€β”€ pdf_analysis/      # PDF analysis use case
β”‚   β”œβ”€β”€ text_extraction/   # Text extraction use case
β”‚   β”œβ”€β”€ toc_extraction/    # Table of contents extraction
β”‚   β”œβ”€β”€ visualization/     # PDF visualization use case
β”‚   β”œβ”€β”€ ocr/              # OCR processing use case
β”‚   β”œβ”€β”€ markdown_conversion/ # Markdown conversion use case
β”‚   └── html_conversion/   # HTML conversion use case
β”œβ”€β”€ adapters/              # Interface Adapters
β”‚   β”œβ”€β”€ infrastructure/    # External service adapters
β”‚   β”œβ”€β”€ ml/               # Machine learning model adapters
β”‚   β”œβ”€β”€ storage/          # File storage adapters
β”‚   └── web/              # Web framework adapters
β”œβ”€β”€ ports/                 # Interface definitions
β”‚   β”œβ”€β”€ services/         # Service interfaces
β”‚   └── repositories/     # Repository interfaces
└── drivers/              # Frameworks & Drivers
    └── web/              # FastAPI application setup
```

### Layer Responsibilities

- **Domain Layer**: Contains core business entities and rules independent of external concerns
- **Use Cases Layer**: Orchestrates domain entities to fulfill specific application requirements
- **Adapters Layer**: Implements interfaces defined by inner layers and adapts external frameworks
- **Drivers Layer**: Contains frameworks, databases, and external agency configurations

### Key Benefits

- πŸ”„ **Dependency Inversion**: High-level modules don't depend on low-level modules
- πŸ§ͺ **Testability**: Easy to unit test business logic in isolation
- πŸ”§ **Maintainability**: Changes to external frameworks don't affect business rules
- πŸ“ˆ **Scalability**: Easy to add new features without modifying existing code

  
## πŸ€– Models

The service offers two complementary model approaches, each optimized for different use cases:

### 1. Vision Grid Transformer (VGT) - High Accuracy Model

**Overview**: A state-of-the-art visual model developed by Alibaba Research Group that "sees" the entire page layout.

**Key Features**:
- 🎯 **High Accuracy**: Best-in-class performance on document layout analysis
- πŸ‘οΈ **Visual Understanding**: Analyzes the entire page context including spatial relationships
- πŸ“Š **Trained on DocLayNet**: Uses the comprehensive [DocLayNet dataset](https://github.com/DS4SD/DocLayNet)
- πŸ”¬ **Research-Backed**: Based on [Advanced Literate Machinery](https://github.com/AlibabaResearch/AdvancedLiterateMachinery)

**Resource Requirements**:
- GPU: 5GB+ VRAM (recommended)
- CPU: Falls back automatically if GPU unavailable
- Processing Speed: ~1.75 seconds/page (GPU [GTX 1070]) or ~13.5 seconds/page (CPU [i7-8700])

### 2. LightGBM Models - Fast & Efficient

**Overview**: Lightweight ensemble of two specialized models using XML-based features from Poppler.

**Key Features**:
- ⚑ **High Speed**: ~0.42 seconds per page on CPU (i7-8700)
- πŸ’Ύ **Low Resource Usage**: CPU-only, minimal memory footprint
- πŸ”„ **Dual Model Approach**:
  - **Token Type Classifier**: Identifies content types (title, text, table, etc.)
  - **Segmentation Model**: Determines proper content boundaries
- πŸ“„ **XML-Based**: Uses Poppler's PDF-to-XML conversion for feature extraction

**Trade-offs**:
- Slightly lower accuracy compared to VGT
- No visual context understanding
- Excellent for batch processing and resource-constrained environments

### OCR Integration

Both models integrate seamlessly with OCR capabilities:

- **Engine**: [Tesseract OCR](https://github.com/tesseract-ocr/tesseract)
- **Processing**: [ocrmypdf](https://ocrmypdf.readthedocs.io/en/latest/index.html)
- **Languages**: 150+ supported languages
- **Output**: Searchable PDFs with preserved layout

### Model Selection Guide

| Use Case | Recommended Model | Reason |
|----------|------------------|---------|
| High accuracy requirements | VGT | Superior visual understanding |
| Batch processing | LightGBM | Faster processing, lower resources |
| GPU available | VGT | Leverages GPU acceleration |
| CPU-only environment | LightGBM | Optimized for CPU processing |
| Real-time applications | LightGBM | Consistent fast response times |
| Research/analysis | VGT | Best accuracy for detailed analysis |

## πŸ“Š Data

### Training Dataset

Both model types are trained on the comprehensive [DocLayNet dataset](https://github.com/DS4SD/DocLayNet), a large-scale document layout analysis dataset containing over 80,000 document pages.

### Document Categories

The models can identify and classify 11 distinct content types:

| ID | Category | Description |
|----|----------|-------------|
| 1 | **Caption** | Image and table captions |
| 2 | **Footnote** | Footnote references and text |
| 3 | **Formula** | Mathematical equations and formulas |
| 4 | **List item** | Bulleted and numbered list items |
| 5 | **Page footer** | Footer content and page numbers |
| 6 | **Page header** | Header content and titles |
| 7 | **Picture** | Images, figures, and graphics |
| 8 | **Section header** | Section and subsection headings |
| 9 | **Table** | Tabular data and structures |
| 10 | **Text** | Regular paragraph text |
| 11 | **Title** | Document and chapter titles |

### Dataset Characteristics

- **Domain Coverage**: Academic papers, technical documents, reports
- **Language**: Primarily English with multilingual support
- **Quality**: High-quality annotations with bounding boxes and labels
- **Diversity**: Various document layouts, fonts, and formatting styles

For detailed information about the dataset, visit the [DocLayNet repository](https://github.com/DS4SD/DocLayNet).

## πŸ”§ Development

### Local Development Setup

1. **Clone the repository:**
   ```bash
   git clone https://github.com/huridocs/pdf-document-layout-analysis.git
   cd pdf-document-layout-analysis
   ```

2. **Create virtual environment:**
   ```bash
   make install_venv
   ```

3. **Activate environment:**
   ```bash
   make activate
   # or manually: source .venv/bin/activate
   ```

4. **Install dependencies:**
   ```bash
   make install
   ```

### Code Quality

**Format code:**
```bash
make formatter
```

**Check formatting:**
```bash
make check_format
```

### Testing

**Run tests:**
```bash
make test
```

**Integration tests:**
```bash
# Tests are located in src/tests/integration/
python -m pytest src/tests/integration/test_end_to_end.py
```

### Docker Development

**Build and start (detached mode):**
```bash
# With GPU
make start_detached_gpu

# Without GPU  
make start_detached
```

**Clean up Docker resources:**
```bash
# Remove containers
make remove_docker_containers

# Remove images
make remove_docker_images
```

### Project Structure

```
pdf-document-layout-analysis/
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ domain/            # Business entities
β”‚   β”œβ”€β”€ use_cases/         # Application logic
β”‚   β”œβ”€β”€ adapters/          # External integrations
β”‚   β”œβ”€β”€ ports/             # Interface definitions
β”‚   └── drivers/           # Framework configurations
β”œβ”€β”€ test_pdfs/             # Test PDF files
β”œβ”€β”€ models/                # ML model storage
β”œβ”€β”€ docker-compose.yml     # Docker configuration
β”œβ”€β”€ Dockerfile             # Container definition
β”œβ”€β”€ Makefile              # Development commands
β”œβ”€β”€ pyproject.toml        # Python project configuration
└── requirements.txt      # Python dependencies
```

### Environment Variables

Key configuration options:

```bash
# OCR configuration
OCR_SOURCE=/tmp/ocr_source

# Model paths (auto-configured)
MODELS_PATH=./models

# Service configuration  
HOST=0.0.0.0
PORT=5060
```

### Adding New Features

1. **Domain Logic**: Add entities in `src/domain/`
2. **Use Cases**: Implement business logic in `src/use_cases/`
3. **Adapters**: Create integrations in `src/adapters/`
4. **Ports**: Define interfaces in `src/ports/`
5. **Controllers**: Add endpoints in `src/adapters/web/`

### Debugging

**View logs:**
```bash
docker compose logs -f
```

**Access container:**
```bash
docker exec -it pdf-document-layout-analysis /bin/bash
```

**Free up disk space:**
```bash
make free_up_space
```

### Order of Output Elements

The service returns SegmentBox elements in a carefully determined reading order:

#### Reading Order Algorithm

1. **Poppler Integration**: Uses [Poppler](https://poppler.freedesktop.org) PDF-to-XML conversion to establish initial token reading order
2. **Segment Averaging**: Calculates average reading order for multi-token segments
3. **Type-Based Sorting**: Prioritizes content types:
   - **Headers** placed first
   - **Main content** in reading order
   - **Footers and footnotes** placed last

#### Non-Text Elements

For segments without text (e.g., images):
- Processed after text-based sorting
- Positioned based on nearest text segment proximity
- Uses spatial distance as the primary criterion

### Advanced Table and Formula Extraction

#### Default Behavior
- **Formulas**: Automatically extracted as LaTeX format in the `text` property
- **Tables**: Basic text extraction included by default

#### Enhanced Table Extraction

OCR tables and extract them in HTML format by setting `ocr_tables=true`:

```bash
curl -X POST -F '[email protected]' -F 'ocr_tables=true' http://localhost:5060
```


#### Extraction Engines
- **Formulas**: [LaTeX-OCR](https://github.com/lukas-blecher/LaTeX-OCR)
- **Tables**: [RapidTable](https://github.com/RapidAI/RapidTable)


## πŸ“ˆ Benchmarks

### Performance

VGT model performance on PubLayNet dataset:

| Metric | Overall | Text | Title | List | Table | Figure |
|--------|---------|------|-------|------|-------|--------|
| **F1 Score** | **0.962** | 0.950 | 0.939 | 0.968 | 0.981 | 0.971 |

> πŸ“Š **Comparison**: View comprehensive model comparisons at [Papers With Code](https://paperswithcode.com/sota/document-layout-analysis-on-publaynet-val)

### Speed

Performance benchmarks on 15-page academic documents:

| Model | Hardware | Speed (sec/page) | Use Case |
|-------|----------|------------------|----------|
| **LightGBM** | CPU (i7-8700 3.2GHz) | **0.42** | Fast processing |
| **VGT** | GPU (GTX 1070) | **1.75** | High accuracy |
| **VGT** | CPU (i7-8700 3.2GHz) | 13.5 | CPU fallback |

### Performance Recommendations

- **GPU Available**: Use VGT for best accuracy-speed balance
- **CPU Only**: Use LightGBM for optimal performance
- **Batch Processing**: LightGBM for consistent throughput
- **High Accuracy**: VGT with GPU for best results


## 🌐 Installation of More Languages for OCR

The service uses Tesseract OCR with support for 150+ languages. The Docker image includes only common languages to minimize image size.

### Installing Additional Languages

#### 1. Access the Container
```bash
docker exec -it --user root pdf-document-layout-analysis /bin/bash
```

#### 2. Install Language Packs
```bash
# Install specific language
apt-get update
apt-get install tesseract-ocr-[LANGCODE]
```

#### 3. Common Language Examples

```bash
# Korean
apt-get install tesseract-ocr-kor

# German  
apt-get install tesseract-ocr-deu

# French
apt-get install tesseract-ocr-fra

# Spanish
apt-get install tesseract-ocr-spa

# Chinese Simplified
apt-get install tesseract-ocr-chi-sim

# Arabic
apt-get install tesseract-ocr-ara

# Japanese
apt-get install tesseract-ocr-jpn
```

#### 4. Verify Installation

```bash
curl http://localhost:5060/info
```

### Language Code Reference

Find Tesseract language codes in the [ISO to Tesseract mapping](https://github.com/huridocs/pdf-document-layout-analysis/blob/main/src/adapters/infrastructure/ocr/languages.py).

### Supported Languages

Common language codes:
- `eng` - English
- `fra` - French  
- `deu` - German
- `spa` - Spanish
- `ita` - Italian
- `por` - Portuguese
- `rus` - Russian
- `chi-sim` - Chinese Simplified
- `chi-tra` - Chinese Traditional
- `jpn` - Japanese
- `kor` - Korean
- `ara` - Arabic
- `hin` - Hindi

### Usage with Multiple Languages

```bash
# OCR with specific language
curl -X POST \
  -F '[email protected]' \
  -F 'language=fr' \
  http://localhost:5060/ocr \
  --output french_ocr.pdf
```


## πŸ”— Related Services

Explore our ecosystem of PDF processing services built on this foundation:

### [PDF Table of Contents Extractor](https://github.com/huridocs/pdf-table-of-contents-extractor)
πŸ” **Purpose**: Intelligent extraction of structured table of contents from PDF documents

**Key Features**:
- Leverages layout analysis for accurate TOC identification
- Hierarchical structure recognition
- Multiple output formats supported
- Integration-ready API

### [PDF Text Extraction](https://github.com/huridocs/pdf-text-extraction)
πŸ“ **Purpose**: Advanced text extraction with layout awareness

**Key Features**:
- Content-type aware extraction
- Preserves document structure
- Reading order optimization
- Clean text output with metadata

### Integration Benefits

These services work seamlessly together:
- **Shared Analysis**: Reuse layout analysis results across services
- **Consistent Output**: Standardized JSON format for easy integration
- **Scalable Architecture**: Deploy services independently or together
- **Docker Ready**: All services containerized for easy deployment

## 🀝 Contributing

We welcome contributions to improve the PDF Document Layout Analysis service!

### How to Contribute

1. **Fork the Repository**
   ```bash
   git clone https://github.com/your-username/pdf-document-layout-analysis.git
   ```

2. **Create a Feature Branch**
   ```bash
   git checkout -b feature/your-feature-name
   ```

3. **Set Up Development Environment**
   ```bash
   make install_venv
   make install
   ```

4. **Make Your Changes**
   - Follow the Clean Architecture principles
   - Add tests for new features
   - Update documentation as needed

5. **Run Tests and Quality Checks**
   ```bash
   make test
   make check_format
   ```

6. **Submit a Pull Request**
   - Provide clear description of changes
   - Include test results
   - Reference any related issues

### Contribution Guidelines

#### Code Standards
- **Python**: Follow PEP 8 with 125-character line length
- **Architecture**: Maintain Clean Architecture boundaries
- **Testing**: Include unit tests for new functionality
- **Documentation**: Update README and docstrings

#### Areas for Contribution

- πŸ› **Bug Fixes**: Report and fix issues
- ✨ **New Features**: Add new endpoints or functionality
- πŸ“š **Documentation**: Improve guides and examples
- πŸ§ͺ **Testing**: Expand test coverage
- πŸš€ **Performance**: Optimize processing speed
- 🌐 **Internationalization**: Add language support

#### Development Workflow

1. **Issue First**: Create or comment on relevant issues
2. **Small PRs**: Keep pull requests focused and manageable
3. **Clean Commits**: Use descriptive commit messages
4. **Documentation**: Update relevant documentation
5. **Testing**: Ensure all tests pass

### Getting Help

- πŸ“š **Documentation**: Check this README and inline docs
- πŸ’¬ **Issues**: Search existing issues or create new ones
- πŸ” **Code**: Explore the codebase structure
- πŸ“§ **Contact**: Reach out to maintainers for guidance

---

### License

This project is licensed under the terms specified in the [LICENSE](https://github.com/huridocs/pdf-document-layout-analysis/blob/main/LICENSE) file.