Instructions to use mattmdjaga/clip-vit-base-patch32_handler with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mattmdjaga/clip-vit-base-patch32_handler with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-image-classification", model="mattmdjaga/clip-vit-base-patch32_handler") pipe( "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png", candidate_labels=["animals", "humans", "landscape"], )# Load model directly from transformers import AutoProcessor, AutoModelForZeroShotImageClassification processor = AutoProcessor.from_pretrained("mattmdjaga/clip-vit-base-patch32_handler") model = AutoModelForZeroShotImageClassification.from_pretrained("mattmdjaga/clip-vit-base-patch32_handler", device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 1,453 Bytes
d364efb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from typing import Dict, List, Any
from PIL import Image
from io import BytesIO
from transformers import CLIPProcessor, CLIPModel
import base64
import torch
class EndpointHandler():
def __init__(self, path="."):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = CLIPModel.from_pretrained(path).to(self.device).eval()
self.processor = CLIPProcessor.from_pretrained(path)
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
data args:
images (:obj:`PIL.Image`)
candiates (:obj:`list`)
Return:
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
"""
inputs = data.pop("inputs", data)
# decode base64 image to PIL
image = Image.open(BytesIO(base64.b64decode(inputs['image'])))
txt = inputs['text']
# preprocess image
txt = self.processor(text=txt, return_tensors="pt",padding=True).to(self.device)
image = self.processor(images=image, return_tensors="pt",padding=True).to(self.device)
with torch.no_grad():
txt_features = self.model.get_text_features(**txt)
image_features = self.model.get_image_features(**image)
img = image_features.tolist()
txt = txt_features.tolist()
pred = {"image": img, "text": txt}
return pred
|