Instructions to use s3-tresio/blip-image-captioning-base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use s3-tresio/blip-image-captioning-base 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="s3-tresio/blip-image-captioning-base")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("s3-tresio/blip-image-captioning-base") model = AutoModelForMultimodalLM.from_pretrained("s3-tresio/blip-image-captioning-base", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from s3-tresio/blip-image-captioning-base: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/s3-tresio/blip-image-captioning-base/resolve/main/handler.py
- Command line
-
hf download hf://s3-tresio/blip-image-captioning-base/handler.py
-
curl -L -o handler.py https://huggingface.co/s3-tresio/blip-image-captioning-base/resolve/main/handler.py
2.03 kB
| # + | |
| from typing import Dict, List, Any | |
| from PIL import Image | |
| import torch | |
| import requests | |
| from io import BytesIO | |
| from transformers import BlipForConditionalGeneration, BlipProcessor | |
| from functools import reduce | |
| # - | |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| model = "Salesforce/blip-image-captioning-large" | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| # load the optimized model | |
| self.processor = BlipProcessor.from_pretrained(model) | |
| self.model = BlipForConditionalGeneration.from_pretrained(model).to(device) | |
| self.model.eval() | |
| self.model = self.model.to(device) | |
| def __call__(self, data: Any) -> Dict[str, Any]: | |
| """ | |
| Args: | |
| data (:obj:): | |
| includes the input data and the parameters for the inference. | |
| Return: | |
| A :obj:`dict`:. The object returned should be a dict of one list like {"captions": ["A hugging face at the office"]} containing : | |
| - "caption": A string corresponding to the generated caption. | |
| """ | |
| inputs = data.pop("inputs", data) | |
| parameters = data.pop("parameters", {}) | |
| raw_images = [Image.open(BytesIO(requests.get(_img).content)) for _img in inputs] | |
| processed_image = self.processor(images=raw_images, return_tensors="pt") | |
| processed_image["pixel_values"] = processed_image["pixel_values"].to(device) | |
| processed_image = {**processed_image, **parameters} | |
| with torch.no_grad(): | |
| out = self.model.generate( | |
| **processed_image | |
| ) | |
| captions = self.processor.batch_decode(out, skip_special_tokens=True) | |
| replace_sets = ("arafed", ""), ("araffe", ""), ("araff", "") | |
| filtered_captions = [reduce(lambda a, kv: a.replace(*kv).strip(), replace_sets, caption) for caption in captions] | |
| # postprocess the prediction | |
| return {"captions": filtered_captions} | |