Instructions to use Sheryl815/clip-vit-base-patch32-purr with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Sheryl815/clip-vit-base-patch32-purr with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("zero-shot-image-classification", model="Sheryl815/clip-vit-base-patch32-purr") pipe( "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png", candidate_labels=["animals", "humans", "landscape"], )# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForZeroShotImageClassification processor = AutoProcessor.from_pretrained("Sheryl815/clip-vit-base-patch32-purr") model = AutoModelForZeroShotImageClassification.from_pretrained("Sheryl815/clip-vit-base-patch32-purr", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from Sheryl815/clip-vit-base-patch32-purr: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/Sheryl815/clip-vit-base-patch32-purr/resolve/main/handler.py
- Command line
-
hf download hf://Sheryl815/clip-vit-base-patch32-purr/handler.py
-
curl -L -o handler.py https://huggingface.co/Sheryl815/clip-vit-base-patch32-purr/resolve/main/handler.py
1.92 kB
| from typing import Dict, List, Any | |
| from transformers import AutoProcessor, AutoTokenizer, CLIPModel | |
| from PIL import Image | |
| import requests | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| self.model = CLIPModel.from_pretrained(path) | |
| self.processor = AutoProcessor.from_pretrained(path) | |
| self.tokenizer = AutoTokenizer.from_pretrained(path) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| """ | |
| data args: | |
| inputs (:obj: `str`) | |
| date (:obj: `str`) | |
| Return: | |
| A :obj:`list` | `dict`: will be serialized and returned | |
| """ | |
| # get inputs | |
| inputs = data.pop("inputs", data) # if type is image, data is url | |
| params = data.pop("parameters", data) | |
| if params['datatype'] == 'image': | |
| image = Image.open(requests.get(inputs, stream=True).raw) | |
| # embed image | |
| features = self.embed_image(image) | |
| elif params['datatype'] == 'text': | |
| # embed text | |
| features = self.embed_text(inputs) | |
| # return features | |
| return {"features": features[0]} | |
| def embed_text(self, text): | |
| inputs = self.tokenizer(text, padding=True, return_tensors="pt") | |
| text_features = self.model.get_text_features(**inputs) | |
| # normalize | |
| text_features = text_features / text_features.norm(dim=-1, keepdim=True) | |
| # make a list of text features | |
| text_features = text_features.tolist() | |
| return text_features | |
| def embed_image(self, image): | |
| inputs = self.processor(images=image, return_tensors="pt") | |
| image_features = self.model.get_image_features(**inputs) | |
| # normalize | |
| image_features = image_features / image_features.norm(dim=-1, keepdim=True) | |
| # make a list of image features | |
| image_features = image_features.tolist() | |
| return image_features |