File size: 895 Bytes
54509a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image

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

def generate_caption(image: Image.Image) -> str:
    # Prepare the image for the model
    inputs = processor(images=image, return_tensors="pt")
    
    # Generate caption
    output = model.generate(**inputs)
    
    # Decode the caption
    caption = processor.decode(output[0], skip_special_tokens=True)
    return caption

def run():
    demo = gr.Interface(
        fn=generate_caption,
        inputs=gr.Image(type="pil"),
        outputs="text",
    )

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

if __name__ == "__main__":
    run()