Instructions to use aayushgs/Salesforce-blip-image-captioning-large-custom-handler with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use aayushgs/Salesforce-blip-image-captioning-large-custom-handler 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="aayushgs/Salesforce-blip-image-captioning-large-custom-handler")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("aayushgs/Salesforce-blip-image-captioning-large-custom-handler") model = AutoModelForMultimodalLM.from_pretrained("aayushgs/Salesforce-blip-image-captioning-large-custom-handler", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from aayushgs/Salesforce-blip-image-captioning-large-custom-handler: direct link, hf CLI and curl.
- Browser
- Download file 2.13 kB
-
https://huggingface.co/aayushgs/Salesforce-blip-image-captioning-large-custom-handler/resolve/main/handler.py
- Command line
-
hf download hf://aayushgs/Salesforce-blip-image-captioning-large-custom-handler/handler.py
-
curl -L -o handler.py https://huggingface.co/aayushgs/Salesforce-blip-image-captioning-large-custom-handler/resolve/main/handler.py
2.13 kB
| import requests | |
| from typing import Dict, Any | |
| from PIL import Image | |
| import torch | |
| import base64 | |
| from io import BytesIO | |
| from transformers import BlipForConditionalGeneration, BlipProcessor | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| 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" | |
| ).to(device) | |
| self.model.eval() | |
| def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: | |
| input_data = data.get("inputs", {}) | |
| encoded_images = input_data.get("images") | |
| if not encoded_images: | |
| return {"captions": [], "error": "No images provided"} | |
| texts = input_data.get("texts", [""] * len(encoded_images)) | |
| try: | |
| raw_images = [ | |
| Image.open(BytesIO(base64.b64decode(img))).convert("RGB") | |
| for img in encoded_images | |
| ] | |
| processed_inputs = [ | |
| self.processor(image, text, return_tensors="pt") | |
| for image, text in zip(raw_images, texts) | |
| ] | |
| processed_inputs = { | |
| "pixel_values": torch.cat( | |
| [inp["pixel_values"] for inp in processed_inputs], dim=0 | |
| ).to(device), | |
| "input_ids": torch.cat( | |
| [inp["input_ids"] for inp in processed_inputs], dim=0 | |
| ).to(device), | |
| "attention_mask": torch.cat( | |
| [inp["attention_mask"] for inp in processed_inputs], dim=0 | |
| ).to(device), | |
| } | |
| with torch.no_grad(): | |
| out = self.model.generate(**processed_inputs) | |
| captions = self.processor.batch_decode(out, skip_special_tokens=True) | |
| return {"captions": captions} | |
| except Exception as e: | |
| print(f"Error during processing: {str(e)}") | |
| return {"captions": [], "error": str(e)} | |