clarifai 10.8.4__py3-none-any.whl → 10.8.5__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/__init__.py +1 -1
- clarifai/client/dataset.py +9 -3
- clarifai/constants/dataset.py +1 -1
- clarifai/datasets/upload/base.py +6 -3
- clarifai/datasets/upload/features.py +10 -0
- clarifai/datasets/upload/image.py +22 -13
- clarifai/datasets/upload/multimodal.py +70 -0
- clarifai/datasets/upload/text.py +8 -5
- clarifai/utils/misc.py +6 -0
- {clarifai-10.8.4.dist-info → clarifai-10.8.5.dist-info}/METADATA +2 -1
- {clarifai-10.8.4.dist-info → clarifai-10.8.5.dist-info}/RECORD +15 -58
- clarifai/models/model_serving/README.md +0 -158
- clarifai/models/model_serving/__init__.py +0 -14
- clarifai/models/model_serving/cli/__init__.py +0 -12
- clarifai/models/model_serving/cli/_utils.py +0 -53
- clarifai/models/model_serving/cli/base.py +0 -14
- clarifai/models/model_serving/cli/build.py +0 -79
- clarifai/models/model_serving/cli/clarifai_clis.py +0 -33
- clarifai/models/model_serving/cli/create.py +0 -171
- clarifai/models/model_serving/cli/example_cli.py +0 -34
- clarifai/models/model_serving/cli/login.py +0 -26
- clarifai/models/model_serving/cli/upload.py +0 -183
- clarifai/models/model_serving/constants.py +0 -21
- clarifai/models/model_serving/docs/cli.md +0 -161
- clarifai/models/model_serving/docs/concepts.md +0 -229
- clarifai/models/model_serving/docs/dependencies.md +0 -11
- clarifai/models/model_serving/docs/inference_parameters.md +0 -139
- clarifai/models/model_serving/docs/model_types.md +0 -19
- clarifai/models/model_serving/model_config/__init__.py +0 -16
- clarifai/models/model_serving/model_config/base.py +0 -369
- clarifai/models/model_serving/model_config/config.py +0 -312
- clarifai/models/model_serving/model_config/inference_parameter.py +0 -129
- clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +0 -25
- clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +0 -19
- clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +0 -20
- clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +0 -19
- clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +0 -19
- clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +0 -22
- clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +0 -32
- clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +0 -19
- clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +0 -19
- clarifai/models/model_serving/model_config/output.py +0 -133
- clarifai/models/model_serving/model_config/triton/__init__.py +0 -14
- clarifai/models/model_serving/model_config/triton/serializer.py +0 -136
- clarifai/models/model_serving/model_config/triton/triton_config.py +0 -182
- clarifai/models/model_serving/model_config/triton/wrappers.py +0 -281
- clarifai/models/model_serving/repo_build/__init__.py +0 -14
- clarifai/models/model_serving/repo_build/build.py +0 -198
- clarifai/models/model_serving/repo_build/static_files/_requirements.txt +0 -2
- clarifai/models/model_serving/repo_build/static_files/base_test.py +0 -169
- clarifai/models/model_serving/repo_build/static_files/inference.py +0 -26
- clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +0 -25
- clarifai/models/model_serving/repo_build/static_files/test.py +0 -40
- clarifai/models/model_serving/repo_build/static_files/triton/model.py +0 -75
- clarifai/models/model_serving/utils.py +0 -31
- {clarifai-10.8.4.dist-info → clarifai-10.8.5.dist-info}/LICENSE +0 -0
- {clarifai-10.8.4.dist-info → clarifai-10.8.5.dist-info}/WHEEL +0 -0
- {clarifai-10.8.4.dist-info → clarifai-10.8.5.dist-info}/entry_points.txt +0 -0
- {clarifai-10.8.4.dist-info → clarifai-10.8.5.dist-info}/top_level.txt +0 -0
@@ -1,169 +0,0 @@
|
|
1
|
-
import os
|
2
|
-
from copy import deepcopy
|
3
|
-
from typing import Dict, Iterable, List, Union
|
4
|
-
|
5
|
-
import numpy as np
|
6
|
-
import yaml
|
7
|
-
|
8
|
-
from clarifai.models.model_serving.constants import IMAGE_TENSOR_NAME, TEXT_TENSOR_NAME
|
9
|
-
from clarifai.models.model_serving.model_config import (
|
10
|
-
ClassifierOutput, EmbeddingOutput, ImageOutput, InferParam, InferParamManager, MasksOutput,
|
11
|
-
ModelTypes, TextOutput, VisualDetector, load_user_config)
|
12
|
-
|
13
|
-
_default_texts = ["Photo of a cat", "A cat is playing around", "Hello, this is test"]
|
14
|
-
|
15
|
-
_default_images = [
|
16
|
-
np.zeros((100, 100, 3), dtype='uint8'), #black
|
17
|
-
np.ones((100, 100, 3), dtype='uint8') * 255, #white
|
18
|
-
np.random.uniform(0, 255, (100, 100, 3)).astype('uint8') #noise
|
19
|
-
]
|
20
|
-
|
21
|
-
|
22
|
-
def _is_valid_logit(x: np.array):
|
23
|
-
return np.all(0 <= x) and np.all(x <= 1)
|
24
|
-
|
25
|
-
|
26
|
-
def _is_non_negative(x: np.array):
|
27
|
-
return np.all(x >= 0)
|
28
|
-
|
29
|
-
|
30
|
-
def _is_integer(x):
|
31
|
-
return np.all(np.equal(np.mod(x, 1), 0))
|
32
|
-
|
33
|
-
|
34
|
-
class BaseTest:
|
35
|
-
init_inference_parameters = {}
|
36
|
-
|
37
|
-
def __init__(self, init_inference_parameters={}) -> None:
|
38
|
-
import sys
|
39
|
-
if 'inference' in sys.modules:
|
40
|
-
del sys.modules['inference']
|
41
|
-
import inference
|
42
|
-
from inference import InferenceModel
|
43
|
-
self.model = InferenceModel()
|
44
|
-
self._base_dir = os.path.dirname(inference.__file__)
|
45
|
-
self.cfg_path = os.path.join(self._base_dir, "clarifai_config.yaml")
|
46
|
-
self.user_config = load_user_config(self.cfg_path)
|
47
|
-
self._user_labels = None
|
48
|
-
# check if labels exists
|
49
|
-
for output_config in self.user_config.serving_backend.triton.output:
|
50
|
-
if output_config.label_filename:
|
51
|
-
self._user_labels = self.user_config.clarifai_model.labels
|
52
|
-
assert self._user_labels, f"Model type `{self.user_config.clarifai_model.type}` requires labels, "\
|
53
|
-
f"but can not found value of `clarifai_model.labels` in {self.cfg_path}. Please update this attribute to build the model"
|
54
|
-
|
55
|
-
# update init vs user_defined params
|
56
|
-
user_defined_infer_params = [
|
57
|
-
InferParam(**each) for each in self.user_config.clarifai_model.inference_parameters
|
58
|
-
]
|
59
|
-
total_infer_params = []
|
60
|
-
if init_inference_parameters:
|
61
|
-
self.init_inference_parameters = init_inference_parameters
|
62
|
-
for k, v in self.init_inference_parameters.items():
|
63
|
-
_exist = False
|
64
|
-
for user_param in user_defined_infer_params:
|
65
|
-
if user_param.path == k:
|
66
|
-
if user_param.default_value != v:
|
67
|
-
print(f"Warning: Overwrite parameter `{k}` with default value `{v}`")
|
68
|
-
user_param.default_value = v
|
69
|
-
_exist = True
|
70
|
-
total_infer_params.append(user_param)
|
71
|
-
user_defined_infer_params.remove(user_param)
|
72
|
-
break
|
73
|
-
if not _exist:
|
74
|
-
total_infer_params.append(InferParamManager.from_kwargs(**{k: v}).params[0])
|
75
|
-
|
76
|
-
self.infer_param_manager = InferParamManager(
|
77
|
-
params=total_infer_params + user_defined_infer_params)
|
78
|
-
self.user_config.clarifai_model.inference_parameters = self.infer_param_manager.get_list_params(
|
79
|
-
)
|
80
|
-
self._overwrite_cfg()
|
81
|
-
|
82
|
-
@property
|
83
|
-
def user_labels(self):
|
84
|
-
return self._user_labels
|
85
|
-
|
86
|
-
def _overwrite_cfg(self):
|
87
|
-
config = yaml.dump(self.user_config.dump_to_user_config(),)
|
88
|
-
with open(self.cfg_path, "w") as f:
|
89
|
-
f.write(config)
|
90
|
-
|
91
|
-
def predict(self, input_data: Union[List[np.ndarray], List[str], Dict[str, Union[List[
|
92
|
-
np.ndarray], List[str]]]], **inference_parameters) -> Iterable:
|
93
|
-
"""
|
94
|
-
Test Prediction method is exact `InferenceModel.predict` method with
|
95
|
-
checking inference paramters.
|
96
|
-
|
97
|
-
Args:
|
98
|
-
-----
|
99
|
-
- input_data: A list of input data item to predict on. The type depends on model input type:
|
100
|
-
* `image`: List[np.ndarray]
|
101
|
-
* `text`: List[str]
|
102
|
-
* `multimodal`:
|
103
|
-
input_data is list of dict where key is input type name e.i. `image`, `text` and value is list.
|
104
|
-
{"image": List[np.ndarray], "text": List[str]}
|
105
|
-
|
106
|
-
- **inference_parameters: keyword args of your inference parameters.
|
107
|
-
|
108
|
-
Returns:
|
109
|
-
--------
|
110
|
-
List of your inference model output type
|
111
|
-
"""
|
112
|
-
infer_params = self.infer_param_manager.validate(**inference_parameters)
|
113
|
-
outputs = self.model.predict(input_data=input_data, inference_parameters=infer_params)
|
114
|
-
outputs = self._verify_outputs(outputs)
|
115
|
-
return outputs
|
116
|
-
|
117
|
-
def _verify_outputs(self, outputs: List[Union[ClassifierOutput, VisualDetector, EmbeddingOutput,
|
118
|
-
TextOutput, ImageOutput, MasksOutput]]):
|
119
|
-
"""Test output value/dims
|
120
|
-
|
121
|
-
Args:
|
122
|
-
outputs (List[Union[ClassifierOutput, VisualDetector, EmbeddingOutput, TextOutput, ImageOutput, MasksOutput]]): Outputs of `predict` method
|
123
|
-
"""
|
124
|
-
_outputs = deepcopy(outputs)
|
125
|
-
_output = _outputs[0]
|
126
|
-
|
127
|
-
if isinstance(_output, EmbeddingOutput):
|
128
|
-
# not test
|
129
|
-
pass
|
130
|
-
elif isinstance(_output, ClassifierOutput):
|
131
|
-
for each in _outputs:
|
132
|
-
assert _is_valid_logit(each.predicted_scores), "`predicted_scores` must be in range [0, 1]"
|
133
|
-
assert len(each.predicted_scores) == len(
|
134
|
-
self.user_labels
|
135
|
-
), f"`predicted_scores` dim must be equal to labels, got {len(each.predicted_scores)} != labels {len(self.user_labels)}"
|
136
|
-
elif isinstance(_output, VisualDetector):
|
137
|
-
for each in _outputs:
|
138
|
-
assert _is_valid_logit(each.predicted_scores), "`predicted_scores` must be in range [0, 1]"
|
139
|
-
assert _is_integer(each.predicted_labels), "`predicted_labels` must be integer"
|
140
|
-
assert np.all(0 <= each.predicted_labels) and np.all(each.predicted_labels < len(
|
141
|
-
self.user_labels)), f"`predicted_labels` must be in [0, {len(self.user_labels) - 1}]"
|
142
|
-
assert _is_non_negative(each.predicted_bboxes), "`predicted_bboxes` must be >= 0"
|
143
|
-
elif isinstance(_output, MasksOutput):
|
144
|
-
for each in _outputs:
|
145
|
-
assert np.all(0 <= each.predicted_mask) and np.all(each.predicted_mask < len(
|
146
|
-
self.user_labels)), f"`predicted_mask` must be in [0, {len(self.user_labels) - 1}]"
|
147
|
-
elif isinstance(_output, TextOutput):
|
148
|
-
pass
|
149
|
-
elif isinstance(_output, ImageOutput):
|
150
|
-
for each in _outputs:
|
151
|
-
assert _is_non_negative(each.image), "`image` must be >= 0"
|
152
|
-
else:
|
153
|
-
pass
|
154
|
-
|
155
|
-
return outputs
|
156
|
-
|
157
|
-
def test_with_default_inputs(self):
|
158
|
-
model_type = self.user_config.clarifai_model.type
|
159
|
-
if model_type == ModelTypes.multimodal_embedder:
|
160
|
-
self.predict(input_data=[{IMAGE_TENSOR_NAME: each} for each in _default_images])
|
161
|
-
self.predict(input_data=[{TEXT_TENSOR_NAME: each} for each in _default_texts])
|
162
|
-
self.predict(input_data=[{
|
163
|
-
TEXT_TENSOR_NAME: text,
|
164
|
-
IMAGE_TENSOR_NAME: img
|
165
|
-
} for text, img in zip(_default_texts, _default_images)])
|
166
|
-
elif model_type.startswith("visual"):
|
167
|
-
self.predict(input_data=_default_images)
|
168
|
-
else:
|
169
|
-
self.predict(input_data=_default_texts)
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# User model inference script.
|
2
|
-
|
3
|
-
import os
|
4
|
-
from pathlib import Path
|
5
|
-
from typing import Dict, Union
|
6
|
-
from clarifai.models.model_serving.model_config import * # noqa
|
7
|
-
|
8
|
-
|
9
|
-
class InferenceModel():
|
10
|
-
"""User model inference class."""
|
11
|
-
|
12
|
-
def __init__(self) -> None:
|
13
|
-
"""
|
14
|
-
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
15
|
-
in this method so they are loaded only once for faster inference.
|
16
|
-
"""
|
17
|
-
# current directory
|
18
|
-
self.base_path: Path = os.path.dirname(__file__)
|
19
|
-
|
20
|
-
def predict(self,
|
21
|
-
input_data: list,
|
22
|
-
inference_parameters: Dict[str, Union[bool, str, float, int]] = {}) -> list:
|
23
|
-
"""predict_docstring
|
24
|
-
"""
|
25
|
-
|
26
|
-
raise NotImplementedError()
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# Sample config of inference_parameters and labels
|
2
|
-
# For detail, please refer to docs
|
3
|
-
# --------------------
|
4
|
-
# inference_parameters:
|
5
|
-
# - path: boolean_var
|
6
|
-
# default_value: true
|
7
|
-
# field_type: 1
|
8
|
-
# description: a boolean variable
|
9
|
-
# - path: string_var
|
10
|
-
# default_value: "a string"
|
11
|
-
# field_type: 2
|
12
|
-
# description: a string variable
|
13
|
-
# - path: number_var
|
14
|
-
# default_value: 1
|
15
|
-
# field_type: 3
|
16
|
-
# description: a number variable
|
17
|
-
# - path: secret_string_var
|
18
|
-
# default_value: "YOUR_SECRET"
|
19
|
-
# field_type: 21
|
20
|
-
# description: a string variable contains secret like API key
|
21
|
-
# labels:
|
22
|
-
# - concept1
|
23
|
-
# - concept2
|
24
|
-
# - concept3
|
25
|
-
# - concept4
|
@@ -1,40 +0,0 @@
|
|
1
|
-
import unittest
|
2
|
-
|
3
|
-
from clarifai.models.model_serving.repo_build import BaseTest
|
4
|
-
|
5
|
-
|
6
|
-
class CustomTest(unittest.TestCase):
|
7
|
-
"""
|
8
|
-
BaseTest loads the InferenceModel from the inference.py file in the current working directory.
|
9
|
-
To execute the predict method of the InferenceModel, use the predict method in BaseTest.
|
10
|
-
It takes the exact same inputs and inference parameters, returning the same outputs as InferenceModel.predict.
|
11
|
-
The difference is that BaseTest.predict verifies your_infer_parameters against config.clarifai_models.inference_parameters and checks the output values.
|
12
|
-
|
13
|
-
For example, test input value of visual-classifier
|
14
|
-
|
15
|
-
def test_input(self):
|
16
|
-
import cv2
|
17
|
-
path = "path/to/image"
|
18
|
-
img = cv2.imread(path)
|
19
|
-
outputs = self.model.predict([img], infer_param1=..., infer_param2=...)
|
20
|
-
print(outputs)
|
21
|
-
assert outputs
|
22
|
-
|
23
|
-
"""
|
24
|
-
|
25
|
-
def setUp(self) -> None:
|
26
|
-
your_infer_parameter = dict(
|
27
|
-
) # for example dict(float_var=0.12, string_var="test", _secret_string_var="secret")
|
28
|
-
self.model = BaseTest(your_infer_parameter)
|
29
|
-
|
30
|
-
def test_default_cases(self):
|
31
|
-
"""Test your model with dummy inputs.
|
32
|
-
In general, you only need to run this test to check your InferneceModel implementation.
|
33
|
-
In case the default inputs makes your model failed for some reason (not because of assert in `test_with_default_inputs`),
|
34
|
-
you can comment out this test.
|
35
|
-
"""
|
36
|
-
self.model.test_with_default_inputs()
|
37
|
-
|
38
|
-
def test_specific_case1(self):
|
39
|
-
""" Implement your test case"""
|
40
|
-
pass
|
@@ -1,75 +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 clarifai.models.model_serving.model_config.inference_parameter import parse_req_parameters
|
23
|
-
|
24
|
-
|
25
|
-
class TritonPythonModel:
|
26
|
-
"""
|
27
|
-
Triton Python BE Model.
|
28
|
-
"""
|
29
|
-
|
30
|
-
def initialize(self, args):
|
31
|
-
"""
|
32
|
-
Triton server init.
|
33
|
-
"""
|
34
|
-
sys.path.append(os.path.dirname(__file__))
|
35
|
-
from inference import InferenceModel
|
36
|
-
|
37
|
-
self.inference_obj = InferenceModel()
|
38
|
-
|
39
|
-
# Read input_name from config file
|
40
|
-
self.input_names = [inp.name for inp in self.inference_obj.config.serving_backend.triton.input]
|
41
|
-
|
42
|
-
def execute(self, requests):
|
43
|
-
"""
|
44
|
-
Serve model inference requests.
|
45
|
-
"""
|
46
|
-
responses = []
|
47
|
-
|
48
|
-
for request in requests:
|
49
|
-
try:
|
50
|
-
parameters = request.parameters()
|
51
|
-
except Exception:
|
52
|
-
print(
|
53
|
-
"It seems this triton version does not support `parameters()` in request. "
|
54
|
-
"Please upgrade tritonserver version otherwise can not use `inference_parameters`. Error message: {e}"
|
55
|
-
)
|
56
|
-
parameters = None
|
57
|
-
|
58
|
-
parameters = parse_req_parameters(parameters) if parameters else {}
|
59
|
-
|
60
|
-
if len(self.input_names) == 1:
|
61
|
-
in_batch = pb_utils.get_input_tensor_by_name(request, self.input_names[0])
|
62
|
-
in_batch = in_batch.as_numpy()
|
63
|
-
data = in_batch
|
64
|
-
else:
|
65
|
-
data = {}
|
66
|
-
for input_name in self.input_names:
|
67
|
-
in_batch = pb_utils.get_input_tensor_by_name(request, input_name)
|
68
|
-
in_batch = in_batch.as_numpy() if in_batch is not None else []
|
69
|
-
data.update({input_name: in_batch})
|
70
|
-
|
71
|
-
inference_response = self.inference_obj._tritonserver_predict(
|
72
|
-
input_data=data, inference_parameters=parameters)
|
73
|
-
responses.append(inference_response)
|
74
|
-
|
75
|
-
return responses
|
@@ -1,31 +0,0 @@
|
|
1
|
-
import os
|
2
|
-
|
3
|
-
from clarifai.models.model_serving.constants import CLARIFAI_PAT_PATH
|
4
|
-
from clarifai.utils.constants import CLARIFAI_PAT_ENV_VAR
|
5
|
-
|
6
|
-
|
7
|
-
def _persist_pat(pat: str):
|
8
|
-
""" Write down pat to CLARIFAI_PAT_PATH """
|
9
|
-
with open(CLARIFAI_PAT_PATH, "w") as f:
|
10
|
-
f.write(pat)
|
11
|
-
|
12
|
-
|
13
|
-
def _read_pat():
|
14
|
-
if not os.path.exists(CLARIFAI_PAT_PATH) and not os.environ.get(CLARIFAI_PAT_ENV_VAR, ""):
|
15
|
-
return None
|
16
|
-
if os.path.exists(CLARIFAI_PAT_PATH):
|
17
|
-
with open(CLARIFAI_PAT_PATH, "r") as f:
|
18
|
-
return f.read().replace("\n", "").replace("\r", "").strip()
|
19
|
-
elif os.environ.get(CLARIFAI_PAT_ENV_VAR):
|
20
|
-
return os.environ.get(CLARIFAI_PAT_ENV_VAR)
|
21
|
-
else:
|
22
|
-
raise ValueError(
|
23
|
-
f"PAT not found, please run `clarifai login` to persist your PAT or set it as an environment variable under the name '{CLARIFAI_PAT_ENV_VAR}'"
|
24
|
-
)
|
25
|
-
|
26
|
-
|
27
|
-
def login(pat=None):
|
28
|
-
""" if pat provided, set pat to CLARIFAI_PAT otherwise read pat from file"""
|
29
|
-
pat = pat or _read_pat()
|
30
|
-
assert pat, Exception("PAT is not found, please run `clarifai login` to persist your PAT")
|
31
|
-
os.environ["CLARIFAI_PAT"] = pat
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|