clarifai 11.1.5__py3-none-any.whl → 11.1.5rc2__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 (123) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  3. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  4. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  5. clarifai/cli/__main__.py~ +4 -0
  6. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  7. clarifai/cli/__pycache__/__main__.cpython-310.pyc +0 -0
  8. clarifai/cli/__pycache__/base.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__/nodepool.cpython-310.pyc +0 -0
  13. clarifai/cli/model.py +25 -0
  14. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  15. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  16. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  17. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  18. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  19. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  20. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  21. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  22. clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
  23. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  24. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  25. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  26. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  27. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  28. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  29. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  30. clarifai/client/model.py +89 -365
  31. clarifai/client/model_client.py +422 -0
  32. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  33. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  34. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  35. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  36. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  37. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  38. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  39. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  40. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  41. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  42. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  43. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  44. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-310.pyc +0 -0
  45. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-310.pyc +0 -0
  46. clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
  47. clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
  48. clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
  49. clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  50. clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
  51. clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
  52. clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
  53. clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
  54. clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
  55. clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
  56. clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
  57. clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
  58. clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
  59. clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
  60. clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
  61. clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
  62. clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
  63. clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
  64. clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
  65. clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
  66. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  67. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  68. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  69. clarifai/runners/__init__.py +2 -7
  70. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  71. clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
  72. clarifai/runners/dockerfile_template/Dockerfile.debug +11 -0
  73. clarifai/runners/dockerfile_template/Dockerfile.debug~ +9 -0
  74. clarifai/runners/dockerfile_template/Dockerfile.template +3 -0
  75. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  76. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  77. clarifai/runners/models/__pycache__/model_builder.cpython-310.pyc +0 -0
  78. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  79. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  80. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  81. clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
  82. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  83. clarifai/runners/models/model_builder.py +33 -7
  84. clarifai/runners/models/model_class.py +269 -28
  85. clarifai/runners/models/model_run_locally.py +3 -78
  86. clarifai/runners/models/model_runner.py +2 -0
  87. clarifai/runners/models/model_servicer.py +11 -2
  88. clarifai/runners/server.py +5 -1
  89. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  90. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  91. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  92. clarifai/runners/utils/__pycache__/data_types.cpython-310.pyc +0 -0
  93. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  94. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  95. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  96. clarifai/runners/utils/__pycache__/method_signatures.cpython-310.pyc +0 -0
  97. clarifai/runners/utils/__pycache__/serializers.cpython-310.pyc +0 -0
  98. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  99. clarifai/runners/utils/data_handler.py +308 -205
  100. clarifai/runners/utils/data_types.py +334 -0
  101. clarifai/runners/utils/method_signatures.py +452 -0
  102. clarifai/runners/utils/serializers.py +132 -0
  103. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  104. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  105. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  106. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  107. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  108. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  109. clarifai/utils/evaluation/__pycache__/__init__.cpython-310.pyc +0 -0
  110. clarifai/utils/evaluation/__pycache__/helpers.cpython-310.pyc +0 -0
  111. clarifai/utils/evaluation/__pycache__/main.cpython-310.pyc +0 -0
  112. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  113. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  114. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  115. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  116. {clarifai-11.1.5.dist-info → clarifai-11.1.5rc2.dist-info}/METADATA +16 -26
  117. clarifai-11.1.5rc2.dist-info/RECORD +203 -0
  118. {clarifai-11.1.5.dist-info → clarifai-11.1.5rc2.dist-info}/WHEEL +1 -1
  119. clarifai/runners/models/base_typed_model.py +0 -238
  120. clarifai-11.1.5.dist-info/RECORD +0 -101
  121. {clarifai-11.1.5.dist-info → clarifai-11.1.5rc2.dist-info}/LICENSE +0 -0
  122. {clarifai-11.1.5.dist-info → clarifai-11.1.5rc2.dist-info}/entry_points.txt +0 -0
  123. {clarifai-11.1.5.dist-info → clarifai-11.1.5rc2.dist-info}/top_level.txt +0 -0
@@ -1,231 +1,334 @@
1
- from typing import Dict, List, Tuple, Union
1
+ import io
2
+ from typing import Iterable, List, get_args, get_origin
2
3
 
3
4
  import numpy as np
4
- from clarifai_grpc.grpc.api import resources_pb2
5
- from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
6
- from PIL import Image
5
+ from clarifai_grpc.grpc.api.resources_pb2 import Audio as AudioProto
6
+ from clarifai_grpc.grpc.api.resources_pb2 import Concept as ConceptProto
7
+ from clarifai_grpc.grpc.api.resources_pb2 import Frame as FrameProto
8
+ from clarifai_grpc.grpc.api.resources_pb2 import Image as ImageProto
9
+ from clarifai_grpc.grpc.api.resources_pb2 import Region as RegionProto
10
+ from clarifai_grpc.grpc.api.resources_pb2 import Text as TextProto
11
+ from clarifai_grpc.grpc.api.resources_pb2 import Video as VideoProto
12
+ from PIL import Image as PILImage
7
13
 
8
- from clarifai.client.auth.helper import ClarifaiAuthHelper
9
14
 
10
- from .data_utils import bytes_to_image, image_to_bytes
15
+ class MessageData:
11
16
 
17
+ def to_proto(self):
18
+ raise NotImplementedError
12
19
 
13
- class BaseDataHandler:
20
+ @classmethod
21
+ def from_proto(cls, proto):
22
+ raise NotImplementedError
23
+
24
+ def cast(self, python_type):
25
+ if python_type == self.__class__:
26
+ return self
27
+ raise TypeError(f'Incompatible type for {self.__class__.__name__}: {python_type}')
28
+
29
+
30
+ class Output(dict):
31
+ __getattr__ = dict.__getitem__
32
+ __setattr__ = dict.__setitem__
33
+
34
+ def __origin__(self):
35
+ return self
36
+
37
+ def __args__(self):
38
+ return list(self.keys())
39
+
40
+
41
+ class Input(dict):
42
+ __getattr__ = dict.__getitem__
43
+ __setattr__ = dict.__setitem__
44
+
45
+ def __origin__(self):
46
+ return self
47
+
48
+ def __args__(self):
49
+ return list(self.keys())
50
+
51
+
52
+ class Stream(Iterable):
53
+ pass
54
+
55
+
56
+ class Text(MessageData):
57
+
58
+ def __init__(self, text: str, url: str = None):
59
+ self.text = text
60
+ self.url = url
61
+
62
+ def to_proto(self) -> TextProto:
63
+ return TextProto(raw=self.text or '', self_url=self.url or '')
64
+
65
+ @classmethod
66
+ def from_proto(cls, proto: TextProto) -> "Text":
67
+ return cls(proto.raw, proto.url or None)
68
+
69
+ def cast(self, python_type):
70
+ if python_type == str:
71
+ return self.text
72
+ if python_type == Text:
73
+ return self
74
+ raise TypeError(f'Incompatible type for Text: {python_type}')
75
+
76
+
77
+ class Concept(MessageData):
14
78
 
15
- def __init__(self,
16
- proto: Union[resources_pb2.Input, resources_pb2.Output],
17
- auth: ClarifaiAuthHelper = None):
18
- self._proto = proto
19
- self._auth = auth
79
+ def __init__(self, name: str, value: float = 0):
80
+ self.name = name
81
+ self.value = value
20
82
 
21
- #
22
- def to_python(self):
23
- return dict(text=self.text, image=self.image, audio=self.audio)
83
+ def __repr__(self) -> str:
84
+ return f"Concept(name={self.name!r}, value={self.value})"
85
+
86
+ def to_proto(self):
87
+ return ConceptProto(name=self.name, value=self.value)
88
+
89
+ @classmethod
90
+ def from_proto(cls, proto: ConceptProto) -> "Concept":
91
+ return cls(proto.name, proto.value)
92
+
93
+
94
+ class Region(MessageData):
95
+
96
+ def __init__(self, proto_region: RegionProto):
97
+ self.proto = proto_region
24
98
 
25
- # ---------------- Start get/setters ---------------- #
26
- # Proto
27
99
  @property
28
- def proto(self):
29
- return self._proto
100
+ def box(self) -> List[float]:
101
+ bbox = self.proto.region_info.bounding_box
102
+ return [bbox.left_col, bbox.top_row, bbox.right_col, bbox.bottom_row] # x1, y1, x2, y2
103
+
104
+ @box.setter
105
+ def box(self, value: List[float]):
106
+ bbox = self.proto.region_info.bounding_box
107
+ bbox.left_col, bbox.top_row, bbox.right_col, bbox.bottom_row = value
30
108
 
31
- # Status
32
109
  @property
33
- def status(self) -> status_pb2.Status:
34
- return self._proto.status
110
+ def concepts(self) -> List[Concept]:
111
+ return [Concept.from_proto(proto) for proto in self.proto.data.concepts]
112
+
113
+ @concepts.setter
114
+ def concepts(self, value: List[Concept]):
115
+ self.proto.data.concepts.extend([concept.to_proto() for concept in value])
116
+
117
+ def __repr__(self) -> str:
118
+ return f"Region(box={self.box}, concepts={self.concepts})"
119
+
120
+ def to_proto(self) -> RegionProto:
121
+ return self.proto
122
+
123
+ @classmethod
124
+ def from_proto(cls, proto: RegionProto) -> "Region":
125
+ return cls(proto)
126
+
127
+
128
+ class Image(MessageData):
129
+
130
+ def __init__(self, proto_image: ImageProto = None, url: str = None, bytes: bytes = None):
131
+ if proto_image is None:
132
+ proto_image = ImageProto()
133
+ self.proto = proto_image
134
+ # use setters for init vals
135
+ if url:
136
+ self.url = url
137
+ if bytes:
138
+ self.bytes = bytes
139
+
140
+ @property
141
+ def url(self) -> str:
142
+ return self.proto.url
143
+
144
+ @url.setter
145
+ def url(self, value: str):
146
+ self.proto.url = value
147
+
148
+ @property
149
+ def bytes(self) -> bytes:
150
+ return self.proto.base64
151
+
152
+ @bytes.setter
153
+ def bytes(self, value: bytes):
154
+ self.proto.base64 = value
35
155
 
36
- def set_status(self, code: str, description: str = ""):
37
- self._proto.status.code = code
38
- self._proto.status.description = description
156
+ def __repr__(self) -> str:
157
+ attrs = []
158
+ if self.url:
159
+ attrs.append(f"url={self.url!r}")
160
+ if self.bytes:
161
+ attrs.append(f"bytes=<{len(self.bytes)} bytes>")
162
+ return f"Image({', '.join(attrs)})"
163
+
164
+ @classmethod
165
+ def from_url(cls, url: str) -> "Image":
166
+ proto_image = ImageProto(url=url)
167
+ return cls(proto_image)
168
+
169
+ @classmethod
170
+ def from_pil(cls, pil_image: PILImage.Image) -> "Image":
171
+ with io.BytesIO() as output:
172
+ pil_image.save(output, format="PNG")
173
+ image_bytes = output.getvalue()
174
+ proto_image = ImageProto(base64=image_bytes)
175
+ return cls(proto_image)
176
+
177
+ def to_pil(self) -> PILImage.Image:
178
+ if not self.proto.base64:
179
+ raise ValueError("Image has no bytes")
180
+ return PILImage.open(io.BytesIO(self.proto.base64))
181
+
182
+ def to_numpy(self) -> np.ndarray:
183
+ return np.asarray(self.to_pil())
184
+
185
+ def to_proto(self) -> ImageProto:
186
+ return self.proto
187
+
188
+ @classmethod
189
+ def from_proto(cls, proto: ImageProto) -> "Image":
190
+ return cls(proto)
191
+
192
+ def cast(self, python_type):
193
+ if python_type == Image:
194
+ return self
195
+ if python_type in (PILImage.Image, PILImage):
196
+ return self.to_pil()
197
+ if python_type == np.ndarray or get_origin(python_type) == np.ndarray:
198
+ return self.to_numpy()
199
+ raise TypeError(f'Incompatible type for Image: {python_type}')
200
+
201
+
202
+ class Audio(MessageData):
203
+
204
+ def __init__(self, proto_audio: AudioProto):
205
+ self.proto = proto_audio
39
206
 
40
- # Text
41
207
  @property
42
- def text(self) -> Union[None, str]:
43
- data = self._proto.data.text
44
- text = None
45
- if data.ByteSize():
46
- if data.raw:
47
- text = data.raw
48
- else:
49
- raise NotImplementedError
50
- return text
51
-
52
- def set_text(self, text: str):
53
- self._proto.data.text.raw = text
54
-
55
- # Image
208
+ def url(self) -> str:
209
+ return self.proto.url
210
+
211
+ @url.setter
212
+ def url(self, value: str):
213
+ self.proto.url = value
214
+
56
215
  @property
57
- def image(self, format: str = "np") -> Union[None, Image.Image, np.ndarray]:
58
- data = self._proto.data.image
59
- image = None
60
- if data.ByteSize():
61
- data: resources_pb2.Image = data
62
- if data.base64:
63
- image = data.base64
64
- elif data.url:
65
- raise NotImplementedError
66
- image = bytes_to_image(image)
67
- image = image if not format == "np" else np.asarray(image).astype("uint8")
68
- return image
69
-
70
- def set_image(self, image: Union[Image.Image, np.ndarray]):
71
- if isinstance(image, np.ndarray):
72
- image = Image.fromarray(image)
73
- self._proto.data.image.base64 = image_to_bytes(image)
74
-
75
- # Audio
216
+ def bytes(self) -> bytes:
217
+ return self.proto.base64
218
+
219
+ @bytes.setter
220
+ def bytes(self, value: bytes):
221
+ self.proto.base64 = value
222
+
223
+ @classmethod
224
+ def from_url(cls, url: str) -> "Audio":
225
+ proto_audio = AudioProto(url=url)
226
+ return cls(proto_audio)
227
+
228
+ def __repr__(self) -> str:
229
+ attrs = []
230
+ if self.url:
231
+ attrs.append(f"url={self.url!r}")
232
+ if self.bytes:
233
+ attrs.append(f"bytes=<{len(self.bytes)} bytes>")
234
+ return f"Audio({', '.join(attrs)})"
235
+
236
+ def to_proto(self) -> AudioProto:
237
+ return self.proto
238
+
239
+ @classmethod
240
+ def from_proto(cls, proto: AudioProto) -> "Audio":
241
+ return cls(proto)
242
+
243
+
244
+ class Frame(MessageData):
245
+
246
+ def __init__(self, proto_frame: FrameProto):
247
+ self.proto = proto_frame
248
+
76
249
  @property
77
- def audio(self) -> bytes:
78
- data = self._proto.data.audio
79
- audio = None
80
- if data.ByteSize():
81
- if data.base64:
82
- audio = data.base64
83
- return audio
84
-
85
- def set_audio(self, audio: bytes):
86
- self._proto.data.audio.base64 = audio
87
-
88
- # Bboxes
250
+ def time(self) -> float:
251
+ # TODO: time is a uint32, so this will overflow at 49.7 days
252
+ # we should be using double or uint64 in the proto instead
253
+ return self.proto.frame_info.time / 1000.0
254
+
255
+ @time.setter
256
+ def time(self, value: float):
257
+ self.proto.frame_info.time = int(value * 1000)
258
+
89
259
  @property
90
- def bboxes(self, real_coord: bool = False, image_width: int = None,
91
- image_height: int = None) -> Tuple[List, List, List]:
92
- if real_coord:
93
- assert (image_height or image_width
94
- ), "image_height and image_width are required when when return real coordinates"
95
- xyxy = []
96
- scores = []
97
- concepts = []
98
- for _, each in enumerate(self._proto.data.regions):
99
- box = each.region_info
100
- score = each.value
101
- concept = each.data.concepts[0].id
102
- x1 = box.left_col
103
- y1 = box.top_row
104
- x2 = box.right_col
105
- y2 = box.bottom_row
106
- if real_coord:
107
- x1 = x1 * image_width
108
- y1 = y1 * image_height
109
- x2 = x2 * image_width
110
- y2 = y2 * image_height
111
- xyxy.append([x1, y1, x2, y2])
112
- scores.append(score)
113
- concepts.append(concept)
114
-
115
- return xyxy, scores, concepts
116
-
117
- def set_bboxes(self,
118
- boxes: list,
119
- scores: list,
120
- concepts: list,
121
- real_coord: bool = False,
122
- image_width: int = None,
123
- image_height: int = None):
124
- if real_coord:
125
- assert (image_height and
126
- image_width), "image_height and image_width are required when `real_coord` is set"
127
- bboxes = [[x[1] / image_height, x[0] / image_width, x[3] / image_height, x[2] / image_width]
128
- for x in boxes] # normalize the bboxes to [0,1] and [y1 x1 y2 x2]
129
- bboxes = np.clip(bboxes, 0, 1.0)
130
-
131
- regions = []
132
- for ith, bbox in enumerate(bboxes):
133
- score = scores[ith]
134
- concept = concepts[ith]
135
- if any([each > 1.0 for each in bbox]):
136
- assert ValueError(
137
- "Box coordinates is not normalized between [0, 1]. Please set format_box to True and provide image_height and image_width to normalize"
138
- )
139
- region = resources_pb2.RegionInfo(bounding_box=resources_pb2.BoundingBox(
140
- top_row=bbox[0], # y_min
141
- left_col=bbox[1], # x_min
142
- bottom_row=bbox[2], # y_max
143
- right_col=bbox[3], # x_max
144
- ))
145
- data = resources_pb2.Data(concepts=resources_pb2.Concept(id=concept, value=score))
146
- regions.append(resources_pb2.Region(region_info=region, data=data))
147
-
148
- self._proto.data.regions = regions
149
-
150
- # Concepts
260
+ def image(self) -> Image:
261
+ return Image.from_proto(self.proto.data.image)
262
+
263
+ @image.setter
264
+ def image(self, value: Image):
265
+ self.proto.data.image.CopyFrom(value.to_proto())
266
+
151
267
  @property
152
- def concepts(self) -> Dict[str, float]:
153
- con_scores = {}
154
- for each in self.proto.data.concepts:
155
- con_scores.update({each.id: each.value})
156
- return con_scores
157
-
158
- def set_concepts(self, concept_score_pairs: Dict[str, float]):
159
- concepts = []
160
- for concept, score in concept_score_pairs.items():
161
- con_score = resources_pb2.Concept(id=concept, name=concept, value=score)
162
- concepts.append(con_score)
163
- if concepts:
164
- self._proto.data.ClearField("concepts")
165
- for each in concepts:
166
- self._proto.data.concepts.append(each)
167
-
168
- # Embeddings
268
+ def regions(self) -> List[Region]:
269
+ return [Region(region) for region in self.proto.data.regions]
270
+
271
+ @regions.setter
272
+ def regions(self, value: List[Region]):
273
+ self.proto.data.regions.extend([region.proto for region in value])
274
+
275
+ def to_proto(self) -> FrameProto:
276
+ return self.proto
277
+
278
+ @classmethod
279
+ def from_proto(cls, proto: FrameProto) -> "Frame":
280
+ return cls(proto)
281
+
282
+
283
+ class Video(MessageData):
284
+
285
+ def __init__(self, proto_video: VideoProto):
286
+ self.proto = proto_video
287
+
169
288
  @property
170
- def embeddings(self) -> List[List[float]]:
171
- return [each.vector for each in self.proto.data.embeddings]
289
+ def url(self) -> str:
290
+ return self.proto.url
172
291
 
173
- def set_embeddings(self, list_vectors: List[List[float]]):
174
- if list_vectors[0]:
175
- self._proto.data.ClearField("embeddings")
176
- for vec in list_vectors:
177
- self._proto.data.embeddings.append(
178
- resources_pb2.Embedding(vector=vec, num_dimensions=len(vec)))
292
+ @url.setter
293
+ def url(self, value: str):
294
+ self.proto.url = value
179
295
 
180
- # ---------------- End get/setters ---------------- #
296
+ @property
297
+ def bytes(self) -> bytes:
298
+ return self.proto.base64
299
+
300
+ @bytes.setter
301
+ def bytes(self, value: bytes):
302
+ self.proto.base64 = value
181
303
 
182
- # Constructors
183
304
  @classmethod
184
- def from_proto(cls, proto):
185
- clss = cls(proto=proto)
186
- return clss
305
+ def from_url(cls, url: str) -> "Video":
306
+ proto_video = VideoProto(url=url)
307
+ return cls(proto_video)
308
+
309
+ def __repr__(self) -> str:
310
+ attrs = []
311
+ if self.url:
312
+ attrs.append(f"url={self.url!r}")
313
+ if self.bytes:
314
+ attrs.append(f"bytes=<{len(self.bytes)} bytes>")
315
+ return f"Video({', '.join(attrs)})"
316
+
317
+ def to_proto(self) -> VideoProto:
318
+ return self.proto
187
319
 
188
320
  @classmethod
189
- def from_data(
190
- cls,
191
- status_code: int = status_code_pb2.SUCCESS,
192
- status_description: str = "",
193
- text: str = None,
194
- image: Union[Image.Image, np.ndarray] = None,
195
- audio: bytes = None,
196
- boxes: dict = None,
197
- concepts: Dict[str, float] = {},
198
- embeddings: List[List[float]] = [],
199
- ) -> 'OutputDataHandler':
200
- clss = cls(proto=resources_pb2.Output())
201
- if isinstance(image, Image.Image) or isinstance(image, np.ndarray):
202
- clss.set_image(image)
203
- if text:
204
- clss.set_text(text)
205
- if audio:
206
- clss.set_audio(audio)
207
- if boxes:
208
- clss.set_bboxes(**boxes)
209
- if concepts:
210
- clss.set_concepts(concepts)
211
- if embeddings:
212
- clss.set_embeddings(embeddings)
213
-
214
- clss.set_status(code=status_code, description=status_description)
215
- return clss
216
-
217
-
218
- class InputDataHandler(BaseDataHandler):
219
-
220
- def __init__(self,
221
- proto: resources_pb2.Input = resources_pb2.Input(),
222
- auth: ClarifaiAuthHelper = None):
223
- super().__init__(proto=proto, auth=auth)
224
-
225
-
226
- class OutputDataHandler(BaseDataHandler):
227
-
228
- def __init__(self,
229
- proto: resources_pb2.Output = resources_pb2.Output(),
230
- auth: ClarifaiAuthHelper = None):
231
- super().__init__(proto=proto, auth=auth)
321
+ def from_proto(cls, proto: VideoProto) -> "Video":
322
+ return cls(proto)
323
+
324
+
325
+ def cast(value, python_type):
326
+ list_type = (get_origin(python_type) == list)
327
+ if isinstance(value, MessageData):
328
+ return value.cast(python_type)
329
+ if list_type and isinstance(value, np.ndarray):
330
+ return value.tolist()
331
+ if list_type and isinstance(value, list):
332
+ inner_type = get_args(python_type)[0]
333
+ return [cast(item, inner_type) for item in value]
334
+ return value