-
Notifications
You must be signed in to change notification settings - Fork 580
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
Conversation
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. |
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)
},
}
]
}
] |
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. |
There was a problem hiding this 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @Wauplin
There was a problem hiding this 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") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the reviews! I've updated the example to use |
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 asTextToAudio
).Also added an example in docs: