Run this model with HF Jobs
Run Salesforce/blip-image-captioning-large on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/Salesforce/blip-image-captioning-large. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/Salesforce/blip-image-captioning-large on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use Salesforce/blip-image-captioning-large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Salesforce/blip-image-captioning-large 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="Salesforce/blip-image-captioning-large")# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large") model = AutoModelForMultimodalLM.from_pretrained("Salesforce/blip-image-captioning-large", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Create Handler.py
#46
by uriahadeniran065 - opened
12345678910111213141516171819202122232425262728293031323334353637383940414243from transformers import BlipProcessor, BlipForConditionalGenerationfrom PIL import Imageimport requestsimport torchimport base64import io
class EndpointHandler: def __init__(self, path): self.processor = BlipProcessor.from_pretrained(path) self.model = BlipForConditionalGeneration.from_pretrained( path, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32 ) self.device = "cuda" if torch.cuda.is_available() else "cpu" self.model.to(self.device)
def _load_image(self, image_input): # URL if isinstance(image_input, str) and image_input.startswith("http"): return Image.open(requests.get(image_input, stream=True).raw).convert("RGB")
# Base64 if isinstance(image_input, str): image_bytes = base64.b64decode(image_input) return Image.open(io.BytesIO(image_bytes)).convert("RGB")
raise ValueError("Unsupported image input format")
def __call__(self, data): image_input = data.get("inputs") if image_input is None: raise ValueError("No image provided")
image = self._load_image(image_input)
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
output = self.model.generate(**inputs, max_new_tokens=50)
caption = self.processor.decode(output[0], skip_special_tokens=True) return {"caption": caption}Handler.pyAdded+43