Instructions to use movementso/blip-image-captioning-large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use movementso/blip-image-captioning-large with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("image-to-text", model="movementso/blip-image-captioning-large")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("movementso/blip-image-captioning-large") model = AutoModelForMultimodalLM.from_pretrained("movementso/blip-image-captioning-large", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 1,301 Bytes
79f86bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from typing import Dict, List, Any
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import requests
class EndpointHandler():
def __init__(self, path=""):
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
data args:
image_url (:obj: `str`): URL of the image to be captioned
Return:
A :obj:`list` | `dict`: will be serialized and returned
"""
# get inputs
image_url = data.pop("image_url", None)
# check if image_url exists
if image_url is None:
return [{"error": "image_url not provided"}]
# get image from URL
try:
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
except:
return [{"error": "unable to load image from the provided URL"}]
# unconditional image captioning
inputs = self.processor(raw_image, return_tensors="pt")
# generate captions
out = self.model.generate(**inputs)
# return the generated captions
return [{"caption": self.processor.decode(out[0], skip_special_tokens=True)}]
|