File size: 751 Bytes
8c792ce
 
 
 
 
 
 
 
 
 
2e80f2b
 
 
 
 
8c792ce
 
2e80f2b
 
 
8c792ce
2e80f2b
 
 
 
 
 
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
from PIL import Image
import gradio as gr

from transformers import BlipProcessor, BlipForConditionalGeneration

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

model = BlipForConditionalGeneration.from_pretrained(model_id)
processor = BlipProcessor.from_pretrained(model_id)

def launch(input_image):
    # Convert Gradio image input to PIL Image
    image = Image.fromarray(input_image)

    # Process the image and generate a caption
    inputs = processor(image, return_tensors="pt")
    out = model.generate(**inputs)
    caption = processor.decode(out[0], skip_special_tokens=True)

    return caption

iface = gr.Interface(
    launch, 
    inputs=gr.inputs.Image(type="pil"),  # Set input type to image
    outputs="text"
)
iface.launch()