Skip to content

Commit f84d8e2

Browse files
authored
add size checks to other options (#612)
1 parent 1309f45 commit f84d8e2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

libs/infinity_emb/infinity_emb/transformer/vision/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
def resolve_from_img_obj(img_obj: "ImageClassType") -> ImageSingle:
2323
"""Resolve an image from a ImageClassType Object."""
24+
assert_image_has_valid_size(img_obj)
2425
return ImageSingle(image=img_obj)
2526

2627

@@ -34,22 +35,27 @@ async def resolve_from_img_url(img_url: str, session: "aiohttp.ClientSession") -
3435

3536
try:
3637
img = Image.open(io.BytesIO(downloaded_img))
37-
if img.size[0] < 3 or img.size[1] < 3:
38-
# https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png
39-
raise ImageCorruption(
40-
f"An image in your request is too small for processing {img.size}"
41-
)
38+
assert_image_has_valid_size(img)
4239
return ImageSingle(image=img)
4340
except Exception as e:
4441
raise ImageCorruption(
4542
f"error opening the payload from an image in your request from url: {e}"
4643
)
4744

4845

46+
def assert_image_has_valid_size(img: "ImageClassType") -> None:
47+
if img.size[0] < 3 or img.size[1] < 3:
48+
# https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png
49+
raise ImageCorruption(
50+
f"An image in your request is too small for processing {img.size}. Minimum required size is 3x3."
51+
)
52+
53+
4954
def resolve_from_img_bytes(bytes_img: bytes) -> ImageSingle:
5055
"""Resolve an image from a Data URI"""
5156
try:
5257
img = Image.open(io.BytesIO(bytes_img))
58+
assert_image_has_valid_size(img)
5359
return ImageSingle(image=img)
5460
except Exception as e:
5561
raise ImageCorruption(f"error decoding data URI: {e}")

libs/infinity_emb/tests/end_to_end/test_torch_vision.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,24 @@ async def test_vision_fail(client):
220220
json={"model": MODEL, "input": ["test"]},
221221
)
222222
assert response_unsupported.status_code == status.HTTP_400_BAD_REQUEST
223+
224+
225+
@pytest.mark.anyio
226+
async def test_vision_base64_too_small(client, image_sample):
227+
response = await client.post(
228+
f"{PREFIX}/embeddings_image",
229+
json={
230+
"model": MODEL,
231+
"input": [
232+
"data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9sXsdrgAAAAASUVORK5CYII=",
233+
pytest.IMAGE_SAMPLE_URL,
234+
],
235+
},
236+
)
237+
assert response.status_code == status.HTTP_400_BAD_REQUEST
238+
rdata = response.json()
239+
message = rdata["error"]["message"]
240+
assert (
241+
message
242+
== "<class 'infinity_emb.primitives.ImageCorruption'> -> error decoding data URI: An image in your request is too small for processing (1, 1). Minimum required size is 3x3."
243+
)

libs/infinity_emb/tests/unit_test/test_engine.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
EmbeddingDtype,
1515
InferenceEngine,
1616
ModelNotDeployedError,
17+
ImageCorruption,
1718
)
1819

1920
# Only compile on Linux 3.9-3.11 with torch
@@ -238,6 +239,15 @@ async def test_torch_clip_embed():
238239
for i in range(1, len(sentences)):
239240
assert np.dot(emb_text_np[0], emb_image_np[0]) > np.dot(emb_text_np[i], emb_image_np[0])
240241

242+
async with engine:
243+
with pytest.raises(
244+
ImageCorruption,
245+
match=r"An image in your request is too small for processing \(1, 1\)\. Minimum required size is 3x3\.",
246+
):
247+
small_image = Image.new("L", (1, 1))
248+
t_small_image = asyncio.create_task(engine.image_embed(images=[small_image]))
249+
await t_small_image
250+
241251

242252
@pytest.mark.anyio
243253
async def test_clap_like_model(audio_sample):

0 commit comments

Comments
 (0)