Create image_caption.py
Browse files- utils/image_caption.py +29 -0
utils/image_caption.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
class ImageCaption:
|
5 |
+
def __init__(self):
|
6 |
+
self.pil_image = None
|
7 |
+
|
8 |
+
def load_image(self):
|
9 |
+
# Використовуємо унікальний ключ для file_uploader
|
10 |
+
self.image = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'], key="uploader_1")
|
11 |
+
|
12 |
+
if self.image is not None:
|
13 |
+
self.pil_image = Image.open(self.image)
|
14 |
+
st.image(self.pil_image, caption="Uploaded Image", use_column_width=True)
|
15 |
+
|
16 |
+
return self.pil_image
|
17 |
+
|
18 |
+
def send2ai(self, model, prompt):
|
19 |
+
if not model.model_name() == "gemini-1.5-flash":
|
20 |
+
raise Exception(f"VLM should be gemini-1.5-flash but got {model.model_name()}")
|
21 |
+
|
22 |
+
if self.pil_image is None:
|
23 |
+
raise ValueError("Image should be np.ndarray or PIL.Image but got None")
|
24 |
+
|
25 |
+
if prompt is None:
|
26 |
+
raise ValueError("Prompt should be str but got None")
|
27 |
+
|
28 |
+
response = model.execution().generate_content([prompt, self.pil_image])
|
29 |
+
return response.text
|