Spaces:
Sleeping
Sleeping
Create blip_caption.py
Browse files- blip_caption.py +12 -0
blip_caption.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
# Load once on startup for performance
|
| 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(pil_img):
|
| 9 |
+
inputs = processor(images=pil_img, return_tensors="pt")
|
| 10 |
+
out = model.generate(**inputs)
|
| 11 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 12 |
+
return caption.lower()
|