Skip to content

Commit

Permalink
[Misc] Multi-Modality Support for Loading Local image Files
Browse files Browse the repository at this point in the history
FIX vllm-project#8730

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
  • Loading branch information
chaunceyjiang committed Nov 1, 2024
1 parent 06386a6 commit 3da0d02
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions vllm/multimodal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
cached_get_tokenizer = lru_cache(get_tokenizer)


def _load_image_from_bytes(b: bytes):
def _load_image_from_bytes(b: bytes) -> Image.Image:
image = Image.open(BytesIO(b))
image.load()
return image


def _load_image_from_data_url(image_url: str):
def _load_image_from_file(image_url: str) -> Image.Image:
# Only split once and assume the second part is the image path
_, image_path = image_url.split("file://", 1)
image = Image.open(image_path)
image.load()
return image


def _load_image_from_data_url(image_url: str) -> Image.Image:
# Only split once and assume the second part is the base64 encoded image
_, image_base64 = image_url.split(",", 1)
return load_image_from_base64(image_base64)
Expand All @@ -43,9 +51,11 @@ def fetch_image(image_url: str, *, image_mode: str = "RGB") -> Image.Image:

elif image_url.startswith('data:image'):
image = _load_image_from_data_url(image_url)
elif image_url.startswith('file://'):
image = _load_image_from_file(image_url)
else:
raise ValueError("Invalid 'image_url': A valid 'image_url' must start "
"with either 'data:image' or 'http'.")
"with either 'data:image', 'file://' or 'http'.")

return image.convert(image_mode)

Expand All @@ -65,9 +75,11 @@ 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_file(image_url)
else:
raise ValueError("Invalid 'image_url': A valid 'image_url' must start "
"with either 'data:image' or 'http'.")
"with either 'data:image', 'file://' or 'http'.")

return image.convert(image_mode)

Expand Down

0 comments on commit 3da0d02

Please sign in to comment.