File size: 1,037 Bytes
117a8ee
 
84c2666
 
117a8ee
22b7206
117a8ee
 
84c2666
22b7206
117a8ee
 
 
 
 
 
22b7206
117a8ee
84c2666
117a8ee
22b7206
117a8ee
84c2666
22b7206
117a8ee
84c2666
117a8ee
84c2666
117a8ee
 
22b7206
 
117a8ee
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
import gradio as gr
import numpy as np
import torch
from PIL import Image
from transformers import AutoProcessor, BlipForConditionalGeneration

# Load the pretrained processor and model
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

def caption_image(input_image: np.ndarray):
    # Convert numpy array to PIL Image
    raw_image = Image.fromarray(input_image).convert("RGB")

    # Prepare inputs
    inputs = processor(raw_image, return_tensors="pt")

    # Generate caption
    with torch.no_grad():
        out = model.generate(**inputs, max_length=50)

    caption = processor.decode(out[0], skip_special_tokens=True)
    return caption

iface = gr.Interface(
    fn=caption_image,
    inputs=gr.Image(type="numpy"),
    outputs="text",
    title="Image Captioning",
    description="Upload an image and the BLIP model will generate a caption."
)

iface.launch(server_name="0.0.0.0", server_port=7860)