clarifai 11.3.0__py3-none-any.whl → 11.3.0rc2__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/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/__pycache__/errors.cpython-310.pyc +0 -0
- clarifai/__pycache__/errors.cpython-311.pyc +0 -0
- clarifai/__pycache__/versions.cpython-310.pyc +0 -0
- clarifai/__pycache__/versions.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
- clarifai/client/cli/__init__.py +0 -0
- clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/base_cli.py +88 -0
- clarifai/client/cli/model_cli.py +29 -0
- clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
- clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
- clarifai/runners/dockerfile_template/Dockerfile.cuda.template +42 -0
- clarifai/runners/dockerfile_template/Dockerfile.nim +71 -0
- clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
- clarifai/runners/models/base_typed_model.py +238 -0
- clarifai/runners/models/model_builder.py +14 -1
- clarifai/runners/models/model_class_refract.py +80 -0
- clarifai/runners/models/model_upload.py +607 -0
- clarifai/runners/models/temp.py +25 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
- clarifai/runners/utils/data_handler.py +231 -0
- clarifai/runners/utils/data_handler_refract.py +213 -0
- clarifai/runners/utils/data_utils.py +107 -14
- clarifai/runners/utils/logger.py +0 -0
- clarifai/runners/utils/openai_format.py +87 -0
- clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
- {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/METADATA +3 -15
- clarifai-11.3.0rc2.dist-info/RECORD +322 -0
- {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/WHEEL +1 -1
- clarifai-11.3.0.dist-info/RECORD +0 -107
- {clarifai-11.3.0.dist-info/licenses → clarifai-11.3.0rc2.dist-info}/LICENSE +0 -0
- {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/entry_points.txt +0 -0
- {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,238 @@
|
|
1
|
+
import itertools
|
2
|
+
from typing import Any, Dict, Iterator, List, Tuple
|
3
|
+
|
4
|
+
import numpy as np
|
5
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
6
|
+
from clarifai_grpc.grpc.api.service_pb2 import PostModelOutputsRequest
|
7
|
+
from google.protobuf import json_format
|
8
|
+
|
9
|
+
from ..utils.data_handler import InputDataHandler, OutputDataHandler
|
10
|
+
from .model_class import ModelClass
|
11
|
+
|
12
|
+
|
13
|
+
class AnyAnyModel(ModelClass):
|
14
|
+
|
15
|
+
def load_model(self):
|
16
|
+
"""
|
17
|
+
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
18
|
+
in this method so they are loaded only once for faster inference.
|
19
|
+
"""
|
20
|
+
raise NotImplementedError
|
21
|
+
|
22
|
+
def parse_input_request(
|
23
|
+
self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
|
24
|
+
list_input_dict = [
|
25
|
+
InputDataHandler.from_proto(input).to_python() for input in input_request.inputs
|
26
|
+
]
|
27
|
+
inference_params = json_format.MessageToDict(
|
28
|
+
input_request.model.model_version.output_info.params)
|
29
|
+
|
30
|
+
return list_input_dict, inference_params
|
31
|
+
|
32
|
+
def convert_output_to_proto(self, outputs: list):
|
33
|
+
assert (isinstance(outputs, Iterator) or isinstance(outputs, list) or
|
34
|
+
isinstance(outputs, tuple)), "outputs must be an Iterator"
|
35
|
+
output_protos = []
|
36
|
+
for output in outputs:
|
37
|
+
if isinstance(output, OutputDataHandler):
|
38
|
+
output = output.proto
|
39
|
+
elif isinstance(output, resources_pb2.Output):
|
40
|
+
pass
|
41
|
+
else:
|
42
|
+
raise NotImplementedError
|
43
|
+
output_protos.append(output)
|
44
|
+
|
45
|
+
return service_pb2.MultiOutputResponse(outputs=output_protos)
|
46
|
+
|
47
|
+
def predict_wrapper(
|
48
|
+
self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
|
49
|
+
list_dict_input, inference_params = self.parse_input_request(request)
|
50
|
+
outputs = self.predict(list_dict_input, inference_parameters=inference_params)
|
51
|
+
return self.convert_output_to_proto(outputs)
|
52
|
+
|
53
|
+
def generate_wrapper(
|
54
|
+
self, request: PostModelOutputsRequest) -> Iterator[service_pb2.MultiOutputResponse]:
|
55
|
+
list_dict_input, inference_params = self.parse_input_request(request)
|
56
|
+
outputs = self.generate(list_dict_input, inference_parameters=inference_params)
|
57
|
+
for output in outputs:
|
58
|
+
yield self.convert_output_to_proto(output)
|
59
|
+
|
60
|
+
def _preprocess_stream(
|
61
|
+
self, request: Iterator[PostModelOutputsRequest]) -> Iterator[Tuple[List[Dict], List[Dict]]]:
|
62
|
+
"""Return generator of processed data (from proto to python) and inference parameters like predict and generate"""
|
63
|
+
for i, req in enumerate(request):
|
64
|
+
input_data, _ = self.parse_input_request(req)
|
65
|
+
yield input_data
|
66
|
+
|
67
|
+
def stream_wrapper(self, request: Iterator[PostModelOutputsRequest]
|
68
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
69
|
+
first_request = next(request)
|
70
|
+
_, inference_params = self.parse_input_request(first_request)
|
71
|
+
request_iterator = itertools.chain([first_request], request)
|
72
|
+
outputs = self.stream(self._preprocess_stream(request_iterator), inference_params)
|
73
|
+
for output in outputs:
|
74
|
+
yield self.convert_output_to_proto(output)
|
75
|
+
|
76
|
+
def predict(self, input_data: List[Dict],
|
77
|
+
inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
|
78
|
+
"""
|
79
|
+
Prediction method.
|
80
|
+
|
81
|
+
Args:
|
82
|
+
-----
|
83
|
+
- input_data: is list of dict where key is input type name.
|
84
|
+
* image: np.ndarray
|
85
|
+
* text: str
|
86
|
+
* audio: bytes
|
87
|
+
|
88
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
89
|
+
|
90
|
+
Returns:
|
91
|
+
--------
|
92
|
+
List of OutputDataHandler
|
93
|
+
"""
|
94
|
+
raise NotImplementedError
|
95
|
+
|
96
|
+
def generate(self, input_data: List[Dict],
|
97
|
+
inference_parameters: Dict[str, Any] = {}) -> Iterator[List[OutputDataHandler]]:
|
98
|
+
"""
|
99
|
+
Generate method.
|
100
|
+
|
101
|
+
Args:
|
102
|
+
-----
|
103
|
+
- input_data: is list of dict where key is input type name.
|
104
|
+
* image: np.ndarray
|
105
|
+
* text: str
|
106
|
+
* audio: bytes
|
107
|
+
|
108
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
109
|
+
|
110
|
+
Yield:
|
111
|
+
--------
|
112
|
+
List of OutputDataHandler
|
113
|
+
"""
|
114
|
+
raise NotImplementedError
|
115
|
+
|
116
|
+
def stream(self, inputs: Iterator[List[Dict[str, Any]]],
|
117
|
+
inference_params: Dict[str, Any]) -> Iterator[List[OutputDataHandler]]:
|
118
|
+
"""
|
119
|
+
Stream method.
|
120
|
+
|
121
|
+
Args:
|
122
|
+
-----
|
123
|
+
input_request: is an Iterator of Tuple which
|
124
|
+
- First element (List[Dict[str, Union[np.ndarray, str, bytes]]]) is list of dict input data type which keys and values are:
|
125
|
+
* image: np.ndarray
|
126
|
+
* text: str
|
127
|
+
* audio: bytes
|
128
|
+
|
129
|
+
- Second element (Dict[str, Union[bool, str, float, int]]): is a dict of inference_parameters
|
130
|
+
|
131
|
+
Yield:
|
132
|
+
--------
|
133
|
+
List of OutputDataHandler
|
134
|
+
"""
|
135
|
+
raise NotImplementedError
|
136
|
+
|
137
|
+
|
138
|
+
class VisualInputModel(AnyAnyModel):
|
139
|
+
|
140
|
+
def parse_input_request(
|
141
|
+
self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
|
142
|
+
list_input_dict = [
|
143
|
+
InputDataHandler.from_proto(input).image(format="np") for input in input_request.inputs
|
144
|
+
]
|
145
|
+
inference_params = json_format.MessageToDict(
|
146
|
+
input_request.model.model_version.output_info.params)
|
147
|
+
|
148
|
+
return list_input_dict, inference_params
|
149
|
+
|
150
|
+
def load_model(self):
|
151
|
+
"""
|
152
|
+
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
153
|
+
in this method so they are loaded only once for faster inference.
|
154
|
+
"""
|
155
|
+
raise NotImplementedError
|
156
|
+
|
157
|
+
def predict(self, input_data: List[np.ndarray],
|
158
|
+
inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
|
159
|
+
"""
|
160
|
+
Prediction method.
|
161
|
+
|
162
|
+
Args:
|
163
|
+
-----
|
164
|
+
- input_data(List[np.ndarray]): is list of image as np.ndarray type
|
165
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
166
|
+
|
167
|
+
Returns:
|
168
|
+
--------
|
169
|
+
List of OutputDataHandler
|
170
|
+
"""
|
171
|
+
raise NotImplementedError
|
172
|
+
|
173
|
+
|
174
|
+
class TextInputModel(AnyAnyModel):
|
175
|
+
|
176
|
+
def load_model(self):
|
177
|
+
"""
|
178
|
+
Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
|
179
|
+
in this method so they are loaded only once for faster inference.
|
180
|
+
"""
|
181
|
+
raise NotImplementedError
|
182
|
+
|
183
|
+
def parse_input_request(
|
184
|
+
self, input_request: service_pb2.PostModelOutputsRequest) -> Tuple[List[Dict], Dict]:
|
185
|
+
list_input_text = [InputDataHandler.from_proto(input).text for input in input_request.inputs]
|
186
|
+
inference_params = json_format.MessageToDict(
|
187
|
+
input_request.model.model_version.output_info.params)
|
188
|
+
|
189
|
+
return list_input_text, inference_params
|
190
|
+
|
191
|
+
def predict(self, input_data: List[str],
|
192
|
+
inference_parameters: Dict[str, Any] = {}) -> List[OutputDataHandler]:
|
193
|
+
"""
|
194
|
+
Prediction method.
|
195
|
+
|
196
|
+
Args:
|
197
|
+
-----
|
198
|
+
- input_data(List[str]): is list of text as str type
|
199
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
200
|
+
|
201
|
+
Returns:
|
202
|
+
--------
|
203
|
+
List of OutputDataHandler
|
204
|
+
"""
|
205
|
+
raise NotImplementedError
|
206
|
+
|
207
|
+
def generate(self, input_data: List[str],
|
208
|
+
inference_parameters: Dict[str, Any] = {}) -> Iterator[List[OutputDataHandler]]:
|
209
|
+
"""
|
210
|
+
Prediction method.
|
211
|
+
|
212
|
+
Args:
|
213
|
+
-----
|
214
|
+
- input_data(List[str]): is list of text as str type
|
215
|
+
- inference_parameters (Dict[str, Union[bool, str, float, int]]): your inference parameters.
|
216
|
+
|
217
|
+
Yield:
|
218
|
+
--------
|
219
|
+
List of OutputDataHandler
|
220
|
+
"""
|
221
|
+
raise NotImplementedError
|
222
|
+
|
223
|
+
def stream(self, inputs: Iterator[List[str]],
|
224
|
+
inference_params: Dict[str, Any]) -> Iterator[List[OutputDataHandler]]:
|
225
|
+
"""
|
226
|
+
Stream method.
|
227
|
+
|
228
|
+
Args:
|
229
|
+
-----
|
230
|
+
input_request: is an Iterator of Tuple which
|
231
|
+
- First element (List[str]) is list of input text:
|
232
|
+
- Second element (Dict[str, Union[bool, str, float, int]]): is a dict of inference_parameters
|
233
|
+
|
234
|
+
Yield:
|
235
|
+
--------
|
236
|
+
List of OutputDataHandler
|
237
|
+
"""
|
238
|
+
raise NotImplementedError
|
@@ -43,6 +43,19 @@ def _clear_line(n: int = 1) -> None:
|
|
43
43
|
print(LINE_UP, end=LINE_CLEAR, flush=True)
|
44
44
|
|
45
45
|
|
46
|
+
def is_related(object_class, main_class):
|
47
|
+
# Check if the object_class is a subclass of main_class
|
48
|
+
if issubclass(object_class, main_class):
|
49
|
+
return True
|
50
|
+
|
51
|
+
# Check if the object_class is a subclass of any of the parent classes of main_class
|
52
|
+
parent_classes = object_class.__bases__
|
53
|
+
for parent in parent_classes:
|
54
|
+
if main_class in parent.__bases__:
|
55
|
+
return True
|
56
|
+
return False
|
57
|
+
|
58
|
+
|
46
59
|
class ModelBuilder:
|
47
60
|
DEFAULT_CHECKPOINT_SIZE = 50 * 1024**3 # 50 GiB
|
48
61
|
|
@@ -125,7 +138,7 @@ class ModelBuilder:
|
|
125
138
|
# Find all classes in the model.py file that are subclasses of ModelClass
|
126
139
|
classes = [
|
127
140
|
cls for _, cls in inspect.getmembers(module, inspect.isclass)
|
128
|
-
if
|
141
|
+
if is_related(cls, ModelClass) and cls.__module__ == module.__name__
|
129
142
|
]
|
130
143
|
# Ensure there is exactly one subclass of BaseRunner in the model.py file
|
131
144
|
if len(classes) != 1:
|
@@ -0,0 +1,80 @@
|
|
1
|
+
import inspect
|
2
|
+
from abc import ABC, abstractmethod
|
3
|
+
from concurrent.futures import ThreadPoolExecutor
|
4
|
+
from typing import Any, Dict, Iterator, List, get_type_hints
|
5
|
+
|
6
|
+
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
7
|
+
|
8
|
+
from clarifai.runners.utils.data_handler import Output, kwargs_to_proto, proto_to_kwargs
|
9
|
+
|
10
|
+
|
11
|
+
class ModelClass(ABC):
|
12
|
+
|
13
|
+
@abstractmethod
|
14
|
+
def load_model(self):
|
15
|
+
raise NotImplementedError("load_model() not implemented")
|
16
|
+
|
17
|
+
@abstractmethod
|
18
|
+
def predict(self, *args, **kwargs) -> Output:
|
19
|
+
raise NotImplementedError("predict() not implemented")
|
20
|
+
|
21
|
+
@abstractmethod
|
22
|
+
def generate(self, *args, **kwargs) -> Iterator[Output]:
|
23
|
+
raise NotImplementedError("generate() not implemented")
|
24
|
+
|
25
|
+
@abstractmethod
|
26
|
+
def stream(self, *args, **kwargs) -> Iterator[Output]:
|
27
|
+
raise NotImplementedError("stream() not implemented")
|
28
|
+
|
29
|
+
def batch_predict(self, inputs: List[Dict[str, Any]]) -> List[Output]:
|
30
|
+
with ThreadPoolExecutor() as executor:
|
31
|
+
return list(executor.map(lambda x: self.predict(**x), inputs))
|
32
|
+
|
33
|
+
def _process_request(self, request, process_func, is_stream=False):
|
34
|
+
inputs = self._convert_proto_to_python(request.inputs)
|
35
|
+
if len(inputs) == 1:
|
36
|
+
result = process_func(**inputs[0])
|
37
|
+
if is_stream:
|
38
|
+
return (self._convert_output_to_proto(output) for output in result)
|
39
|
+
else:
|
40
|
+
return [self._convert_output_to_proto(result)]
|
41
|
+
else:
|
42
|
+
results = self.batch_predict(inputs) if not is_stream else []
|
43
|
+
return [self._convert_output_to_proto(output) for output in results]
|
44
|
+
|
45
|
+
def predict_wrapper(
|
46
|
+
self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
|
47
|
+
outputs = self._process_request(request, self.predict)
|
48
|
+
return service_pb2.MultiOutputResponse(outputs=outputs)
|
49
|
+
|
50
|
+
def generate_wrapper(self, request: service_pb2.PostModelOutputsRequest
|
51
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
52
|
+
outputs = self._process_request(request, self.generate, is_stream=True)
|
53
|
+
for output in outputs:
|
54
|
+
yield service_pb2.MultiOutputResponse(outputs=[output])
|
55
|
+
|
56
|
+
def stream_wrapper(self, requests: Iterator[service_pb2.PostModelOutputsRequest]
|
57
|
+
) -> Iterator[service_pb2.MultiOutputResponse]:
|
58
|
+
for request in requests:
|
59
|
+
outputs = self._process_request(request, self.stream, is_stream=True)
|
60
|
+
yield service_pb2.MultiOutputResponse(outputs=outputs)
|
61
|
+
|
62
|
+
def _convert_proto_to_python(self, inputs: List[resources_pb2.Input]) -> List[Dict[str, Any]]:
|
63
|
+
get_type_hints(self.predict)
|
64
|
+
required_params = [
|
65
|
+
name for name, param in inspect.signature(self.predict).parameters.items()
|
66
|
+
if param.default == inspect.Parameter.empty
|
67
|
+
]
|
68
|
+
kwargs_list = []
|
69
|
+
for input_proto in inputs:
|
70
|
+
kwargs = proto_to_kwargs(input_proto.data)
|
71
|
+
missing = [name for name in required_params if name not in kwargs]
|
72
|
+
if missing:
|
73
|
+
raise ValueError(f"Missing required parameters: {missing}")
|
74
|
+
kwargs_list.append(kwargs)
|
75
|
+
return kwargs_list
|
76
|
+
|
77
|
+
def _convert_output_to_proto(self, output: Any) -> resources_pb2.Output:
|
78
|
+
if isinstance(output, Output):
|
79
|
+
return output.to_proto()
|
80
|
+
return kwargs_to_proto(**output).outputs.add()
|