File size: 2,712 Bytes
d30d0af
 
 
 
d613a26
d30d0af
 
 
 
 
 
 
d613a26
d30d0af
 
 
d613a26
 
 
 
 
d30d0af
d613a26
 
 
 
 
 
d30d0af
 
d613a26
d30d0af
d613a26
d30d0af
 
 
 
 
 
 
 
 
 
 
d613a26
d30d0af
 
d613a26
d30d0af
 
 
 
 
 
 
 
d613a26
d30d0af
 
 
 
 
 
 
 
 
 
 
 
d613a26
d30d0af
 
 
 
 
d613a26
d30d0af
 
 
 
 
 
 
7156ba8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
import torch
import time
from PIL import Image

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

def generate_caption(image):
    if image is None:
        return "Please upload an image to get started"
   
    try:
        start_time = time.time()
        
        # Ensure image is in RGB format and PIL Image
        if not isinstance(image, Image.Image):
            image = Image.fromarray(image)
        if image.mode != 'RGB':
            image = image.convert('RGB')
        
        # Add padding=True and return_tensors="pt"
        inputs = processor(images=image, return_tensors="pt", padding=True)
       
        with torch.no_grad():
            outputs = model.generate(**inputs, max_length=50, num_beams=3, early_stopping=True)
       
        caption = processor.decode(outputs[0], skip_special_tokens=True)
        processing_time = time.time() - start_time
       
        return f"**Caption:** {caption}\n\n*Processing time: {processing_time:.2f} seconds*"
   
    except Exception as e:
        return f"**Error:** {str(e)}"

custom_css = """
.gradio-container {
    max-width: 1000px;
    margin: 0 auto;
}
"""

with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("# Image Captioning AI")
    gr.Markdown("Using BLIP for AI-generated captions")
    gr.Markdown("Upload an image and get an AI-generated caption")
   
    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(
                type="pil",
                label="Upload Image",
                height=400,
                show_label=True,
            )
       
        with gr.Column(scale=1):
            output_text = gr.Markdown("Upload an image to get started")
            caption_btn = gr.Button("Generate Caption", variant="primary", size="lg")
            gr.Markdown("**Try these examples:**")
            gr.Examples(
                examples=[
                    "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=500",
                    "https://images.unsplash.com/photo-1574158622682-e40e69881006?w=500",
                    "https://images.unsplash.com/photo-1449824913935-59a10b8d2000?w=500"
                ],
                inputs=image_input
            )
    
    caption_btn.click(
        fn=generate_caption,
        inputs=image_input,
        outputs=output_text
    )
   
    image_input.change(
        fn=generate_caption,
        inputs=image_input,
        outputs=output_text
    )

if __name__ == "__main__":
    demo.launch()