File size: 990 Bytes
57757ab
 
 
 
 
 
4d48127
57757ab
 
 
 
 
4d48127
57757ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
853dcfb
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
import gradio as gr
from PIL import Image
import requests
from transformers import BlipProcessor, BlipForConditionalGeneration

processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to("cpu")

def generate_caption(image_url):
    raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
    
    # Unconditional image captioning
    inputs = processor(raw_image, return_tensors="pt").to("cpu")
    out = model.generate(**inputs)
    caption = processor.decode(out[0], skip_special_tokens=True)
    
    return caption

# Define the Gradio interface
iface = gr.Interface(
    fn=generate_caption,
    inputs="text",  # URL input
    outputs="text",  # Caption output
    title="Image Captioning with BLIP",
    description="Provide an image URL, and the model will generate a caption."
)

if __name__ == "__main__":
    iface.launch(share=True)