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