Vladyslav Humennyy commited on
Commit
0154070
·
1 Parent(s): 14729c6

Fix image input

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -160,6 +160,29 @@ def _clean_history_for_display(history: list[dict[str, Any]]) -> list[dict[str,
160
 
161
  return cleaned
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  @spaces.GPU
165
  def bot(
@@ -207,6 +230,8 @@ def bot(
207
  formatted_content = []
208
  for item in content:
209
  if isinstance(item, dict):
 
 
210
  if item.get("type") == "text":
211
  formatted_content.append({"type": "text", "text": item.get("text", "")})
212
  elif item.get("type") == "image":
@@ -222,7 +247,14 @@ def bot(
222
  pil_image = Image.open(item["path"])
223
 
224
  if pil_image is not None:
225
- formatted_content.append({"type": "image", "image": pil_image})
 
 
 
 
 
 
 
226
  if formatted_content:
227
  processor_history.append({"role": role, "content": formatted_content})
228
 
 
160
 
161
  return cleaned
162
 
163
+ def format_message_with_image(
164
+ text: str, role: str, image: Optional[Image.Image] = None
165
+ ) -> Dict[str, Any]:
166
+ """Format message for VLLM API with optional image."""
167
+ if image is not None:
168
+ # Convert PIL image to base64
169
+ buffered = io.BytesIO()
170
+ image.save(buffered, format="JPEG")
171
+ img_base64 = base64.b64encode(buffered.getvalue()).decode()
172
+
173
+ return {
174
+ "role": role,
175
+ "content": [
176
+ {"type": "text", "text": text},
177
+ {
178
+ "type": "image_url",
179
+ "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"},
180
+ },
181
+ ],
182
+ }
183
+ else:
184
+ return {"role": role, "content": text}
185
+
186
 
187
  @spaces.GPU
188
  def bot(
 
230
  formatted_content = []
231
  for item in content:
232
  if isinstance(item, dict):
233
+
234
+ # Add text
235
  if item.get("type") == "text":
236
  formatted_content.append({"type": "text", "text": item.get("text", "")})
237
  elif item.get("type") == "image":
 
247
  pil_image = Image.open(item["path"])
248
 
249
  if pil_image is not None:
250
+ # formatted_content.append({"type": "image", "image": pil_image})
251
+ buffered = io.BytesIO()
252
+ pil_image.save(buffered, format="JPEG")
253
+ img_base64 = base64.b64encode(buffered.getvalue()).decode()
254
+ {
255
+ "type": "image_url",
256
+ "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"},
257
+ }
258
  if formatted_content:
259
  processor_history.append({"role": role, "content": formatted_content})
260