Instructions to use advaitadasein/blip2_test with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use advaitadasein/blip2_test 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="advaitadasein/blip2_test")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("advaitadasein/blip2_test") model = AutoModelForMultimodalLM.from_pretrained("advaitadasein/blip2_test", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from advaitadasein/blip2_test: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/advaitadasein/blip2_test/resolve/main/handler.py
- Command line
-
hf download hf://advaitadasein/blip2_test/handler.py
-
curl -L -o handler.py https://huggingface.co/advaitadasein/blip2_test/resolve/main/handler.py
1.21 kB
| import requests | |
| from PIL import Image | |
| from transformers import Blip2Processor, Blip2ForConditionalGeneration | |
| from typing import Dict, List, Any | |
| import torch | |
| import base64 | |
| from io import BytesIO | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| self.processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b") | |
| self.model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b") | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| self.model.to(self.device) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| image_encoded = data.pop("inputs", data) | |
| text = data["text"] | |
| # image = Image.open(image_path) | |
| image = self.decode_base64_image(image_encoded) | |
| processed = self.processor(images=image, text=text, return_tensors="pt").to(self.device) | |
| out = self.model.generate(**processed) | |
| return self.processor.decode(out[0], skip_special_tokens=True) | |
| def decode_base64_image(self, image_string): | |
| base64_image = base64.b64decode(image_string) | |
| buffer = BytesIO(base64_image) | |
| image = Image.open(buffer) | |
| return image |