initial commit
Browse files- .gitignore +3 -0
- README.md +30 -0
- handler.py +31 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# PhpStorm / IDEA
|
2 |
+
.idea
|
3 |
+
|
README.md
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- vision
|
4 |
+
- zero-shot-image-classification
|
5 |
+
- endpoints-template
|
6 |
+
inference: true
|
7 |
+
pipeline_tag: zero-shot-image-classification
|
8 |
+
base_model: openai/clip-vit-large-patch14
|
9 |
+
library_name: generic
|
10 |
+
---
|
11 |
+
|
12 |
+
# Fork of [openai/clip-vit-base-patch32](https://huggingface.co/openai/clip-vit-base-patch32) for a `zero-sho-image-classification` Inference endpoint.
|
13 |
+
|
14 |
+
This repository implements a `custom` task for `zero-shot-image-classification` for 🤗 Inference Endpoints. The code for the customized
|
15 |
+
pipeline is in the handler.py.
|
16 |
+
|
17 |
+
To use deploy this model an Inference Endpoint you have to select `Custom` as task to use the `handler.py` file.
|
18 |
+
|
19 |
+
### expected Request payload
|
20 |
+
|
21 |
+
```json
|
22 |
+
{
|
23 |
+
"image": encoded_image,
|
24 |
+
"parameters": {
|
25 |
+
"candidate_labels": "green, yellow, blue, white, silver"
|
26 |
+
}
|
27 |
+
}
|
28 |
+
```
|
29 |
+
|
30 |
+
`encoded_image` is a base64 encoded image.
|
handler.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
from transformers import pipeline
|
5 |
+
import base64
|
6 |
+
|
7 |
+
|
8 |
+
class EndpointHandler():
|
9 |
+
def __init__(self, path=""):
|
10 |
+
self.pipeline=pipeline("zero-shot-image-classification",model="openai/clip-vit-large-patch14-336")
|
11 |
+
|
12 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
13 |
+
"""
|
14 |
+
data args:
|
15 |
+
inputs (:obj:`string`)
|
16 |
+
parameters (:obj:)
|
17 |
+
Return:
|
18 |
+
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
|
19 |
+
"""
|
20 |
+
image_data = data.pop("inputs", data)
|
21 |
+
# decode base64 image to PIL
|
22 |
+
image = Image.open(BytesIO(base64.b64decode(image_data)))
|
23 |
+
|
24 |
+
parameters = data.pop("parameters", data)
|
25 |
+
candidate_labels = parameters['candidate_labels']
|
26 |
+
|
27 |
+
candidate_labels_array = list(map(str.strip, candidate_labels.split(',')))
|
28 |
+
|
29 |
+
# run prediction one image wit provided candiates
|
30 |
+
prediction = self.pipeline(images=[image], candidate_labels=candidate_labels_array)
|
31 |
+
return prediction[0]
|