From 66ce2fa7c1a2ef56237578f3058267851d2b0291 Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Wed, 4 Oct 2023 17:35:29 +0200 Subject: [PATCH] Receive base64 encoded images for idefics. (#1096) # What does this PR do? Fix #1095 Fixes # (issue) ## Before submitting - [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case). - [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests), Pull Request section? - [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link to it if that's the case. - [ ] Did you make sure to update the documentation with your changes? Here are the [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation). - [ ] Did you write any new necessary tests? ## 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. --- .../custom_modeling/idefics_image_processing.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 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 28525e86..21aa3ff3 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 @@ -35,6 +35,7 @@ from transformers.image_utils import ( valid_images, ) from io import BytesIO +import base64 import requests from transformers import TensorType, is_torch_available @@ -194,10 +195,17 @@ 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, timeout=(1, 5)) - response.raise_for_status() + image = image_url_or_urls + + if image.startswith("http://") or image.startswith("https://"): + response = requests.get(image_url_or_urls, stream=True, headers=headers, timeout=(1, 5)) + response.raise_for_status() + content = response.content + else: + content = base64.b64decode(image) + try: - image = Image.open(BytesIO(response.content)) + image = Image.open(BytesIO(content)) # image.verify() except Exception: raise ValueError(f"Could not load image from url {image_url_or_urls}")