|
1 | 1 | import logging |
2 | 2 | import os |
3 | 3 | from pathlib import Path |
| 4 | +from typing import Optional |
4 | 5 |
|
5 | 6 | import requests |
| 7 | +from docling_core.types.doc.page import SegmentedPage |
6 | 8 | from dotenv import load_dotenv |
7 | 9 |
|
8 | 10 | from docling.datamodel.base_models import InputFormat |
@@ -32,6 +34,69 @@ def lms_vlm_options(model: str, prompt: str, format: ResponseFormat): |
32 | 34 | return options |
33 | 35 |
|
34 | 36 |
|
| 37 | +#### Using LM Studio with OlmOcr model |
| 38 | + |
| 39 | + |
| 40 | +def lms_olmocr_vlm_options(model: str): |
| 41 | + def _dynamic_olmocr_prompt(page: Optional[SegmentedPage]): |
| 42 | + if page is None: |
| 43 | + return ( |
| 44 | + "Below is the image of one page of a document. Just return the plain text" |
| 45 | + " representation of this document as if you were reading it naturally.\n" |
| 46 | + "Do not hallucinate.\n" |
| 47 | + ) |
| 48 | + |
| 49 | + anchor = [ |
| 50 | + f"Page dimensions: {int(page.dimension.width)}x{int(page.dimension.height)}" |
| 51 | + ] |
| 52 | + |
| 53 | + for text_cell in page.textline_cells: |
| 54 | + if not text_cell.text.strip(): |
| 55 | + continue |
| 56 | + bbox = text_cell.rect.to_bounding_box().to_bottom_left_origin( |
| 57 | + page.dimension.height |
| 58 | + ) |
| 59 | + anchor.append(f"[{int(bbox.l)}x{int(bbox.b)}] {text_cell.text}") |
| 60 | + |
| 61 | + for image_cell in page.bitmap_resources: |
| 62 | + bbox = image_cell.rect.to_bounding_box().to_bottom_left_origin( |
| 63 | + page.dimension.height |
| 64 | + ) |
| 65 | + anchor.append( |
| 66 | + f"[Image {int(bbox.l)}x{int(bbox.b)} to {int(bbox.r)}x{int(bbox.t)}]" |
| 67 | + ) |
| 68 | + |
| 69 | + if len(anchor) == 1: |
| 70 | + anchor.append( |
| 71 | + f"[Image 0x0 to {int(page.dimension.width)}x{int(page.dimension.height)}]" |
| 72 | + ) |
| 73 | + |
| 74 | + # Original prompt uses cells sorting. We are skipping it in this demo. |
| 75 | + |
| 76 | + base_text = "\n".join(anchor) |
| 77 | + |
| 78 | + return ( |
| 79 | + f"Below is the image of one page of a document, as well as some raw textual" |
| 80 | + f" content that was previously extracted for it. Just return the plain text" |
| 81 | + f" representation of this document as if you were reading it naturally.\n" |
| 82 | + f"Do not hallucinate.\n" |
| 83 | + f"RAW_TEXT_START\n{base_text}\nRAW_TEXT_END" |
| 84 | + ) |
| 85 | + |
| 86 | + options = ApiVlmOptions( |
| 87 | + url="http://localhost:1234/v1/chat/completions", |
| 88 | + params=dict( |
| 89 | + model=model, |
| 90 | + ), |
| 91 | + prompt=_dynamic_olmocr_prompt, |
| 92 | + timeout=90, |
| 93 | + scale=1.0, |
| 94 | + max_size=1024, # from OlmOcr pipeline |
| 95 | + response_format=ResponseFormat.MARKDOWN, |
| 96 | + ) |
| 97 | + return options |
| 98 | + |
| 99 | + |
35 | 100 | #### Using Ollama |
36 | 101 |
|
37 | 102 |
|
@@ -123,6 +188,12 @@ def main(): |
123 | 188 | # format=ResponseFormat.MARKDOWN, |
124 | 189 | # ) |
125 | 190 |
|
| 191 | + # Example using the OlmOcr (dynamic prompt) model with LM Studio: |
| 192 | + # (uncomment the following lines) |
| 193 | + # pipeline_options.vlm_options = lms_olmocr_vlm_options( |
| 194 | + # model="hf.co/lmstudio-community/olmOCR-7B-0225-preview-GGUF", |
| 195 | + # ) |
| 196 | + |
126 | 197 | # Example using the Granite Vision model with Ollama: |
127 | 198 | # (uncomment the following lines) |
128 | 199 | # pipeline_options.vlm_options = ollama_vlm_options( |
|
0 commit comments