import gradio as gr import numpy as np 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) -> str: # Convert NumPy array to PIL Image and ensure RGB raw_image = Image.fromarray(input_image).convert("RGB") # Prepare inputs for BLIP (image-only captioning) inputs = processor(images=raw_image, return_tensors="pt") # Generate a caption (limit length for responsiveness) outputs = model.generate(**inputs, max_length=50) # Decode tokens to text caption = processor.decode(outputs[0], skip_special_tokens=True) return caption iface = gr.Interface( fn=caption_image, inputs=gr.Image(label="Upload an image"), outputs=gr.Textbox(label="Generated caption"), title="Image Captioning", description="A simple web app for generating captions using the BLIP model." ) if __name__ == "__main__": iface.launch()