|
10 | 10 | from onyx.configs.app_configs import IMAGE_SUMMARIZATION_USER_PROMPT
|
11 | 11 | from onyx.llm.interfaces import LLM
|
12 | 12 | from onyx.llm.utils import message_to_string
|
| 13 | +from onyx.utils.b64 import get_image_type_from_bytes |
13 | 14 | from onyx.utils.logger import setup_logger
|
14 | 15 |
|
15 | 16 | logger = setup_logger()
|
16 | 17 |
|
17 | 18 |
|
| 19 | +class UnsupportedImageFormatError(ValueError): |
| 20 | + """Raised when an image uses a MIME type unsupported by the summarization flow.""" |
| 21 | + |
| 22 | + |
18 | 23 | def prepare_image_bytes(image_data: bytes) -> str:
|
19 | 24 | """Prepare image bytes for summarization.
|
20 | 25 | Resizes image if it's larger than 20MB. Encodes image as a base64 string."""
|
@@ -74,7 +79,14 @@ def summarize_image_with_error_handling(
|
74 | 79 | user_prompt = (
|
75 | 80 | f"The image has the file name '{context_name}'.\n{user_prompt_template}"
|
76 | 81 | )
|
77 |
| - return summarize_image_pipeline(llm, image_data, user_prompt, system_prompt) |
| 82 | + try: |
| 83 | + return summarize_image_pipeline(llm, image_data, user_prompt, system_prompt) |
| 84 | + except UnsupportedImageFormatError: |
| 85 | + logger.info( |
| 86 | + "Skipping image summarization due to unsupported MIME type for %s", |
| 87 | + context_name, |
| 88 | + ) |
| 89 | + return None |
78 | 90 |
|
79 | 91 |
|
80 | 92 | def _summarize_image(
|
@@ -109,10 +121,17 @@ def _summarize_image(
|
109 | 121 |
|
110 | 122 |
|
111 | 123 | def _encode_image_for_llm_prompt(image_data: bytes) -> str:
|
112 |
| - """Getting the base64 string.""" |
| 124 | + """Prepare a data URL with the correct MIME type for the LLM message.""" |
| 125 | + try: |
| 126 | + mime_type = get_image_type_from_bytes(image_data) |
| 127 | + except ValueError as exc: |
| 128 | + raise UnsupportedImageFormatError( |
| 129 | + "Unsupported image format for summarization" |
| 130 | + ) from exc |
| 131 | + |
113 | 132 | base64_encoded_data = base64.b64encode(image_data).decode("utf-8")
|
114 | 133 |
|
115 |
| - return f"data:image/jpeg;base64,{base64_encoded_data}" |
| 134 | + return f"data:{mime_type};base64,{base64_encoded_data}" |
116 | 135 |
|
117 | 136 |
|
118 | 137 | def _resize_image_if_needed(image_data: bytes, max_size_mb: int = 20) -> bytes:
|
|
0 commit comments