File size: 1,406 Bytes
937eb55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
import torch
import gradio as gr

model_name = "Salesforce/blip-image-captioning-base"

caption_processor = BlipProcessor.from_pretrained(model_name)
model = BlipForConditionalGeneration.from_pretrained(model_name)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

def generate_captions(image, num_captions=5,size=(512, 512)):
    image = image.resize(size)
    if image.mode != 'RGB':
        image = image.convert('RGB')
    pixel_values = caption_processor(image, return_tensors='pt').to(device)
    
    caption_ids = model.generate(
        **pixel_values, 
        max_length=30,
        num_beams=5,
        num_return_sequences=num_captions,
        temperature=1.0
    )
    
    captions = [
        caption_processor.decode(ids, skip_special_tokens=True) 
        for ids in caption_ids
    ]
    
    return captions

from gradio.components import Image, Textbox,Slider

interface = gr.Interface(
    fn=generate_captions,
    inputs=[
        Image(type="pil", label="Input Image"),
        Slider(minimum=1, maximum=5, step=1, label="Number of Captions")
    ],
    outputs=Textbox(type="text", label="Captions"),
    title="Image Caption Generator",
    description="AI tool that creates captions based on the image provided by the user.",
)

interface.launch()