clarifai 11.6.4__py3-none-any.whl → 11.6.4rc2__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/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/__pycache__/errors.cpython-311.pyc +0 -0
- clarifai/__pycache__/errors.cpython-39.pyc +0 -0
- clarifai/__pycache__/versions.cpython-311.pyc +0 -0
- clarifai/__pycache__/versions.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-39.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-39.pyc +0 -0
- clarifai/cli/base.py +11 -27
- clarifai/cli/model.py +171 -41
- clarifai/cli/model_templates.py +243 -0
- clarifai/cli/pipeline_step_templates.py +64 -0
- clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/model_client.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/model_client.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/runner.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-39.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-39.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-39.pyc +0 -0
- clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/base.cpython-39.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-39.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-39.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-39.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-39.pyc +0 -0
- clarifai/models/model_serving/README.md +158 -0
- clarifai/models/model_serving/__init__.py +14 -0
- clarifai/models/model_serving/cli/__init__.py +12 -0
- clarifai/models/model_serving/cli/_utils.py +53 -0
- clarifai/models/model_serving/cli/base.py +14 -0
- clarifai/models/model_serving/cli/build.py +79 -0
- clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
- clarifai/models/model_serving/cli/create.py +171 -0
- clarifai/models/model_serving/cli/example_cli.py +34 -0
- clarifai/models/model_serving/cli/login.py +26 -0
- clarifai/models/model_serving/cli/upload.py +179 -0
- clarifai/models/model_serving/constants.py +21 -0
- clarifai/models/model_serving/docs/cli.md +161 -0
- clarifai/models/model_serving/docs/concepts.md +229 -0
- clarifai/models/model_serving/docs/dependencies.md +11 -0
- clarifai/models/model_serving/docs/inference_parameters.md +139 -0
- clarifai/models/model_serving/docs/model_types.md +19 -0
- clarifai/models/model_serving/model_config/__init__.py +16 -0
- clarifai/models/model_serving/model_config/base.py +369 -0
- clarifai/models/model_serving/model_config/config.py +312 -0
- clarifai/models/model_serving/model_config/inference_parameter.py +129 -0
- clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -0
- clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -0
- clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -0
- clarifai/models/model_serving/model_config/output.py +133 -0
- clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
- clarifai/models/model_serving/model_config/triton/serializer.py +136 -0
- clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
- clarifai/models/model_serving/model_config/triton/wrappers.py +281 -0
- clarifai/models/model_serving/repo_build/__init__.py +14 -0
- clarifai/models/model_serving/repo_build/build.py +198 -0
- clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
- clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
- clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
- clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
- clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
- clarifai/models/model_serving/repo_build/static_files/triton/model.py +75 -0
- clarifai/models/model_serving/utils.py +23 -0
- clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/mcp_class.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/openai_class.cpython-311.pyc +0 -0
- clarifai/runners/models/base_typed_model.py +238 -0
- clarifai/runners/models/model_upload.py +607 -0
- clarifai/runners/server.py +1 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/code_script.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/code_script.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/method_signatures.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/model_utils.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/openai_convertor.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/serializers.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
- clarifai/runners/utils/code_script.py +52 -46
- clarifai/runners/utils/data_handler.py +231 -0
- clarifai/runners/utils/data_types/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/utils/data_types/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/utils/data_types/__pycache__/data_types.cpython-311.pyc +0 -0
- clarifai/runners/utils/data_types/__pycache__/data_types.cpython-39.pyc +0 -0
- clarifai/runners/utils/data_types.py +471 -0
- clarifai/runners/utils/temp.py +59 -0
- clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/config.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/protobuf.cpython-39.pyc +0 -0
- clarifai/utils/cli.py +14 -15
- clarifai/utils/constants.py +2 -0
- clarifai/utils/misc.py +382 -1
- clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
- {clarifai-11.6.4.dist-info → clarifai-11.6.4rc2.dist-info}/METADATA +1 -1
- clarifai-11.6.4rc2.dist-info/RECORD +301 -0
- {clarifai-11.6.4.dist-info → clarifai-11.6.4rc2.dist-info}/WHEEL +1 -1
- clarifai-11.6.4.dist-info/RECORD +0 -127
- {clarifai-11.6.4.dist-info → clarifai-11.6.4rc2.dist-info}/entry_points.txt +0 -0
- {clarifai-11.6.4.dist-info → clarifai-11.6.4rc2.dist-info}/licenses/LICENSE +0 -0
- {clarifai-11.6.4.dist-info → clarifai-11.6.4rc2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,471 @@
|
|
1
|
+
import io
|
2
|
+
import json
|
3
|
+
from typing import Iterable, List, get_args, get_origin
|
4
|
+
|
5
|
+
import numpy as np
|
6
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Audio as AudioProto
|
7
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Concept as ConceptProto
|
8
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Frame as FrameProto
|
9
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Image as ImageProto
|
10
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Region as RegionProto
|
11
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Text as TextProto
|
12
|
+
from clarifai_grpc.grpc.api.resources_pb2 import Video as VideoProto
|
13
|
+
from PIL import Image as PILImage
|
14
|
+
|
15
|
+
|
16
|
+
class MessageData:
|
17
|
+
def to_proto(self):
|
18
|
+
raise NotImplementedError
|
19
|
+
|
20
|
+
@classmethod
|
21
|
+
def from_proto(cls, proto):
|
22
|
+
raise NotImplementedError
|
23
|
+
|
24
|
+
@classmethod
|
25
|
+
def from_value(cls, value):
|
26
|
+
if isinstance(value, cls):
|
27
|
+
return value
|
28
|
+
return cls(value)
|
29
|
+
|
30
|
+
def cast(self, python_type):
|
31
|
+
if python_type is self.__class__:
|
32
|
+
return self
|
33
|
+
raise TypeError(f'Incompatible type for {self.__class__.__name__}: {python_type}')
|
34
|
+
|
35
|
+
|
36
|
+
class NamedFieldsMeta(type):
|
37
|
+
"""Metaclass to create NamedFields subclasses with __annotations__ when fields are specified."""
|
38
|
+
|
39
|
+
def __call__(cls, *args, **kwargs):
|
40
|
+
# Check if keyword arguments are types (used in type annotations)
|
41
|
+
if kwargs and all(isinstance(v, type) for v in kwargs.values()):
|
42
|
+
# Dynamically create a subclass with __annotations__
|
43
|
+
name = f"NamedFields({', '.join(f'{k}:{v.__name__}' for k, v in kwargs.items())})"
|
44
|
+
return type(name, (cls,), {'__annotations__': kwargs})
|
45
|
+
else:
|
46
|
+
# Create a normal instance for runtime data
|
47
|
+
return super().__call__(*args, **kwargs)
|
48
|
+
|
49
|
+
|
50
|
+
class NamedFields(metaclass=NamedFieldsMeta):
|
51
|
+
"""A class that can be used to store named fields with values."""
|
52
|
+
|
53
|
+
def __init__(self, **kwargs):
|
54
|
+
for key, value in kwargs.items():
|
55
|
+
setattr(self, key, value)
|
56
|
+
|
57
|
+
def items(self):
|
58
|
+
return self.__dict__.items()
|
59
|
+
|
60
|
+
def keys(self):
|
61
|
+
return self.__dict__.keys()
|
62
|
+
|
63
|
+
def values(self):
|
64
|
+
return self.__dict__.values()
|
65
|
+
|
66
|
+
def __contains__(self, key):
|
67
|
+
return key in self.__dict__
|
68
|
+
|
69
|
+
def __getitem__(self, key):
|
70
|
+
return getattr(self, key)
|
71
|
+
|
72
|
+
def __setitem__(self, key, value):
|
73
|
+
setattr(self, key, value)
|
74
|
+
|
75
|
+
def __repr__(self):
|
76
|
+
return f"{self.__class__.__name__}({', '.join(f'{key}={value!r}' for key, value in self.__dict__.items())})"
|
77
|
+
|
78
|
+
def __origin__(self):
|
79
|
+
return self
|
80
|
+
|
81
|
+
def __args__(self):
|
82
|
+
return list(self.keys())
|
83
|
+
|
84
|
+
|
85
|
+
class JSON:
|
86
|
+
def __init__(self, value):
|
87
|
+
self.value = value
|
88
|
+
|
89
|
+
def __eq__(self, other):
|
90
|
+
return self.value == other
|
91
|
+
|
92
|
+
def __bool__(self):
|
93
|
+
return bool(self.value)
|
94
|
+
|
95
|
+
def __repr__(self):
|
96
|
+
return f"JSON({self.value})"
|
97
|
+
|
98
|
+
def to_json(self):
|
99
|
+
return json.dumps(self.value)
|
100
|
+
|
101
|
+
@classmethod
|
102
|
+
def from_json(cls, json_str):
|
103
|
+
return cls(json.loads(json_str))
|
104
|
+
|
105
|
+
@classmethod
|
106
|
+
def from_value(cls, value):
|
107
|
+
return cls(value)
|
108
|
+
|
109
|
+
def cast(self, python_type):
|
110
|
+
if not isinstance(self.value, python_type):
|
111
|
+
raise TypeError(f'Incompatible type {type(self.value)} for {python_type}')
|
112
|
+
return self.value
|
113
|
+
|
114
|
+
|
115
|
+
class Text(MessageData):
|
116
|
+
def __init__(self, text: str, url: str = None):
|
117
|
+
self.text = text
|
118
|
+
self.url = url
|
119
|
+
|
120
|
+
def __eq__(self, other):
|
121
|
+
if isinstance(other, Text):
|
122
|
+
return self.text == other.text and self.url == other.url
|
123
|
+
if isinstance(other, str):
|
124
|
+
return self.text == other
|
125
|
+
return False
|
126
|
+
|
127
|
+
def __bool__(self):
|
128
|
+
return bool(self.text) or bool(self.url)
|
129
|
+
|
130
|
+
def __repr__(self) -> str:
|
131
|
+
return f"Text(text={self.text!r}, url={self.url!r})"
|
132
|
+
|
133
|
+
def to_proto(self) -> TextProto:
|
134
|
+
return TextProto(raw=self.text or '', url=self.url or '')
|
135
|
+
|
136
|
+
@classmethod
|
137
|
+
def from_proto(cls, proto: TextProto) -> "Text":
|
138
|
+
return cls(proto.raw, proto.url or None)
|
139
|
+
|
140
|
+
@classmethod
|
141
|
+
def from_value(cls, value):
|
142
|
+
if isinstance(value, str):
|
143
|
+
return cls(value)
|
144
|
+
if isinstance(value, Text):
|
145
|
+
return value
|
146
|
+
if isinstance(value, dict):
|
147
|
+
return cls(value.get('text'), value.get('url'))
|
148
|
+
raise TypeError(f'Incompatible type for Text: {type(value)}')
|
149
|
+
|
150
|
+
def cast(self, python_type):
|
151
|
+
if python_type is str:
|
152
|
+
return self.text
|
153
|
+
if python_type is Text:
|
154
|
+
return self
|
155
|
+
raise TypeError(f'Incompatible type for Text: {python_type}')
|
156
|
+
|
157
|
+
|
158
|
+
class Concept(MessageData):
|
159
|
+
def __init__(self, name: str, value: float = 1):
|
160
|
+
self.id = name
|
161
|
+
self.name = name
|
162
|
+
self.value = value
|
163
|
+
|
164
|
+
def __repr__(self) -> str:
|
165
|
+
return f"Concept(id={self.id!r}, name={self.name!r}, value={self.value})"
|
166
|
+
|
167
|
+
def to_proto(self):
|
168
|
+
return ConceptProto(id=self.id, name=self.name, value=self.value)
|
169
|
+
|
170
|
+
@classmethod
|
171
|
+
def from_proto(cls, proto: ConceptProto) -> "Concept":
|
172
|
+
return cls(proto.name, proto.value)
|
173
|
+
|
174
|
+
|
175
|
+
class Region(MessageData):
|
176
|
+
def __init__(
|
177
|
+
self,
|
178
|
+
proto_region: RegionProto = None,
|
179
|
+
box: List[float] = None,
|
180
|
+
concepts: List[Concept] = None,
|
181
|
+
):
|
182
|
+
if proto_region is None:
|
183
|
+
proto_region = RegionProto()
|
184
|
+
self.proto = proto_region
|
185
|
+
# use setters for init vals
|
186
|
+
if (
|
187
|
+
box
|
188
|
+
and isinstance(box, list)
|
189
|
+
and len(box) == 4
|
190
|
+
and all(isinstance(val, (int, float)) for val in box)
|
191
|
+
):
|
192
|
+
self.box = box
|
193
|
+
if (
|
194
|
+
concepts
|
195
|
+
and isinstance(concepts, list)
|
196
|
+
and all(isinstance(concept, Concept) for concept in concepts)
|
197
|
+
):
|
198
|
+
self.concepts = concepts
|
199
|
+
|
200
|
+
@property
|
201
|
+
def box(self) -> List[float]:
|
202
|
+
bbox = self.proto.region_info.bounding_box
|
203
|
+
return [bbox.left_col, bbox.top_row, bbox.right_col, bbox.bottom_row] # x1, y1, x2, y2
|
204
|
+
|
205
|
+
@box.setter
|
206
|
+
def box(self, value: List[float]):
|
207
|
+
bbox = self.proto.region_info.bounding_box
|
208
|
+
bbox.left_col, bbox.top_row, bbox.right_col, bbox.bottom_row = value
|
209
|
+
|
210
|
+
@property
|
211
|
+
def concepts(self) -> List[Concept]:
|
212
|
+
return [Concept.from_proto(proto) for proto in self.proto.data.concepts]
|
213
|
+
|
214
|
+
@concepts.setter
|
215
|
+
def concepts(self, value: List[Concept]):
|
216
|
+
self.proto.data.concepts.extend([concept.to_proto() for concept in value])
|
217
|
+
|
218
|
+
def __repr__(self) -> str:
|
219
|
+
return f"Region(box={self.box}, concepts={self.concepts})"
|
220
|
+
|
221
|
+
def to_proto(self) -> RegionProto:
|
222
|
+
return self.proto
|
223
|
+
|
224
|
+
@classmethod
|
225
|
+
def from_proto(cls, proto: RegionProto) -> "Region":
|
226
|
+
return cls(proto)
|
227
|
+
|
228
|
+
|
229
|
+
class Image(MessageData):
|
230
|
+
def __init__(self, proto_image: ImageProto = None, url: str = None, bytes: bytes = None):
|
231
|
+
if proto_image is None:
|
232
|
+
proto_image = ImageProto()
|
233
|
+
self.proto = proto_image
|
234
|
+
# use setters for init vals
|
235
|
+
if url:
|
236
|
+
self.url = url
|
237
|
+
if bytes:
|
238
|
+
self.bytes = bytes
|
239
|
+
|
240
|
+
@property
|
241
|
+
def url(self) -> str:
|
242
|
+
return self.proto.url
|
243
|
+
|
244
|
+
@url.setter
|
245
|
+
def url(self, value: str):
|
246
|
+
self.proto.url = value
|
247
|
+
|
248
|
+
@property
|
249
|
+
def bytes(self) -> bytes:
|
250
|
+
return self.proto.base64
|
251
|
+
|
252
|
+
@bytes.setter
|
253
|
+
def bytes(self, value: bytes):
|
254
|
+
self.proto.base64 = value
|
255
|
+
|
256
|
+
def __repr__(self) -> str:
|
257
|
+
attrs = []
|
258
|
+
if self.url:
|
259
|
+
attrs.append(f"url={self.url!r}")
|
260
|
+
if self.bytes:
|
261
|
+
attrs.append(f"bytes=<{len(self.bytes)} bytes>")
|
262
|
+
return f"Image({', '.join(attrs)})"
|
263
|
+
|
264
|
+
@classmethod
|
265
|
+
def from_url(cls, url: str) -> "Image":
|
266
|
+
proto_image = ImageProto(url=url)
|
267
|
+
return cls(proto_image)
|
268
|
+
|
269
|
+
@classmethod
|
270
|
+
def from_pil(cls, pil_image: PILImage.Image) -> "Image":
|
271
|
+
with io.BytesIO() as output:
|
272
|
+
pil_image.save(output, format="PNG")
|
273
|
+
image_bytes = output.getvalue()
|
274
|
+
proto_image = ImageProto(base64=image_bytes)
|
275
|
+
return cls(proto_image)
|
276
|
+
|
277
|
+
def to_pil(self) -> PILImage.Image:
|
278
|
+
if not self.proto.base64:
|
279
|
+
raise ValueError("Image has no bytes")
|
280
|
+
return PILImage.open(io.BytesIO(self.proto.base64))
|
281
|
+
|
282
|
+
def to_numpy(self) -> np.ndarray:
|
283
|
+
return np.asarray(self.to_pil())
|
284
|
+
|
285
|
+
def to_proto(self) -> ImageProto:
|
286
|
+
return self.proto
|
287
|
+
|
288
|
+
@classmethod
|
289
|
+
def from_proto(cls, proto: ImageProto) -> "Image":
|
290
|
+
return cls(proto)
|
291
|
+
|
292
|
+
@classmethod
|
293
|
+
def from_value(cls, value):
|
294
|
+
if isinstance(value, PILImage.Image):
|
295
|
+
return cls.from_pil(value)
|
296
|
+
if isinstance(value, Image):
|
297
|
+
return value
|
298
|
+
raise TypeError(f'Incompatible type for Image: {type(value)}')
|
299
|
+
|
300
|
+
def cast(self, python_type):
|
301
|
+
if python_type is Image:
|
302
|
+
return self
|
303
|
+
if python_type is PILImage.Image or python_type is PILImage:
|
304
|
+
return self.to_pil()
|
305
|
+
if python_type is np.ndarray or get_origin(python_type) is np.ndarray:
|
306
|
+
return self.to_numpy()
|
307
|
+
raise TypeError(f'Incompatible type for Image: {python_type}')
|
308
|
+
|
309
|
+
|
310
|
+
class Audio(MessageData):
|
311
|
+
def __init__(self, proto_audio: AudioProto = None, url: str = None, bytes: bytes = None):
|
312
|
+
if proto_audio is None:
|
313
|
+
proto_audio = AudioProto()
|
314
|
+
self.proto = proto_audio
|
315
|
+
|
316
|
+
if url:
|
317
|
+
self.url = url
|
318
|
+
if bytes:
|
319
|
+
self.bytes = bytes
|
320
|
+
|
321
|
+
@property
|
322
|
+
def url(self) -> str:
|
323
|
+
return self.proto.url
|
324
|
+
|
325
|
+
@url.setter
|
326
|
+
def url(self, value: str):
|
327
|
+
self.proto.url = value
|
328
|
+
|
329
|
+
@property
|
330
|
+
def bytes(self) -> bytes:
|
331
|
+
return self.proto.base64
|
332
|
+
|
333
|
+
@bytes.setter
|
334
|
+
def bytes(self, value: bytes):
|
335
|
+
self.proto.base64 = value
|
336
|
+
|
337
|
+
@classmethod
|
338
|
+
def from_url(cls, url: str) -> "Audio":
|
339
|
+
proto_audio = AudioProto(url=url)
|
340
|
+
return cls(proto_audio)
|
341
|
+
|
342
|
+
def __repr__(self) -> str:
|
343
|
+
attrs = []
|
344
|
+
if self.url:
|
345
|
+
attrs.append(f"url={self.url!r}")
|
346
|
+
if self.bytes:
|
347
|
+
attrs.append(f"bytes=<{len(self.bytes)} bytes>")
|
348
|
+
return f"Audio({', '.join(attrs)})"
|
349
|
+
|
350
|
+
def to_proto(self) -> AudioProto:
|
351
|
+
return self.proto
|
352
|
+
|
353
|
+
@classmethod
|
354
|
+
def from_proto(cls, proto: AudioProto) -> "Audio":
|
355
|
+
return cls(proto)
|
356
|
+
|
357
|
+
|
358
|
+
class Frame(MessageData):
|
359
|
+
def __init__(
|
360
|
+
self,
|
361
|
+
proto_frame: FrameProto = None,
|
362
|
+
image: Image = None,
|
363
|
+
regions: List[Region] = None,
|
364
|
+
time: float = None,
|
365
|
+
):
|
366
|
+
if proto_frame is None:
|
367
|
+
proto_frame = FrameProto()
|
368
|
+
self.proto = proto_frame
|
369
|
+
# use setters for init vals
|
370
|
+
if image:
|
371
|
+
self.image = image
|
372
|
+
if regions:
|
373
|
+
self.regions = regions
|
374
|
+
if time:
|
375
|
+
self.time = time
|
376
|
+
|
377
|
+
@property
|
378
|
+
def time(self) -> float:
|
379
|
+
# TODO: time is a uint32, so this will overflow at 49.7 days
|
380
|
+
# we should be using double or uint64 in the proto instead
|
381
|
+
return self.proto.frame_info.time / 1000.0
|
382
|
+
|
383
|
+
@time.setter
|
384
|
+
def time(self, value: float):
|
385
|
+
self.proto.frame_info.time = int(value * 1000)
|
386
|
+
|
387
|
+
@property
|
388
|
+
def image(self) -> Image:
|
389
|
+
return Image.from_proto(self.proto.data.image)
|
390
|
+
|
391
|
+
@image.setter
|
392
|
+
def image(self, value: Image):
|
393
|
+
self.proto.data.image.CopyFrom(value.to_proto())
|
394
|
+
|
395
|
+
@property
|
396
|
+
def regions(self) -> List[Region]:
|
397
|
+
return [Region(region) for region in self.proto.data.regions]
|
398
|
+
|
399
|
+
@regions.setter
|
400
|
+
def regions(self, value: List[Region]):
|
401
|
+
self.proto.data.regions.extend([region.proto for region in value])
|
402
|
+
|
403
|
+
def to_proto(self) -> FrameProto:
|
404
|
+
return self.proto
|
405
|
+
|
406
|
+
@classmethod
|
407
|
+
def from_proto(cls, proto: FrameProto) -> "Frame":
|
408
|
+
return cls(proto)
|
409
|
+
|
410
|
+
|
411
|
+
class Video(MessageData):
|
412
|
+
def __init__(self, proto_video: VideoProto = None, url: str = None, bytes: bytes = None):
|
413
|
+
if proto_video is None:
|
414
|
+
proto_video = VideoProto()
|
415
|
+
self.proto = proto_video
|
416
|
+
|
417
|
+
if url:
|
418
|
+
self.url = url
|
419
|
+
if bytes:
|
420
|
+
self.bytes = bytes
|
421
|
+
|
422
|
+
@property
|
423
|
+
def url(self) -> str:
|
424
|
+
return self.proto.url
|
425
|
+
|
426
|
+
@url.setter
|
427
|
+
def url(self, value: str):
|
428
|
+
self.proto.url = value
|
429
|
+
|
430
|
+
@property
|
431
|
+
def bytes(self) -> bytes:
|
432
|
+
return self.proto.base64
|
433
|
+
|
434
|
+
@bytes.setter
|
435
|
+
def bytes(self, value: bytes):
|
436
|
+
self.proto.base64 = value
|
437
|
+
|
438
|
+
@classmethod
|
439
|
+
def from_url(cls, url: str) -> "Video":
|
440
|
+
proto_video = VideoProto(url=url)
|
441
|
+
return cls(proto_video)
|
442
|
+
|
443
|
+
def __repr__(self) -> str:
|
444
|
+
attrs = []
|
445
|
+
if self.url:
|
446
|
+
attrs.append(f"url={self.url!r}")
|
447
|
+
if self.bytes:
|
448
|
+
attrs.append(f"bytes=<{len(self.bytes)} bytes>")
|
449
|
+
return f"Video({', '.join(attrs)})"
|
450
|
+
|
451
|
+
def to_proto(self) -> VideoProto:
|
452
|
+
return self.proto
|
453
|
+
|
454
|
+
@classmethod
|
455
|
+
def from_proto(cls, proto: VideoProto) -> "Video":
|
456
|
+
return cls(proto)
|
457
|
+
|
458
|
+
|
459
|
+
def cast(value, python_type):
|
460
|
+
list_type = get_origin(python_type) is list
|
461
|
+
if isinstance(value, MessageData):
|
462
|
+
return value.cast(python_type)
|
463
|
+
if list_type and isinstance(value, np.ndarray):
|
464
|
+
return value.tolist()
|
465
|
+
if list_type and isinstance(value, list):
|
466
|
+
if get_args(python_type):
|
467
|
+
inner_type = get_args(python_type)[0]
|
468
|
+
return [cast(item, inner_type) for item in value]
|
469
|
+
if not isinstance(value, Iterable):
|
470
|
+
raise TypeError(f'Expected list, got {type(value)}')
|
471
|
+
return value
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import json
|
2
|
+
from clarifai.runners.utils import data_utils
|
3
|
+
from clarifai.runners.models.model_class import ModelClass
|
4
|
+
|
5
|
+
import inspect
|
6
|
+
|
7
|
+
def log_stack():
|
8
|
+
for frame_info in inspect.stack():
|
9
|
+
print(f"{frame_info.filename}:{frame_info.lineno} in {frame_info.function}")
|
10
|
+
|
11
|
+
|
12
|
+
default = 27
|
13
|
+
|
14
|
+
x = json.dumps(default)
|
15
|
+
|
16
|
+
print(x, type(x))
|
17
|
+
|
18
|
+
x = json.loads(x)
|
19
|
+
|
20
|
+
print(x, type(x))
|
21
|
+
|
22
|
+
param = data_utils.Param(default)
|
23
|
+
|
24
|
+
print(param, type(param))
|
25
|
+
|
26
|
+
print(param.default)
|
27
|
+
|
28
|
+
param_json = json.dumps(param)
|
29
|
+
|
30
|
+
print(param_json, type(param_json))
|
31
|
+
|
32
|
+
param_json = json.loads(param_json)
|
33
|
+
|
34
|
+
print(param_json, type(param_json))
|
35
|
+
|
36
|
+
# param_json = json.loads(param_json)
|
37
|
+
|
38
|
+
# print(param_json, type(param_json))
|
39
|
+
|
40
|
+
def test_int(default: int = data_utils.Param(default=27)):
|
41
|
+
print(default, type(default))
|
42
|
+
|
43
|
+
test_int()
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
class Test(ModelClass):
|
50
|
+
|
51
|
+
@ModelClass.method
|
52
|
+
def test(self, max_tokens: int = data_utils.Param(default=27)) -> int:
|
53
|
+
print(max_tokens, type(max_tokens))
|
54
|
+
return max_tokens
|
55
|
+
|
56
|
+
test = Test()
|
57
|
+
|
58
|
+
test.test()
|
59
|
+
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
clarifai/utils/cli.py
CHANGED
@@ -4,12 +4,14 @@ import pkgutil
|
|
4
4
|
import sys
|
5
5
|
import typing as t
|
6
6
|
from collections import defaultdict
|
7
|
-
from typing import OrderedDict
|
7
|
+
from typing import OrderedDict
|
8
8
|
|
9
9
|
import click
|
10
10
|
import yaml
|
11
11
|
from tabulate import tabulate
|
12
12
|
|
13
|
+
from clarifai.utils.logging import logger
|
14
|
+
|
13
15
|
|
14
16
|
def from_yaml(filename: str):
|
15
17
|
try:
|
@@ -174,7 +176,7 @@ def validate_context(ctx):
|
|
174
176
|
sys.exit(1)
|
175
177
|
|
176
178
|
|
177
|
-
def
|
179
|
+
def validate_context_auth(pat: str, user_id: str, api_base: str = None):
|
178
180
|
"""
|
179
181
|
Validate a Personal Access Token (PAT) by making a test API call.
|
180
182
|
|
@@ -182,17 +184,14 @@ def validate_pat_token(pat: str, user_id: str, api_base: str = None) -> Tuple[bo
|
|
182
184
|
pat (str): The Personal Access Token to validate
|
183
185
|
user_id (str): The user ID associated with the token
|
184
186
|
api_base (str): The API base URL. Defaults to None (uses default).
|
185
|
-
|
186
|
-
Returns:
|
187
|
-
tuple[bool, str]: A tuple of (is_valid, error_message)
|
188
|
-
If valid: (True, "")
|
189
|
-
If invalid: (False, error_description)
|
190
187
|
"""
|
191
188
|
try:
|
192
189
|
from clarifai_grpc.grpc.api.status import status_code_pb2
|
193
190
|
|
194
191
|
from clarifai.client.user import User
|
195
192
|
|
193
|
+
logger.info("Validating the Context Credentials...")
|
194
|
+
|
196
195
|
# Create user client for validation
|
197
196
|
if api_base:
|
198
197
|
user_client = User(user_id=user_id, pat=pat, base_url=api_base)
|
@@ -203,21 +202,21 @@ def validate_pat_token(pat: str, user_id: str, api_base: str = None) -> Tuple[bo
|
|
203
202
|
response = user_client.get_user_info()
|
204
203
|
|
205
204
|
if response.status.code == status_code_pb2.SUCCESS:
|
206
|
-
|
207
|
-
else:
|
208
|
-
return False, f"Authentication failed: {response.status.description}"
|
205
|
+
logger.info("✅ Context is valid")
|
209
206
|
|
210
207
|
except Exception as e:
|
211
208
|
error_msg = str(e)
|
212
209
|
|
213
210
|
# Check for common authentication errors and provide user-friendly messages
|
214
211
|
if "PERMISSION_DENIED" in error_msg or "Unauthorized" in error_msg:
|
215
|
-
|
212
|
+
logger.error(f"Invalid PAT token or incorrect user ID '{user_id}': {error_msg}")
|
216
213
|
elif "UNAUTHENTICATED" in error_msg:
|
217
|
-
|
214
|
+
logger.error(f"Invalid PAT token or user ID: {error_msg}")
|
218
215
|
elif "SSL" in error_msg or "certificate" in error_msg:
|
219
|
-
|
216
|
+
logger.error(f"SSL/Certificate error: {error_msg}")
|
220
217
|
elif "Connection" in error_msg or "timeout" in error_msg:
|
221
|
-
|
218
|
+
logger.error(f"Network connection error: {error_msg}")
|
222
219
|
else:
|
223
|
-
|
220
|
+
logger.error(f"❌ Validation failed: \n{error_msg}")
|
221
|
+
logger.error("Please check your credentials and try again.")
|
222
|
+
raise click.Abort() # Exit without saving the configuration
|