|
| 1 | +"""Picture description model using LangChain primitives.""" |
| 2 | + |
| 3 | +import base64 |
| 4 | +import io |
| 5 | +from collections.abc import Iterable |
| 6 | +from pathlib import Path |
| 7 | +from typing import ClassVar, Literal, Optional, Type, Union |
| 8 | + |
| 9 | +from docling.datamodel.accelerator_options import AcceleratorOptions |
| 10 | +from docling.datamodel.pipeline_options import PictureDescriptionBaseOptions |
| 11 | +from docling.models.picture_description_base_model import PictureDescriptionBaseModel |
| 12 | +from docling.models.utils.hf_model_download import HuggingFaceModelDownloadMixin |
| 13 | +from langchain_core.language_models.chat_models import BaseChatModel |
| 14 | +from PIL import Image |
| 15 | + |
| 16 | + |
| 17 | +class PictureDescriptionLangChainOptions(PictureDescriptionBaseOptions): |
| 18 | + """Options for the PictureDescriptionLangChainModel.""" |
| 19 | + |
| 20 | + kind: ClassVar[Literal["langchain"]] = "langchain" |
| 21 | + llm: BaseChatModel |
| 22 | + prompt: str = "Describe this document picture in a few sentences." |
| 23 | + provenance: Optional[str] = None |
| 24 | + |
| 25 | + |
| 26 | +class PictureDescriptionLangChainModel( |
| 27 | + PictureDescriptionBaseModel, HuggingFaceModelDownloadMixin |
| 28 | +): |
| 29 | + """Implementation of a PictureDescription model using LangChain.""" |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def get_options_type(cls) -> Type[PictureDescriptionBaseOptions]: |
| 33 | + """Define the option type for the factory.""" |
| 34 | + return PictureDescriptionLangChainOptions |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + enabled: bool, |
| 39 | + enable_remote_services: bool, |
| 40 | + artifacts_path: Optional[Union[Path, str]], |
| 41 | + options: PictureDescriptionLangChainOptions, |
| 42 | + accelerator_options: AcceleratorOptions, |
| 43 | + ): |
| 44 | + """Initialize PictureDescriptionLangChainModel.""" |
| 45 | + super().__init__( |
| 46 | + enabled=enabled, |
| 47 | + enable_remote_services=enable_remote_services, |
| 48 | + artifacts_path=artifacts_path, |
| 49 | + options=options, |
| 50 | + accelerator_options=accelerator_options, |
| 51 | + ) |
| 52 | + self.options: PictureDescriptionLangChainOptions |
| 53 | + |
| 54 | + if self.enabled: |
| 55 | + self.llm = self.options.llm |
| 56 | + self.provenance = "langchain" |
| 57 | + if self.options.provenance: |
| 58 | + self.provenance += f"-{self.options.provenance}" |
| 59 | + |
| 60 | + def _annotate_images(self, images: Iterable[Image.Image]) -> Iterable[str]: |
| 61 | + """Annotate the images with the LangChain model.""" |
| 62 | + # Create input messages |
| 63 | + batch_messages = [] |
| 64 | + |
| 65 | + for image in images: |
| 66 | + buffered = io.BytesIO() |
| 67 | + image.save(buffered, format="PNG") |
| 68 | + image_data = base64.b64encode(buffered.getvalue()).decode("utf-8") |
| 69 | + batch_messages.append( |
| 70 | + [ |
| 71 | + { |
| 72 | + "role": "user", |
| 73 | + "content": [ |
| 74 | + {"type": "text", "text": self.options.prompt}, |
| 75 | + { |
| 76 | + "type": "image_url", |
| 77 | + "image_url": { |
| 78 | + "url": f"data:image/png;base64,{image_data}" |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + } |
| 83 | + ] |
| 84 | + ) |
| 85 | + |
| 86 | + responses = self.llm.batch(batch_messages) |
| 87 | + for resp in responses: |
| 88 | + yield resp.text() |
0 commit comments