File size: 803 Bytes
221edf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration

model_id = "dblasko/blip-dalle3-img2prompt"
model = BlipForConditionalGeneration.from_pretrained(model_id)
processor = BlipProcessor.from_pretrained(model_id)


def gen_caption(image):
    inputs = processor(images=image, return_tensors="pt")
    pixel_values = inputs.pixel_values

    generated_ids = model.generate(pixel_values=pixel_values, max_length=70)
    generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[
        0
    ]

    return generated_caption


# Create a gradio interface with an image input and a textbox output
demo = gr.Interface(
    fn=gen_caption,
    inputs=gr.Image(shape=(224, 224)),
    outputs=gr.Textbox(label="Generated caption"),
)
demo.launch()