Instructions to use adasdimchom/blip2-opt-2.7b-coco with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use adasdimchom/blip2-opt-2.7b-coco 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="adasdimchom/blip2-opt-2.7b-coco")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("adasdimchom/blip2-opt-2.7b-coco") model = AutoModelForMultimodalLM.from_pretrained("adasdimchom/blip2-opt-2.7b-coco", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from adasdimchom/blip2-opt-2.7b-coco: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/adasdimchom/blip2-opt-2.7b-coco/resolve/refs%2Fpr%2F1/handler.py
- Command line
-
hf download hf://adasdimchom/blip2-opt-2.7b-coco@refs/pr/1/handler.py
-
curl -L -o handler.py https://huggingface.co/adasdimchom/blip2-opt-2.7b-coco/resolve/refs%2Fpr%2F1/handler.py
1.59 kB
| from transformers import Blip2Processor, Blip2ForConditionalGeneration | |
| from typing import Dict, List, Any | |
| from PIL import Image | |
| from transformers import pipeline | |
| import requests | |
| import torch | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| """ | |
| path: | |
| """ | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| self.processor = Blip2Processor.from_pretrained(path) | |
| self.model = Blip2ForConditionalGeneration.from_pretrained(path, torch_dtype=torch.float16).to(self.device) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| """ | |
| data args: | |
| inputs (:obj: `str` | `PIL.Image` | `np.array`) | |
| kwargs | |
| Return: | |
| A :obj:`list` | `dict`: will be serialized and returned | |
| """ | |
| result = {} | |
| inputs = data.pop("inputs", data) | |
| image_url = inputs['image_url'] | |
| if "prompt" in inputs: | |
| prompt = inputs["prompt"] | |
| else: | |
| prompt = None | |
| image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB') | |
| if prompt: | |
| processed_image = self.processor(images=image, text=prompt, return_tensors="pt").to(self.device, torch.float16) | |
| else: | |
| processed_image = self.processor(images=image, return_tensors="pt").to(self.device, torch.float16) | |
| output = self.model.generate(**processed_image) | |
| text_output = self.processor.decode(output[0], skip_special_tokens=True) | |
| result["text_output"] = text_output | |
| return result | |