Download handler.py from Fluf22/openai-clip-large: direct link, hf CLI and curl.
- Browser
- Download file 917 Bytes
-
https://huggingface.co/Fluf22/openai-clip-large/resolve/main/handler.py
- Command line
-
hf download hf://Fluf22/openai-clip-large/handler.py
-
curl -L -o handler.py https://huggingface.co/Fluf22/openai-clip-large/resolve/main/handler.py
917 Bytes
| from typing import Dict, List, Any | |
| from PIL import Image | |
| from io import BytesIO | |
| # Needs a specific package to install on the container | |
| # from optimum.nvidia.pipelines import pipeline | |
| from transformers import pipeline | |
| import base64 | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| self.pipeline = pipeline("zero-shot-image-classification", model=path, device=0) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| inputs = data.pop("inputs", {}) | |
| parameters = data.pop("parameters", {}) | |
| candidate_labels = parameters.pop("candidate_labels", []) | |
| print("data: ", data) | |
| # decode base64 image to PIL | |
| image = Image.open(BytesIO(base64.b64decode(inputs))) | |
| # run prediction on the image with provided candidates | |
| prediction = self.pipeline(images=[image], candidate_labels=candidate_labels) | |
| return prediction[0] |