refactor: simplify image file processing to use provided filenames directly

This commit is contained in:
Paul Gauthier (aider) 2024-11-26 19:49:53 -08:00
parent cf4ef8605d
commit 28d1feacf5

View file

@ -728,27 +728,29 @@ class Coder:
return None return None
image_messages = [] image_messages = []
for fname, content in self.get_abs_fnames_content(): for fname in fnames:
if is_image_file(fname): if not is_image_file(fname):
mime_type, _ = mimetypes.guess_type(fname) continue
if not mime_type:
continue
with open(fname, "rb") as image_file: mime_type, _ = mimetypes.guess_type(fname)
encoded_string = base64.b64encode(image_file.read()).decode("utf-8") if not mime_type:
image_url = f"data:{mime_type};base64,{encoded_string}" continue
rel_fname = self.get_rel_fname(fname)
if mime_type.startswith("image/") and supports_images: with open(fname, "rb") as image_file:
image_messages += [ encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
{"type": "text", "text": f"Image file: {rel_fname}"}, image_url = f"data:{mime_type};base64,{encoded_string}"
{"type": "image_url", "image_url": {"url": image_url, "detail": "high"}}, rel_fname = self.get_rel_fname(fname)
]
elif mime_type == "application/pdf" and supports_pdfs: if mime_type.startswith("image/") and supports_images:
image_messages += [ image_messages += [
{"type": "text", "text": f"PDF file: {rel_fname}"}, {"type": "text", "text": f"Image file: {rel_fname}"},
{"type": "image_url", "image_url": image_url}, {"type": "image_url", "image_url": {"url": image_url, "detail": "high"}},
] ]
elif mime_type == "application/pdf" and supports_pdfs:
image_messages += [
{"type": "text", "text": f"PDF file: {rel_fname}"},
{"type": "image_url", "image_url": image_url},
]
if not image_messages: if not image_messages:
return None return None