ZubairAhmed777 commited on
Commit
982307f
·
verified ·
1 Parent(s): 3e4ae80

Create app.py

Browse files

Image caption app

Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BlipProcessor, BlipForConditionalGeneration
2
+ from PIL import Image
3
+ import gradio as gr
4
+
5
+ # Load BLIP processor and model
6
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
7
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
8
+
9
+ def generate_caption(image):
10
+ try:
11
+ # No need to open the image, Gradio provides it as a PIL object
12
+ inputs = processor(images=image, return_tensors="pt") # Use the image directly
13
+
14
+ # Generate caption
15
+ outputs = model.generate(**inputs)
16
+
17
+ # Decode and return the generated caption
18
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
19
+ return caption
20
+ except Exception as e:
21
+ return f"Error generating caption: {str(e)}"
22
+
23
+ # Create Gradio interface
24
+ iface = gr.Interface(
25
+ fn=generate_caption,
26
+ inputs=gr.Image(type="pil"),
27
+ outputs=gr.Textbox(label="Generated Caption"), # Use Textbox for text output
28
+ title="Image Captioning with BLIP",
29
+ description="Generate captions for your images using the BLIP model."
30
+ )
31
+
32
+ # Launch the interface
33
+ iface.launch()
34
+