fix: control UnidentifiedImageError

This commit is contained in:
Alfonso Lozana
2026-07-29 11:44:48 +02:00
parent e4d1522611
commit df2cc8ccee

View File

@@ -11,8 +11,10 @@ from enum import Enum
from pathlib import Path
from typing import Any
from PIL import UnidentifiedImageError
from llama_index.core.ingestion import arun_transformations
from llama_index.core.schema import BaseNode, Document
from pptx.exc import PackageNotFoundError
from pptx2md import convert # ty:ignore[unresolved-import]
from private_gpt.celery.notify import NotifyProtocol
@@ -177,11 +179,10 @@ class PPTX2MdReader(IngestionReader):
output_md = temp_dir / "output.md"
image_dir = temp_dir / "img"
convert(
convert_kwargs = dict(
pptx_path=str(file_info.file_data.absolute()),
output=str(output_md),
image_dir=str(image_dir),
disable_image=False,
disable_wmf=False,
disable_notes=True,
disable_escaping=True,
@@ -190,6 +191,24 @@ class PPTX2MdReader(IngestionReader):
min_block_size=5,
)
try:
convert(disable_image=False, **convert_kwargs)
except UnidentifiedImageError as e:
logger.warning(
"Image extraction failed for %s due to an unsupported/corrupt "
"embedded image (%s). Retrying conversion with images disabled.",
file_info.file_name,
e,
)
convert(disable_image=True, **convert_kwargs)
except PackageNotFoundError as e:
logger.error(
"File %s could not be opened as a valid PPTX package: %s",
file_info.file_name,
e,
)
raise
with open(output_md, encoding="utf-8") as f:
text = f.read()