Instructions to use Vidensogende/image-captioning-with-blip with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Vidensogende/image-captioning-with-blip 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="Vidensogende/image-captioning-with-blip")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Vidensogende/image-captioning-with-blip") model = AutoModelForMultimodalLM.from_pretrained("Vidensogende/image-captioning-with-blip", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from Vidensogende/image-captioning-with-blip: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/Vidensogende/image-captioning-with-blip/resolve/main/handler.py
- Command line
-
hf download hf://Vidensogende/image-captioning-with-blip/handler.py
-
curl -L -o handler.py https://huggingface.co/Vidensogende/image-captioning-with-blip/resolve/main/handler.py
1.97 kB
| import requests | |
| from PIL import Image | |
| from transformers import BlipProcessor, BlipForConditionalGeneration | |
| import torch | |
| from typing import Dict, List, Any | |
| 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") | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| self.model.to(self.device) | |
| def process_single_image(self, img_url, text=None): | |
| raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB') | |
| if text: | |
| inputs = self.processor(raw_image, text, return_tensors="pt").to(self.device) | |
| else: | |
| inputs = self.processor(raw_image, return_tensors="pt").to(self.device) | |
| out = self.model.generate(**inputs) | |
| return self.processor.decode(out[0], skip_special_tokens=True) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| try: | |
| print(f"Received data: {data}") | |
| if not data or "images" not in data: | |
| return [{"error": "No images data provided in the request."}] | |
| images_data = data.get("images") | |
| alt_texts = [] | |
| for image in images_data: | |
| img_id = image.get("id") | |
| img_url = image.get("url") | |
| text = image.get("text", None) | |
| alt_text = self.process_single_image(img_url, text) | |
| alt_texts.append({ | |
| "image_id": img_id, | |
| "image_url": img_url, | |
| "alt_text": alt_text | |
| }) | |
| return alt_texts | |
| except Exception as e: | |
| print(f"Error processing data: {e}") | |
| return [{"error": str(e)}] | |
| def get_pipeline(model_dir, task): | |
| return EndpointHandler(model_dir) | |