clarifai 11.1.7rc3__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.
- clarifai/__init__.py +1 -1
- clarifai/cli/base.py +16 -3
- clarifai/cli/model.py +0 -25
- clarifai/client/model.py +393 -157
- clarifai/runners/__init__.py +7 -2
- clarifai/runners/models/model_builder.py +12 -80
- clarifai/runners/models/model_class.py +28 -279
- clarifai/runners/models/model_run_locally.py +80 -15
- clarifai/runners/models/model_runner.py +0 -2
- clarifai/runners/models/model_servicer.py +2 -11
- clarifai/runners/utils/data_handler.py +210 -271
- {clarifai-11.1.7rc3.dist-info → clarifai-11.2.0.dist-info}/METADATA +16 -4
- clarifai-11.2.0.dist-info/RECORD +101 -0
- {clarifai-11.1.7rc3.dist-info → clarifai-11.2.0.dist-info}/WHEEL +1 -1
- clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/__pycache__/errors.cpython-310.pyc +0 -0
- clarifai/__pycache__/versions.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
- clarifai/client/cli/__init__.py +0 -0
- clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/base_cli.py +0 -88
- clarifai/client/cli/model_cli.py +0 -29
- clarifai/client/model_client.py +0 -448
- clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -31
- clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -42
- clarifai/runners/dockerfile_template/Dockerfile.nim +0 -71
- clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
- clarifai/runners/models/model_class_refract.py +0 -80
- clarifai/runners/models/model_upload.py +0 -607
- clarifai/runners/models/temp.py +0 -25
- clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
- clarifai/runners/utils/data_handler_refract.py +0 -213
- clarifai/runners/utils/data_types.py +0 -427
- clarifai/runners/utils/logger.py +0 -0
- clarifai/runners/utils/method_signatures.py +0 -477
- clarifai/runners/utils/serializers.py +0 -222
- clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
- clarifai-11.1.7rc3.dist-info/RECORD +0 -237
- {clarifai-11.1.7rc3.dist-info → clarifai-11.2.0.dist-info}/entry_points.txt +0 -0
- {clarifai-11.1.7rc3.dist-info → clarifai-11.2.0.dist-info/licenses}/LICENSE +0 -0
- {clarifai-11.1.7rc3.dist-info → clarifai-11.2.0.dist-info}/top_level.txt +0 -0
clarifai/runners/__init__.py
CHANGED
@@ -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
|
-
"
|
9
|
+
"InputDataHandler",
|
10
|
+
"OutputDataHandler",
|
11
|
+
"AnyAnyModel",
|
12
|
+
"TextInputModel",
|
13
|
+
"VisualInputModel",
|
9
14
|
]
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import builtins
|
2
1
|
import importlib
|
3
2
|
import inspect
|
4
3
|
import os
|
@@ -7,7 +6,6 @@ import sys
|
|
7
6
|
import tarfile
|
8
7
|
import time
|
9
8
|
from string import Template
|
10
|
-
from unittest.mock import MagicMock
|
11
9
|
|
12
10
|
import yaml
|
13
11
|
from clarifai_grpc.grpc.api import resources_pb2, service_pb2
|
@@ -16,14 +14,13 @@ from google.protobuf import json_format
|
|
16
14
|
from rich import print
|
17
15
|
from rich.markup import escape
|
18
16
|
|
19
|
-
from clarifai.client
|
17
|
+
from clarifai.client import BaseClient
|
20
18
|
from clarifai.runners.models.model_class import ModelClass
|
21
19
|
from clarifai.runners.utils.const import (
|
22
20
|
AVAILABLE_PYTHON_IMAGES, AVAILABLE_TORCH_IMAGES, CONCEPTS_REQUIRED_MODEL_TYPE,
|
23
21
|
DEFAULT_DOWNLOAD_CHECKPOINT_WHEN, DEFAULT_PYTHON_VERSION, DEFAULT_RUNTIME_DOWNLOAD_PATH,
|
24
22
|
PYTHON_BASE_IMAGE, TORCH_BASE_IMAGE)
|
25
23
|
from clarifai.runners.utils.loader import HuggingFaceLoader
|
26
|
-
from clarifai.runners.utils.method_signatures import signatures_to_yaml
|
27
24
|
from clarifai.urls.helper import ClarifaiUrlHelper
|
28
25
|
from clarifai.utils.logging import logger
|
29
26
|
from clarifai.versions import CLIENT_VERSION
|
@@ -72,18 +69,6 @@ class ModelBuilder:
|
|
72
69
|
"""
|
73
70
|
Create an instance of the model class, as specified in the config file.
|
74
71
|
"""
|
75
|
-
model_class = self.load_model_class()
|
76
|
-
|
77
|
-
# initialize the model
|
78
|
-
model = model_class()
|
79
|
-
if load_model:
|
80
|
-
model.load_model()
|
81
|
-
return model
|
82
|
-
|
83
|
-
def load_model_class(self):
|
84
|
-
"""
|
85
|
-
Import the model class from the model.py file, dynamically handling missing dependencies
|
86
|
-
"""
|
87
72
|
# look for default model.py file location
|
88
73
|
for loc in ["model.py", "1/model.py"]:
|
89
74
|
model_file = os.path.join(self.folder, loc)
|
@@ -97,29 +82,7 @@ class ModelBuilder:
|
|
97
82
|
spec = importlib.util.spec_from_file_location(module_name, model_file)
|
98
83
|
module = importlib.util.module_from_spec(spec)
|
99
84
|
sys.modules[module_name] = module
|
100
|
-
|
101
|
-
original_import = builtins.__import__
|
102
|
-
|
103
|
-
def custom_import(name, globals=None, locals=None, fromlist=(), level=0):
|
104
|
-
|
105
|
-
# Allow standard libraries and clarifai
|
106
|
-
if self._is_standard_or_clarifai(name):
|
107
|
-
return original_import(name, globals, locals, fromlist, level)
|
108
|
-
|
109
|
-
# Mock all third-party imports to avoid ImportErrors or other issues
|
110
|
-
return MagicMock()
|
111
|
-
|
112
|
-
# Replace the built-in __import__ function with our custom one
|
113
|
-
builtins.__import__ = custom_import
|
114
|
-
|
115
|
-
try:
|
116
|
-
spec.loader.exec_module(module)
|
117
|
-
except Exception as e:
|
118
|
-
logger.error(f"Error loading model.py: {e}")
|
119
|
-
raise
|
120
|
-
finally:
|
121
|
-
# Restore the original __import__ function
|
122
|
-
builtins.__import__ = original_import
|
85
|
+
spec.loader.exec_module(module)
|
123
86
|
|
124
87
|
# Find all classes in the model.py file that are subclasses of ModelClass
|
125
88
|
classes = [
|
@@ -144,24 +107,12 @@ class ModelBuilder:
|
|
144
107
|
"Could not determine model class. There should be exactly one model inheriting from ModelClass defined in the model.py"
|
145
108
|
)
|
146
109
|
model_class = classes[0]
|
147
|
-
return model_class
|
148
|
-
|
149
|
-
def _is_standard_or_clarifai(self, name):
|
150
|
-
"""Check if import is from standard library or clarifai"""
|
151
|
-
if name.startswith("clarifai"):
|
152
|
-
return True
|
153
|
-
|
154
|
-
# Handle Python <3.10 compatibility
|
155
|
-
stdlib_names = getattr(sys, "stdlib_module_names", sys.builtin_module_names)
|
156
|
-
if name in stdlib_names:
|
157
|
-
return True
|
158
110
|
|
159
|
-
#
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
return False
|
111
|
+
# initialize the model
|
112
|
+
model = model_class()
|
113
|
+
if load_model:
|
114
|
+
model.load_model()
|
115
|
+
return model
|
165
116
|
|
166
117
|
def _validate_folder(self, folder):
|
167
118
|
if folder == ".":
|
@@ -281,7 +232,7 @@ class ModelBuilder:
|
|
281
232
|
f"`num_threads` must be an integer greater than or equal to 1. Received type {type(num_threads)} with value {num_threads}."
|
282
233
|
)
|
283
234
|
else:
|
284
|
-
num_threads = int(os.environ.get("CLARIFAI_NUM_THREADS",
|
235
|
+
num_threads = int(os.environ.get("CLARIFAI_NUM_THREADS", 16))
|
285
236
|
self.config["num_threads"] = num_threads
|
286
237
|
|
287
238
|
@staticmethod
|
@@ -302,24 +253,6 @@ class ModelBuilder:
|
|
302
253
|
total_size += member.size
|
303
254
|
return total_size
|
304
255
|
|
305
|
-
def method_signatures_yaml(self):
|
306
|
-
"""
|
307
|
-
Returns the method signatures for the model class in YAML format.
|
308
|
-
"""
|
309
|
-
model_class = self.load_model_class()
|
310
|
-
method_info = model_class._get_method_info()
|
311
|
-
signatures = {name: m.signature for name, m in method_info.values()}
|
312
|
-
return signatures_to_yaml(signatures)
|
313
|
-
|
314
|
-
def get_method_signatures(self):
|
315
|
-
"""
|
316
|
-
Returns the method signatures for the model class.
|
317
|
-
"""
|
318
|
-
model_class = self.load_model_class()
|
319
|
-
method_info = model_class._get_method_info()
|
320
|
-
signatures = [method.signature for method in method_info.values()]
|
321
|
-
return signatures
|
322
|
-
|
323
256
|
@property
|
324
257
|
def client(self):
|
325
258
|
if self._client is None:
|
@@ -353,8 +286,9 @@ class ModelBuilder:
|
|
353
286
|
|
354
287
|
assert "model_type_id" in model, "model_type_id not found in the config file"
|
355
288
|
assert "id" in model, "model_id not found in the config file"
|
356
|
-
|
357
|
-
|
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"
|
358
292
|
|
359
293
|
model_proto = json_format.ParseDict(model, resources_pb2.Model())
|
360
294
|
|
@@ -598,11 +532,10 @@ class ModelBuilder:
|
|
598
532
|
logger.info(f"Updated config.yaml with {len(concepts)} concepts.")
|
599
533
|
|
600
534
|
def get_model_version_proto(self):
|
601
|
-
|
535
|
+
|
602
536
|
model_version_proto = resources_pb2.ModelVersion(
|
603
537
|
pretrained_model_config=resources_pb2.PretrainedModelConfig(),
|
604
538
|
inference_compute_info=self.inference_compute_info,
|
605
|
-
method_signatures=signatures,
|
606
539
|
)
|
607
540
|
|
608
541
|
model_type_id = self.config.get('model').get('model_type_id')
|
@@ -790,7 +723,6 @@ class ModelBuilder:
|
|
790
723
|
model_id=self.model_proto.id,
|
791
724
|
version_id=self.model_version_id,
|
792
725
|
))
|
793
|
-
|
794
726
|
status_code = resp.model_version.status.code
|
795
727
|
logs = self.get_model_build_logs()
|
796
728
|
for log_entry in logs.log_entries:
|
@@ -1,292 +1,41 @@
|
|
1
|
-
import
|
2
|
-
import
|
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
|
10
|
-
from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
|
11
|
-
from google.protobuf import json_format
|
12
|
-
|
13
|
-
from clarifai.runners.utils import data_types
|
14
|
-
from clarifai.runners.utils.method_signatures import (build_function_signature, deserialize,
|
15
|
-
get_stream_from_signature, serialize,
|
16
|
-
signatures_to_json)
|
17
|
-
|
18
|
-
_METHOD_INFO_ATTR = '_cf_method_info'
|
19
|
-
|
20
|
-
_RAISE_EXCEPTIONS = os.getenv("RAISE_EXCEPTIONS", "false").lower() in ("true", "1")
|
4
|
+
from clarifai_grpc.grpc.api import service_pb2
|
21
5
|
|
22
6
|
|
23
7
|
class ModelClass(ABC):
|
24
|
-
'''
|
25
|
-
Base class for model classes that can be run as a service.
|
26
|
-
|
27
|
-
Define predict, generate, or stream methods using the @ModelClass.method decorator.
|
28
|
-
|
29
|
-
Example:
|
30
|
-
|
31
|
-
from clarifai.runners.model_class import ModelClass
|
32
|
-
from clarifai.runners.utils.data_types import NamedFields, Stream
|
33
|
-
|
34
|
-
class MyModel(ModelClass):
|
35
|
-
|
36
|
-
@ModelClass.method
|
37
|
-
def predict(self, x: str, y: int) -> List[str]:
|
38
|
-
return [x] * y
|
39
|
-
|
40
|
-
@ModelClass.method
|
41
|
-
def generate(self, x: str, y: int) -> Stream[str]:
|
42
|
-
for i in range(y):
|
43
|
-
yield x + str(i)
|
44
|
-
|
45
|
-
@ModelClass.method
|
46
|
-
def stream(self, input_stream: Stream[NamedFields(x=str, y=int)]) -> Stream[str]:
|
47
|
-
for item in input_stream:
|
48
|
-
yield item.x + ' ' + str(item.y)
|
49
|
-
'''
|
50
|
-
|
51
|
-
@staticmethod
|
52
|
-
def method(func):
|
53
|
-
setattr(func, _METHOD_INFO_ATTR, _MethodInfo(func))
|
54
|
-
return func
|
55
|
-
|
56
|
-
def load_model(self):
|
57
|
-
"""Load the model."""
|
58
|
-
|
59
|
-
def _handle_get_signatures_request(self) -> service_pb2.MultiOutputResponse:
|
60
|
-
methods = self._get_method_info()
|
61
|
-
signatures = {method.name: method.signature for method in methods.values()}
|
62
|
-
resp = service_pb2.MultiOutputResponse(status=status_pb2.Status(code=status_code_pb2.SUCCESS))
|
63
|
-
output = resp.outputs.add()
|
64
|
-
output.status.code = status_code_pb2.SUCCESS
|
65
|
-
output.data.text.raw = signatures_to_json(signatures)
|
66
|
-
return resp
|
67
|
-
|
68
|
-
def _batch_predict(self, method, inputs: List[Dict[str, Any]]) -> List[Any]:
|
69
|
-
"""Batch predict method for multiple inputs."""
|
70
|
-
outputs = []
|
71
|
-
for input in inputs:
|
72
|
-
output = method(**input)
|
73
|
-
outputs.append(output)
|
74
|
-
return outputs
|
75
|
-
|
76
|
-
def _batch_generate(self, method, inputs: List[Dict[str, Any]]) -> Iterator[List[Any]]:
|
77
|
-
"""Batch generate method for multiple inputs."""
|
78
|
-
generators = [method(**input) for input in inputs]
|
79
|
-
for outputs in itertools.zip_longest(*generators):
|
80
|
-
yield outputs
|
81
8
|
|
82
9
|
def predict_wrapper(
|
83
10
|
self, request: service_pb2.PostModelOutputsRequest) -> service_pb2.MultiOutputResponse:
|
84
|
-
|
85
|
-
|
86
|
-
# TODO add method name field to proto
|
87
|
-
method_name = 'predict'
|
88
|
-
inference_params = get_inference_params(request)
|
89
|
-
if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
|
90
|
-
method_name = request.inputs[0].data.metadata['_method_name']
|
91
|
-
if method_name == '_GET_SIGNATURES': # special case to fetch signatures, TODO add endpoint for this
|
92
|
-
return self._handle_get_signatures_request()
|
93
|
-
if method_name not in self._get_method_info():
|
94
|
-
raise ValueError(f"Method {method_name} not found in model class")
|
95
|
-
method = getattr(self, method_name)
|
96
|
-
method_info = method._cf_method_info
|
97
|
-
signature = method_info.signature
|
98
|
-
python_param_types = method_info.python_param_types
|
99
|
-
inputs = self._convert_input_protos_to_python(request.inputs, inference_params,
|
100
|
-
signature.input_fields, python_param_types)
|
101
|
-
if len(inputs) == 1:
|
102
|
-
inputs = inputs[0]
|
103
|
-
output = method(**inputs)
|
104
|
-
outputs.append(self._convert_output_to_proto(output, signature.output_fields))
|
105
|
-
else:
|
106
|
-
outputs = self._batch_predict(method, inputs)
|
107
|
-
outputs = [
|
108
|
-
self._convert_output_to_proto(output, signature.output_fields) for output in outputs
|
109
|
-
]
|
110
|
-
|
111
|
-
return service_pb2.MultiOutputResponse(
|
112
|
-
outputs=outputs, status=status_pb2.Status(code=status_code_pb2.SUCCESS))
|
113
|
-
except Exception as e:
|
114
|
-
if _RAISE_EXCEPTIONS:
|
115
|
-
raise
|
116
|
-
logging.exception("Error in predict")
|
117
|
-
return service_pb2.MultiOutputResponse(status=status_pb2.Status(
|
118
|
-
code=status_code_pb2.FAILURE,
|
119
|
-
details=str(e),
|
120
|
-
stack_trace=traceback.format_exc().split('\n')))
|
11
|
+
"""This method is used for input/output proto data conversion"""
|
12
|
+
return self.predict(request)
|
121
13
|
|
122
14
|
def generate_wrapper(self, request: service_pb2.PostModelOutputsRequest
|
123
15
|
) -> Iterator[service_pb2.MultiOutputResponse]:
|
124
|
-
|
125
|
-
|
126
|
-
inference_params = get_inference_params(request)
|
127
|
-
if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
|
128
|
-
method_name = request.inputs[0].data.metadata['_method_name']
|
129
|
-
method = getattr(self, method_name)
|
130
|
-
method_info = method._cf_method_info
|
131
|
-
signature = method_info.signature
|
132
|
-
python_param_types = method_info.python_param_types
|
16
|
+
"""This method is used for input/output proto data conversion and yield outcome"""
|
17
|
+
return self.generate(request)
|
133
18
|
|
134
|
-
|
135
|
-
signature.input_fields, python_param_types)
|
136
|
-
if len(inputs) == 1:
|
137
|
-
inputs = inputs[0]
|
138
|
-
for output in method(**inputs):
|
139
|
-
resp = service_pb2.MultiOutputResponse()
|
140
|
-
self._convert_output_to_proto(output, signature.output_fields, proto=resp.outputs.add())
|
141
|
-
resp.status.code = status_code_pb2.SUCCESS
|
142
|
-
yield resp
|
143
|
-
else:
|
144
|
-
for outputs in self._batch_generate(method, inputs):
|
145
|
-
resp = service_pb2.MultiOutputResponse()
|
146
|
-
for output in outputs:
|
147
|
-
self._convert_output_to_proto(
|
148
|
-
output, signature.output_fields, proto=resp.outputs.add())
|
149
|
-
resp.status.code = status_code_pb2.SUCCESS
|
150
|
-
yield resp
|
151
|
-
except Exception as e:
|
152
|
-
if _RAISE_EXCEPTIONS:
|
153
|
-
raise
|
154
|
-
logging.exception("Error in generate")
|
155
|
-
yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
|
156
|
-
code=status_code_pb2.FAILURE,
|
157
|
-
details=str(e),
|
158
|
-
stack_trace=traceback.format_exc().split('\n')))
|
159
|
-
|
160
|
-
def stream_wrapper(self, request_iterator: Iterator[service_pb2.PostModelOutputsRequest]
|
19
|
+
def stream_wrapper(self, request: service_pb2.PostModelOutputsRequest
|
161
20
|
) -> Iterator[service_pb2.MultiOutputResponse]:
|
162
|
-
|
163
|
-
|
164
|
-
assert len(request.inputs) == 1, "Streaming requires exactly one input"
|
165
|
-
|
166
|
-
method_name = 'generate'
|
167
|
-
inference_params = get_inference_params(request)
|
168
|
-
if len(request.inputs) > 0 and '_method_name' in request.inputs[0].data.metadata:
|
169
|
-
method_name = request.inputs[0].data.metadata['_method_name']
|
170
|
-
method = getattr(self, method_name)
|
171
|
-
method_info = method._cf_method_info
|
172
|
-
signature = method_info.signature
|
173
|
-
python_param_types = method_info.python_param_types
|
174
|
-
|
175
|
-
# find the streaming vars in the signature
|
176
|
-
stream_sig = get_stream_from_signature(signature.input_fields)
|
177
|
-
if stream_sig is None:
|
178
|
-
raise ValueError("Streaming method must have a Stream input")
|
179
|
-
stream_argname = stream_sig.name
|
180
|
-
|
181
|
-
# convert all inputs for the first request, including the first stream value
|
182
|
-
inputs = self._convert_input_protos_to_python(request.inputs, inference_params,
|
183
|
-
signature.input_fields, python_param_types)
|
184
|
-
kwargs = inputs[0]
|
185
|
-
|
186
|
-
# first streaming item
|
187
|
-
first_item = kwargs.pop(stream_argname)
|
188
|
-
|
189
|
-
# streaming generator
|
190
|
-
def InputStream():
|
191
|
-
yield first_item
|
192
|
-
# subsequent streaming items contain only the streaming input
|
193
|
-
for request in request_iterator:
|
194
|
-
item = self._convert_input_protos_to_python(request.inputs, inference_params,
|
195
|
-
[stream_sig], python_param_types)
|
196
|
-
item = item[0][stream_argname]
|
197
|
-
yield item
|
198
|
-
|
199
|
-
# add stream generator back to the input kwargs
|
200
|
-
kwargs[stream_argname] = InputStream()
|
201
|
-
|
202
|
-
for output in method(**kwargs):
|
203
|
-
resp = service_pb2.MultiOutputResponse()
|
204
|
-
self._convert_output_to_proto(output, signature.output_fields, proto=resp.outputs.add())
|
205
|
-
resp.status.code = status_code_pb2.SUCCESS
|
206
|
-
yield resp
|
207
|
-
except Exception as e:
|
208
|
-
if _RAISE_EXCEPTIONS:
|
209
|
-
raise
|
210
|
-
logging.exception("Error in stream")
|
211
|
-
yield service_pb2.MultiOutputResponse(status=status_pb2.Status(
|
212
|
-
code=status_code_pb2.FAILURE,
|
213
|
-
details=str(e),
|
214
|
-
stack_trace=traceback.format_exc().split('\n')))
|
215
|
-
|
216
|
-
def _convert_input_protos_to_python(self, inputs: List[resources_pb2.Input],
|
217
|
-
inference_params: dict,
|
218
|
-
variables_signature: List[resources_pb2.ModelTypeField],
|
219
|
-
python_param_types) -> List[Dict[str, Any]]:
|
220
|
-
result = []
|
221
|
-
for input in inputs:
|
222
|
-
kwargs = deserialize(input.data, variables_signature, inference_params)
|
223
|
-
# dynamic cast to annotated types
|
224
|
-
for k, v in kwargs.items():
|
225
|
-
if k not in python_param_types:
|
226
|
-
continue
|
227
|
-
kwargs[k] = data_types.cast(v, python_param_types[k])
|
228
|
-
result.append(kwargs)
|
229
|
-
return result
|
21
|
+
"""This method is used for input/output proto data conversion and yield outcome"""
|
22
|
+
return self.stream(request)
|
230
23
|
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
continue
|
250
|
-
methods[name] = method_info
|
251
|
-
# check for generic predict(request) -> response, etc. methods
|
252
|
-
#for name in ('predict', 'generate', 'stream'):
|
253
|
-
# if hasattr(cls, name):
|
254
|
-
# method = getattr(cls, name)
|
255
|
-
# if not hasattr(method, _METHOD_INFO_ATTR): # not already put in registry
|
256
|
-
# methods[name] = _MethodInfo(method)
|
257
|
-
# set method table for this class in the registry
|
258
|
-
return methods
|
259
|
-
|
260
|
-
@classmethod
|
261
|
-
def _get_method_info(cls, func_name=None):
|
262
|
-
if not hasattr(cls, _METHOD_INFO_ATTR):
|
263
|
-
setattr(cls, _METHOD_INFO_ATTR, cls._register_model_methods())
|
264
|
-
method_info = getattr(cls, _METHOD_INFO_ATTR)
|
265
|
-
if func_name:
|
266
|
-
return method_info[func_name]
|
267
|
-
return method_info
|
268
|
-
|
269
|
-
|
270
|
-
# Helper function to get the inference params
|
271
|
-
def get_inference_params(request) -> dict:
|
272
|
-
"""Get the inference params from the request."""
|
273
|
-
inference_params = {}
|
274
|
-
if request.model.model_version.id != "":
|
275
|
-
output_info = request.model.model_version.output_info
|
276
|
-
output_info = json_format.MessageToDict(output_info, preserving_proto_field_name=True)
|
277
|
-
if "params" in output_info:
|
278
|
-
inference_params = output_info["params"]
|
279
|
-
return inference_params
|
280
|
-
|
281
|
-
|
282
|
-
class _MethodInfo:
|
283
|
-
|
284
|
-
def __init__(self, method):
|
285
|
-
self.name = method.__name__
|
286
|
-
self.signature = build_function_signature(method)
|
287
|
-
self.python_param_types = {
|
288
|
-
p.name: p.annotation
|
289
|
-
for p in inspect.signature(method).parameters.values()
|
290
|
-
if p.annotation != inspect.Parameter.empty
|
291
|
-
}
|
292
|
-
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
|
-
#
|
116
|
-
|
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
|
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(
|