Skip to content
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

Add base64 images support #51

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions deepseek_vl/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import PIL.Image
import torch
import base64
import io
from transformers import AutoModelForCausalLM

from deepseek_vl.models import MultiModalityCausalLM, VLChatProcessor
Expand All @@ -42,6 +44,8 @@ def load_pretrained_model(model_path: str):
def load_pil_images(conversations: List[Dict[str, str]]) -> List[PIL.Image.Image]:
"""

Support file path or base64 images.

Args:
conversations (List[Dict[str, str]]): the conversations with a list of messages. An example is :
[
Expand All @@ -64,8 +68,15 @@ def load_pil_images(conversations: List[Dict[str, str]]) -> List[PIL.Image.Image
if "images" not in message:
continue

for image_path in message["images"]:
pil_img = PIL.Image.open(image_path)
for image_data in message["images"]:
if image_data.startswith("data:image"):
# Image data is in base64 format
_, image_data = image_data.split(",", 1)
image_bytes = base64.b64decode(image_data)
pil_img = PIL.Image.open(io.BytesIO(image_bytes))
else:
# Image data is a file path
pil_img = PIL.Image.open(image_data)
pil_img = pil_img.convert("RGB")
pil_images.append(pil_img)

Expand Down