File size: 1,030 Bytes
d107a3b
 
 
 
 
 
 
 
 
 
 
 
84f674d
 
d107a3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69aec30
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
30
31
32
33
from PIL import Image
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration 

class ImageCaption:
    def __init__(self):
        self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")  
        self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

    def generate(self,img):
        if isinstance(img,str):
            img =  Image.open(img)
        # text = "Explain this image,that what she is doing and what kind of expression is it"
        input = self.processor(img,return_tensors='pt')
        # print(**input)
        output = self.model.generate(**input)
        caption = self.processor.decode(output[0],skip_special_tokens = True)
        return caption
    

ic = ImageCaption()
app = gr.Interface(
    fn = ic.generate,
    inputs=gr.Image(type='pil'),
    outputs="text",
    description="upload image to generate caption"

)

app.launch()


# print(ic.generate(input("Enter the source of image: ")))