clarifai 11.1.5rc7__py3-none-any.whl → 11.1.6__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.
Files changed (122) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/cli/model.py +40 -50
  3. clarifai/client/model.py +365 -95
  4. clarifai/runners/__init__.py +7 -2
  5. clarifai/runners/dockerfile_template/Dockerfile.template +1 -4
  6. clarifai/runners/models/base_typed_model.py +238 -0
  7. clarifai/runners/models/model_builder.py +9 -26
  8. clarifai/runners/models/model_class.py +28 -273
  9. clarifai/runners/models/model_run_locally.py +78 -3
  10. clarifai/runners/models/model_runner.py +0 -2
  11. clarifai/runners/models/model_servicer.py +2 -11
  12. clarifai/runners/utils/data_handler.py +205 -308
  13. {clarifai-11.1.5rc7.dist-info → clarifai-11.1.6.dist-info}/METADATA +26 -16
  14. clarifai-11.1.6.dist-info/RECORD +101 -0
  15. {clarifai-11.1.5rc7.dist-info → clarifai-11.1.6.dist-info}/WHEEL +1 -1
  16. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  17. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  18. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  19. clarifai/cli/__main__.py~ +0 -4
  20. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  21. clarifai/cli/__pycache__/__main__.cpython-310.pyc +0 -0
  22. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  23. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  24. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  25. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  26. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  27. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  28. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  29. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  30. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  31. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  32. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  33. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  34. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  35. clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
  36. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  37. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  38. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  39. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  40. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  41. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  42. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  43. clarifai/client/model_client.py +0 -432
  44. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  45. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  46. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  47. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  48. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  49. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  50. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  51. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  52. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  53. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  54. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  55. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  56. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-310.pyc +0 -0
  57. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-310.pyc +0 -0
  58. clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
  59. clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
  60. clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
  61. clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  62. clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
  63. clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
  64. clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
  65. clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
  66. clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
  67. clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
  68. clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
  69. clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
  70. clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
  71. clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
  72. clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
  73. clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
  74. clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
  75. clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
  76. clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
  77. clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
  78. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  79. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  80. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  81. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  82. clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
  83. clarifai/runners/dockerfile_template/Dockerfile.debug +0 -11
  84. clarifai/runners/dockerfile_template/Dockerfile.debug~ +0 -9
  85. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  86. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  87. clarifai/runners/models/__pycache__/model_builder.cpython-310.pyc +0 -0
  88. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  89. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  90. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  91. clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
  92. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  93. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  94. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  95. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  96. clarifai/runners/utils/__pycache__/data_types.cpython-310.pyc +0 -0
  97. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  98. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  99. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  100. clarifai/runners/utils/__pycache__/method_signatures.cpython-310.pyc +0 -0
  101. clarifai/runners/utils/__pycache__/serializers.cpython-310.pyc +0 -0
  102. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  103. clarifai/runners/utils/data_types.py +0 -334
  104. clarifai/runners/utils/method_signatures.py +0 -452
  105. clarifai/runners/utils/serializers.py +0 -132
  106. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  107. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  108. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  109. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  110. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  111. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  112. clarifai/utils/evaluation/__pycache__/__init__.cpython-310.pyc +0 -0
  113. clarifai/utils/evaluation/__pycache__/helpers.cpython-310.pyc +0 -0
  114. clarifai/utils/evaluation/__pycache__/main.cpython-310.pyc +0 -0
  115. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  116. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  117. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  118. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  119. clarifai-11.1.5rc7.dist-info/RECORD +0 -203
  120. {clarifai-11.1.5rc7.dist-info → clarifai-11.1.6.dist-info}/LICENSE +0 -0
  121. {clarifai-11.1.5rc7.dist-info → clarifai-11.1.6.dist-info}/entry_points.txt +0 -0
  122. {clarifai-11.1.5rc7.dist-info → clarifai-11.1.6.dist-info}/top_level.txt +0 -0
@@ -1,9 +1,14 @@
1
+ from .models.base_typed_model import AnyAnyModel, TextInputModel, VisualInputModel
1
2
  from .models.model_builder import ModelBuilder
2
- from .models.model_class import ModelClass
3
3
  from .models.model_runner import ModelRunner
4
+ from .utils.data_handler import InputDataHandler, OutputDataHandler
4
5
 
5
6
  __all__ = [
6
7
  "ModelRunner",
7
8
  "ModelBuilder",
8
- "ModelClass",
9
+ "InputDataHandler",
10
+ "OutputDataHandler",
11
+ "AnyAnyModel",
12
+ "TextInputModel",
13
+ "VisualInputModel",
9
14
  ]
@@ -24,7 +24,7 @@ COPY --chown=nonroot:nonroot downloader/unused.yaml /home/nonroot/main/1/checkpo
24
24
  #####
25
25
  # Download checkpoints if config.yaml has checkpoints.when = "build"
26
26
  COPY --link=true config.yaml /home/nonroot/main/
27
- RUN ["python", "-m", "clarifai.cli", "model", "download-checkpoints", "--model_path", "/home/nonroot/main", "--out_path", "/home/nonroot/main/1/checkpoints", "--stage", "build"]
27
+ RUN ["python", "-m", "clarifai.cli", "model", "download-checkpoints", "/home/nonroot/main", "--out_path", "/home/nonroot/main/1/checkpoints", "--stage", "build"]
28
28
  #####
29
29
 
30
30
  # Copy in the actual files like config.yaml, requirements.txt, and most importantly 1/model.py
@@ -44,9 +44,6 @@ ENV PYTHONPATH=${PYTHONPATH}:/home/nonroot/main \
44
44
  CLARIFAI_COMPUTE_CLUSTER_ID=${CLARIFAI_COMPUTE_CLUSTER_ID} \
45
45
  CLARIFAI_API_BASE=${CLARIFAI_API_BASE:-https://api.clarifai.com}
46
46
 
47
- # # Write out the model function signatures
48
- # RUN ["python", "-m", "clarifai.cli", "model", "signatures", "--model_path", "/home/nonroot/main", "--out_path", "/home/nonroot/main/signatures.yaml"]
49
-
50
47
  # Finally run the clarifai entrypoint to start the runner loop and local dev server.
51
48
  # Note(zeiler): we may want to make this a clarifai CLI call.
52
49
  ENTRYPOINT ["python", "-m", "clarifai.runners.server"]
@@ -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
@@ -14,14 +14,13 @@ from google.protobuf import json_format
14
14
  from rich import print
15
15
  from rich.markup import escape
16
16
 
17
- from clarifai.client.base import BaseClient
17
+ from clarifai.client import BaseClient
18
18
  from clarifai.runners.models.model_class import ModelClass
19
19
  from clarifai.runners.utils.const import (
20
20
  AVAILABLE_PYTHON_IMAGES, AVAILABLE_TORCH_IMAGES, CONCEPTS_REQUIRED_MODEL_TYPE,
21
21
  DEFAULT_DOWNLOAD_CHECKPOINT_WHEN, DEFAULT_PYTHON_VERSION, DEFAULT_RUNTIME_DOWNLOAD_PATH,
22
22
  PYTHON_BASE_IMAGE, TORCH_BASE_IMAGE)
23
23
  from clarifai.runners.utils.loader import HuggingFaceLoader
24
- from clarifai.runners.utils.method_signatures import signatures_to_yaml
25
24
  from clarifai.urls.helper import ClarifaiUrlHelper
26
25
  from clarifai.utils.logging import logger
27
26
  from clarifai.versions import CLIENT_VERSION
@@ -70,18 +69,6 @@ class ModelBuilder:
70
69
  """
71
70
  Create an instance of the model class, as specified in the config file.
72
71
  """
73
- model_class = self.load_model_class()
74
-
75
- # initialize the model
76
- model = model_class()
77
- if load_model:
78
- model.load_model()
79
- return model
80
-
81
- def load_model_class(self):
82
- """
83
- Import the model class from the model.py file.
84
- """
85
72
  # look for default model.py file location
86
73
  for loc in ["model.py", "1/model.py"]:
87
74
  model_file = os.path.join(self.folder, loc)
@@ -120,7 +107,12 @@ class ModelBuilder:
120
107
  "Could not determine model class. There should be exactly one model inheriting from ModelClass defined in the model.py"
121
108
  )
122
109
  model_class = classes[0]
123
- return model_class
110
+
111
+ # initialize the model
112
+ model = model_class()
113
+ if load_model:
114
+ model.load_model()
115
+ return model
124
116
 
125
117
  def _validate_folder(self, folder):
126
118
  if folder == ".":
@@ -261,15 +253,6 @@ class ModelBuilder:
261
253
  total_size += member.size
262
254
  return total_size
263
255
 
264
- def method_signatures_yaml(self):
265
- """
266
- Returns the method signatures for the model class in YAML format.
267
- """
268
- model_class = self.load_model_class()
269
- method_info = model_class._get_method_info()
270
- signatures = {name: m.signature for name, m in method_info.items()}
271
- return signatures_to_yaml(signatures)
272
-
273
256
  @property
274
257
  def client(self):
275
258
  if self._client is None:
@@ -639,9 +622,9 @@ class ModelBuilder:
639
622
  checkpoint_size = self.DEFAULT_CHECKPOINT_SIZE
640
623
  self.storage_request_size += checkpoint_size
641
624
 
642
- self.maybe_create_model()
625
+ resp = self.maybe_create_model()
643
626
  if not self.check_model_exists():
644
- logger.error(f"Failed to create model: {self.model_proto.id}")
627
+ logger.error(f"Failed to create model: {self.model_proto.id}. Details: {resp}")
645
628
  sys.exit(1)
646
629
 
647
630
  for response in self.client.STUB.PostModelVersionsUpload(
@@ -1,286 +1,41 @@
1
- import inspect
2
- import itertools
3
- import logging
4
- import os
5
- import traceback
6
- from abc import ABC
7
- from typing import Any, Dict, Iterator, List
1
+ from abc import ABC, abstractmethod
2
+ from typing import Iterator
8
3
 
9
- from clarifai_grpc.grpc.api import resources_pb2, service_pb2
10
- from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
11
-
12
- from clarifai.runners.utils import data_types
13
- from clarifai.runners.utils.method_signatures import (build_function_signature, deserialize,
14
- get_stream_from_signature, serialize,
15
- signatures_to_json)
16
-
17
- _METHOD_INFO_ATTR = '_cf_method_info'
18
-
19
- _RAISE_EXCEPTIONS = os.getenv("RAISE_EXCEPTIONS", "false").lower() == "true"
20
-
21
-
22
- class methods:
23
- '''
24
- Decorators to mark methods as predict, generate, or stream methods.
25
- '''
26
-
27
- @staticmethod
28
- def predict(method):
29
- setattr(method, _METHOD_INFO_ATTR, _MethodInfo(method, 'predict'))
30
- return method
31
-
32
- @staticmethod
33
- def generate(method):
34
- setattr(method, _METHOD_INFO_ATTR, _MethodInfo(method, 'generate'))
35
- return method
36
-
37
- @staticmethod
38
- def stream(method):
39
- setattr(method, _METHOD_INFO_ATTR, _MethodInfo(method, 'stream'))
40
- return method
4
+ from clarifai_grpc.grpc.api import service_pb2
41
5
 
42
6
 
43
7
  class ModelClass(ABC):
44
- '''
45
- Base class for model classes that can be run as a service.
46
-
47
- Define methods as predict, generate, or stream methods using the @methods decorators.
48
-
49
- Example:
50
-
51
- from clarifai.runners.model_class import ModelClass, methods
52
- from clarifai.runners.utils.data_types import Input, Stream
53
-
54
- class MyModel(ModelClass):
55
-
56
- @methods.predict
57
- def predict(self, x: str, y: int) -> List[str]:
58
- return [x] * y
59
-
60
- @methods.generate
61
- def generate(self, x: str, y: int) -> Stream[str]:
62
- for i in range(y):
63
- yield x + str(i)
64
-
65
- @methods.stream
66
- def stream(self, input_stream: Stream[Input(x=str, y=int)]) -> Stream[str]:
67
- for item in input_stream:
68
- yield item.x + ' ' + str(item.y)
69
- '''
70
-
71
- def load_model(self):
72
- """Load the model."""
73
-
74
- def _handle_get_signatures_request(self) -> service_pb2.MultiOutputResponse:
75
- methods = self._get_method_info()
76
- signatures = {method.name: method.signature for method in methods.values()}
77
- resp = service_pb2.MultiOutputResponse(status=status_pb2.Status(code=status_code_pb2.SUCCESS))
78
- output = resp.outputs.add()
79
- output.status.code = status_code_pb2.SUCCESS
80
- output.data.text.raw = signatures_to_json(signatures)
81
- return resp
82
-
83
- def batch_predict(self, method, inputs: List[Dict[str, Any]]) -> List[Any]:
84
- """Batch predict method for multiple inputs."""
85
- outputs = []
86
- for input in inputs:
87
- output = method(**input)
88
- outputs.append(output)
89
- return outputs
90
-
91
- def batch_generate(self, method, inputs: List[Dict[str, Any]]) -> Iterator[List[Any]]:
92
- """Batch generate method for multiple inputs."""
93
- generators = [method(**input) for input in inputs]
94
- for outputs in itertools.zip_longest(*generators):
95
- yield outputs
96
8
 
97
9
  def predict_wrapper(
98
10
  self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
99
- outputs = []
100
- try:
101
- # TODO add method name field to proto
102
- method_name = None
103
- if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
104
- method_name = request.inputs[0].data.metadata['_method_name']
105
- # call_params = dict(request.model.model_version.output_info.params)
106
- # method_name = call_params.get('_method_name', 'predict')
107
- if method_name == '_GET_SIGNATURES': # special case to fetch signatures, TODO add endpoint for this
108
- return self._handle_get_signatures_request()
109
- if method_name not in self._get_method_info():
110
- raise ValueError(f"Method {method_name} not found in model class")
111
- method = getattr(self, method_name)
112
- method_info = method._cf_method_info
113
- signature = method_info.signature
114
- python_param_types = method_info.python_param_types
115
- inputs = self._convert_input_protos_to_python(request.inputs, signature.inputs,
116
- python_param_types)
117
- if len(inputs) == 1:
118
- inputs = inputs[0]
119
- output = method(**inputs)
120
- outputs.append(self._convert_output_to_proto(output, signature.outputs))
121
- else:
122
- outputs = self.batch_predict(method, inputs)
123
- outputs = [self._convert_output_to_proto(output, signature.outputs) for output in outputs]
124
-
125
- return service_pb2.MultiOutputResponse(
126
- outputs=outputs, status=status_pb2.Status(code=status_code_pb2.SUCCESS))
127
- except Exception as e:
128
- if _RAISE_EXCEPTIONS:
129
- raise
130
- logging.exception("Error in predict")
131
- return service_pb2.MultiOutputResponse(status=status_pb2.Status(
132
- code=status_code_pb2.FAILURE,
133
- details=str(e),
134
- stack_trace=traceback.format_exc().split('\n')))
11
+ """This method is used for input/output proto data conversion"""
12
+ return self.predict(request)
135
13
 
136
14
  def generate_wrapper(self, request: service_pb2.PostModelOutputsRequest
137
15
  ) -> Iterator[service_pb2.MultiOutputResponse]:
138
- try:
139
- call_params = dict(request.model.model_version.output_info.params)
140
- method_name = call_params.get('_method_name', 'generate')
141
- method = getattr(self, method_name)
142
- method_info = method._cf_method_info
143
- signature = method_info.signature
144
- python_param_types = method_info.python_param_types
16
+ """This method is used for input/output proto data conversion and yield outcome"""
17
+ return self.generate(request)
145
18
 
146
- inputs = self._convert_input_protos_to_python(request.inputs, signature.inputs,
147
- python_param_types)
148
- if len(inputs) == 1:
149
- inputs = inputs[0]
150
- for output in method(**inputs):
151
- resp = service_pb2.MultiOutputResponse()
152
- self._convert_output_to_proto(output, signature.outputs, proto=resp.outputs.add())
153
- resp.status.code = status_code_pb2.SUCCESS
154
- yield resp
155
- else:
156
- for outputs in self.batch_generate(method, inputs):
157
- resp = service_pb2.MultiOutputResponse()
158
- for output in outputs:
159
- self._convert_output_to_proto(output, signature.outputs, proto=resp.outputs.add())
160
- resp.status.code = status_code_pb2.SUCCESS
161
- yield resp
162
- except Exception as e:
163
- if _RAISE_EXCEPTIONS:
164
- raise
165
- logging.exception("Error in generate")
166
- yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
167
- code=status_code_pb2.FAILURE,
168
- details=str(e),
169
- stack_trace=traceback.format_exc().split('\n')))
170
-
171
- def stream_wrapper(self, request_iterator: Iterator[service_pb2.PostModelOutputsRequest]
19
+ def stream_wrapper(self, request: service_pb2.PostModelOutputsRequest
172
20
  ) -> Iterator[service_pb2.MultiOutputResponse]:
173
- try:
174
- request = next(request_iterator) # get first request to determine method
175
- assert len(request.inputs) == 1, "Streaming requires exactly one input"
176
-
177
- call_params = dict(request.model.model_version.output_info.params)
178
- method_name = call_params.get('_method_name', 'stream')
179
- method = getattr(self, method_name)
180
- method_info = method._cf_method_info
181
- signature = method_info.signature
182
- python_param_types = method_info.python_param_types
183
-
184
- # find the streaming vars in the signature
185
- stream_argname, streaming_var_signatures = get_stream_from_signature(signature.inputs)
186
-
187
- # convert all inputs for the first request, including the first stream value
188
- inputs = self._convert_input_protos_to_python(request.inputs, signature.inputs,
189
- python_param_types)
190
- kwargs = inputs[0]
191
-
192
- # first streaming item
193
- first_item = kwargs.pop(stream_argname)
194
-
195
- # streaming generator
196
- def InputStream():
197
- yield first_item
198
- # subsequent streaming items contain only the streaming input
199
- for request in request_iterator:
200
- item = self._convert_input_protos_to_python(request.inputs, streaming_var_signatures,
201
- python_param_types)
202
- item = item[0][stream_argname]
203
- yield item
21
+ """This method is used for input/output proto data conversion and yield outcome"""
22
+ return self.stream(request)
204
23
 
205
- # add stream generator back to the input kwargs
206
- kwargs[stream_argname] = InputStream()
207
-
208
- for output in method(**kwargs):
209
- resp = service_pb2.MultiOutputResponse()
210
- self._convert_output_to_proto(output, signature.outputs, proto=resp.outputs.add())
211
- resp.status.code = status_code_pb2.SUCCESS
212
- yield resp
213
- except Exception as e:
214
- if _RAISE_EXCEPTIONS:
215
- raise
216
- logging.exception("Error in stream")
217
- yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
218
- code=status_code_pb2.FAILURE,
219
- details=str(e),
220
- stack_trace=traceback.format_exc().split('\n')))
221
-
222
- def _convert_input_protos_to_python(self, inputs: List[resources_pb2.Input], variables_signature,
223
- python_param_types) -> List[Dict[str, Any]]:
224
- result = []
225
- for input in inputs:
226
- kwargs = deserialize(input.data, variables_signature)
227
- # dynamic cast to annotated types
228
- for k, v in kwargs.items():
229
- if k not in python_param_types:
230
- continue
231
- kwargs[k] = data_types.cast(v, python_param_types[k])
232
- result.append(kwargs)
233
- return result
234
-
235
- def _convert_output_to_proto(self, output: Any, variables_signature,
236
- proto=None) -> resources_pb2.Output:
237
- if proto is None:
238
- proto = resources_pb2.Output()
239
- if isinstance(output, tuple):
240
- output = {f'return.{i}': item for i, item in enumerate(output)}
241
- if not isinstance(output, dict): # TODO Output type, not just dict
242
- output = {'return': output}
243
- serialize(output, variables_signature, proto.data, is_output=True)
244
- proto.status.code = status_code_pb2.SUCCESS
245
- return proto
246
-
247
- @classmethod
248
- def _register_model_methods(cls):
249
- # go up the class hierarchy to find all decorated methods, and add to registry of current class
250
- methods = {}
251
- for base in reversed(cls.__mro__):
252
- for name, method in base.__dict__.items():
253
- method_info = getattr(method, _METHOD_INFO_ATTR, None)
254
- if not method_info: # regular function, not a model method
255
- continue
256
- methods[name] = method_info
257
- # check for generic predict(request) -> response, etc. methods
258
- #for name in ('predict', 'generate', 'stream'):
259
- # if hasattr(cls, name):
260
- # method = getattr(cls, name)
261
- # if not hasattr(method, _METHOD_INFO_ATTR): # not already put in registry
262
- # methods[name] = _MethodInfo(method, method_type=name)
263
- # set method table for this class in the registry
264
- return methods
265
-
266
- @classmethod
267
- def _get_method_info(cls, func_name=None):
268
- if not hasattr(cls, _METHOD_INFO_ATTR):
269
- setattr(cls, _METHOD_INFO_ATTR, cls._register_model_methods())
270
- method_info = getattr(cls, _METHOD_INFO_ATTR)
271
- if func_name:
272
- return method_info[func_name]
273
- return method_info
274
-
275
-
276
- class _MethodInfo:
277
-
278
- def __init__(self, method, method_type):
279
- self.name = method.__name__
280
- self.signature = build_function_signature(method, method_type)
281
- self.python_param_types = {
282
- p.name: p.annotation
283
- for p in inspect.signature(method).parameters.values()
284
- if p.annotation != inspect.Parameter.empty
285
- }
286
- self.python_param_types.pop('self', None)
24
+ @abstractmethod
25
+ def load_model(self):
26
+ raise NotImplementedError("load_model() not implemented")
27
+
28
+ @abstractmethod
29
+ def predict(self,
30
+ request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
31
+ raise NotImplementedError("run_input() not implemented")
32
+
33
+ @abstractmethod
34
+ def generate(self, request: service_pb2.PostModelOutputsRequest
35
+ ) -> Iterator[service_pb2.MultiOutputResponse]:
36
+ raise NotImplementedError("generate() not implemented")
37
+
38
+ @abstractmethod
39
+ def stream(self, request_iterator: Iterator[service_pb2.PostModelOutputsRequest]
40
+ ) -> Iterator[service_pb2.MultiOutputResponse]:
41
+ raise NotImplementedError("stream() not implemented")