File size: 1,086 Bytes
0be5a46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import streamlit as st
from PIL import Image

class ImageCaption:
    def __init__(self):
        self.pil_image = None

    def load_image(self):
        # Використовуємо унікальний ключ для file_uploader
        self.image = st.file_uploader("Choose an image", type=['png', 'jpg', 'jpeg'], key="uploader_1")
        
        if self.image is not None:
            self.pil_image = Image.open(self.image)
            st.image(self.pil_image, caption="Uploaded Image", use_column_width=True)

        return self.pil_image

    def send2ai(self, model, prompt):
        if not model.model_name() == "gemini-1.5-flash":
            raise Exception(f"VLM should be gemini-1.5-flash but got {model.model_name()}")
        
        if self.pil_image is None:
            raise ValueError("Image should be np.ndarray or PIL.Image but got None")
        
        if prompt is None:
            raise ValueError("Prompt should be str but got None")
    
        response = model.execution().generate_content([prompt, self.pil_image])
        return response.text