Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
+
import gradio as gr
|
| 5 |
+
# Load the processor and model
|
| 6 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 8 |
+
def generate_caption(image):
|
| 9 |
+
# Preprocess the image
|
| 10 |
+
inputs = processor(image, return_tensors="pt")
|
| 11 |
+
|
| 12 |
+
# Generate text
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
output = model.generate(**inputs)
|
| 15 |
+
|
| 16 |
+
# Decode the generated text
|
| 17 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 18 |
+
return caption
|
| 19 |
+
def interface(image):
|
| 20 |
+
try:
|
| 21 |
+
# Ensure image is a PIL Image
|
| 22 |
+
image = image.convert("RGB") # Convert to RGB to ensure compatibility
|
| 23 |
+
# Generate caption
|
| 24 |
+
caption = generate_caption(image)
|
| 25 |
+
return caption # Return only the caption
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return str(e) # Return error message if any issue occurs
|
| 28 |
+
|
| 29 |
+
# Create the Gradio interface
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=interface,
|
| 32 |
+
inputs=gr.Image(type="pil", label="Upload an Image"), # Input for uploading an image
|
| 33 |
+
outputs=gr.Textbox(label="What image tells???"), # Output will be the caption
|
| 34 |
+
title="Image Captioning with BLIP",
|
| 35 |
+
description="Upload an image to generate a caption using the BLIP model."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the interface
|
| 39 |
+
iface.launch(inline = False)
|