Skip to content

Commit

Permalink
Image shrink fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KillianLucas committed Jul 29, 2024
1 parent f1eb5f2 commit e0d78fa
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions interpreter/core/llm/utils/convert_to_openai_messages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import io
import json
import sys

from PIL import Image

Expand Down Expand Up @@ -151,7 +152,7 @@ def convert_to_openai_messages(
# Shrink to less than 5mb

# Calculate size
content_size_bytes = len(content) * 3 / 4
content_size_bytes = sys.getsizeof(str(content))

# Convert the size to MB
content_size_mb = content_size_bytes / (1024 * 1024)
Expand All @@ -162,12 +163,6 @@ def convert_to_openai_messages(
img_data = base64.b64decode(encoded_string)
img = Image.open(io.BytesIO(img_data))

# Calculate the size of the original binary data in bytes
content_size_bytes = len(img_data)

# Convert the size to MB
content_size_mb = content_size_bytes / (1024 * 1024)

# Run in a loop to make SURE it's less than 5mb
for _ in range(10):
# Calculate the scale factor needed to reduce the image size to 4.9 MB
Expand All @@ -191,10 +186,13 @@ def convert_to_openai_messages(
content = f"data:image/{extension};base64,{encoded_string}"

# Recalculate the size of the content in bytes
content_size_bytes = len(content) * 3 / 4
content_size_bytes = sys.getsizeof(str(content))

# Convert the size to MB
content_size_mb = content_size_bytes / (1024 * 1024)

if content_size_mb < 5:
break
else:
print(
"Attempted to shrink the image but failed. Sending to the LLM anyway."
Expand Down

0 comments on commit e0d78fa

Please sign in to comment.