Passing local images to chat (workaround).

#6
by averoo - opened

Seems like there are no option to pass a local file to chat in vllm/multimodal/utils.py.

Snippet to overcome it:

import os
import base64

def file_to_data_url(file_path: str):
    """
    Convert a local image file to a data URL.
    """    
    with open(file_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
    
    _, extension = os.path.splitext(file_path)
    mime_type = f"image/{extension[1:].lower()}"
    
    return f"data:{mime_type};base64,{encoded_string}"

Using:


prompt = "Name the city on the photo"
image_url = "./yakutsk.png"

image_source = file_to_data_url(image_url)

messages = [
    {
        "role": "user",
        "content": [{"type": "text", "text": prompt}, {"type": "image_url", "image_url": {"url": image_source}}]
    },
]


sampling_params = SamplingParams(max_tokens=8192)

outputs = llm.chat(messages, sampling_params=sampling_params)

print(outputs[0].outputs[0].text)

Sign up or log in to comment