clarifai 11.1.7__py3-none-any.whl → 11.1.7rc2__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 (154) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  3. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  4. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  5. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  6. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  7. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  8. clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  9. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  10. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  11. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  12. clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  13. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  14. clarifai/cli/model.py +25 -0
  15. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  16. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  17. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  18. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  19. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  20. clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  21. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  22. clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
  23. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  24. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  25. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  26. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  27. clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
  28. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  29. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  30. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  31. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  32. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  33. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  34. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  35. clarifai/client/cli/__init__.py +0 -0
  36. clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  37. clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  38. clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  39. clarifai/client/cli/base_cli.py +88 -0
  40. clarifai/client/cli/model_cli.py +29 -0
  41. clarifai/client/model.py +157 -393
  42. clarifai/client/model_client.py +447 -0
  43. clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
  44. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  45. clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
  46. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  47. clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
  48. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  49. clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
  50. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  51. clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  52. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  53. clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
  54. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  55. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  56. clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
  57. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  58. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  59. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  60. clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
  61. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  62. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  63. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
  64. clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
  65. clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
  66. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  67. clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
  68. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  69. clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
  70. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  71. clarifai/runners/__init__.py +2 -7
  72. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  73. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  74. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
  75. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +42 -0
  76. clarifai/runners/dockerfile_template/Dockerfile.nim +71 -0
  77. clarifai/runners/dockerfile_template/Dockerfile.template +3 -0
  78. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  79. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  80. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  81. clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
  82. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  83. clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
  84. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  85. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  86. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  87. clarifai/runners/models/model_builder.py +35 -7
  88. clarifai/runners/models/model_class.py +262 -28
  89. clarifai/runners/models/model_class_refract.py +80 -0
  90. clarifai/runners/models/model_run_locally.py +3 -78
  91. clarifai/runners/models/model_runner.py +2 -0
  92. clarifai/runners/models/model_servicer.py +11 -2
  93. clarifai/runners/models/model_upload.py +607 -0
  94. clarifai/runners/models/temp.py +25 -0
  95. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  96. clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
  97. clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  98. clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
  99. clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
  100. clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
  101. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  102. clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
  103. clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
  104. clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
  105. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  106. clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
  107. clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
  108. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  109. clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
  110. clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
  111. clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
  112. clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
  113. clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
  114. clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
  115. clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
  116. clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
  117. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  118. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  119. clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
  120. clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
  121. clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
  122. clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
  123. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  124. clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
  125. clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
  126. clarifai/runners/utils/data_handler.py +271 -210
  127. clarifai/runners/utils/data_handler_refract.py +213 -0
  128. clarifai/runners/utils/data_types.py +427 -0
  129. clarifai/runners/utils/logger.py +0 -0
  130. clarifai/runners/utils/method_signatures.py +472 -0
  131. clarifai/runners/utils/serializers.py +222 -0
  132. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  133. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  134. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  135. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  136. clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
  137. clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
  138. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  139. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  140. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  141. clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
  142. clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
  143. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  144. clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
  145. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  146. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  147. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  148. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc2.dist-info}/METADATA +3 -13
  149. clarifai-11.1.7rc2.dist-info/RECORD +237 -0
  150. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc2.dist-info}/WHEEL +1 -1
  151. clarifai-11.1.7.dist-info/RECORD +0 -101
  152. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc2.dist-info}/LICENSE +0 -0
  153. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc2.dist-info}/entry_points.txt +0 -0
  154. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,71 @@
1
+ FROM nvcr.io/nim/meta/llama-3.1-8b-instruct:1.1.2 as build
2
+
3
+ FROM gcr.io/distroless/python3-debian12:debug
4
+
5
+
6
+ COPY --from=build /bin/bash /bin/rbash
7
+ COPY --from=build /bin/sh /bin/sh
8
+ COPY --from=build /bin/rsh /bin/rsh
9
+
10
+ # we have to overwrite the python3 binary that the distroless image uses
11
+ COPY --from=build /opt/nim/llm/.venv/bin/python3.10 /usr/bin/python3
12
+ COPY --from=build /opt/nim/llm/.venv/bin/python3.10 /usr/local/bin/python3.10
13
+
14
+ # also copy in all the lib files for it.
15
+ COPY --from=build /lib /lib
16
+ COPY --from=build /lib64 /lib64
17
+ COPY --from=build /usr/lib/ /usr/lib/
18
+ COPY --from=build /usr/local/lib/ /usr/local/lib/
19
+ # ldconfig is needed to update the shared library cache so system libraries (like CUDA) can be found
20
+ COPY --from=build /usr/sbin/ldconfig /sbin/ldconfig
21
+ COPY --from=build /usr/sbin/ldconfig.real /sbin/ldconfig.real
22
+ COPY --from=build /etc/ld.so.conf /etc/ld.so.conf
23
+ COPY --from=build /etc/ld.so.cache /etc/ld.so.cache
24
+ COPY --from=build /etc/ld.so.conf.d/ /etc/ld.so.conf.d/
25
+
26
+ # COPY NIM files
27
+ COPY --from=build /opt /opt
28
+ COPY --from=build /etc/nim /etc/nim
29
+
30
+ # Set environment variables to use the nim libraries and python
31
+ ENV PYTHONPATH=${PYTHONPATH}:/opt/nim/llm/.venv/lib/python3.10/site-packages:/opt/nim/llm
32
+ ENV PATH="/opt/nim/llm/.venv/bin:/opt/hpcx/ucc/bin:/opt/hpcx/ucx/bin:/opt/hpcx/ompi/bin:$PATH"
33
+
34
+ ENV LD_LIBRARY_PATH="/opt/hpcx/ucc/lib/ucc:/opt/hpcx/ucc/lib:/opt/hpcx/ucx/lib/ucx:/opt/hpcx/ucx/lib:/opt/hpcx/ompi/lib:/opt/hpcx/ompi/lib/openmpi:/opt/nim/llm/.venv/lib/python3.10/site-packages/tensorrt_llm/libs:/opt/nim/llm/.venv/lib/python3.10/site-packages/nvidia/cublas/lib:/opt/nim/llm/.venv/lib/python3.10/site-packages/tensorrt_libs:/opt/nim/llm/.venv/lib/python3.10/site-packages/nvidia/nccl/lib:$LD_LIBRARY_PATH"
35
+
36
+ ENV LIBRARY_PATH=/opt/hpcx/ucc/lib:/opt/hpcx/ucx/lib:/opt/hpcx/ompi/lib:$LIBRARY_PATH
37
+
38
+ ENV CPATH=/opt/hpcx/ompi/include:/opt/hpcx/ucc/include:/opt/hpcx/ucx/include:$CPATH
39
+ ENV LLM_PROJECT_DIR=/opt/nim/llm
40
+
41
+ # Set environment variables for MPI
42
+ ENV OMPI_HOME=/opt/hpcx/ompi
43
+ ENV HPCX_MPI_DIR=/opt/hpcx/ompi
44
+ ENV MPIf_HOME=/opt/hpcx/ompi
45
+ ENV OPAL_PREFIX=/opt/hpcx/ompi
46
+
47
+ # Set environment variables for UCC
48
+ ENV UCC_DIR=/opt/hpcx/ucc/lib/cmake/ucc
49
+ ENV UCC_HOME=/opt/hpcx/ucc
50
+ ENV HPCX_UCC_DIR=/opt/hpcx/ucc
51
+ ENV USE_UCC=1
52
+ ENV USE_SYSTEM_UCC=1
53
+
54
+ # Set environment variables for HPC-X
55
+ ENV HPCX_DIR=/opt/hpcx
56
+ ENV HPCX_UCX_DIR=/opt/hpcx/ucx
57
+ ENV HPCX_MPI_DIR=/opt/hpcx/ompi
58
+
59
+ # Set environment variables for UCX
60
+ ENV UCX_DIR=/opt/hpcx/ucx/lib/cmake/ucx
61
+ ENV UCX_HOME=/opt/hpcx/ucx
62
+
63
+ ENV HOME=/opt/nim/llm
64
+
65
+ # ln is needed to create symbolic links (needed by nvidia-container-runtime)
66
+ COPY --from=build /usr/bin/ln /usr/bin/ln
67
+
68
+ # Run ldconfig in the build stage to update the library cache else CUDA libraries won't be found
69
+ RUN ldconfig -v
70
+
71
+ SHELL ["/bin/rbash", "-c"]
@@ -44,6 +44,9 @@ 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
+
47
50
  # Finally run the clarifai entrypoint to start the runner loop and local dev server.
48
51
  # Note(zeiler): we may want to make this a clarifai CLI call.
49
52
  ENTRYPOINT ["python", "-m", "clarifai.runners.server"]
@@ -14,13 +14,14 @@ 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 import BaseClient
17
+ from clarifai.client.base 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
24
25
  from clarifai.urls.helper import ClarifaiUrlHelper
25
26
  from clarifai.utils.logging import logger
26
27
  from clarifai.versions import CLIENT_VERSION
@@ -69,6 +70,18 @@ class ModelBuilder:
69
70
  """
70
71
  Create an instance of the model class, as specified in the config file.
71
72
  """
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
+ """
72
85
  # look for default model.py file location
73
86
  for loc in ["model.py", "1/model.py"]:
74
87
  model_file = os.path.join(self.folder, loc)
@@ -107,12 +120,7 @@ class ModelBuilder:
107
120
  "Could not determine model class. There should be exactly one model inheriting from ModelClass defined in the model.py"
108
121
  )
109
122
  model_class = classes[0]
110
-
111
- # initialize the model
112
- model = model_class()
113
- if load_model:
114
- model.load_model()
115
- return model
123
+ return model_class
116
124
 
117
125
  def _validate_folder(self, folder):
118
126
  if folder == ".":
@@ -253,6 +261,24 @@ class ModelBuilder:
253
261
  total_size += member.size
254
262
  return total_size
255
263
 
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
+
256
282
  @property
257
283
  def client(self):
258
284
  if self._client is None:
@@ -536,6 +562,8 @@ class ModelBuilder:
536
562
  pretrained_model_config=resources_pb2.PretrainedModelConfig(),
537
563
  inference_compute_info=self.inference_compute_info,
538
564
  )
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())
539
567
 
540
568
  model_type_id = self.config.get('model').get('model_type_id')
541
569
  if model_type_id in CONCEPTS_REQUIRED_MODEL_TYPE:
@@ -1,41 +1,275 @@
1
- from abc import ABC, abstractmethod
2
- from typing import Iterator
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
3
8
 
4
- from clarifai_grpc.grpc.api import service_pb2
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")
5
20
 
6
21
 
7
22
  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
8
80
 
9
81
  def predict_wrapper(
10
82
  self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
11
- """This method is used for input/output proto data conversion"""
12
- return self.predict(request)
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')))
13
119
 
14
120
  def generate_wrapper(self, request: service_pb2.PostModelOutputsRequest
15
121
  ) -> Iterator[service_pb2.MultiOutputResponse]:
16
- """This method is used for input/output proto data conversion and yield outcome"""
17
- return self.generate(request)
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')))
18
156
 
19
- def stream_wrapper(self, request: service_pb2.PostModelOutputsRequest
157
+ def stream_wrapper(self, request_iterator: Iterator[service_pb2.PostModelOutputsRequest]
20
158
  ) -> Iterator[service_pb2.MultiOutputResponse]:
21
- """This method is used for input/output proto data conversion and yield outcome"""
22
- return self.stream(request)
159
+ try:
160
+ request = next(request_iterator) # get first request to determine method
161
+ assert len(request.inputs) == 1, "Streaming requires exactly one input"
23
162
 
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")
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()
197
+
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)
@@ -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()
@@ -7,14 +7,11 @@ import subprocess
7
7
  import sys
8
8
  import tempfile
9
9
  import time
10
- import traceback
11
10
  import venv
12
11
 
13
12
  from clarifai_grpc.grpc.api import resources_pb2, service_pb2
14
- from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
15
13
 
16
14
  from clarifai.runners.models.model_builder import ModelBuilder
17
- from clarifai.runners.utils.url_fetcher import ensure_urls_downloaded
18
15
  from clarifai.utils.logging import logger
19
16
 
20
17
 
@@ -111,85 +108,13 @@ class ModelRunLocally:
111
108
  for i in range(1):
112
109
  yield request
113
110
 
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
-
187
111
  def _run_test(self):
188
112
  """Test the model locally by making a prediction."""
189
113
  # Create the model
190
114
  model = self.builder.create_model_instance()
191
- # send an inference.
192
- self._run_model_inference(model)
115
+ # call its test method, if it has one
116
+ if hasattr(model, "test"):
117
+ model.test()
193
118
 
194
119
  def test_model(self):
195
120
  """Test the model by running it locally in the virtual environment."""