Source code for mllm_shap.backends.transformers.image

"""Image parts for a Hugging Face processor.

@domain backends
@see [[knowledge/architecture/backends]]
@tested-by [[tests/backends/test_transformers_image.py]]
"""

import io
from typing import Any

from ...alignment.io import require
from ...domain.conversation import ImagePart
from .audio import template_mentions


[docs] def image_template_part(part: ImagePart) -> dict[str, Any]: """Return a chat-template image part holding ``part`` as an RGB Pillow image.""" image_module = require("PIL.Image") with image_module.open(io.BytesIO(part.data())) as image: return {"type": "image", "image": image.convert("RGB")}
[docs] def template_has_image(processor: Any) -> bool: """Return whether the processor's chat template takes image parts.""" return template_mentions(processor, "image")