clarifai 11.1.7rc2__py3-none-any.whl → 11.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/cli/base.py +16 -3
  3. clarifai/cli/model.py +0 -25
  4. clarifai/client/model.py +393 -157
  5. clarifai/runners/__init__.py +7 -2
  6. clarifai/runners/dockerfile_template/Dockerfile.template +0 -3
  7. clarifai/runners/models/model_builder.py +11 -38
  8. clarifai/runners/models/model_class.py +28 -262
  9. clarifai/runners/models/model_run_locally.py +80 -15
  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 +210 -271
  13. {clarifai-11.1.7rc2.dist-info → clarifai-11.2.0.dist-info}/METADATA +17 -5
  14. clarifai-11.2.0.dist-info/RECORD +101 -0
  15. {clarifai-11.1.7rc2.dist-info → clarifai-11.2.0.dist-info}/WHEEL +1 -1
  16. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  17. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  18. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  19. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  20. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  21. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  22. clarifai/cli/__pycache__/base_cli.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__/model_cli.cpython-310.pyc +0 -0
  27. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  28. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  29. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  30. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  31. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  32. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  33. clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  34. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  35. clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
  36. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  37. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  38. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  39. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  40. clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
  41. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  42. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  43. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  44. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  45. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  46. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  47. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  48. clarifai/client/cli/__init__.py +0 -0
  49. clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  50. clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  51. clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  52. clarifai/client/cli/base_cli.py +0 -88
  53. clarifai/client/cli/model_cli.py +0 -29
  54. clarifai/client/model_client.py +0 -447
  55. clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
  56. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  57. clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
  58. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  59. clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
  60. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  61. clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
  62. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  63. clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  64. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  65. clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
  66. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  67. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  68. clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
  69. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  70. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  71. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  72. clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
  73. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  74. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  75. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
  76. clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
  77. clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
  78. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  79. clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
  80. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  81. clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
  82. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  83. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  84. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  85. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -31
  86. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -42
  87. clarifai/runners/dockerfile_template/Dockerfile.nim +0 -71
  88. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  89. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  90. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  91. clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
  92. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  93. clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
  94. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  95. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  96. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  97. clarifai/runners/models/model_class_refract.py +0 -80
  98. clarifai/runners/models/model_upload.py +0 -607
  99. clarifai/runners/models/temp.py +0 -25
  100. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  101. clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
  102. clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  103. clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
  104. clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
  105. clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
  106. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  107. clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
  108. clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
  109. clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
  110. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  111. clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
  112. clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
  113. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  114. clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
  115. clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
  116. clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
  117. clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
  118. clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
  119. clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
  120. clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
  121. clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
  122. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  123. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  124. clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
  125. clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
  126. clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
  127. clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
  128. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  129. clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
  130. clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
  131. clarifai/runners/utils/data_handler_refract.py +0 -213
  132. clarifai/runners/utils/data_types.py +0 -427
  133. clarifai/runners/utils/logger.py +0 -0
  134. clarifai/runners/utils/method_signatures.py +0 -472
  135. clarifai/runners/utils/serializers.py +0 -222
  136. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  137. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  138. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  139. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  140. clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
  141. clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
  142. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  143. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  144. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  145. clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
  146. clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
  147. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  148. clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
  149. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  150. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  151. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  152. clarifai-11.1.7rc2.dist-info/RECORD +0 -237
  153. {clarifai-11.1.7rc2.dist-info → clarifai-11.2.0.dist-info}/entry_points.txt +0 -0
  154. {clarifai-11.1.7rc2.dist-info → clarifai-11.2.0.dist-info/licenses}/LICENSE +0 -0
  155. {clarifai-11.1.7rc2.dist-info → clarifai-11.2.0.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
  ]
@@ -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"]
@@ -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 == ".":
@@ -240,7 +232,7 @@ class ModelBuilder:
240
232
  f"`num_threads` must be an integer greater than or equal to 1. Received type {type(num_threads)} with value {num_threads}."
241
233
  )
242
234
  else:
243
- num_threads = int(os.environ.get("CLARIFAI_NUM_THREADS", 1))
235
+ num_threads = int(os.environ.get("CLARIFAI_NUM_THREADS", 16))
244
236
  self.config["num_threads"] = num_threads
245
237
 
246
238
  @staticmethod
@@ -261,24 +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.values()}
271
- return signatures_to_yaml(signatures)
272
-
273
- def get_method_signatures(self):
274
- """
275
- Returns the method signatures for the model class.
276
- """
277
- model_class = self.load_model_class()
278
- method_info = model_class._get_method_info()
279
- signatures = [method.signature for method in method_info.values()]
280
- return signatures
281
-
282
256
  @property
283
257
  def client(self):
284
258
  if self._client is None:
@@ -312,8 +286,9 @@ class ModelBuilder:
312
286
 
313
287
  assert "model_type_id" in model, "model_type_id not found in the config file"
314
288
  assert "id" in model, "model_id not found in the config file"
315
- assert "user_id" in model, "user_id not found in the config file"
316
- assert "app_id" in model, "app_id not found in the config file"
289
+ if not self.download_validation_only:
290
+ assert "user_id" in model, "user_id not found in the config file"
291
+ assert "app_id" in model, "app_id not found in the config file"
317
292
 
318
293
  model_proto = json_format.ParseDict(model, resources_pb2.Model())
319
294
 
@@ -562,8 +537,6 @@ class ModelBuilder:
562
537
  pretrained_model_config=resources_pb2.PretrainedModelConfig(),
563
538
  inference_compute_info=self.inference_compute_info,
564
539
  )
565
- # TODO: update this to `model_signatures` field when it's available in the API
566
- model_version_proto.model_signature.extend(self.get_method_signatures())
567
540
 
568
541
  model_type_id = self.config.get('model').get('model_type_id')
569
542
  if model_type_id in CONCEPTS_REQUIRED_MODEL_TYPE:
@@ -1,275 +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() in ("true", "1")
4
+ from clarifai_grpc.grpc.api import service_pb2
20
5
 
21
6
 
22
7
  class ModelClass(ABC):
23
- '''
24
- Base class for model classes that can be run as a service.
25
-
26
- Define predict, generate, or stream methods using the @ModelClass.method decorator.
27
-
28
- Example:
29
-
30
- from clarifai.runners.model_class import ModelClass
31
- from clarifai.runners.utils.data_types import NamedFields, Stream
32
-
33
- class MyModel(ModelClass):
34
-
35
- @ModelClass.method
36
- def predict(self, x: str, y: int) -> List[str]:
37
- return [x] * y
38
-
39
- @ModelClass.method
40
- def generate(self, x: str, y: int) -> Stream[str]:
41
- for i in range(y):
42
- yield x + str(i)
43
-
44
- @ModelClass.method
45
- def stream(self, input_stream: Stream[NamedFields(x=str, y=int)]) -> Stream[str]:
46
- for item in input_stream:
47
- yield item.x + ' ' + str(item.y)
48
- '''
49
-
50
- @staticmethod
51
- def method(func):
52
- setattr(func, _METHOD_INFO_ATTR, _MethodInfo(func))
53
- return func
54
-
55
- def load_model(self):
56
- """Load the model."""
57
-
58
- def _handle_get_signatures_request(self) -> service_pb2.MultiOutputResponse:
59
- methods = self._get_method_info()
60
- signatures = {method.name: method.signature for method in methods.values()}
61
- resp = service_pb2.MultiOutputResponse(status=status_pb2.Status(code=status_code_pb2.SUCCESS))
62
- output = resp.outputs.add()
63
- output.status.code = status_code_pb2.SUCCESS
64
- output.data.text.raw = signatures_to_json(signatures)
65
- return resp
66
-
67
- def _batch_predict(self, method, inputs: List[Dict[str, Any]]) -> List[Any]:
68
- """Batch predict method for multiple inputs."""
69
- outputs = []
70
- for input in inputs:
71
- output = method(**input)
72
- outputs.append(output)
73
- return outputs
74
-
75
- def _batch_generate(self, method, inputs: List[Dict[str, Any]]) -> Iterator[List[Any]]:
76
- """Batch generate method for multiple inputs."""
77
- generators = [method(**input) for input in inputs]
78
- for outputs in itertools.zip_longest(*generators):
79
- yield outputs
80
8
 
81
9
  def predict_wrapper(
82
10
  self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
83
- outputs = []
84
- try:
85
- # TODO add method name field to proto
86
- method_name = 'predict'
87
- if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
88
- method_name = request.inputs[0].data.metadata['_method_name']
89
- if method_name == '_GET_SIGNATURES': # special case to fetch signatures, TODO add endpoint for this
90
- return self._handle_get_signatures_request()
91
- if method_name not in self._get_method_info():
92
- raise ValueError(f"Method {method_name} not found in model class")
93
- method = getattr(self, method_name)
94
- method_info = method._cf_method_info
95
- signature = method_info.signature
96
- python_param_types = method_info.python_param_types
97
- inputs = self._convert_input_protos_to_python(request.inputs, signature.input_fields,
98
- python_param_types)
99
- if len(inputs) == 1:
100
- inputs = inputs[0]
101
- output = method(**inputs)
102
- outputs.append(self._convert_output_to_proto(output, signature.output_fields))
103
- else:
104
- outputs = self._batch_predict(method, inputs)
105
- outputs = [
106
- self._convert_output_to_proto(output, signature.output_fields) for output in outputs
107
- ]
108
-
109
- return service_pb2.MultiOutputResponse(
110
- outputs=outputs, status=status_pb2.Status(code=status_code_pb2.SUCCESS))
111
- except Exception as e:
112
- if _RAISE_EXCEPTIONS:
113
- raise
114
- logging.exception("Error in predict")
115
- return service_pb2.MultiOutputResponse(status=status_pb2.Status(
116
- code=status_code_pb2.FAILURE,
117
- details=str(e),
118
- stack_trace=traceback.format_exc().split('\n')))
11
+ """This method is used for input/output proto data conversion"""
12
+ return self.predict(request)
119
13
 
120
14
  def generate_wrapper(self, request: service_pb2.PostModelOutputsRequest
121
15
  ) -> Iterator[service_pb2.MultiOutputResponse]:
122
- try:
123
- method_name = 'generate'
124
- if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
125
- method_name = request.inputs[0].data.metadata['_method_name']
126
- method = getattr(self, method_name)
127
- method_info = method._cf_method_info
128
- signature = method_info.signature
129
- python_param_types = method_info.python_param_types
130
-
131
- inputs = self._convert_input_protos_to_python(request.inputs, signature.input_fields,
132
- python_param_types)
133
- if len(inputs) == 1:
134
- inputs = inputs[0]
135
- for output in method(**inputs):
136
- resp = service_pb2.MultiOutputResponse()
137
- self._convert_output_to_proto(output, signature.output_fields, proto=resp.outputs.add())
138
- resp.status.code = status_code_pb2.SUCCESS
139
- yield resp
140
- else:
141
- for outputs in self._batch_generate(method, inputs):
142
- resp = service_pb2.MultiOutputResponse()
143
- for output in outputs:
144
- self._convert_output_to_proto(
145
- output, signature.output_fields, proto=resp.outputs.add())
146
- resp.status.code = status_code_pb2.SUCCESS
147
- yield resp
148
- except Exception as e:
149
- if _RAISE_EXCEPTIONS:
150
- raise
151
- logging.exception("Error in generate")
152
- yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
153
- code=status_code_pb2.FAILURE,
154
- details=str(e),
155
- stack_trace=traceback.format_exc().split('\n')))
16
+ """This method is used for input/output proto data conversion and yield outcome"""
17
+ return self.generate(request)
156
18
 
157
- def stream_wrapper(self, request_iterator: Iterator[service_pb2.PostModelOutputsRequest]
19
+ def stream_wrapper(self, request: service_pb2.PostModelOutputsRequest
158
20
  ) -> Iterator[service_pb2.MultiOutputResponse]:
159
- try:
160
- request = next(request_iterator) # get first request to determine method
161
- assert len(request.inputs) == 1, "Streaming requires exactly one input"
162
-
163
- method_name = 'generate'
164
- if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
165
- method_name = request.inputs[0].data.metadata['_method_name']
166
- method = getattr(self, method_name)
167
- method_info = method._cf_method_info
168
- signature = method_info.signature
169
- python_param_types = method_info.python_param_types
170
-
171
- # find the streaming vars in the signature
172
- stream_sig = get_stream_from_signature(signature.input_fields)
173
- if stream_sig is None:
174
- raise ValueError("Streaming method must have a Stream input")
175
- stream_argname = stream_sig.name
176
-
177
- # convert all inputs for the first request, including the first stream value
178
- inputs = self._convert_input_protos_to_python(request.inputs, signature.input_fields,
179
- python_param_types)
180
- kwargs = inputs[0]
181
-
182
- # first streaming item
183
- first_item = kwargs.pop(stream_argname)
184
-
185
- # streaming generator
186
- def InputStream():
187
- yield first_item
188
- # subsequent streaming items contain only the streaming input
189
- for request in request_iterator:
190
- item = self._convert_input_protos_to_python(request.inputs, [stream_sig],
191
- python_param_types)
192
- item = item[0][stream_argname]
193
- yield item
194
-
195
- # add stream generator back to the input kwargs
196
- kwargs[stream_argname] = InputStream()
21
+ """This method is used for input/output proto data conversion and yield outcome"""
22
+ return self.stream(request)
197
23
 
198
- for output in method(**kwargs):
199
- resp = service_pb2.MultiOutputResponse()
200
- self._convert_output_to_proto(output, signature.output_fields, proto=resp.outputs.add())
201
- resp.status.code = status_code_pb2.SUCCESS
202
- yield resp
203
- except Exception as e:
204
- if _RAISE_EXCEPTIONS:
205
- raise
206
- logging.exception("Error in stream")
207
- yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
208
- code=status_code_pb2.FAILURE,
209
- details=str(e),
210
- stack_trace=traceback.format_exc().split('\n')))
211
-
212
- def _convert_input_protos_to_python(self, inputs: List[resources_pb2.Input],
213
- variables_signature: List[resources_pb2.ModelTypeField],
214
- python_param_types) -> List[Dict[str, Any]]:
215
- result = []
216
- for input in inputs:
217
- kwargs = deserialize(input.data, variables_signature)
218
- # dynamic cast to annotated types
219
- for k, v in kwargs.items():
220
- if k not in python_param_types:
221
- continue
222
- kwargs[k] = data_types.cast(v, python_param_types[k])
223
- result.append(kwargs)
224
- return result
225
-
226
- def _convert_output_to_proto(self,
227
- output: Any,
228
- variables_signature: List[resources_pb2.ModelTypeField],
229
- proto=None) -> resources_pb2.Output:
230
- if proto is None:
231
- proto = resources_pb2.Output()
232
- serialize({'return': output}, variables_signature, proto.data, is_output=True)
233
- proto.status.code = status_code_pb2.SUCCESS
234
- return proto
235
-
236
- @classmethod
237
- def _register_model_methods(cls):
238
- # go up the class hierarchy to find all decorated methods, and add to registry of current class
239
- methods = {}
240
- for base in reversed(cls.__mro__):
241
- for name, method in base.__dict__.items():
242
- method_info = getattr(method, _METHOD_INFO_ATTR, None)
243
- if not method_info: # regular function, not a model method
244
- continue
245
- methods[name] = method_info
246
- # check for generic predict(request) -> response, etc. methods
247
- #for name in ('predict', 'generate', 'stream'):
248
- # if hasattr(cls, name):
249
- # method = getattr(cls, name)
250
- # if not hasattr(method, _METHOD_INFO_ATTR): # not already put in registry
251
- # methods[name] = _MethodInfo(method)
252
- # set method table for this class in the registry
253
- return methods
254
-
255
- @classmethod
256
- def _get_method_info(cls, func_name=None):
257
- if not hasattr(cls, _METHOD_INFO_ATTR):
258
- setattr(cls, _METHOD_INFO_ATTR, cls._register_model_methods())
259
- method_info = getattr(cls, _METHOD_INFO_ATTR)
260
- if func_name:
261
- return method_info[func_name]
262
- return method_info
263
-
264
-
265
- class _MethodInfo:
266
-
267
- def __init__(self, method):
268
- self.name = method.__name__
269
- self.signature = build_function_signature(method)
270
- self.python_param_types = {
271
- p.name: p.annotation
272
- for p in inspect.signature(method).parameters.values()
273
- if p.annotation != inspect.Parameter.empty
274
- }
275
- 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")
@@ -7,11 +7,14 @@ import subprocess
7
7
  import sys
8
8
  import tempfile
9
9
  import time
10
+ import traceback
10
11
  import venv
11
12
 
12
13
  from clarifai_grpc.grpc.api import resources_pb2, service_pb2
14
+ from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
13
15
 
14
16
  from clarifai.runners.models.model_builder import ModelBuilder
17
+ from clarifai.runners.utils.url_fetcher import ensure_urls_downloaded
15
18
  from clarifai.utils.logging import logger
16
19
 
17
20
 
@@ -108,13 +111,85 @@ class ModelRunLocally:
108
111
  for i in range(1):
109
112
  yield request
110
113
 
114
+ def _run_model_inference(self, model):
115
+ """Perform inference using the model."""
116
+ request = self._build_request()
117
+ stream_request = self._build_stream_request()
118
+
119
+ ensure_urls_downloaded(request)
120
+ predict_response = None
121
+ generate_response = None
122
+ stream_response = None
123
+ try:
124
+ predict_response = model.predict(request)
125
+ except NotImplementedError:
126
+ logger.info("Model does not implement predict() method.")
127
+ except Exception as e:
128
+ logger.error(f"Model Prediction failed: {e}")
129
+ traceback.print_exc()
130
+ predict_response = service_pb2.MultiOutputResponse(status=status_pb2.Status(
131
+ code=status_code_pb2.MODEL_PREDICTION_FAILED,
132
+ description="Prediction failed",
133
+ details="",
134
+ internal_details=str(e),
135
+ ))
136
+
137
+ if predict_response:
138
+ if predict_response.outputs[0].status.code != status_code_pb2.SUCCESS:
139
+ logger.error(f"Moddel Prediction failed: {predict_response}")
140
+ else:
141
+ logger.info(f"Model Prediction succeeded: {predict_response}")
142
+
143
+ try:
144
+ generate_response = model.generate(request)
145
+ except NotImplementedError:
146
+ logger.info("Model does not implement generate() method.")
147
+ except Exception as e:
148
+ logger.error(f"Model Generation failed: {e}")
149
+ traceback.print_exc()
150
+ generate_response = service_pb2.MultiOutputResponse(status=status_pb2.Status(
151
+ code=status_code_pb2.MODEL_GENERATION_FAILED,
152
+ description="Generation failed",
153
+ details="",
154
+ internal_details=str(e),
155
+ ))
156
+
157
+ if generate_response:
158
+ generate_first_res = next(generate_response)
159
+ if generate_first_res.outputs[0].status.code != status_code_pb2.SUCCESS:
160
+ logger.error(f"Moddel Prediction failed: {generate_first_res}")
161
+ else:
162
+ logger.info(
163
+ f"Model Prediction succeeded for generate and first response: {generate_first_res}")
164
+
165
+ try:
166
+ stream_response = model.stream(stream_request)
167
+ except NotImplementedError:
168
+ logger.info("Model does not implement stream() method.")
169
+ except Exception as e:
170
+ logger.error(f"Model Stream failed: {e}")
171
+ traceback.print_exc()
172
+ stream_response = service_pb2.MultiOutputResponse(status=status_pb2.Status(
173
+ code=status_code_pb2.MODEL_STREAM_FAILED,
174
+ description="Stream failed",
175
+ details="",
176
+ internal_details=str(e),
177
+ ))
178
+
179
+ if stream_response:
180
+ stream_first_res = next(stream_response)
181
+ if stream_first_res.outputs[0].status.code != status_code_pb2.SUCCESS:
182
+ logger.error(f"Moddel Prediction failed: {stream_first_res}")
183
+ else:
184
+ logger.info(
185
+ f"Model Prediction succeeded for stream and first response: {stream_first_res}")
186
+
111
187
  def _run_test(self):
112
188
  """Test the model locally by making a prediction."""
113
189
  # Create the model
114
190
  model = self.builder.create_model_instance()
115
- # call its test method, if it has one
116
- if hasattr(model, "test"):
117
- model.test()
191
+ # send an inference.
192
+ self._run_model_inference(model)
118
193
 
119
194
  def test_model(self):
120
195
  """Test the model by running it locally in the virtual environment."""
@@ -400,11 +475,6 @@ def main(model_path,
400
475
  keep_env=False,
401
476
  keep_image=False):
402
477
 
403
- if not os.environ.get("CLARIFAI_PAT", None):
404
- logger.error(
405
- "CLARIFAI_PAT environment variable is not set! Please set your PAT in the 'CLARIFAI_PAT' environment variable."
406
- )
407
- sys.exit(1)
408
478
  manager = ModelRunLocally(model_path)
409
479
  # get whatever stage is in config.yaml to force download now
410
480
  # also always write to where upload/build wants to, not the /tmp folder that runtime stage uses
@@ -421,16 +491,11 @@ def main(model_path,
421
491
  if not manager.docker_image_exists(image_name):
422
492
  manager.build_docker_image(image_name=image_name)
423
493
  try:
424
- envs = {
425
- 'CLARIFAI_PAT': os.environ['CLARIFAI_PAT'],
426
- 'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')
427
- }
428
494
  if run_model_server:
429
495
  manager.run_docker_container(
430
- image_name=image_name, container_name=container_name, port=port, env_vars=envs)
496
+ image_name=image_name, container_name=container_name, port=port)
431
497
  else:
432
- manager.test_model_container(
433
- image_name=image_name, container_name=container_name, env_vars=envs)
498
+ manager.test_model_container(image_name=image_name, container_name=container_name)
434
499
  finally:
435
500
  if manager.container_exists(container_name):
436
501
  manager.stop_docker_container(container_name)
@@ -82,8 +82,6 @@ class ModelRunner(BaseRunner, HealthProbeRequestHandler):
82
82
  ensure_urls_downloaded(request)
83
83
 
84
84
  resp = self.model.predict_wrapper(request)
85
- if resp.status.code != status_code_pb2.SUCCESS:
86
- return service_pb2.RunnerItemOutput(multi_output_response=resp)
87
85
  successes = [o.status.code == status_code_pb2.SUCCESS for o in resp.outputs]
88
86
  if all(successes):
89
87
  status = status_pb2.Status(
@@ -1,4 +1,3 @@
1
- import os
2
1
  from itertools import tee
3
2
  from typing import Iterator
4
3
 
@@ -7,8 +6,6 @@ from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
7
6
 
8
7
  from ..utils.url_fetcher import ensure_urls_downloaded
9
8
 
10
- _RAISE_EXCEPTIONS = os.getenv("RAISE_EXCEPTIONS", "false").lower() in ("true", "1")
11
-
12
9
 
13
10
  class ModelServicer(service_pb2_grpc.V2Servicer):
14
11
  """
@@ -36,8 +33,6 @@ class ModelServicer(service_pb2_grpc.V2Servicer):
36
33
  try:
37
34
  return self.model.predict_wrapper(request)
38
35
  except Exception as e:
39
- if _RAISE_EXCEPTIONS:
40
- raise
41
36
  return service_pb2.MultiOutputResponse(status=status_pb2.Status(
42
37
  code=status_code_pb2.MODEL_PREDICTION_FAILED,
43
38
  description="Failed",
@@ -55,10 +50,8 @@ class ModelServicer(service_pb2_grpc.V2Servicer):
55
50
  ensure_urls_downloaded(request)
56
51
 
57
52
  try:
58
- yield from self.model.generate_wrapper(request)
53
+ return self.model.generate_wrapper(request)
59
54
  except Exception as e:
60
- if _RAISE_EXCEPTIONS:
61
- raise
62
55
  yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
63
56
  code=status_code_pb2.MODEL_PREDICTION_FAILED,
64
57
  description="Failed",
@@ -81,10 +74,8 @@ class ModelServicer(service_pb2_grpc.V2Servicer):
81
74
  ensure_urls_downloaded(req)
82
75
 
83
76
  try:
84
- yield from self.model.stream_wrapper(request_copy)
77
+ return self.model.stream_wrapper(request_copy)
85
78
  except Exception as e:
86
- if _RAISE_EXCEPTIONS:
87
- raise
88
79
  yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
89
80
  code=status_code_pb2.MODEL_PREDICTION_FAILED,
90
81
  description="Failed",