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
Download handler.py from movementso/blip-image-captioning-large: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/movementso/blip-image-captioning-large/resolve/refs%2Fpr%2F1/handler.py
- Command line
-
hf download hf://movementso/blip-image-captioning-large@refs/pr/1/handler.py
-
curl -L -o handler.py https://huggingface.co/movementso/blip-image-captioning-large/resolve/refs%2Fpr%2F1/handler.py
1.3 kB
| 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)}] | |