Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LLM-based image description to PptxConverter #218

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,17 @@ def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]:
except Exception:
pass

# Try describing the image using GPTV
llm_client = kwargs.get("llm_client")
llm_model = kwargs.get("llm_model")
if llm_client is not None and llm_model is not None:
alt_text += self._get_llm_description(
shape.image,
llm_client,
llm_model,
prompt=kwargs.get("llm_prompt"),
).strip()

# A placeholder name
filename = re.sub(r"\W", "", shape.name) + ".jpg"
md_content += (
Expand Down Expand Up @@ -857,6 +868,37 @@ def _convert_chart_to_markdown(self, chart):
separator = "|" + "|".join(["---"] * len(data[0])) + "|"
return md + "\n".join([header, separator] + markdown_table[1:])

def _get_llm_description(self, image, client, model, prompt=None):
if image.content_type not in [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
]:
return "" # https://platform.openai.com/docs/guides/vision#what-type-of-files-can-i-upload
if prompt is None or prompt.strip() == "":
prompt = "Write a caption for this image."
image_base64 = base64.b64encode(image.blob).decode("utf-8")
data_uri = f"data:{image.content_type};base64,{image_base64}"

messages = [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": data_uri,
},
},
],
}
]

response = client.chat.completions.create(model=model, messages=messages)
return response.choices[0].message.content


class MediaConverter(DocumentConverter):
"""
Expand Down
Binary file modified tests/test_files/test.pptx
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/test_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,26 @@ def test_markitdown_llm() -> None:
assert test_string in result.text_content.lower()


@pytest.mark.skipif(
skip_llm,
reason="do not run llm tests without a key",
)
def test_markitdown_pptx_llm() -> None:
client = openai.OpenAI()
markitdown = MarkItDown(llm_client=client, llm_model="gpt-4o-mini")

result = markitdown.convert(os.path.join(TEST_FILES_DIR, "test.pptx"))

# like test_markitdown_llm, this should be improved
for test_string in ["red", "blue"]:
assert test_string in result.text_content.lower()


if __name__ == "__main__":
"""Runs this file's tests from the command line."""
test_markitdown_remote()
test_markitdown_local()
test_markitdown_exiftool()
test_markitdown_deprecation()
test_markitdown_llm()
test_markitdown_pptx_llm()