Skip to content

Commit

Permalink
File URL support for image chats
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuxax committed Oct 13, 2024
1 parent f519902 commit c900ce6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions vllm/multimodal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,28 @@ def _load_image_from_data_url(image_url: str):
_, image_base64 = image_url.split(",", 1)
return load_image_from_base64(image_base64)

def _load_image_from_path(p: str):
image = Image.open(p.replace("file://", ""))
image.load()
return image

def fetch_image(image_url: str, *, image_mode: str = "RGB") -> Image.Image:
"""
Load a PIL image from a HTTP or base64 data URL.
Load a PIL image from a HTTP or base64 data URL. file:// URLs are
acceptable too.
By default, the image is converted into RGB format.
"""
if image_url.startswith('http'):
image_raw = global_http_connection.get_bytes(
image_url, timeout=VLLM_IMAGE_FETCH_TIMEOUT)
image = _load_image_from_bytes(image_raw)

elif image_url.startswith('data:image'):
image = _load_image_from_data_url(image_url)
elif image_url.startswith('file://'):
image = _load_image_from_path(image_url)
else:
logger.error(f"broken, image_url is {image_url}")
raise ValueError("Invalid 'image_url': A valid 'image_url' must start "
"with either 'data:image' or 'http'.")

Expand All @@ -65,7 +72,10 @@ async def async_fetch_image(image_url: str,

elif image_url.startswith('data:image'):
image = _load_image_from_data_url(image_url)
elif image_url.startswith('file://'):
image = _load_image_from_path(image_url)
else:
logger.error(f"broken, image_url is {image_url}")
raise ValueError("Invalid 'image_url': A valid 'image_url' must start "
"with either 'data:image' or 'http'.")

Expand Down

0 comments on commit c900ce6

Please sign in to comment.