Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,29 +4,43 @@ from PIL import Image
|
|
| 4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
import time
|
| 6 |
|
|
|
|
|
|
|
| 7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 8 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 9 |
|
| 10 |
-
def caption(img,
|
| 11 |
raw_image = Image.open(img).convert('RGB')
|
| 12 |
-
|
| 13 |
inputs = processor(raw_image, return_tensors="pt")
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
return processor.decode(out[0], skip_special_tokens=True)
|
| 17 |
|
| 18 |
-
def greet(img,
|
|
|
|
|
|
|
| 19 |
start = time.time()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
iface = gr.Interface(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
Image.MAX_IMAGE_PIXELS = None # disable pillow’s size limit
|
| 8 |
+
|
| 9 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 10 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 11 |
|
| 12 |
+
def caption(img, min_new, max_new):
|
| 13 |
raw_image = Image.open(img).convert('RGB')
|
| 14 |
+
raw_image.thumbnail((1024, 1024))
|
| 15 |
inputs = processor(raw_image, return_tensors="pt")
|
| 16 |
+
out = model.generate(
|
| 17 |
+
**inputs,
|
| 18 |
+
min_new_tokens=min_new,
|
| 19 |
+
max_new_tokens=max_new
|
| 20 |
+
)
|
| 21 |
return processor.decode(out[0], skip_special_tokens=True)
|
| 22 |
|
| 23 |
+
def greet(img, min_new, max_new):
|
| 24 |
+
if img is None:
|
| 25 |
+
return "❌ Please upload an image."
|
| 26 |
start = time.time()
|
| 27 |
+
try:
|
| 28 |
+
result = caption(img, min_new, max_new)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"⚠️ Error: {e}"
|
| 31 |
+
elapsed = time.time() - start
|
| 32 |
+
return f"{result}\n⏱ Took {elapsed:.2f} seconds"
|
| 33 |
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=greet,
|
| 36 |
+
title='BLIP Image Captioning (large)',
|
| 37 |
+
description="Uses Salesforce/blip-image-captioning-large on CPU.",
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Image(type='filepath', label='Image'),
|
| 40 |
+
gr.Slider(label='Min New Tokens', minimum=1, maximum=50, value=5),
|
| 41 |
+
gr.Slider(label='Max New Tokens', minimum=1, maximum=100, value=20),
|
| 42 |
+
],
|
| 43 |
+
outputs=gr.Textbox(label='Caption'),
|
| 44 |
+
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
|
| 45 |
+
)
|
| 46 |
+
iface.launch()
|