Skip to content

Commit f67ec8b

Browse files
committed
add image base64 to llm
1 parent 15be8e0 commit f67ec8b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/inferencesh/models/llm.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from threading import Thread
66
import time
77
from contextlib import contextmanager
8+
import base64
89

910
from .base import BaseAppInput, BaseAppOutput
1011
from .file import File
@@ -136,6 +137,10 @@ def stats(self):
136137
finally:
137138
pass
138139

140+
def image_to_base64_data_uri(file_path):
141+
with open(file_path, "rb") as img_file:
142+
base64_data = base64.b64encode(img_file.read()).decode('utf-8')
143+
return f"data:image/png;base64,{base64_data}"
139144

140145
def build_messages(
141146
input_data: LLMInput,
@@ -164,7 +169,8 @@ def build_messages(
164169
message_content.append({"type": "text", "text": text})
165170
if hasattr(msg, 'image') and msg.image:
166171
if msg.image.path:
167-
message_content.append({"type": "image_url", "image_url": {"url": msg.image.path}})
172+
image_data_uri = image_to_base64_data_uri(msg.image.path)
173+
message_content.append({"type": "image_url", "image_url": {"url": image_data_uri}})
168174
elif msg.image.uri:
169175
message_content.append({"type": "image_url", "image_url": {"url": msg.image.uri}})
170176
messages.append({
@@ -181,7 +187,8 @@ def build_messages(
181187
user_content.append({"type": "text", "text": text})
182188
if hasattr(input_data, 'image') and input_data.image:
183189
if input_data.image.path:
184-
user_content.append({"type": "image_url", "image_url": {"url": input_data.image.path}})
190+
image_data_uri = image_to_base64_data_uri(input_data.image.path)
191+
user_content.append({"type": "image_url", "image_url": {"url": image_data_uri}})
185192
elif input_data.image.uri:
186193
user_content.append({"type": "image_url", "image_url": {"url": input_data.image.uri}})
187194
messages.append({"role": "user", "content": user_content})

0 commit comments

Comments
 (0)