Unverified Commit e9cdf622 authored by Nicolas Patry's avatar Nicolas Patry Committed by GitHub
Browse files

Hotfixing idefics base64 parsing. (#1103)

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet
though.

Once merged, your PR is going to appear in the release notes with the
title you set, so make sure it's a great title that fully reflects the
extent of your awesome contribution.

Then, please replace this with a description of the change and which
issue is fixed (if applicable). Please also include relevant motivation
and context. List any dependencies (if any) that are required for this
change.

Once you're done, someone will review your PR shortly (see the section
"Who can review?" below to tag some potential reviewers). They may
suggest changes to make the code even better. If no one reviewed your PR
after a week has passed, don't hesitate to post a new comment
@-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typ...
parent 3c373dcc
...@@ -201,8 +201,13 @@ class IdeficsImageProcessor(BaseImageProcessor): ...@@ -201,8 +201,13 @@ class IdeficsImageProcessor(BaseImageProcessor):
response = requests.get(image_url_or_urls, stream=True, headers=headers, timeout=(1, 5)) response = requests.get(image_url_or_urls, stream=True, headers=headers, timeout=(1, 5))
response.raise_for_status() response.raise_for_status()
content = response.content content = response.content
else: elif image.startswith("data:"):
# https://stackoverflow.com/questions/17090571/is-there-a-way-to-set-background-image-as-a-base64-encoded-image
# data:image/png;base64,xxx
image = image.split(",")[-1]
content = base64.b64decode(image) content = base64.b64decode(image)
else:
raise ValueError(f"Unrecognized image {image}")
try: try:
image = Image.open(BytesIO(content)) image = Image.open(BytesIO(content))
......
...@@ -112,6 +112,11 @@ def is_url(string): ...@@ -112,6 +112,11 @@ def is_url(string):
result = urlparse(string) result = urlparse(string)
return all([result.scheme, result.netloc]) return all([result.scheme, result.netloc])
def is_image(string):
"""Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately
invalidated the url"""
return is_url(string) or string.startswith("data:")
class IdeficsProcessor(ProcessorMixin): class IdeficsProcessor(ProcessorMixin):
r""" r"""
...@@ -314,7 +319,7 @@ class IdeficsProcessor(ProcessorMixin): ...@@ -314,7 +319,7 @@ class IdeficsProcessor(ProcessorMixin):
if isinstance(item, str): if isinstance(item, str):
item = item.strip(" ") item = item.strip(" ")
if is_url(item): if is_image(item):
image = self.image_processor.fetch_images(item) image = self.image_processor.fetch_images(item)
full_text += image_tokens(last_was_image) full_text += image_tokens(last_was_image)
image_objects.append(image) image_objects.append(image)
...@@ -339,6 +344,7 @@ class IdeficsProcessor(ProcessorMixin): ...@@ -339,6 +344,7 @@ class IdeficsProcessor(ProcessorMixin):
image_objects = self.image_processor(image_objects, transform=transform) image_objects = self.image_processor(image_objects, transform=transform)
text_encoding = self.tokenizer( text_encoding = self.tokenizer(
text=full_text, text=full_text,
add_special_tokens=False, add_special_tokens=False,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment