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

Support VLM in chat completion (+some specs updates) #2556

Merged
merged 4 commits into from
Sep 25, 2024

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Sep 20, 2024

Follow-up PR after huggingface/huggingface.js#915.

Updates chat completion input parameters (tools and stream_options) + add supports for VLMs => sending image as text message.

Also adds TextToSpeech spec definition but not used at the moment (same specs as TextToAudio).

Also added an example in docs:

from huggingface_hub import InferenceClient

# works with remote url or base64 encoded url
image_url ="https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"

client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
output = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {"url": image_url},
                },
                {
                    "type": "text",
                    "text": "Describe this image in one sentence.",
                },
            ],
        },
    ],
)

print(output.choices[0].message.content)
#A determine figure of Lady Liberty stands tall, holding a torch aloft, atop a pedestal on an island.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@MoritzLaurer
Copy link
Contributor

Cool! One great addition would be to also explicitly document how people can use a local, non-public image in PIL and convert it to a base64 string which the API also understands. People often get confused about this. I use code similar to this:

import base64
from io import BytesIO
from PIL import Image

# Open an image using PIL
image = Image.open('path_to_your_image.png')

def image_to_data_url(image):
    # Use the image's format or default to 'PNG' if format is None
    format = image.format or 'PNG'
    with BytesIO() as buffer:
        image.save(buffer, format=format)
        img_str = base64.b64encode(buffer.getvalue()).decode('utf-8')
    return f"data:image/{format.lower()};base64,{img_str}"

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "some prompt"
            },
            {
                "type": "image_url",
                "image_url": {
                    "url": image_to_data_url(image)
                },
            }
        ]
    }
]

@Wauplin
Copy link
Contributor Author

Wauplin commented Sep 20, 2024

I'll see how I can integrate that in the docs 👍 I don't want the snippet to be bloated with 3 different use cases. For now I have:

>>> image_path = "/path/to/image.jpeg"
>>> with open(image_path, "rb") as f:
...     base64_image = base64.b64encode(f.read()).decode("utf-8")
>>> image_url = f"data:image/jpeg;base64,{base64_image}"

I'm thinking that in addition to the method docstring we should maybe add a section in the Inference guide typically to explain better how to deal with system prompt, tools, images, etc.

Copy link
Contributor

@hanouticelina hanouticelina left a comment

Choose a reason for hiding this comment

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

All good for me! thanks @Wauplin

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Thank you @Wauplin

Copy link
Contributor

@osanseviero osanseviero left a comment

Choose a reason for hiding this comment

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

Nice!

... base64_image = base64.b64encode(f.read()).decode("utf-8")
>>> image_url = f"data:image/jpeg;base64,{base64_image}"

>>> client = AsyncInferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be pixtral? nemo instruct is text-only

Copy link
Contributor Author

@Wauplin Wauplin Sep 23, 2024

Choose a reason for hiding this comment

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

Definitely an oversight from me. It seems that when the model is an LLM (not VLM), the data is still transferred to as text. In my case the URL contains "statue of liberty" so the LLM has been able to describe it (and tricked me into thinking the image has been used).

Copy link
Contributor

Choose a reason for hiding this comment

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

LLM: 1
Humans: 0

inputs: Any
"""The input audio data"""
inputs: str
"""The input audio data as a base64-encoded string. If no `parameters` are provided, you can
Copy link
Contributor

Choose a reason for hiding this comment

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

I just realized that client.audio_classification() current implementation can take also a local audio file, or a URL pointing to an audio file as input (thanks to the _open_as_binary() context manager). We also handle base64 encoding in the code. This is actually the case for other tasks that take image or audio as input.

While it's quite convenient for users to not worry about file handling or encoding, it makes the documentation of the library not aligned with the current API specs for the inputs field.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Wauplin as discussed, this will be addressed in #2561
LGTM :)

@Wauplin
Copy link
Contributor Author

Wauplin commented Sep 25, 2024

Thanks for the reviews! I've updated the example to use "HuggingFaceM4/idefics2-8b-chatty" for now.

@Wauplin Wauplin merged commit 12ec449 into main Sep 25, 2024
18 checks passed
@Wauplin Wauplin deleted the support-vlm-in-chat-completion branch September 25, 2024 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants