SLM Document Parser
Extract tabular metrics, metadata structures, and key paragraphs from PDFs, Word docs, and plain text files locally under strict MIT-licensing parameters.
🚀 Overview & Capabilities
The SLM Document Parser leverages Microsoft's MIT-licensed Phi-3.5-mini-instruct and Florence-2-large models optimized via ONNX Runtime GenAI to parse multi-formatted documents offline. It supports high-fidelity layout preservation for scanned PDFs and legacied formats (.pdf, .docx, .doc, .pptx, .ppt, .txt) to output structured text and semantic RAG database chunks.
🤖 Truly Agentic Hybrid Visual Pipeline
The Document Parser handles layout mapping by combining visual coordinate classifiers with standard extraction loops:
- LibreOffice Conversion Layer: Converts complex formats (DOC/PPT/PPTX) into intermediate PDFs in the background using headless
sofficeconversions. - Page Image Rendering: Renders PDF page indices to images sequentially using
pypdfium2. - Florence-2 OCR & OD: Scans rendered page layouts, running Object Detection (
<OD>) to locate table and figure boxes. Crops table boxes to OCR them individually, and runs detailed caption generation (<DETAILED_CAPTION>) for diagrams. - Layout Reconstruction: Passes text, parsed tables, and diagram captions to Phi-3.5 to synthesize a unified, formatted Markdown page representation.
- Semantic Chunker & Linker: Segments markdown into structured paragraph chunks, extracts heading hierarchies, resolves product entities, and links related sections together.
📊 Complex Table Visual OCR Extraction
When the visual parser encounters complex tables inside a document scan, it crops the table coordinates, routes it to the vision parser, and synthesizes a natural language text description. Below is an example of an input expense table and its corresponding parsed description:
### Q2 FY2024 Company Expense Summary
The table outlines corporate expenditures across multiple categories:
- **Infrastructure**: Spent $1,195,450 against a $1,250,000 budget (a decrease of 4.36%).
- **AI Compute Nodes**: Spent $3,842,910 against a $3,500,000 budget (an increase of 9.80%).
- **Model Conversion**: Spent $412,300 against a $450,000 budget (a decrease of 8.38%).
- **Data Storage**: Spent $845,600 against a $800,000 budget (an increase of 5.70%).
- **GPU Clusters**: Spent $2,215,800 against a $2,100,000 budget (an increase of 5.51%).
⚡ CPU Performance Tuning Guidelines
Follow these configuration rules to optimize latency on standard CPUs:
- Core Thread Cap: Set
n_threadsstrictly to the number of physical cores to avoid context switching thread collisions. - Sequential Processing: Process multi-page images sequentially instead of in parallel batches to keep the memory footprint under 2.0 GB.
- OpenMP Settings: Keep thread counts aligned in your shell environment:
export OMP_NUM_THREADS=4 export MKL_NUM_THREADS=4
API Reference
`SLMDocumentParser` Initialization
from slm_document_parser.document_parser import SLMDocumentParser
parser = SLMDocumentParser(n_ctx=4096, n_threads=4)
| Parameter | Type / Default | Description |
|---|---|---|
| model_path | str | None | Local path to Phi-3.5 weight checkpoints. Defaults to "../../models/phi-3.5-mini-instruct-onnx". |
| n_ctx | int | 4096 | Inference token window size. Phi-3.5 supports up to 128K context tokens. |
| n_threads | int | 4 | Allocated CPU cores for ORT execution. |
| system_prompt | str | None | Optional custom system prompt instructions overriding the default template. |
| user_input | str | None | Optional additional user-supplied target parameters or variables. |
`extract_text` Method
Converts document layouts and images into a single reconstructed Markdown text string:
markdown_content = parser.extract_text("financial_report.pdf")
`chunk_document` Method
Extracts markdown text and slices it into linked semantic chunks with metadata properties:
chunks = parser.chunk_document("financial_report.pdf")
print(chunks[0])
{
"text": "SpaceX successfully launched the Falcon 9 rocket from Cape Canaveral Space Force Station, landing the booster return flight for the 15th time. The mission delivered communication payloads into low Earth orbit.",
"metadata": {
"source": "financial_report.pdf",
"heading": "1. Launch Milestones",
"subheading": "Falcon 9 Performance",
"product": "SpaceX",
"key_terms": ["Falcon 9", "Cape Canaveral", "booster"],
"format": "pdf",
"chunk_index": 0,
"related_chunks": [1, 2]
}
}
`parse_and_chunk_stream` Method
Streaming generator yielding semantic chunks page-by-page as they are processed to reduce first-chunk response times (TTFT):
for chunk in parser.parse_and_chunk_stream("multi_page_manual.pdf"):
print(f"Processed Chunk: {chunk['text'][:100]}...")
`export_chunks_to_excel` Method
Saves the parsed chunks to an Excel spreadsheet:
# Exports to workbook. Set append=True to write to an existing sheet
parser.export_chunks_to_excel(chunks, "rag_dataset.xlsx", append=False)
| Chunk Index | Source File | Heading | Subheading | Product | Related Chunks | Text |
|---|---|---|---|---|---|---|
| 0 | financial_report.pdf | 1. Launch Milestones | Falcon 9 Performance | SpaceX | 1,2 | SpaceX successfully launched the Falcon 9... |
| system_prompt | str | None | Optional custom system prompt instructions overriding the default template. | ||||
| user_input | str | None | Optional additional user-supplied target parameters or variables. |
🐙 Checkout from GitHub
Clone only this agent's folder from the monorepo using Git sparse-checkout — no need to download the full repository:
Option 1 — Sparse Checkout (Recommended)
Option 2 — Full Repository Clone
💡 Tip: After checkout, install the package locally with pip install -e ./slm_document_parser to run in editable mode without publishing to PyPI.