Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
)
|
| 9 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 6 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 7 |
+
|
| 8 |
+
def generate_caption(image):
|
| 9 |
+
# Now directly using the PIL Image object
|
| 10 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 11 |
+
outputs = model.generate(**inputs)
|
| 12 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 13 |
+
return caption
|
| 14 |
+
|
| 15 |
+
def caption_image(image):
|
| 16 |
+
"""
|
| 17 |
+
Takes a PIL Image input and returns a caption.
|
| 18 |
+
"""
|
| 19 |
+
try:
|
| 20 |
+
caption = generate_caption(image)
|
| 21 |
+
return caption
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"An error occurred: {str(e)}"
|
| 24 |
+
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=caption_image,
|
| 27 |
+
inputs=gr.Image(type="pil"),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Image Captioning with BLIP",
|
| 30 |
+
description="Upload an image to generate a caption."
|
| 31 |
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|