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