File size: 905 Bytes
854fcef
 
 
 
 
 
 
 
 
fbfec82
 
 
1a004cd
 
854fcef
1a004cd
 
854fcef
1a004cd
 
854fcef
 
fbfec82
1a004cd
 
 
 
 
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
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 generate_caption(image_array):
    # Convert numpy array to PIL Image
    image = Image.fromarray(image_array.astype('uint8')).convert('RGB')
    
    # Process the image to generate tensor inputs
    inputs = processor(image, return_tensors="pt")
    
    # Generate caption for the image
    out = model.generate(**inputs)
    
    # Decode and return the generated caption
    return processor.decode(out[0], skip_special_tokens=True)


# Gradio interface setup to accept image input and produce text output
iface = gr.Interface(generate_caption, inputs="image", outputs="text")

# Launch the interface
iface.launch()