Skip to content

Commit

Permalink
Merge pull request #34 from gabewillen/main
Browse files Browse the repository at this point in the history
Update load_image function to handle BytesIO input
  • Loading branch information
Blaizzy authored May 31, 2024
2 parents c6747a5 + 706b74c commit 6b46a27
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mlx_vlm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import shutil
import time
from io import BytesIO
from pathlib import Path
from textwrap import dedent
from typing import Any, Callable, Dict, Generator, Optional, Tuple, Union
Expand Down Expand Up @@ -642,11 +643,17 @@ def convert(
upload_to_hub(mlx_path, upload_repo, hf_path)


def load_image(image_source):
def load_image(image_source: Union[str, Path, BytesIO]):
"""
Helper function to load an image from either a URL or file.
"""
if image_source.startswith(("http://", "https://")):
if isinstance(image_source, BytesIO):
# for base64 encoded images
try:
return Image.open(image_source)
except IOError as e:
raise ValueError(f"Failed to load image from BytesIO with error: {e}")
elif image_source.startswith(("http://", "https://")):
try:
response = requests.get(image_source, stream=True)
response.raise_for_status()
Expand Down

0 comments on commit 6b46a27

Please sign in to comment.