mrbeliever commited on
Commit
11f5df3
·
verified ·
1 Parent(s): b7cb27b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -2,10 +2,22 @@ import streamlit as st
2
  import base64
3
  import os
4
  import requests
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Function to convert uploaded image to base64
7
  def convert_image_to_base64(image):
8
- image_bytes = image.read()
 
9
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
10
  return encoded_image
11
 
@@ -29,12 +41,12 @@ def generate_caption(encoded_image):
29
  {
30
  "role": "user",
31
  "content": "Write a caption for this image"
 
 
 
 
32
  }
33
  ],
34
- "image": {
35
- "type": "image_url",
36
- "image_url": f"data:image/png;base64,{encoded_image}"
37
- },
38
  "temperature": 0
39
  }
40
 
 
2
  import base64
3
  import os
4
  import requests
5
+ from PIL import Image
6
+ from io import BytesIO
7
+
8
+ # Function to compress and resize the image before base64 encoding
9
+ def compress_and_resize_image(image, max_size=(1024, 1024), quality=85):
10
+ img = Image.open(image)
11
+ img.thumbnail(max_size) # Resize image while maintaining aspect ratio
12
+ with BytesIO() as byte_io:
13
+ img.save(byte_io, format="JPEG", quality=quality) # Save with reduced quality
14
+ byte_io.seek(0)
15
+ return byte_io
16
 
17
  # Function to convert uploaded image to base64
18
  def convert_image_to_base64(image):
19
+ compressed_image = compress_and_resize_image(image)
20
+ image_bytes = compressed_image.read()
21
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
22
  return encoded_image
23
 
 
41
  {
42
  "role": "user",
43
  "content": "Write a caption for this image"
44
+ },
45
+ {
46
+ "role": "user",
47
+ "content": f"data:image/png;base64,{encoded_image}" # This is where the image is passed as base64 directly
48
  }
49
  ],
 
 
 
 
50
  "temperature": 0
51
  }
52