From b8fefa6b55fc1f562f470360fface75f4fb8f2fe Mon Sep 17 00:00:00 2001 From: Leo Tronchon Date: Tue, 3 Oct 2023 10:26:10 +0200 Subject: [PATCH] raise exception on invalid images (#999) # What does this PR do? This PR is meant to handle cases in which the images provided are invalid. ## Who can review? Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR. @Narsil --------- Co-authored-by: Nicolas Patry --- .../models/custom_modeling/idefics_image_processing.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/server/text_generation_server/models/custom_modeling/idefics_image_processing.py b/server/text_generation_server/models/custom_modeling/idefics_image_processing.py index 6fb009999..061243fb5 100644 --- a/server/text_generation_server/models/custom_modeling/idefics_image_processing.py +++ b/server/text_generation_server/models/custom_modeling/idefics_image_processing.py @@ -194,9 +194,14 @@ class IdeficsImageProcessor(BaseImageProcessor): if isinstance(image_url_or_urls, list): return [self.fetch_images(x) for x in image_url_or_urls] elif isinstance(image_url_or_urls, str): - response = requests.get(image_url_or_urls, stream=True, headers=headers) + response = requests.get(image_url_or_urls, stream=True, headers=headers, timeout=(1, 5)) response.raise_for_status() - return Image.open(BytesIO(response.content)) + try: + image = Image.open(BytesIO(response.content)) + image.verify() + except Exception: + raise ValueError(f"Could not load image from url {image_url_or_urls}") + return image else: raise ValueError( f"only a single or a list of entries is supported but got type={type(image_url_or_urls)}"