clarifai 9.11.1__py3-none-any.whl → 10.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- clarifai/client/input.py +34 -1
- clarifai/client/workflow.py +6 -2
- clarifai/constants/rag.py +1 -0
- clarifai/models/model_serving/README.md +1 -1
- clarifai/models/model_serving/models/default_test.py +3 -0
- clarifai/rag/__init__.py +3 -0
- clarifai/rag/rag.py +261 -0
- clarifai/rag/utils.py +102 -0
- clarifai/versions.py +1 -1
- {clarifai-9.11.1.dist-info → clarifai-10.0.0.dist-info}/METADATA +16 -3
- clarifai-10.0.0.dist-info/RECORD +103 -0
- clarifai/models/model_serving/examples/README.md +0 -7
- clarifai/models/model_serving/examples/image_classification/README.md +0 -12
- clarifai/models/model_serving/examples/image_classification/age_vit/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/image_classification/age_vit/1/inference.py +0 -64
- clarifai/models/model_serving/examples/image_classification/age_vit/1/model.py +0 -74
- clarifai/models/model_serving/examples/image_classification/age_vit/1/vit-age-classifier/README.md +0 -11
- clarifai/models/model_serving/examples/image_classification/age_vit/1/vit-age-classifier/config.json +0 -42
- clarifai/models/model_serving/examples/image_classification/age_vit/1/vit-age-classifier/preprocessor_config.json +0 -15
- clarifai/models/model_serving/examples/image_classification/age_vit/config.pbtxt +0 -23
- clarifai/models/model_serving/examples/image_classification/age_vit/labels.txt +0 -9
- clarifai/models/model_serving/examples/image_classification/age_vit/requirements.txt +0 -7
- clarifai/models/model_serving/examples/multimodal_embedder/README.md +0 -12
- clarifai/models/model_serving/examples/multimodal_embedder/clip/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/multimodal_embedder/clip/1/inference.py +0 -66
- clarifai/models/model_serving/examples/multimodal_embedder/clip/1/model.py +0 -74
- clarifai/models/model_serving/examples/multimodal_embedder/clip/1/test.py +0 -64
- clarifai/models/model_serving/examples/multimodal_embedder/clip/config.pbtxt +0 -29
- clarifai/models/model_serving/examples/multimodal_embedder/clip/requirements.txt +0 -4
- clarifai/models/model_serving/examples/text_classification/README.md +0 -12
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/1/inference.py +0 -62
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/1/model.py +0 -74
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/1/twitter-xlm-roberta-base-sentiment/README.md +0 -12
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/1/twitter-xlm-roberta-base-sentiment/config.json +0 -34
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/1/twitter-xlm-roberta-base-sentiment/special_tokens_map.json +0 -1
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/config.pbtxt +0 -21
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/labels.txt +0 -3
- clarifai/models/model_serving/examples/text_classification/xlm-roberta/requirements.txt +0 -7
- clarifai/models/model_serving/examples/text_embedding/README.md +0 -12
- clarifai/models/model_serving/examples/text_embedding/instructor-xl/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/text_embedding/instructor-xl/1/inference.py +0 -63
- clarifai/models/model_serving/examples/text_embedding/instructor-xl/1/model.py +0 -74
- clarifai/models/model_serving/examples/text_embedding/instructor-xl/1/test.py +0 -64
- clarifai/models/model_serving/examples/text_embedding/instructor-xl/config.pbtxt +0 -20
- clarifai/models/model_serving/examples/text_embedding/instructor-xl/requirements.txt +0 -9
- clarifai/models/model_serving/examples/text_to_image/README.md +0 -10
- clarifai/models/model_serving/examples/text_to_image/sd-v1.5/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/text_to_image/sd-v1.5/1/inference.py +0 -58
- clarifai/models/model_serving/examples/text_to_image/sd-v1.5/1/model.py +0 -74
- clarifai/models/model_serving/examples/text_to_image/sd-v1.5/config.pbtxt +0 -22
- clarifai/models/model_serving/examples/text_to_image/sd-v1.5/requirements.txt +0 -6
- clarifai/models/model_serving/examples/text_to_text/README.md +0 -12
- clarifai/models/model_serving/examples/text_to_text/bart-summarize/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/text_to_text/bart-summarize/1/inference.py +0 -59
- clarifai/models/model_serving/examples/text_to_text/bart-summarize/1/model.py +0 -74
- clarifai/models/model_serving/examples/text_to_text/bart-summarize/config.pbtxt +0 -20
- clarifai/models/model_serving/examples/text_to_text/bart-summarize/requirements.txt +0 -4
- clarifai/models/model_serving/examples/visual_detection/Readme.md +0 -61
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/1/inference.py +0 -96
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/1/model.py +0 -74
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/1/model_store/hub/checkpoints/keep +0 -0
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/1/test.py +0 -62
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/config.pbtxt +0 -35
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/labels.txt +0 -80
- clarifai/models/model_serving/examples/visual_detection/faster-rcnn_torchserve/requirements.txt +0 -3
- clarifai/models/model_serving/examples/visual_detection/yolof/1/config/yolof_r50_c5_8x8_1x_coco.py +0 -245
- clarifai/models/model_serving/examples/visual_detection/yolof/1/inference.py +0 -90
- clarifai/models/model_serving/examples/visual_detection/yolof/1/model.py +0 -74
- clarifai/models/model_serving/examples/visual_detection/yolof/1/test.py +0 -64
- clarifai/models/model_serving/examples/visual_detection/yolof/config.pbtxt +0 -36
- clarifai/models/model_serving/examples/visual_detection/yolof/labels.txt +0 -80
- clarifai/models/model_serving/examples/visual_detection/yolof/requirements.txt +0 -8
- clarifai/models/model_serving/examples/visual_embedding/README.md +0 -12
- clarifai/models/model_serving/examples/visual_embedding/vit-base/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/visual_embedding/vit-base/1/inference.py +0 -56
- clarifai/models/model_serving/examples/visual_embedding/vit-base/1/model.py +0 -74
- clarifai/models/model_serving/examples/visual_embedding/vit-base/config.pbtxt +0 -22
- clarifai/models/model_serving/examples/visual_embedding/vit-base/requirements.txt +0 -5
- clarifai/models/model_serving/examples/visual_segmentation/README.md +0 -12
- clarifai/models/model_serving/examples/visual_segmentation/segformer-b2/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/visual_segmentation/segformer-b2/1/inference.py +0 -62
- clarifai/models/model_serving/examples/visual_segmentation/segformer-b2/1/model.py +0 -74
- clarifai/models/model_serving/examples/visual_segmentation/segformer-b2/config.pbtxt +0 -24
- clarifai/models/model_serving/examples/visual_segmentation/segformer-b2/labels.txt +0 -18
- clarifai/models/model_serving/examples/visual_segmentation/segformer-b2/requirements.txt +0 -5
- clarifai/models/model_serving/examples/vllm/Readme.md +0 -12
- clarifai/models/model_serving/examples/vllm/example/1/__init__.py +0 -0
- clarifai/models/model_serving/examples/vllm/example/1/inference.py +0 -56
- clarifai/models/model_serving/examples/vllm/example/1/model.py +0 -74
- clarifai/models/model_serving/examples/vllm/example/1/test.py +0 -64
- clarifai/models/model_serving/examples/vllm/example/1/weights/keep +0 -0
- clarifai/models/model_serving/examples/vllm/example/config.pbtxt +0 -20
- clarifai/models/model_serving/examples/vllm/example/requirements.txt +0 -5
- clarifai-9.11.1.dist-info/RECORD +0 -182
- {clarifai-9.11.1.dist-info → clarifai-10.0.0.dist-info}/LICENSE +0 -0
- {clarifai-9.11.1.dist-info → clarifai-10.0.0.dist-info}/WHEEL +0 -0
- {clarifai-9.11.1.dist-info → clarifai-10.0.0.dist-info}/entry_points.txt +0 -0
- {clarifai-9.11.1.dist-info → clarifai-10.0.0.dist-info}/top_level.txt +0 -0
@@ -1,12 +0,0 @@
|
|
1
|
-
## Image Classification Triton Model Examples
|
2
|
-
|
3
|
-
These can be used on the fly with minimal or no changes to test deploy image classification models to the Clarifai platform. See the required files section for each model below.
|
4
|
-
|
5
|
-
* ### [VIT Age Classifier](./age_vit/)
|
6
|
-
|
7
|
-
Required files to run tests locally:
|
8
|
-
|
9
|
-
* Download the [model checkpoint from huggingface](https://huggingface.co/nateraw/vit-age-classifier/tree/main) and store it under `age_vit/1/checkpoint/`
|
10
|
-
```
|
11
|
-
huggingface-cli download nateraw/vit-age-classifier --local-dir age_vit/1/checkpoint/ --local-dir-use-symlinks False
|
12
|
-
```
|
File without changes
|
@@ -1,64 +0,0 @@
|
|
1
|
-
# This file contains boilerplate code to allow users write their model
|
2
|
-
# inference code that will then interact with the Triton Inference Server
|
3
|
-
# Python backend to serve end user requests.
|
4
|
-
# The module name, module path, class name & get_predictions() method names MUST be maintained as is
|
5
|
-
# but other methods may be added within the class as deemed fit provided
|
6
|
-
# they are invoked within the main get_predictions() inference method
|
7
|
-
# if they play a role in any step of model inference
|
8
|
-
"""User model inference script."""
|
9
|
-
|
10
|
-
import os
|
11
|
-
from pathlib import Path
|
12
|
-
from typing import Callable
|
13
|
-
|
14
|
-
import torch
|
15
|
-
from scipy.special import softmax
|
16
|
-
from transformers import AutoImageProcessor, ViTForImageClassification
|
17
|
-
|
18
|
-
from clarifai.models.model_serving.model_config import ModelTypes, get_model_config
|
19
|
-
from clarifai.models.model_serving.models.output import ClassifierOutput
|
20
|
-
|
21
|
-
config = get_model_config(ModelTypes.visual_classifier)
|
22
|
-
|
23
|
-
|
24
|
-
class InferenceModel:
|
25
|
-
"""User model inference class."""
|
26
|
-
|
27
|
-
def __init__(self) -> None:
|
28
|
-
"""
|
29
|
-
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
30
|
-
in this method so they are loaded only once for faster inference.
|
31
|
-
"""
|
32
|
-
self.base_path: Path = os.path.dirname(__file__)
|
33
|
-
self.huggingface_model_path: Path = os.path.join(self.base_path, "checkpoint")
|
34
|
-
self.transforms = AutoImageProcessor.from_pretrained(self.huggingface_model_path)
|
35
|
-
self.model: Callable = ViTForImageClassification.from_pretrained(self.huggingface_model_path)
|
36
|
-
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
37
|
-
|
38
|
-
@config.inference.wrap_func
|
39
|
-
def get_predictions(self, input_data: list, **kwargs) -> list:
|
40
|
-
"""
|
41
|
-
Main model inference method.
|
42
|
-
|
43
|
-
Args:
|
44
|
-
-----
|
45
|
-
input_data: A list of input data item to predict on.
|
46
|
-
Input data can be an image or text, etc depending on the model type.
|
47
|
-
|
48
|
-
**kwargs: your inference parameters.
|
49
|
-
|
50
|
-
Returns:
|
51
|
-
--------
|
52
|
-
List of one of the `clarifai.models.model_serving.models.output types` or `config.inference.return_type(your_output)`. Refer to the README/docs
|
53
|
-
"""
|
54
|
-
# Transform image and pass it to the model
|
55
|
-
inputs = self.transforms(input_data, return_tensors='pt')
|
56
|
-
with torch.no_grad():
|
57
|
-
preds = self.model(**inputs).logits
|
58
|
-
outputs = []
|
59
|
-
for pred in preds:
|
60
|
-
pred_scores = softmax(
|
61
|
-
pred.detach().numpy()) # alt: softmax(output.logits[0].detach().numpy())
|
62
|
-
outputs.append(ClassifierOutput(predicted_scores=pred_scores))
|
63
|
-
|
64
|
-
return outputs
|
@@ -1,74 +0,0 @@
|
|
1
|
-
# Copyright 2023 Clarifai, Inc.
|
2
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
# you may not use this file except in compliance with the License.
|
4
|
-
# You may obtain a copy of the License at
|
5
|
-
#
|
6
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
#
|
8
|
-
# Unless required by applicable law or agreed to in writing, software
|
9
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
# See the License for the specific language governing permissions and
|
12
|
-
# limitations under the License.
|
13
|
-
"""Triton inference server Python Backend Model."""
|
14
|
-
|
15
|
-
import os
|
16
|
-
import sys
|
17
|
-
|
18
|
-
try:
|
19
|
-
import triton_python_backend_utils as pb_utils
|
20
|
-
except ModuleNotFoundError:
|
21
|
-
pass
|
22
|
-
from google.protobuf import text_format
|
23
|
-
from tritonclient.grpc.model_config_pb2 import ModelConfig
|
24
|
-
from clarifai.models.model_serving.model_config.inference_parameter import parse_req_parameters
|
25
|
-
|
26
|
-
|
27
|
-
class TritonPythonModel:
|
28
|
-
"""
|
29
|
-
Triton Python BE Model.
|
30
|
-
"""
|
31
|
-
|
32
|
-
def initialize(self, args):
|
33
|
-
"""
|
34
|
-
Triton server init.
|
35
|
-
"""
|
36
|
-
args["model_repository"] = args["model_repository"].replace("/1/model.py", "")
|
37
|
-
sys.path.append(os.path.dirname(__file__))
|
38
|
-
from inference import InferenceModel
|
39
|
-
|
40
|
-
self.inference_obj = InferenceModel()
|
41
|
-
|
42
|
-
# Read input_name from config file
|
43
|
-
self.config_msg = ModelConfig()
|
44
|
-
with open(os.path.join(args["model_repository"], "config.pbtxt"), "r") as f:
|
45
|
-
cfg = f.read()
|
46
|
-
text_format.Merge(cfg, self.config_msg)
|
47
|
-
self.input_names = [inp.name for inp in self.config_msg.input]
|
48
|
-
|
49
|
-
def execute(self, requests):
|
50
|
-
"""
|
51
|
-
Serve model inference requests.
|
52
|
-
"""
|
53
|
-
responses = []
|
54
|
-
|
55
|
-
for request in requests:
|
56
|
-
parameters = request.parameters()
|
57
|
-
parameters = parse_req_parameters(parameters) if parameters else {}
|
58
|
-
|
59
|
-
if len(self.input_names) == 1:
|
60
|
-
in_batch = pb_utils.get_input_tensor_by_name(request, self.input_names[0])
|
61
|
-
in_batch = in_batch.as_numpy()
|
62
|
-
inference_response = self.inference_obj.get_predictions(in_batch, **parameters)
|
63
|
-
else:
|
64
|
-
multi_in_batch_dict = {}
|
65
|
-
for input_name in self.input_names:
|
66
|
-
in_batch = pb_utils.get_input_tensor_by_name(request, input_name)
|
67
|
-
in_batch = in_batch.as_numpy() if in_batch is not None else []
|
68
|
-
multi_in_batch_dict.update({input_name: in_batch})
|
69
|
-
|
70
|
-
inference_response = self.inference_obj.get_predictions(multi_in_batch_dict, **parameters)
|
71
|
-
|
72
|
-
responses.append(inference_response)
|
73
|
-
|
74
|
-
return responses
|
clarifai/models/model_serving/examples/image_classification/age_vit/1/vit-age-classifier/config.json
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"_name_or_path": "google/vit-base-patch16-224-in21k",
|
3
|
-
"architectures": [
|
4
|
-
"ViTForImageClassification"
|
5
|
-
],
|
6
|
-
"attention_probs_dropout_prob": 0.0,
|
7
|
-
"hidden_act": "gelu",
|
8
|
-
"hidden_dropout_prob": 0.0,
|
9
|
-
"hidden_size": 768,
|
10
|
-
"id2label": {
|
11
|
-
"0": "0-2",
|
12
|
-
"1": "3-9",
|
13
|
-
"2": "10-19",
|
14
|
-
"3": "20-29",
|
15
|
-
"4": "30-39",
|
16
|
-
"5": "40-49",
|
17
|
-
"6": "50-59",
|
18
|
-
"7": "60-69",
|
19
|
-
"8": "more than 70"
|
20
|
-
},
|
21
|
-
"image_size": 224,
|
22
|
-
"initializer_range": 0.02,
|
23
|
-
"intermediate_size": 3072,
|
24
|
-
"label2id": {
|
25
|
-
"0-2": 0,
|
26
|
-
"3-9": 1,
|
27
|
-
"10-19": 2,
|
28
|
-
"20-29": 3,
|
29
|
-
"30-39": 4,
|
30
|
-
"40-49": 5,
|
31
|
-
"50-59": 6,
|
32
|
-
"60-69": 7,
|
33
|
-
"more than 70": 8
|
34
|
-
},
|
35
|
-
"layer_norm_eps": 1e-12,
|
36
|
-
"model_type": "vit",
|
37
|
-
"num_attention_heads": 12,
|
38
|
-
"num_channels": 3,
|
39
|
-
"num_hidden_layers": 12,
|
40
|
-
"patch_size": 16,
|
41
|
-
"transformers_version": "4.5.0.dev0"
|
42
|
-
}
|
@@ -1,23 +0,0 @@
|
|
1
|
-
name: "age_vit"
|
2
|
-
max_batch_size: 1
|
3
|
-
input {
|
4
|
-
name: "image"
|
5
|
-
data_type: TYPE_UINT8
|
6
|
-
dims: -1
|
7
|
-
dims: -1
|
8
|
-
dims: 3
|
9
|
-
}
|
10
|
-
output {
|
11
|
-
name: "softmax_predictions"
|
12
|
-
data_type: TYPE_FP32
|
13
|
-
dims: -1
|
14
|
-
label_filename: "labels.txt"
|
15
|
-
}
|
16
|
-
instance_group {
|
17
|
-
count: 1
|
18
|
-
kind: KIND_GPU
|
19
|
-
}
|
20
|
-
dynamic_batching {
|
21
|
-
max_queue_delay_microseconds: 500
|
22
|
-
}
|
23
|
-
backend: "python"
|
@@ -1,12 +0,0 @@
|
|
1
|
-
## Multimodal Embedder Triton Model Examples
|
2
|
-
|
3
|
-
These can be used on the fly with minimal or no changes to test deploy image classification models to the Clarifai platform. See the required files section for each model below.
|
4
|
-
|
5
|
-
* ### [CLIP](./clip/)
|
6
|
-
|
7
|
-
Required files to run tests locally:
|
8
|
-
|
9
|
-
* Download the [model checkpoint from huggingface](https://huggingface.co/openai/clip-vit-base-patch32) and store it under `clip/1/checkpoint/`
|
10
|
-
```
|
11
|
-
huggingface-cli download openai/clip-vit-base-patch32 --local-dir clip/1/checkpoint/ --local-dir-use-symlinks False --exclude *.msgpack *.h5
|
12
|
-
```
|
File without changes
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# This file contains boilerplate code to allow users write their model
|
2
|
-
# inference code that will then interact with the Triton Inference Server
|
3
|
-
# Python backend to serve end user requests.
|
4
|
-
# The module name, module path, class name & get_predictions() method names MUST be maintained as is
|
5
|
-
# but other methods may be added within the class as deemed fit provided
|
6
|
-
# they are invoked within the main get_predictions() inference method
|
7
|
-
# if they play a role in any step of model inference
|
8
|
-
"""User model inference script."""
|
9
|
-
|
10
|
-
import os
|
11
|
-
from pathlib import Path
|
12
|
-
|
13
|
-
import torch
|
14
|
-
from transformers import CLIPModel, CLIPProcessor
|
15
|
-
from clarifai.models.model_serving.model_config import ModelTypes, get_model_config
|
16
|
-
|
17
|
-
config = get_model_config(ModelTypes.multimodal_embedder)
|
18
|
-
|
19
|
-
|
20
|
-
class InferenceModel:
|
21
|
-
"""User model inference class."""
|
22
|
-
|
23
|
-
def __init__(self) -> None:
|
24
|
-
"""
|
25
|
-
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
26
|
-
in this method so they are loaded only once for faster inference.
|
27
|
-
"""
|
28
|
-
self.base_path: Path = os.path.dirname(__file__)
|
29
|
-
## sample model loading code:
|
30
|
-
#self.checkpoint_path: Path = os.path.join(self.base_path, "your checkpoint filename/path")
|
31
|
-
#self.model: Callable = <load_your_model_here from checkpoint or folder>
|
32
|
-
self.model = CLIPModel.from_pretrained(os.path.join(self.base_path, "checkpoint"))
|
33
|
-
self.model.eval()
|
34
|
-
#self.text_model = CLIPTextModel.from_pretrained(os.path.join(self.base_path, "openai/clip-vit-base-patch32"))
|
35
|
-
self.processor = CLIPProcessor.from_pretrained(os.path.join(self.base_path, "checkpoint"))
|
36
|
-
|
37
|
-
#Add relevant model type decorator to the method below (see docs/model_types for ref.)
|
38
|
-
@config.inference.wrap_func
|
39
|
-
def get_predictions(self, input_data, **kwargs):
|
40
|
-
"""
|
41
|
-
Main model inference method.
|
42
|
-
|
43
|
-
Args:
|
44
|
-
-----
|
45
|
-
input_data: A single input data item to predict on.
|
46
|
-
Input data can be an image or text, etc depending on the model type.
|
47
|
-
|
48
|
-
Returns:
|
49
|
-
--------
|
50
|
-
One of the clarifai.models.model_serving.models.output types. Refer to the README/docs
|
51
|
-
"""
|
52
|
-
outputs = []
|
53
|
-
for inp in input_data:
|
54
|
-
image, text = inp["image"], inp["text"]
|
55
|
-
with torch.no_grad():
|
56
|
-
inputs = self.processor(text=text, images=image, return_tensors="pt", padding=True)
|
57
|
-
if text is not None:
|
58
|
-
inputs = self.processor(text=text, return_tensors="pt", padding=True)
|
59
|
-
embeddings = self.model.get_text_features(**inputs)
|
60
|
-
else:
|
61
|
-
inputs = self.processor(images=image, return_tensors="pt", padding=True)
|
62
|
-
embeddings = self.model.get_image_features(**inputs)
|
63
|
-
embeddings = embeddings.squeeze().cpu().numpy()
|
64
|
-
outputs.append(config.inference.return_type(embedding_vector=embeddings))
|
65
|
-
|
66
|
-
return outputs
|
@@ -1,74 +0,0 @@
|
|
1
|
-
# Copyright 2023 Clarifai, Inc.
|
2
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
-
# you may not use this file except in compliance with the License.
|
4
|
-
# You may obtain a copy of the License at
|
5
|
-
#
|
6
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
7
|
-
#
|
8
|
-
# Unless required by applicable law or agreed to in writing, software
|
9
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
10
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11
|
-
# See the License for the specific language governing permissions and
|
12
|
-
# limitations under the License.
|
13
|
-
"""Triton inference server Python Backend Model."""
|
14
|
-
|
15
|
-
import os
|
16
|
-
import sys
|
17
|
-
|
18
|
-
try:
|
19
|
-
import triton_python_backend_utils as pb_utils
|
20
|
-
except ModuleNotFoundError:
|
21
|
-
pass
|
22
|
-
from google.protobuf import text_format
|
23
|
-
from tritonclient.grpc.model_config_pb2 import ModelConfig
|
24
|
-
from clarifai.models.model_serving.model_config.inference_parameter import parse_req_parameters
|
25
|
-
|
26
|
-
|
27
|
-
class TritonPythonModel:
|
28
|
-
"""
|
29
|
-
Triton Python BE Model.
|
30
|
-
"""
|
31
|
-
|
32
|
-
def initialize(self, args):
|
33
|
-
"""
|
34
|
-
Triton server init.
|
35
|
-
"""
|
36
|
-
args["model_repository"] = args["model_repository"].replace("/1/model.py", "")
|
37
|
-
sys.path.append(os.path.dirname(__file__))
|
38
|
-
from inference import InferenceModel
|
39
|
-
|
40
|
-
self.inference_obj = InferenceModel()
|
41
|
-
|
42
|
-
# Read input_name from config file
|
43
|
-
self.config_msg = ModelConfig()
|
44
|
-
with open(os.path.join(args["model_repository"], "config.pbtxt"), "r") as f:
|
45
|
-
cfg = f.read()
|
46
|
-
text_format.Merge(cfg, self.config_msg)
|
47
|
-
self.input_names = [inp.name for inp in self.config_msg.input]
|
48
|
-
|
49
|
-
def execute(self, requests):
|
50
|
-
"""
|
51
|
-
Serve model inference requests.
|
52
|
-
"""
|
53
|
-
responses = []
|
54
|
-
|
55
|
-
for request in requests:
|
56
|
-
parameters = request.parameters()
|
57
|
-
parameters = parse_req_parameters(parameters) if parameters else {}
|
58
|
-
|
59
|
-
if len(self.input_names) == 1:
|
60
|
-
in_batch = pb_utils.get_input_tensor_by_name(request, self.input_names[0])
|
61
|
-
in_batch = in_batch.as_numpy()
|
62
|
-
inference_response = self.inference_obj.get_predictions(in_batch, **parameters)
|
63
|
-
else:
|
64
|
-
multi_in_batch_dict = {}
|
65
|
-
for input_name in self.input_names:
|
66
|
-
in_batch = pb_utils.get_input_tensor_by_name(request, input_name)
|
67
|
-
in_batch = in_batch.as_numpy() if in_batch is not None else []
|
68
|
-
multi_in_batch_dict.update({input_name: in_batch})
|
69
|
-
|
70
|
-
inference_response = self.inference_obj.get_predictions(multi_in_batch_dict, **parameters)
|
71
|
-
|
72
|
-
responses.append(inference_response)
|
73
|
-
|
74
|
-
return responses
|
@@ -1,64 +0,0 @@
|
|
1
|
-
import logging
|
2
|
-
import os
|
3
|
-
import unittest
|
4
|
-
|
5
|
-
from clarifai.models.model_serving.models.default_test import DefaultTestInferenceModel
|
6
|
-
|
7
|
-
|
8
|
-
class CustomTestInferenceModel(DefaultTestInferenceModel):
|
9
|
-
"""
|
10
|
-
Run this file to test your implementation of InferenceModel in inference.py with default tests of Triton configuration and its output values based on basic predefined inputs
|
11
|
-
If you want to write custom testcase or just test output value.
|
12
|
-
Please follow these instrucitons:
|
13
|
-
1. Name your test function with prefix "test" so that pytest can execute
|
14
|
-
2. In order to obtain output of InferenceModel, call `self.triton_get_predictions(input_data)`.
|
15
|
-
3. If your input is `image` and you have set custom size of it when building model repository,
|
16
|
-
call `self.preprocess(image)` to obtain correct resized input
|
17
|
-
4. Run this test by calling
|
18
|
-
```bash
|
19
|
-
pytest ./your_triton_folder/1/test.py
|
20
|
-
#to see std output
|
21
|
-
pytest --log-cli-level=INFO -s ./your_triton_folder/1/test.py
|
22
|
-
```
|
23
|
-
|
24
|
-
### Examples:
|
25
|
-
+ test text-to-image output
|
26
|
-
```
|
27
|
-
def test_text_to_image_output(self):
|
28
|
-
text = "Test text"
|
29
|
-
output = self.triton_get_predictions(text)
|
30
|
-
image = output.image # uint8 np.ndarray image
|
31
|
-
#show or save
|
32
|
-
```
|
33
|
-
+ test visual-classifier output
|
34
|
-
```
|
35
|
-
def test_visual_classifier(self):
|
36
|
-
image = cv2.imread("your/local/image.jpg") # Keep in mind of format of image (BGR or RGB)
|
37
|
-
output = self.triton_get_predictions(image)
|
38
|
-
scores = output.predicted_scores # np.ndarray
|
39
|
-
#process scores to get class id and its score
|
40
|
-
logger.info(result)
|
41
|
-
"""
|
42
|
-
|
43
|
-
# Insert your inference parameters json path here
|
44
|
-
# or insert a dictionary of your_parameter_name and value, e.g dict(x=1.5, y="text", c=True)
|
45
|
-
# or Leave it as "" if you don't have it.
|
46
|
-
inference_parameters = ""
|
47
|
-
|
48
|
-
########### Initialization. Do not change it ###########
|
49
|
-
__test__ = True
|
50
|
-
|
51
|
-
def setUp(self) -> None:
|
52
|
-
logging.info("Initializing...")
|
53
|
-
model_type = "multimodal-embedder" # your model type
|
54
|
-
self.intitialize(
|
55
|
-
model_type,
|
56
|
-
repo_version_dir=os.path.dirname(__file__),
|
57
|
-
is_instance_kind_gpu=True,
|
58
|
-
inference_parameters=self.inference_parameters)
|
59
|
-
|
60
|
-
########################################################
|
61
|
-
|
62
|
-
|
63
|
-
if __name__ == '__main__':
|
64
|
-
unittest.main()
|
@@ -1,29 +0,0 @@
|
|
1
|
-
name: "clip"
|
2
|
-
max_batch_size: 1
|
3
|
-
input {
|
4
|
-
name: "image"
|
5
|
-
data_type: TYPE_UINT8
|
6
|
-
dims: -1
|
7
|
-
dims: -1
|
8
|
-
dims: 3
|
9
|
-
optional: true
|
10
|
-
}
|
11
|
-
input {
|
12
|
-
name: "text"
|
13
|
-
data_type: TYPE_STRING
|
14
|
-
dims: 1
|
15
|
-
optional: true
|
16
|
-
}
|
17
|
-
output {
|
18
|
-
name: "embeddings"
|
19
|
-
data_type: TYPE_FP32
|
20
|
-
dims: -1
|
21
|
-
}
|
22
|
-
instance_group {
|
23
|
-
count: 1
|
24
|
-
kind: KIND_GPU
|
25
|
-
}
|
26
|
-
dynamic_batching {
|
27
|
-
max_queue_delay_microseconds: 500
|
28
|
-
}
|
29
|
-
backend: "python"
|
@@ -1,12 +0,0 @@
|
|
1
|
-
## Text Classification Triton Model Examples
|
2
|
-
|
3
|
-
These can be used on the fly with minimal or no changes to test deploy text classification models to the Clarifai platform. See the required files section for each model below.
|
4
|
-
|
5
|
-
* ### [XLM-Roberta Tweet Sentiment Classifier](./xlm-roberta/)
|
6
|
-
|
7
|
-
Required files to run tests locally:
|
8
|
-
|
9
|
-
* Download the [model checkpoint](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment/tree/main) and store it under `xlm-roberta/1/checkpoint/`
|
10
|
-
```
|
11
|
-
huggingface-cli download cardiffnlp/twitter-xlm-roberta-base-sentiment --local-dir xlm-roberta/1/checkpoint/ --local-dir-use-symlinks False
|
12
|
-
```
|
File without changes
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# This file contains boilerplate code to allow users write their model
|
2
|
-
# inference code that will then interact with the Triton Inference Server
|
3
|
-
# Python backend to serve end user requests.
|
4
|
-
# The module name, module path, class name & get_predictions() method names MUST be maintained as is
|
5
|
-
# but other methods may be added within the class as deemed fit provided
|
6
|
-
# they are invoked within the main get_predictions() inference method
|
7
|
-
# if they play a role in any step of model inference
|
8
|
-
"""User model inference script."""
|
9
|
-
|
10
|
-
import os
|
11
|
-
from pathlib import Path
|
12
|
-
from typing import Callable
|
13
|
-
|
14
|
-
import torch
|
15
|
-
from scipy.special import softmax
|
16
|
-
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
17
|
-
|
18
|
-
from clarifai.models.model_serving.model_config import ModelTypes, get_model_config
|
19
|
-
from clarifai.models.model_serving.models.output import ClassifierOutput
|
20
|
-
|
21
|
-
config = get_model_config(ModelTypes.text_classifier)
|
22
|
-
|
23
|
-
|
24
|
-
class InferenceModel:
|
25
|
-
"""User model inference class."""
|
26
|
-
|
27
|
-
def __init__(self) -> None:
|
28
|
-
"""
|
29
|
-
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
30
|
-
in this method so they are loaded only once for faster inference.
|
31
|
-
"""
|
32
|
-
self.base_path: Path = os.path.dirname(__file__)
|
33
|
-
self.checkpoint_path: Path = os.path.join(self.base_path, "checkpoint")
|
34
|
-
self.model: Callable = AutoModelForSequenceClassification.from_pretrained(self.checkpoint_path)
|
35
|
-
self.tokenizer: Callable = AutoTokenizer.from_pretrained(self.checkpoint_path)
|
36
|
-
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
37
|
-
|
38
|
-
@config.inference.wrap_func
|
39
|
-
def get_predictions(self, input_data: list, **kwargs) -> list:
|
40
|
-
"""
|
41
|
-
Main model inference method.
|
42
|
-
|
43
|
-
Args:
|
44
|
-
-----
|
45
|
-
input_data: A list of input data item to predict on.
|
46
|
-
Input data can be an image or text, etc depending on the model type.
|
47
|
-
|
48
|
-
**kwargs: your inference parameters.
|
49
|
-
|
50
|
-
Returns:
|
51
|
-
--------
|
52
|
-
List of one of the `clarifai.models.model_serving.models.output types` or `config.inference.return_type(your_output)`. Refer to the README/docs
|
53
|
-
"""
|
54
|
-
outputs = []
|
55
|
-
for inp in input_data:
|
56
|
-
encoded_input = self.tokenizer(inp, return_tensors='pt')
|
57
|
-
output = self.model(**encoded_input)
|
58
|
-
scores = output[0][0].detach().numpy()
|
59
|
-
scores = softmax(scores)
|
60
|
-
outputs.append(ClassifierOutput(predicted_scores=scores))
|
61
|
-
|
62
|
-
return outputs
|