chatterer 0.1.25__py3-none-any.whl → 0.1.27__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. chatterer/__init__.py +87 -97
  2. chatterer/common_types/__init__.py +21 -21
  3. chatterer/common_types/io.py +19 -19
  4. chatterer/constants.py +5 -0
  5. chatterer/examples/__main__.py +75 -75
  6. chatterer/examples/any2md.py +83 -85
  7. chatterer/examples/pdf2md.py +231 -338
  8. chatterer/examples/pdf2txt.py +52 -54
  9. chatterer/examples/ppt.py +487 -486
  10. chatterer/examples/pw.py +141 -143
  11. chatterer/examples/snippet.py +54 -56
  12. chatterer/examples/transcribe.py +192 -192
  13. chatterer/examples/upstage.py +87 -89
  14. chatterer/examples/web2md.py +80 -80
  15. chatterer/interactive.py +422 -354
  16. chatterer/language_model.py +530 -536
  17. chatterer/messages.py +21 -21
  18. chatterer/tools/__init__.py +46 -46
  19. chatterer/tools/caption_markdown_images.py +388 -384
  20. chatterer/tools/citation_chunking/__init__.py +3 -3
  21. chatterer/tools/citation_chunking/chunks.py +51 -53
  22. chatterer/tools/citation_chunking/citation_chunker.py +117 -118
  23. chatterer/tools/citation_chunking/citations.py +284 -285
  24. chatterer/tools/citation_chunking/prompt.py +157 -157
  25. chatterer/tools/citation_chunking/reference.py +26 -26
  26. chatterer/tools/citation_chunking/utils.py +138 -138
  27. chatterer/tools/convert_pdf_to_markdown.py +636 -645
  28. chatterer/tools/convert_to_text.py +446 -446
  29. chatterer/tools/upstage_document_parser.py +704 -705
  30. chatterer/tools/webpage_to_markdown.py +739 -739
  31. chatterer/tools/youtube.py +146 -147
  32. chatterer/utils/__init__.py +15 -15
  33. chatterer/utils/base64_image.py +349 -293
  34. chatterer/utils/bytesio.py +59 -59
  35. chatterer/utils/code_agent.py +237 -237
  36. chatterer/utils/imghdr.py +145 -148
  37. {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/METADATA +377 -390
  38. chatterer-0.1.27.dist-info/RECORD +43 -0
  39. chatterer/strategies/__init__.py +0 -13
  40. chatterer/strategies/atom_of_thoughts.py +0 -975
  41. chatterer/strategies/base.py +0 -14
  42. chatterer-0.1.25.dist-info/RECORD +0 -45
  43. {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/WHEEL +0 -0
  44. {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/entry_points.txt +0 -0
  45. {chatterer-0.1.25.dist-info → chatterer-0.1.27.dist-info}/top_level.txt +0 -0
@@ -1,293 +1,349 @@
1
- from __future__ import annotations
2
-
3
- import re
4
- from base64 import b64encode
5
- from io import BytesIO
6
- from logging import getLogger
7
- from pathlib import Path
8
- from typing import (
9
- TYPE_CHECKING,
10
- Awaitable,
11
- Callable,
12
- ClassVar,
13
- Literal,
14
- NotRequired,
15
- Optional,
16
- Self,
17
- Sequence,
18
- TypeAlias,
19
- TypedDict,
20
- TypeGuard,
21
- cast,
22
- get_args,
23
- )
24
- from urllib.parse import urlparse
25
-
26
- import requests
27
- from aiohttp import ClientSession
28
- from PIL.Image import Resampling
29
- from PIL.Image import open as image_open
30
- from pydantic import BaseModel
31
-
32
- if TYPE_CHECKING:
33
- from openai.types.chat.chat_completion_content_part_image_param import ChatCompletionContentPartImageParam
34
-
35
- logger = getLogger(__name__)
36
- ImageType: TypeAlias = Literal["jpeg", "jpg", "png", "gif", "webp", "bmp"]
37
-
38
-
39
- class ImageProcessingConfig(TypedDict):
40
- """
41
- 이미지 필터링/변환 시 사용할 설정.
42
- - formats: (Sequence[str]) 허용할 이미지 포맷(소문자, 예: ["jpeg", "png", "webp"]).
43
- - max_size_mb: (float) 이미지 용량 상한(MB). 초과 제외.
44
- - min_largest_side: (int) 가로나 세로 가장 큰 변의 최소 크기. 미만 시 제외.
45
- - resize_if_min_side_exceeds: (int) 가로나 세로 중 작은 변이 이상이면 리스케일.
46
- - resize_target_for_min_side: (int) 리스케일시, '가장 작은 변'을값으로 줄임(비율 유지는 Lanczos).
47
- """
48
-
49
- formats: Sequence[ImageType]
50
- max_size_mb: NotRequired[float]
51
- min_largest_side: NotRequired[int]
52
- resize_if_min_side_exceeds: NotRequired[int]
53
- resize_target_for_min_side: NotRequired[int]
54
-
55
-
56
- def get_default_image_processing_config() -> ImageProcessingConfig:
57
- return {
58
- "max_size_mb": 5,
59
- "min_largest_side": 200,
60
- "resize_if_min_side_exceeds": 2000,
61
- "resize_target_for_min_side": 1000,
62
- "formats": ["png", "jpeg", "jpg", "gif", "bmp", "webp"],
63
- }
64
-
65
-
66
- # image_url: str, headers: dict[str, str]) -> Optional[bytes]:
67
- class Base64Image(BaseModel):
68
- ext: ImageType
69
- data: str
70
-
71
- IMAGE_TYPES: ClassVar[tuple[str, ...]] = tuple(map(str, get_args(ImageType)))
72
- IMAGE_PATTERN: ClassVar[re.Pattern[str]] = re.compile(
73
- r"data:image/(" + "|".join(IMAGE_TYPES) + r");base64,([A-Za-z0-9+/]+={0,2})"
74
- )
75
-
76
- def __hash__(self) -> int:
77
- return hash((self.ext, self.data))
78
-
79
- def model_post_init(self, __context: object) -> None:
80
- if self.ext == "jpg":
81
- self.ext = "jpeg"
82
-
83
- @classmethod
84
- def from_string(cls, data: str) -> Optional[Self]:
85
- match = cls.IMAGE_PATTERN.fullmatch(data)
86
- if not match:
87
- return None
88
- return cls(ext=cast(ImageType, match.group(1)), data=match.group(2))
89
-
90
- @classmethod
91
- def from_bytes(cls, data: bytes, ext: ImageType) -> Self:
92
- return cls(ext=ext, data=b64encode(data).decode("utf-8"))
93
-
94
- @classmethod
95
- def from_url_or_path(
96
- cls,
97
- url_or_path: str,
98
- *,
99
- headers: dict[str, str] = {},
100
- config: ImageProcessingConfig = get_default_image_processing_config(),
101
- img_bytes_fetcher: Optional[Callable[[str, dict[str, str]], bytes]] = None,
102
- ) -> Optional[Self]:
103
- """Return a Base64Image instance from a URL or local file path."""
104
- if maybe_base64 := cls.from_string(url_or_path):
105
- return maybe_base64
106
- elif is_remote_url(url_or_path):
107
- if img_bytes_fetcher:
108
- img_bytes = img_bytes_fetcher(url_or_path, headers)
109
- else:
110
- img_bytes = cls._fetch_remote_image(url_or_path, headers)
111
- if not img_bytes:
112
- return None
113
- return cls._convert_image_into_base64(img_bytes, config)
114
- try:
115
- return cls._process_local_image(Path(url_or_path), config)
116
- except Exception:
117
- return None
118
-
119
- @classmethod
120
- async def afrom_url_or_path(
121
- cls,
122
- url_or_path: str,
123
- *,
124
- headers: dict[str, str] = {},
125
- config: ImageProcessingConfig = get_default_image_processing_config(),
126
- img_bytes_fetcher: Optional[Callable[[str, dict[str, str]], Awaitable[bytes]]] = None,
127
- ) -> Optional[Self]:
128
- """Return a Base64Image instance from a URL or local file path."""
129
- if maybe_base64 := cls.from_string(url_or_path):
130
- return maybe_base64
131
- elif is_remote_url(url_or_path):
132
- if img_bytes_fetcher:
133
- img_bytes = await img_bytes_fetcher(url_or_path, headers)
134
- else:
135
- img_bytes = await cls._afetch_remote_image(url_or_path, headers)
136
- if not img_bytes:
137
- return None
138
- return cls._convert_image_into_base64(img_bytes, config)
139
- try:
140
- return cls._process_local_image(Path(url_or_path), config)
141
- except Exception:
142
- return None
143
-
144
- @property
145
- def data_uri(self) -> str:
146
- return f"data:image/{self.ext.replace('jpg', 'jpeg')};base64,{self.data}"
147
-
148
- @property
149
- def data_uri_content(self) -> "ChatCompletionContentPartImageParam":
150
- return {"type": "image_url", "image_url": {"url": self.data_uri}}
151
-
152
- @property
153
- def data_uri_content_dict(self) -> dict[str, object]:
154
- return {"type": "image_url", "image_url": {"url": self.data_uri}}
155
-
156
- @staticmethod
157
- def _verify_ext(ext: str, allowed_types: Sequence[ImageType]) -> TypeGuard[ImageType]:
158
- return ext in allowed_types
159
-
160
- @classmethod
161
- def _fetch_remote_image(cls, url: str, headers: dict[str, str]) -> bytes:
162
- try:
163
- with requests.Session() as session:
164
- response = session.get(url.strip(), headers={k: str(v) for k, v in headers.items()})
165
- response.raise_for_status()
166
- image_bytes = bytes(response.content or b"")
167
- if not image_bytes:
168
- return b""
169
- return image_bytes
170
- except Exception:
171
- return b""
172
-
173
- @classmethod
174
- async def _afetch_remote_image(cls, url: str, headers: dict[str, str]) -> bytes:
175
- try:
176
- async with ClientSession() as session:
177
- async with session.get(url.strip(), headers={k: str(v) for k, v in headers.items()}) as response:
178
- response.raise_for_status()
179
- return await response.read()
180
- except Exception:
181
- return b""
182
-
183
- @classmethod
184
- def _convert_image_into_base64(cls, image_data: bytes, config: Optional[ImageProcessingConfig]) -> Optional[Self]:
185
- """
186
- Retrieve an image in bytes and return a base64-encoded data URL,
187
- applying dynamic rules from 'config'.
188
- """
189
-
190
- if not config:
191
- # config 없으면 그냥 기존 헤더만 보고 돌려주는 간단 로직
192
- return cls._simple_base64_encode(image_data)
193
-
194
- # 1) 용량 검사
195
- max_size_mb = config.get("max_size_mb", float("inf"))
196
- image_size_mb = len(image_data) / (1024 * 1024)
197
- if image_size_mb > max_size_mb:
198
- logger.error(f"Image too large: {image_size_mb:.2f} MB > {max_size_mb} MB")
199
- return None
200
-
201
- # 2) Pillow로 이미지 열기
202
- try:
203
- with image_open(BytesIO(image_data)) as im:
204
- w, h = im.size
205
- # 가장 큰 변
206
- largest_side = max(w, h)
207
- # 가장 작은
208
- smallest_side = min(w, h)
209
-
210
- # min_largest_side 기준
211
- min_largest_side = config.get("min_largest_side", 1)
212
- if largest_side < min_largest_side:
213
- logger.error(f"Image too small: {largest_side} < {min_largest_side}")
214
- return None
215
-
216
- # resize 로직
217
- resize_if_min_side_exceeds = config.get("resize_if_min_side_exceeds", float("inf"))
218
- if smallest_side >= resize_if_min_side_exceeds:
219
- # resize_target_for_min_side 로 축소
220
- resize_target = config.get("resize_target_for_min_side", 1000)
221
- ratio = resize_target / float(smallest_side)
222
- new_w = int(w * ratio)
223
- new_h = int(h * ratio)
224
- im = im.resize((new_w, new_h), Resampling.LANCZOS)
225
-
226
- # 포맷 제한
227
- # PIL이 인식한 포맷이 대문자(JPEG)일 수 있으므로 소문자로
228
- pil_format: str = (im.format or "").lower()
229
- allowed_formats: Sequence[ImageType] = config.get("formats", [])
230
- if not cls._verify_ext(pil_format, allowed_formats):
231
- logger.error(f"Invalid format: {pil_format} not in {allowed_formats}")
232
- return None
233
-
234
- # 다시 bytes 로 저장
235
- output_buffer = BytesIO()
236
- im.save(output_buffer, format=pil_format.upper()) # PIL에 맞춰서 대문자로
237
- output_buffer.seek(0)
238
- final_bytes = output_buffer.read()
239
-
240
- except Exception:
241
- return None
242
-
243
- # 최종 base64 인코딩
244
- encoded_data = b64encode(final_bytes).decode("utf-8")
245
- return cls(ext=pil_format, data=encoded_data)
246
-
247
- @classmethod
248
- def _simple_base64_encode(cls, image_data: bytes) -> Optional[Self]:
249
- """
250
- Retrieve an image URL and return a base64-encoded data URL.
251
- """
252
- ext = detect_image_type(image_data)
253
- if not ext:
254
- return
255
- return cls(ext=ext, data=b64encode(image_data).decode("utf-8"))
256
-
257
- @classmethod
258
- def _process_local_image(cls, path: Path, config: ImageProcessingConfig) -> Optional[Self]:
259
- """로컬 파일이 존재하고 유효한 이미지 포맷이면 Base64 데이터 URL을 반환, 아니면 None."""
260
- if not path.is_file():
261
- return None
262
- ext = path.suffix.lower().removeprefix(".")
263
- if not cls._verify_ext(ext, config["formats"]):
264
- return None
265
- return cls(ext=ext, data=b64encode(path.read_bytes()).decode("ascii"))
266
-
267
-
268
- def is_remote_url(path: str) -> bool:
269
- parsed = urlparse(path)
270
- return bool(parsed.scheme and parsed.netloc)
271
-
272
-
273
- def detect_image_type(image_data: bytes) -> Optional[ImageType]:
274
- """
275
- Detect the image format based on the image binary signature (header).
276
- Only JPEG, PNG, GIF, WEBP, and BMP are handled as examples.
277
- If the format is not recognized, return None.
278
- """
279
- # JPEG: 시작 바이트가 FF D8 FF
280
- if image_data.startswith(b"\xff\xd8\xff"):
281
- return "jpeg"
282
- # PNG: 시작 바이트가 89 50 4E 47 0D 0A 1A 0A
283
- elif image_data.startswith(b"\x89PNG\r\n\x1a\n"):
284
- return "png"
285
- # GIF: 시작 바이트가 GIF87a 또는 GIF89a
286
- elif image_data.startswith(b"GIF87a") or image_data.startswith(b"GIF89a"):
287
- return "gif"
288
- # WEBP: 시작 바이트가 RIFF....WEBP
289
- elif image_data.startswith(b"RIFF") and image_data[8:12] == b"WEBP":
290
- return "webp"
291
- # BMP: 시작 바이트가 BM
292
- elif image_data.startswith(b"BM"):
293
- return "bmp"
1
+ import re
2
+ from base64 import b64encode
3
+ from io import BytesIO
4
+ from pathlib import Path
5
+ from typing import (
6
+ TYPE_CHECKING,
7
+ Awaitable,
8
+ Callable,
9
+ ClassVar,
10
+ Literal,
11
+ NotRequired,
12
+ Optional,
13
+ Self,
14
+ Sequence,
15
+ TypeAlias,
16
+ TypedDict,
17
+ TypeGuard,
18
+ get_args,
19
+ )
20
+ from urllib.parse import urlparse
21
+
22
+ import requests
23
+ from aiohttp import ClientSession
24
+ from loguru import logger
25
+ from PIL.Image import Resampling
26
+ from PIL.Image import open as image_open
27
+ from pydantic import BaseModel
28
+
29
+ from .imghdr import what
30
+
31
+ if TYPE_CHECKING:
32
+ from openai.types.chat.chat_completion_content_part_image_param import ChatCompletionContentPartImageParam
33
+
34
+ ImageFormat: TypeAlias = Literal["jpeg", "png", "gif", "webp", "bmp"]
35
+ ExtendedImageFormat: TypeAlias = ImageFormat | Literal["jpg", "JPG"] | Literal["JPEG", "PNG", "GIF", "WEBP", "BMP"]
36
+
37
+ ALLOWED_IMAGE_FORMATS: tuple[ImageFormat, ...] = get_args(ImageFormat)
38
+
39
+
40
+ class ImageProcessingConfig(TypedDict):
41
+ """
42
+ 이미지 필터링/변환 사용할 설정.
43
+ - formats: (Sequence[str]) 허용할 이미지 포맷(소문자, 예: ["jpeg", "png", "webp"]).
44
+ - max_size_mb: (float) 이미지 용량 상한(MB). 초과 시 제외.
45
+ - min_largest_side: (int) 가로나 세로 중 가장 변의 최소 크기. 미만 시 제외.
46
+ - resize_if_min_side_exceeds: (int) 가로나 세로 작은 변이 이상이면 리스케일.
47
+ - resize_target_for_min_side: (int) 리스케일시, '가장 작은 변'을 이 값으로 줄임(비율 유지는 Lanczos).
48
+ """
49
+
50
+ formats: Sequence[ImageFormat]
51
+ max_size_mb: NotRequired[float]
52
+ min_largest_side: NotRequired[int]
53
+ resize_if_min_side_exceeds: NotRequired[int]
54
+ resize_target_for_min_side: NotRequired[int]
55
+
56
+
57
+ def get_default_image_processing_config() -> ImageProcessingConfig:
58
+ return {
59
+ "max_size_mb": 5,
60
+ "min_largest_side": 200,
61
+ "resize_if_min_side_exceeds": 2000,
62
+ "resize_target_for_min_side": 1000,
63
+ "formats": ["png", "jpeg", "gif", "bmp", "webp"],
64
+ }
65
+
66
+
67
+ class Base64Image(BaseModel):
68
+ ext: ImageFormat
69
+ data: str
70
+
71
+ IMAGE_TYPES: ClassVar[tuple[str, ...]] = ALLOWED_IMAGE_FORMATS
72
+ IMAGE_PATTERN: ClassVar[re.Pattern[str]] = re.compile(
73
+ r"data:image/(" + "|".join(IMAGE_TYPES) + r");base64,([A-Za-z0-9+/]+={0,2})"
74
+ )
75
+
76
+ def __hash__(self) -> int:
77
+ return hash((self.ext, self.data))
78
+
79
+ @classmethod
80
+ def new(
81
+ cls,
82
+ url_or_path_or_bytes: str | bytes,
83
+ *,
84
+ headers: dict[str, str] = {},
85
+ config: ImageProcessingConfig = get_default_image_processing_config(),
86
+ img_bytes_fetcher: Optional[Callable[[str, dict[str, str]], bytes]] = None,
87
+ ) -> Self:
88
+ if isinstance(url_or_path_or_bytes, bytes):
89
+ ext = what(url_or_path_or_bytes)
90
+ if ext is None:
91
+ raise ValueError(f"Invalid image format: {url_or_path_or_bytes[:8]} ...")
92
+ if not cls._verify_ext(ext, config["formats"]):
93
+ raise ValueError(f"Invalid image format: {ext} not in {config['formats']}")
94
+ return cls.from_bytes(url_or_path_or_bytes, ext=ext)
95
+ elif maybe_base64 := cls.from_string(url_or_path_or_bytes):
96
+ return maybe_base64
97
+ elif maybe_url_or_path := cls.from_url_or_path(
98
+ url_or_path_or_bytes, headers=headers, config=config, img_bytes_fetcher=img_bytes_fetcher
99
+ ):
100
+ return maybe_url_or_path
101
+ else:
102
+ raise ValueError(f"Invalid image format: {url_or_path_or_bytes}")
103
+
104
+ @classmethod
105
+ async def anew(
106
+ cls,
107
+ url_or_path_or_bytes: str | bytes,
108
+ *,
109
+ headers: dict[str, str] = {},
110
+ config: ImageProcessingConfig = get_default_image_processing_config(),
111
+ img_bytes_fetcher: Optional[Callable[[str, dict[str, str]], Awaitable[bytes]]] = None,
112
+ ) -> Self:
113
+ if isinstance(url_or_path_or_bytes, bytes):
114
+ ext = what(url_or_path_or_bytes)
115
+ if ext is None:
116
+ raise ValueError(f"Invalid image format: {url_or_path_or_bytes[:8]} ...")
117
+ if not cls._verify_ext(ext, config["formats"]):
118
+ raise ValueError(f"Invalid image format: {ext} not in {config['formats']}")
119
+ return cls.from_bytes(url_or_path_or_bytes, ext=ext)
120
+ elif maybe_base64 := cls.from_string(url_or_path_or_bytes):
121
+ return maybe_base64
122
+ elif maybe_url_or_path := await cls.afrom_url_or_path(
123
+ url_or_path_or_bytes, headers=headers, config=config, img_bytes_fetcher=img_bytes_fetcher
124
+ ):
125
+ return maybe_url_or_path
126
+ else:
127
+ raise ValueError(f"Invalid image format: {url_or_path_or_bytes}")
128
+
129
+ @classmethod
130
+ def from_string(cls, data: str) -> Optional[Self]:
131
+ match = cls.IMAGE_PATTERN.fullmatch(data)
132
+ if not match:
133
+ return None
134
+ return cls(ext=_to_image_format(match.group(1)), data=match.group(2))
135
+
136
+ @classmethod
137
+ def from_bytes(cls, data: bytes, ext: ExtendedImageFormat) -> Self:
138
+ return cls(ext=_to_image_format(ext), data=b64encode(data).decode("utf-8"))
139
+
140
+ @classmethod
141
+ def from_url_or_path(
142
+ cls,
143
+ url_or_path: str,
144
+ *,
145
+ headers: dict[str, str] = {},
146
+ config: ImageProcessingConfig = get_default_image_processing_config(),
147
+ img_bytes_fetcher: Optional[Callable[[str, dict[str, str]], bytes]] = None,
148
+ ) -> Optional[Self]:
149
+ """Return a Base64Image instance from a URL or local file path."""
150
+ if maybe_base64 := cls.from_string(url_or_path):
151
+ return maybe_base64
152
+ elif is_remote_url(url_or_path):
153
+ if img_bytes_fetcher:
154
+ img_bytes = img_bytes_fetcher(url_or_path, headers)
155
+ else:
156
+ img_bytes = cls._fetch_remote_image(url_or_path, headers)
157
+ if not img_bytes:
158
+ return None
159
+ return cls._convert_image_into_base64(img_bytes, config)
160
+ try:
161
+ return cls._process_local_image(Path(url_or_path), config)
162
+ except Exception:
163
+ return None
164
+
165
+ @classmethod
166
+ async def afrom_url_or_path(
167
+ cls,
168
+ url_or_path: str,
169
+ *,
170
+ headers: dict[str, str] = {},
171
+ config: ImageProcessingConfig = get_default_image_processing_config(),
172
+ img_bytes_fetcher: Optional[Callable[[str, dict[str, str]], Awaitable[bytes]]] = None,
173
+ ) -> Optional[Self]:
174
+ """Return a Base64Image instance from a URL or local file path."""
175
+ if maybe_base64 := cls.from_string(url_or_path):
176
+ return maybe_base64
177
+ elif is_remote_url(url_or_path):
178
+ if img_bytes_fetcher:
179
+ img_bytes = await img_bytes_fetcher(url_or_path, headers)
180
+ else:
181
+ img_bytes = await cls._afetch_remote_image(url_or_path, headers)
182
+ if not img_bytes:
183
+ return None
184
+ return cls._convert_image_into_base64(img_bytes, config)
185
+ try:
186
+ return cls._process_local_image(Path(url_or_path), config)
187
+ except Exception:
188
+ return None
189
+
190
+ @property
191
+ def data_uri(self) -> str:
192
+ return f"data:image/{self.ext.replace('jpg', 'jpeg')};base64,{self.data}"
193
+
194
+ @property
195
+ def data_uri_content(self) -> "ChatCompletionContentPartImageParam":
196
+ return {"type": "image_url", "image_url": {"url": self.data_uri}}
197
+
198
+ @property
199
+ def data_uri_content_dict(self) -> dict[str, object]:
200
+ return {"type": "image_url", "image_url": {"url": self.data_uri}}
201
+
202
+ @staticmethod
203
+ def _verify_ext(ext: str, allowed_types: Sequence[ImageFormat]) -> TypeGuard[ImageFormat]:
204
+ return ext in allowed_types
205
+
206
+ @classmethod
207
+ def _fetch_remote_image(cls, url: str, headers: dict[str, str]) -> bytes:
208
+ try:
209
+ with requests.Session() as session:
210
+ response = session.get(url.strip(), headers={k: str(v) for k, v in headers.items()})
211
+ response.raise_for_status()
212
+ image_bytes = bytes(response.content or b"")
213
+ if not image_bytes:
214
+ return b""
215
+ return image_bytes
216
+ except Exception:
217
+ return b""
218
+
219
+ @classmethod
220
+ async def _afetch_remote_image(cls, url: str, headers: dict[str, str]) -> bytes:
221
+ try:
222
+ async with ClientSession() as session:
223
+ async with session.get(url.strip(), headers={k: str(v) for k, v in headers.items()}) as response:
224
+ response.raise_for_status()
225
+ return await response.read()
226
+ except Exception:
227
+ return b""
228
+
229
+ @classmethod
230
+ def _convert_image_into_base64(cls, image_data: bytes, config: Optional[ImageProcessingConfig]) -> Optional[Self]:
231
+ """
232
+ Retrieve an image in bytes and return a base64-encoded data URL,
233
+ applying dynamic rules from 'config'.
234
+ """
235
+
236
+ if not config:
237
+ # config 없으면 그냥 기존 헤더만 보고 돌려주는 간단 로직
238
+ return cls._simple_base64_encode(image_data)
239
+
240
+ # 1) 용량 검사
241
+ max_size_mb = config.get("max_size_mb", float("inf"))
242
+ image_size_mb = len(image_data) / (1024 * 1024)
243
+ if image_size_mb > max_size_mb:
244
+ logger.error(f"Image too large: {image_size_mb:.2f} MB > {max_size_mb} MB")
245
+ return None
246
+
247
+ # 2) Pillow로 이미지 열기
248
+ try:
249
+ with image_open(BytesIO(image_data)) as im:
250
+ w, h = im.size
251
+ # 가장 큰 변
252
+ largest_side = max(w, h)
253
+ # 가장 작은 변
254
+ smallest_side = min(w, h)
255
+
256
+ # min_largest_side 기준
257
+ min_largest_side = config.get("min_largest_side", 1)
258
+ if largest_side < min_largest_side:
259
+ logger.error(f"Image too small: {largest_side} < {min_largest_side}")
260
+ return None
261
+
262
+ # resize 로직
263
+ resize_if_min_side_exceeds = config.get("resize_if_min_side_exceeds", float("inf"))
264
+ if smallest_side >= resize_if_min_side_exceeds:
265
+ # resize_target_for_min_side 로 축소
266
+ resize_target = config.get("resize_target_for_min_side", 1000)
267
+ ratio = resize_target / float(smallest_side)
268
+ new_w = int(w * ratio)
269
+ new_h = int(h * ratio)
270
+ im = im.resize((new_w, new_h), Resampling.LANCZOS)
271
+
272
+ # 포맷 제한
273
+ # PIL이 인식한 포맷이 대문자(JPEG) 있으므로 소문자로
274
+ pil_format: str = (im.format or "").lower()
275
+ allowed_formats: Sequence[ImageFormat] = config.get("formats", [])
276
+ if not cls._verify_ext(pil_format, allowed_formats):
277
+ logger.error(f"Invalid format: {pil_format} not in {allowed_formats}")
278
+ return None
279
+
280
+ # 다시 bytes 로 저장
281
+ output_buffer = BytesIO()
282
+ im.save(output_buffer, format=pil_format.upper()) # PIL에 맞춰서 대문자로
283
+ output_buffer.seek(0)
284
+ final_bytes = output_buffer.read()
285
+
286
+ except Exception:
287
+ return None
288
+
289
+ # 최종 base64 인코딩
290
+ encoded_data = b64encode(final_bytes).decode("utf-8")
291
+ return cls(ext=pil_format, data=encoded_data)
292
+
293
+ @classmethod
294
+ def _simple_base64_encode(cls, image_data: bytes) -> Optional[Self]:
295
+ """
296
+ Retrieve an image URL and return a base64-encoded data URL.
297
+ """
298
+ ext = detect_image_type(image_data)
299
+ if not ext:
300
+ return
301
+ return cls(ext=ext, data=b64encode(image_data).decode("utf-8"))
302
+
303
+ @classmethod
304
+ def _process_local_image(cls, path: Path, config: ImageProcessingConfig) -> Optional[Self]:
305
+ """로컬 파일이 존재하고 유효한 이미지 포맷이면 Base64 데이터 URL을 반환, 아니면 None."""
306
+ if not path.is_file():
307
+ return None
308
+ ext = path.suffix.lower().removeprefix(".")
309
+ if not cls._verify_ext(ext, config["formats"]):
310
+ return None
311
+ return cls(ext=ext, data=b64encode(path.read_bytes()).decode("ascii"))
312
+
313
+
314
+ def _to_image_format(ext: str) -> ImageFormat:
315
+ lowered = ext.lower()
316
+ if lowered in ALLOWED_IMAGE_FORMATS:
317
+ return lowered
318
+ elif lowered == "jpg":
319
+ return "jpeg" # jpg -> jpeg
320
+ else:
321
+ raise ValueError(f"Invalid image format: {ext}")
322
+
323
+
324
+ def is_remote_url(path: str) -> bool:
325
+ parsed = urlparse(path)
326
+ return bool(parsed.scheme and parsed.netloc)
327
+
328
+
329
+ def detect_image_type(image_data: bytes) -> Optional[ImageFormat]:
330
+ """
331
+ Detect the image format based on the image binary signature (header).
332
+ Only JPEG, PNG, GIF, WEBP, and BMP are handled as examples.
333
+ If the format is not recognized, return None.
334
+ """
335
+ # JPEG: 시작 바이트가 FF D8 FF
336
+ if image_data.startswith(b"\xff\xd8\xff"):
337
+ return "jpeg"
338
+ # PNG: 시작 바이트가 89 50 4E 47 0D 0A 1A 0A
339
+ elif image_data.startswith(b"\x89PNG\r\n\x1a\n"):
340
+ return "png"
341
+ # GIF: 시작 바이트가 GIF87a 또는 GIF89a
342
+ elif image_data.startswith(b"GIF87a") or image_data.startswith(b"GIF89a"):
343
+ return "gif"
344
+ # WEBP: 시작 바이트가 RIFF....WEBP
345
+ elif image_data.startswith(b"RIFF") and image_data[8:12] == b"WEBP":
346
+ return "webp"
347
+ # BMP: 시작 바이트가 BM
348
+ elif image_data.startswith(b"BM"):
349
+ return "bmp"