Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HF][5/n] Image2Text: Allow base64 inputs for images #856

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import base64
import json
from typing import Any, Dict, Optional, List, TYPE_CHECKING
from io import BytesIO
from PIL import Image
from typing import Any, Dict, Optional, List, TYPE_CHECKING, Union
from transformers import (
Pipeline,
pipeline,
Expand Down Expand Up @@ -107,7 +110,7 @@ async def deserialize(
completion_params = refine_completion_params(model_settings)

#Add image inputs
inputs = validate_and_retrieve_image_from_attachments(prompt)
inputs = validate_and_retrieve_images_from_attachments(prompt)
completion_params["inputs"] = inputs

await aiconfig.callback_manager.run_callbacks(CallbackEvent("on_deserialize_complete", __name__, {"output": completion_params}))
Expand Down Expand Up @@ -218,7 +221,7 @@ def validate_attachment_type_is_image(attachment: Attachment):
raise ValueError(f"Invalid attachment mimetype {attachment.mime_type}. Expected image mimetype.")


def validate_and_retrieve_image_from_attachments(prompt: Prompt) -> list[str]:
def validate_and_retrieve_images_from_attachments(prompt: Prompt) -> list[Union[str, Image]]:
"""
Retrieves the image uri's from each attachment in the prompt input.

Expand All @@ -232,15 +235,23 @@ def validate_and_retrieve_image_from_attachments(prompt: Prompt) -> list[str]:
if not hasattr(prompt.input, "attachments") or len(prompt.input.attachments) == 0:
raise ValueError(f"No attachments found in input for prompt {prompt.name}. Please add an image attachment to the prompt input.")

image_uris: list[str] = []
images: list[Union[str, Image]] = []

for i, attachment in enumerate(prompt.input.attachments):
validate_attachment_type_is_image(attachment)

if not isinstance(attachment.data, str):
input_data = attachment.data
if not isinstance(input_data, str):
# See todo above, but for now only support uri's
raise ValueError(f"Attachment #{i} data is not a uri. Please specify a uri for the image attachment in prompt {prompt.name}.")

image_uris.append(attachment.data)
# Really basic heurestic to check if the data is a base64 encoded str
# vs. uri. This will be fixed once we have standardized inputs
# See https://github.com/lastmile-ai/aiconfig/issues/829
if len(input_data) > 10000:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable for now until we standardize input types.

Would it be better to flip it? i.e. check if the input_data is a URL or file path?

Perhaps something like:

from urllib.parse import urlparse
from pathlib import Path

def could_be_url_or_path(s):
    # First, check if it could be a URL.
    try:
        urlparse(s)
    except ValueError:
        pass  # Not a URL. Might still be a path.
    else:
        # URL parsing didn't raise an error, so s might be a URL.
        # Check that s has a scheme and a network location.
        if all([urlparse(s).scheme, urlparse(s).netloc]) or s.startswith('file://'):
            return True  # URL

    # Next, check if it could be a path
    p = Path(s)
    if p.is_absolute() or (p.is_relative_to('.') and p.suffix):
        return True  # Path

    # It's not any of them above -> Return False
    return False

pil_image : Image = Image.open(BytesIO(base64.b64decode(input_data)))
images.append(pil_image)
else:
images.append(input_data)

return image_uris
return images