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.
Files changed (225) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  3. clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
  4. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  5. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  6. clarifai/__pycache__/errors.cpython-311.pyc +0 -0
  7. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  8. clarifai/__pycache__/versions.cpython-311.pyc +0 -0
  9. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  10. clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
  11. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  12. clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
  13. clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  14. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  15. clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  16. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  17. clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
  18. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  19. clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
  20. clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  21. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  22. clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
  23. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  24. clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
  25. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  26. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  27. clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
  28. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  29. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  30. clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
  31. clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  32. clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  33. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  34. clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
  35. clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
  36. clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
  37. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  38. clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
  39. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  40. clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
  41. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  42. clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
  43. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  44. clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
  45. clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
  46. clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
  47. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  48. clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
  49. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  50. clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
  51. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  52. clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
  53. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  54. clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
  55. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  56. clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
  57. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  58. clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
  59. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  60. clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
  61. clarifai/client/cli/__init__.py +0 -0
  62. clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  63. clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  64. clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  65. clarifai/client/cli/base_cli.py +88 -0
  66. clarifai/client/cli/model_cli.py +29 -0
  67. clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
  68. clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
  69. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  70. clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
  71. clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
  72. clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
  73. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  74. clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
  75. clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
  76. clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
  77. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  78. clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
  79. clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
  80. clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
  81. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  82. clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
  83. clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  84. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  85. clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
  86. clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
  87. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  88. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
  89. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  90. clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
  91. clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
  92. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  93. clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
  94. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  95. clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
  96. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  97. clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
  98. clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
  99. clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
  100. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  101. clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
  102. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  103. clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
  104. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
  105. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
  106. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
  107. clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
  108. clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
  109. clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
  110. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  111. clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
  112. clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
  113. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  114. clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
  115. clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
  116. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  117. clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
  118. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  119. clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
  120. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  121. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
  122. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +42 -0
  123. clarifai/runners/dockerfile_template/Dockerfile.nim +71 -0
  124. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  125. clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
  126. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  127. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  128. clarifai/runners/models/__pycache__/base_typed_model.cpython-311.pyc +0 -0
  129. clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
  130. clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
  131. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  132. clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
  133. clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
  134. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  135. clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
  136. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  137. clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
  138. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  139. clarifai/runners/models/base_typed_model.py +238 -0
  140. clarifai/runners/models/model_builder.py +14 -1
  141. clarifai/runners/models/model_class_refract.py +80 -0
  142. clarifai/runners/models/model_upload.py +607 -0
  143. clarifai/runners/models/temp.py +25 -0
  144. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  145. clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  146. clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
  147. clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  148. clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
  149. clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
  150. clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
  151. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  152. clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
  153. clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
  154. clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
  155. clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
  156. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  157. clarifai/runners/utils/__pycache__/data_handler.cpython-311.pyc +0 -0
  158. clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
  159. clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
  160. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  161. clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
  162. clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
  163. clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
  164. clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
  165. clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
  166. clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
  167. clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
  168. clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
  169. clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
  170. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  171. clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
  172. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  173. clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
  174. clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
  175. clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
  176. clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
  177. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  178. clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
  179. clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
  180. clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
  181. clarifai/runners/utils/data_handler.py +231 -0
  182. clarifai/runners/utils/data_handler_refract.py +213 -0
  183. clarifai/runners/utils/data_utils.py +107 -14
  184. clarifai/runners/utils/logger.py +0 -0
  185. clarifai/runners/utils/openai_format.py +87 -0
  186. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  187. clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
  188. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  189. clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
  190. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  191. clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  192. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  193. clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
  194. clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
  195. clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
  196. clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
  197. clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
  198. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  199. clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
  200. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  201. clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
  202. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  203. clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
  204. clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
  205. clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
  206. clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
  207. clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
  208. clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
  209. clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
  210. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  211. clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
  212. clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
  213. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  214. clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
  215. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  216. clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
  217. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  218. clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
  219. {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/METADATA +3 -15
  220. clarifai-11.3.0rc2.dist-info/RECORD +322 -0
  221. {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/WHEEL +1 -1
  222. clarifai-11.3.0.dist-info/RECORD +0 -107
  223. {clarifai-11.3.0.dist-info/licenses → clarifai-11.3.0rc2.dist-info}/LICENSE +0 -0
  224. {clarifai-11.3.0.dist-info → clarifai-11.3.0rc2.dist-info}/entry_points.txt +0 -0
  225. {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 issubclass(cls, ModelClass) and cls.__module__ == module.__name__
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()