From 971deba8da82c6e15fa7cafacf3aab3b78a124c8 Mon Sep 17 00:00:00 2001 From: 72004 Date: Sat, 15 Aug 2026 00:14:01 +0500 Subject: [PATCH] Refactor ImageConverter to reuse llm_caption --- .../markitdown/converters/_image_converter.py | 60 +------------------ 1 file changed, 3 insertions(+), 57 deletions(-) diff --git a/packages/markitdown/src/markitdown/converters/_image_converter.py b/packages/markitdown/src/markitdown/converters/_image_converter.py index dd8fbac61c..b68b5afc47 100644 --- a/packages/markitdown/src/markitdown/converters/_image_converter.py +++ b/packages/markitdown/src/markitdown/converters/_image_converter.py @@ -1,7 +1,6 @@ -from typing import BinaryIO, Any, Union -import base64 -import mimetypes +from typing import BinaryIO, Any from ._exiftool import exiftool_metadata +from ._llm_caption import llm_caption from .._base_converter import DocumentConverter, DocumentConverterResult from .._stream_info import StreamInfo @@ -69,7 +68,7 @@ def convert( llm_client = kwargs.get("llm_client") llm_model = kwargs.get("llm_model") if llm_client is not None and llm_model is not None: - llm_description = self._get_llm_description( + llm_description = llm_caption( file_stream, stream_info, client=llm_client, @@ -83,56 +82,3 @@ def convert( return DocumentConverterResult( markdown=md_content, ) - - def _get_llm_description( - self, - file_stream: BinaryIO, - stream_info: StreamInfo, - *, - client, - model, - prompt=None, - ) -> Union[None, str]: - if prompt is None or prompt.strip() == "": - prompt = "Write a detailed caption for this image." - - # Get the content type - content_type = stream_info.mimetype - if not content_type: - content_type, _ = mimetypes.guess_type( - "_dummy" + (stream_info.extension or "") - ) - if not content_type: - content_type = "application/octet-stream" - - # Convert to base64 - cur_pos = file_stream.tell() - try: - base64_image = base64.b64encode(file_stream.read()).decode("utf-8") - except Exception as e: - return None - finally: - file_stream.seek(cur_pos) - - # Prepare the data-uri - data_uri = f"data:{content_type};base64,{base64_image}" - - # Prepare the OpenAI API request - messages = [ - { - "role": "user", - "content": [ - {"type": "text", "text": prompt}, - { - "type": "image_url", - "image_url": { - "url": data_uri, - }, - }, - ], - } - ] - - # Call the OpenAI API - response = client.chat.completions.create(model=model, messages=messages) - return response.choices[0].message.content