This commit is contained in:
oobabooga 2024-01-22 04:07:25 -08:00
parent b9d1873301
commit 817866c9cf

View File

@ -158,15 +158,18 @@ def convert_history(history):
for item in entry['content']: for item in entry['content']:
if not isinstance(item, dict): if not isinstance(item, dict):
continue continue
if item['type'] == 'image_url' and isinstance(item['image_url'], dict): if item['type'] == 'image_url' and isinstance(item['image_url'], dict):
image_url = item['image_url']['url'] image_url = item['image_url']['url']
elif item['type'] == 'text' and isinstance(item['text'], str): elif item['type'] == 'text' and isinstance(item['text'], str):
content = item['text'] content = item['text']
if image_url and content: if image_url and content:
new_history.append({"image_url": image_url, "role": "user"}) new_history.append({"image_url": image_url, "role": "user"})
new_history.append({"content": content, "role": "user"}) new_history.append({"content": content, "role": "user"})
else: else:
new_history.append(entry) new_history.append(entry)
history = new_history history = new_history
for entry in history: for entry in history:
@ -181,9 +184,11 @@ def convert_history(history):
img = Image.open(BytesIO(my_res.content)) img = Image.open(BytesIO(my_res.content))
except Exception: except Exception:
raise 'Image cannot be loaded from the URL!' raise 'Image cannot be loaded from the URL!'
buffered = BytesIO() buffered = BytesIO()
if img.mode in ("RGBA", "P"): if img.mode in ("RGBA", "P"):
img = img.convert("RGB") img = img.convert("RGB")
img.save(buffered, format="JPEG") img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
content = f'<img src="data:image/jpeg;base64,{img_str}">' content = f'<img src="data:image/jpeg;base64,{img_str}">'
@ -197,6 +202,7 @@ def convert_history(history):
if current_message: if current_message:
chat_dialogue.append([current_message, '']) chat_dialogue.append([current_message, ''])
current_message = "" current_message = ""
current_message = content current_message = content
elif role == "assistant": elif role == "assistant":
current_reply = content current_reply = content