clarifai 11.1.7__py3-none-any.whl → 11.1.7rc1__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 (121) 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/#model_client.py# +430 -0
  15. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  16. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  17. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  18. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  19. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  20. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  21. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  22. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  23. clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
  24. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  25. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  26. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  27. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  28. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  29. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  30. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  31. clarifai/client/model.py +158 -393
  32. clarifai/client/model_client.py +449 -0
  33. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  34. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  35. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  36. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  37. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  38. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  39. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  40. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  41. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  42. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  43. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  44. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  45. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-310.pyc +0 -0
  46. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-310.pyc +0 -0
  47. clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
  48. clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
  49. clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
  50. clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  51. clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
  52. clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
  53. clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
  54. clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
  55. clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
  56. clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
  57. clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
  58. clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
  59. clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
  60. clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
  61. clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
  62. clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
  63. clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
  64. clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
  65. clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
  66. clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
  67. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  68. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  69. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  70. clarifai/runners/__init__.py +2 -7
  71. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  72. clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
  73. clarifai/runners/dockerfile_template/Dockerfile.debug +11 -0
  74. clarifai/runners/dockerfile_template/Dockerfile.debug~ +9 -0
  75. clarifai/runners/dockerfile_template/Dockerfile.template +3 -0
  76. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  77. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  78. clarifai/runners/models/__pycache__/model_builder.cpython-310.pyc +0 -0
  79. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  80. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  81. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  82. clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
  83. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  84. clarifai/runners/models/model_builder.py +24 -7
  85. clarifai/runners/models/model_class.py +256 -28
  86. clarifai/runners/models/model_run_locally.py +3 -78
  87. clarifai/runners/models/model_runner.py +2 -0
  88. clarifai/runners/models/model_servicer.py +11 -2
  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_types.py +427 -0
  100. clarifai/runners/utils/method_signatures.py +500 -0
  101. clarifai/runners/utils/serializers.py +222 -0
  102. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  103. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  104. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  105. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  106. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  107. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  108. clarifai/utils/evaluation/__pycache__/__init__.cpython-310.pyc +0 -0
  109. clarifai/utils/evaluation/__pycache__/helpers.cpython-310.pyc +0 -0
  110. clarifai/utils/evaluation/__pycache__/main.cpython-310.pyc +0 -0
  111. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  112. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  113. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  114. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  115. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc1.dist-info}/METADATA +16 -26
  116. clarifai-11.1.7rc1.dist-info/RECORD +205 -0
  117. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc1.dist-info}/WHEEL +1 -1
  118. clarifai-11.1.7.dist-info/RECORD +0 -101
  119. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc1.dist-info}/LICENSE +0 -0
  120. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc1.dist-info}/entry_points.txt +0 -0
  121. {clarifai-11.1.7.dist-info → clarifai-11.1.7rc1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,500 @@
1
+ import ast
2
+ import inspect
3
+ import json
4
+ import textwrap
5
+ from collections import namedtuple
6
+ from typing import List, Tuple, get_args, get_origin
7
+
8
+ import numpy as np
9
+ import PIL.Image
10
+ import yaml
11
+ from clarifai_grpc.grpc.api import resources_pb2
12
+ from google.protobuf.message import Message as MessageProto
13
+
14
+ from clarifai.runners.utils import data_types
15
+ from clarifai.runners.utils.serializers import (
16
+ AtomicFieldSerializer, JSONSerializer, ListSerializer, MessageSerializer,
17
+ NamedFieldsSerializer, NDArraySerializer, Serializer, TupleSerializer)
18
+
19
+
20
+ def build_function_signature(func):
21
+ '''
22
+ Build a signature for the given function.
23
+ '''
24
+ sig = inspect.signature(func)
25
+
26
+ # check if func is bound, and if not, remove self/cls
27
+ if getattr(func, '__self__', None) is None and sig.parameters and list(
28
+ sig.parameters.values())[0].name in ('self', 'cls'):
29
+ sig = sig.replace(parameters=list(sig.parameters.values())[1:])
30
+
31
+ return_annotation = sig.return_annotation
32
+ if return_annotation == inspect.Parameter.empty:
33
+ raise TypeError('Function must have a return annotation')
34
+
35
+ input_sigs = [
36
+ build_variable_signature(p.name, p.annotation, p.default) for p in sig.parameters.values()
37
+ ]
38
+ input_sigs, input_types, input_streaming = zip(*input_sigs)
39
+ output_sig, output_type, output_streaming = build_variable_signature(
40
+ 'return', return_annotation, is_output=True)
41
+ # TODO: flatten out "return" layer if not needed
42
+
43
+ # check for streams and determine method type
44
+ if sum(input_streaming) > 1:
45
+ raise TypeError('streaming methods must have at most one streaming input')
46
+ input_streaming = any(input_streaming)
47
+ if not (input_streaming or output_streaming):
48
+ method_type = 'predict'
49
+ elif not input_streaming and output_streaming:
50
+ method_type = 'generate'
51
+ elif input_streaming and output_streaming:
52
+ method_type = 'stream'
53
+ else:
54
+ raise TypeError('stream methods with streaming inputs must have streaming outputs')
55
+
56
+ #method_signature = resources_pb2.MethodSignature() # TODO
57
+ method_signature = _SignatureDict() #for now
58
+
59
+ method_signature.name = func.__name__
60
+ #method_signature.method_type = getattr(resources_pb2.RunnerMethodType, method_type)
61
+ assert method_type in ('predict', 'generate', 'stream')
62
+ method_signature.method_type = method_type
63
+ method_signature.docstring = func.__doc__
64
+ method_signature.annotations_json = json.dumps(_get_annotations_source(func))
65
+
66
+ #method_signature.inputs.extend(input_vars)
67
+ #method_signature.outputs.extend(output_vars)
68
+ method_signature.inputs = input_sigs
69
+ method_signature.outputs = [output_sig]
70
+ return method_signature
71
+
72
+
73
+ def _get_annotations_source(func):
74
+ """Extracts raw annotation strings from the function source."""
75
+ source = inspect.getsource(func) # Get function source code
76
+ source = textwrap.dedent(source) # Dedent source code
77
+ tree = ast.parse(source) # Parse into AST
78
+ func_node = next(node for node in tree.body
79
+ if isinstance(node, ast.FunctionDef)) # Get function node
80
+
81
+ annotations = {}
82
+ for arg in func_node.args.args: # Process arguments
83
+ if arg.annotation:
84
+ annotations[arg.arg] = ast.unparse(arg.annotation) # Get raw annotation string
85
+
86
+ if func_node.returns: # Process return type
87
+ annotations["return"] = ast.unparse(func_node.returns)
88
+
89
+ return annotations
90
+
91
+
92
+ def build_variable_signature(name, annotation, default=inspect.Parameter.empty, is_output=False):
93
+ '''
94
+ Build a data proto signature and get the normalized python type for the given annotation.
95
+ '''
96
+
97
+ # check valid names (should already be constrained by python naming, but check anyway)
98
+ if not name.isidentifier():
99
+ raise ValueError(f'Invalid variable name: {name}')
100
+
101
+ # get fields for each variable based on type
102
+ tp, streaming = _normalize_type(annotation)
103
+
104
+ #var = resources_pb2.VariableSignature() # TODO
105
+ sig = _VariableSignature() #for now
106
+ sig.name = name
107
+
108
+ _fill_signature_type(sig, tp)
109
+
110
+ sig.streaming = streaming
111
+
112
+ if not is_output:
113
+ sig.required = (default is inspect.Parameter.empty)
114
+ if not sig.required:
115
+ sig.default = str(default)
116
+
117
+ return sig, type, streaming
118
+
119
+
120
+ def _fill_signature_type(sig, tp):
121
+ try:
122
+ if tp in _DATA_TYPES:
123
+ sig.type = _DATA_TYPES[tp].type
124
+ return
125
+ except TypeError:
126
+ pass # not hashable type
127
+
128
+ # Check for dynamically generated NamedFields subclasses (from type annotations)
129
+ if inspect.isclass(tp) and issubclass(tp, data_types.NamedFields) and hasattr(
130
+ tp, '__annotations__'):
131
+ sig.type = DataType.NAMED_FIELDS
132
+ for name, inner_type in tp.__annotations__.items():
133
+ inner_sig = _VariableSignature()
134
+ inner_sig.name = name
135
+ _fill_signature_type(inner_sig, inner_type)
136
+ sig.type_args.append(inner_sig)
137
+ return
138
+
139
+ if isinstance(tp, data_types.NamedFields):
140
+ sig.type = DataType.NAMED_FIELDS
141
+ for name, inner_type in tp.items():
142
+ # inner_sig = sig.type_args.add()
143
+ sig.type_args.append(inner_sig := _VariableSignature())
144
+ inner_sig.name = name
145
+ _fill_signature_type(inner_sig, inner_type)
146
+ return
147
+
148
+ if get_origin(tp) == tuple:
149
+ sig.type = DataType.TUPLE
150
+ for inner_type in get_args(tp):
151
+ #inner_sig = sig.type_args.add()
152
+ sig.type_args.append(inner_sig := _VariableSignature())
153
+ _fill_signature_type(inner_sig, inner_type)
154
+ return
155
+
156
+ if get_origin(tp) == list:
157
+ sig.type = DataType.LIST
158
+ inner_type = get_args(tp)[0]
159
+ #inner_sig = sig.type_args.add()
160
+ sig.type_args.append(inner_sig := _VariableSignature())
161
+ _fill_signature_type(inner_sig, inner_type)
162
+ return
163
+
164
+ raise TypeError(f'Unsupported type: {tp}')
165
+
166
+
167
+ def serializer_from_signature(signature):
168
+ '''
169
+ Get the serializer for the given signature.
170
+ '''
171
+ if signature.type in _SERIALIZERS_BY_TYPE_ENUM:
172
+ return _SERIALIZERS_BY_TYPE_ENUM[signature.type]
173
+ if signature.type == DataType.LIST:
174
+ return ListSerializer(serializer_from_signature(signature.type_args[0]))
175
+ if signature.type == DataType.TUPLE:
176
+ return TupleSerializer([serializer_from_signature(sig) for sig in signature.type_args])
177
+ if signature.type == DataType.NAMED_FIELDS:
178
+ return NamedFieldsSerializer(
179
+ {sig.name: serializer_from_signature(sig)
180
+ for sig in signature.type_args})
181
+ raise ValueError(f'Unsupported type: {signature.type}')
182
+
183
+
184
+ def signatures_to_json(signatures):
185
+ assert isinstance(
186
+ signatures, dict), 'Expected dict of signatures {name: signature}, got %s' % type(signatures)
187
+ # TODO change to proto when ready
188
+ #signatures = {name: MessageToDict(sig) for name, sig in signatures.items()}
189
+ return json.dumps(signatures)
190
+
191
+
192
+ def signatures_from_json(json_str):
193
+ d = json.loads(json_str, object_pairs_hook=_SignatureDict)
194
+ return d
195
+
196
+
197
+ def signatures_to_yaml(signatures):
198
+ # XXX go in/out of json to get the correct format and python dict types
199
+ d = json.loads(signatures_to_json(signatures))
200
+
201
+ def _filter_empty(d):
202
+ if isinstance(d, (list, tuple)):
203
+ return [_filter_empty(v) for v in d if v]
204
+ if isinstance(d, dict):
205
+ return {k: _filter_empty(v) for k, v in d.items() if v}
206
+ return d
207
+
208
+ return yaml.dump(_filter_empty(d), default_flow_style=False)
209
+
210
+
211
+ def signatures_from_yaml(yaml_str):
212
+ d = yaml.safe_load(yaml_str)
213
+ return signatures_from_json(json.dumps(d))
214
+
215
+
216
+ def serialize(kwargs, signatures, proto=None, is_output=False):
217
+ '''
218
+ Serialize the given kwargs into the proto using the given signatures.
219
+ '''
220
+ if proto is None:
221
+ proto = resources_pb2.Data()
222
+ unknown = set(kwargs.keys()) - set(sig.name for sig in signatures)
223
+ if unknown:
224
+ if unknown == {'return'} and len(signatures) > 1:
225
+ raise TypeError('Got a single return value, but expected multiple outputs {%s}' %
226
+ ', '.join(sig.name for sig in signatures))
227
+ raise TypeError('Got unexpected key: %s' % ', '.join(unknown))
228
+ inline_first_value = False
229
+ if (is_output and len(signatures) == 1 and signatures[0].name == 'return' and
230
+ len(kwargs) == 1 and 'return' in kwargs):
231
+ # if there is only one output, flatten it and return directly
232
+ inline_first_value = True
233
+ if signatures and signatures[0].type not in _NON_INLINABLE_TYPES:
234
+ inline_first_value = True
235
+ for sig_i, sig in enumerate(signatures):
236
+ if sig.name not in kwargs:
237
+ if sig.required:
238
+ raise TypeError(f'Missing required argument: {sig.name}')
239
+ continue # skip missing fields, they can be set to default on the server
240
+ data = kwargs[sig.name]
241
+ serializer = serializer_from_signature(sig)
242
+ # TODO determine if any (esp the first) var can go in the proto without parts
243
+ # and whether to put this in the signature or dynamically determine it
244
+ if inline_first_value and sig_i == 0 and id(data) not in _ZERO_VALUE_IDS:
245
+ # inlined first value; note data must not be empty or 0 to inline, since that
246
+ # will correspond to the missing value case (which uses function defaults).
247
+ # empty values are put explicitly in parts.
248
+ serializer.serialize(proto, data)
249
+ else:
250
+ # add the part to the proto
251
+ part = proto.parts.add()
252
+ part.id = sig.name
253
+ serializer.serialize(part.data, data)
254
+ return proto
255
+
256
+
257
+ def deserialize(proto, signatures, is_output=False):
258
+ '''
259
+ Deserialize the given proto into kwargs using the given signatures.
260
+ '''
261
+ if isinstance(signatures, dict):
262
+ signatures = [signatures] # TODO update return key level and make consistnet
263
+ kwargs = {}
264
+ parts_by_name = {part.id: part for part in proto.parts}
265
+ for sig_i, sig in enumerate(signatures):
266
+ serializer = serializer_from_signature(sig)
267
+ part = parts_by_name.get(sig.name)
268
+ if part is None:
269
+ if sig_i == 0:
270
+ # possible inlined first value
271
+ value = serializer.deserialize(proto)
272
+ if id(value) not in _ZERO_VALUE_IDS:
273
+ # note missing values are not set to defaults, since they are not in parts
274
+ # an actual zero value passed in must be set in an explicit part
275
+ kwargs[sig.name] = value
276
+ continue
277
+ if sig.required or is_output: # TODO allow optional outputs?
278
+ raise ValueError(f'Missing required field: {sig.name}')
279
+ continue
280
+ kwargs[sig.name] = serializer.deserialize(part.data)
281
+ if len(kwargs) == 1 and 'return' in kwargs:
282
+ return kwargs['return']
283
+ return kwargs
284
+
285
+
286
+ def get_stream_from_signature(signatures):
287
+ '''
288
+ Get the stream signature from the given signatures.
289
+ '''
290
+ for sig in signatures:
291
+ if sig.streaming:
292
+ return sig
293
+ return None
294
+
295
+
296
+ def _is_empty_proto_data(data):
297
+ if isinstance(data, np.ndarray):
298
+ return False
299
+ if isinstance(data, MessageProto):
300
+ return not data.ByteSize()
301
+ return not data
302
+
303
+
304
+ def _normalize_type(tp):
305
+ '''
306
+ Normalize the types for the given parameter.
307
+ Returns the normalized type and whether the parameter is streaming.
308
+ '''
309
+ # stream type indicates streaming, not part of the data itself
310
+ # it can only be used at the top-level of the var type
311
+ streaming = (get_origin(tp) == data_types.Stream)
312
+ if streaming:
313
+ tp = get_args(tp)[0]
314
+
315
+ return _normalize_data_type(tp), streaming
316
+
317
+
318
+ def _normalize_data_type(tp):
319
+
320
+ # jsonable list and dict, these can be serialized as json
321
+ # (tuple we want to keep as a tuple for args and returns, so don't include here)
322
+ if tp in (list, dict) or (get_origin(tp) in (list, dict) and _is_jsonable(tp)):
323
+ return data_types.JSON
324
+
325
+ # container types that need to be serialized as parts
326
+ if get_origin(tp) == list and get_args(tp):
327
+ return List[_normalize_data_type(get_args(tp)[0])]
328
+
329
+ if get_origin(tp) == tuple:
330
+ if not get_args(tp):
331
+ raise TypeError('Tuple must have types specified')
332
+ return Tuple[tuple(_normalize_data_type(val) for val in get_args(tp))]
333
+
334
+ if isinstance(tp, (tuple, list)):
335
+ return Tuple[tuple(_normalize_data_type(val) for val in tp)]
336
+
337
+ if tp == data_types.NamedFields:
338
+ raise TypeError('NamedFields must have types specified')
339
+
340
+ # Handle dynamically generated NamedFields subclasses with annotations
341
+ if isinstance(tp, type) and issubclass(tp, data_types.NamedFields) and hasattr(
342
+ tp, '__annotations__'):
343
+ return data_types.NamedFields(
344
+ **{k: _normalize_data_type(v)
345
+ for k, v in tp.__annotations__.items()})
346
+
347
+ if isinstance(tp, (dict, data_types.NamedFields)):
348
+ return data_types.NamedFields(**{name: _normalize_data_type(val) for name, val in tp.items()})
349
+
350
+ # check if numpy array type, and if so, use ndarray
351
+ if get_origin(tp) == np.ndarray:
352
+ return np.ndarray
353
+
354
+ # check for PIL images (sometimes types use the module, sometimes the class)
355
+ # set these to use the Image data handler
356
+ if tp in (data_types.Image, PIL.Image.Image):
357
+ return data_types.Image
358
+
359
+ if tp == PIL.Image:
360
+ raise TypeError(
361
+ 'Use the Image class from the PIL.Image module i.e. `PIL.Image.Image`, not the module itself'
362
+ )
363
+
364
+ # check for known data types
365
+ try:
366
+ if tp in _DATA_TYPES:
367
+ return tp
368
+ except TypeError:
369
+ pass # not hashable type
370
+
371
+ raise TypeError(f'Unsupported type: {tp}')
372
+
373
+
374
+ def _is_jsonable(tp):
375
+ if tp in (dict, list, tuple, str, int, float, bool, type(None)):
376
+ return True
377
+ if get_origin(tp) in (tuple, list, dict):
378
+ return all(_is_jsonable(val) for val in get_args(tp))
379
+ return False
380
+
381
+
382
+ # TODO --- tmp classes to stand-in for protos until they are defined and built into this package
383
+ class _SignatureDict(dict):
384
+ __getattr__ = dict.__getitem__
385
+ __setattr__ = dict.__setitem__
386
+
387
+
388
+ class _VariableSignature(_SignatureDict):
389
+
390
+ def __init__(self):
391
+ super().__init__()
392
+ self.name = ''
393
+ self.type = ''
394
+ self.type_args = []
395
+ self.streaming = False
396
+ self.required = False
397
+ self.default = ''
398
+ self.description = ''
399
+
400
+
401
+ # type: name of the data type
402
+ # data_field: name of the field in the data proto
403
+ # serializer: serializer for the data type
404
+ _DataType = namedtuple('_DataType', ('type', 'serializer'))
405
+
406
+
407
+ # this will come from the proto module, but for now, define it here
408
+ class DataType:
409
+ NOT_SET = 'NOT_SET'
410
+
411
+ STR = 'STR'
412
+ BYTES = 'BYTES'
413
+ INT = 'INT'
414
+ FLOAT = 'FLOAT'
415
+ BOOL = 'BOOL'
416
+ NDARRAY = 'NDARRAY'
417
+ JSON = 'JSON'
418
+
419
+ TEXT = 'TEXT'
420
+ IMAGE = 'IMAGE'
421
+ CONCEPT = 'CONCEPT'
422
+ REGION = 'REGION'
423
+ FRAME = 'FRAME'
424
+ AUDIO = 'AUDIO'
425
+ VIDEO = 'VIDEO'
426
+
427
+ NAMED_FIELDS = 'NAMED_FIELDS'
428
+ TUPLE = 'TUPLE'
429
+ LIST = 'LIST'
430
+
431
+
432
+ _NON_INLINABLE_TYPES = {DataType.NAMED_FIELDS, DataType.TUPLE, DataType.LIST}
433
+ _ZERO_VALUE_IDS = {id(None), id(''), id(b''), id(0), id(0.0), id(False)}
434
+
435
+ # simple, non-container types that correspond directly to a data field
436
+ _DATA_TYPES = {
437
+ str:
438
+ _DataType(DataType.STR, AtomicFieldSerializer('string_value')),
439
+ bytes:
440
+ _DataType(DataType.BYTES, AtomicFieldSerializer('bytes_value')),
441
+ int:
442
+ _DataType(DataType.INT, AtomicFieldSerializer('int_value')),
443
+ float:
444
+ _DataType(DataType.FLOAT, AtomicFieldSerializer('float_value')),
445
+ bool:
446
+ _DataType(DataType.BOOL, AtomicFieldSerializer('bool_value')),
447
+ np.ndarray:
448
+ _DataType(DataType.NDARRAY, NDArraySerializer('ndarray')),
449
+ data_types.JSON:
450
+ _DataType(DataType.JSON, JSONSerializer('string_value')
451
+ ), # TODO change to json_value when new proto is ready
452
+ data_types.Text:
453
+ _DataType(DataType.TEXT, MessageSerializer('text', data_types.Text)),
454
+ data_types.Image:
455
+ _DataType(DataType.IMAGE, MessageSerializer('image', data_types.Image)),
456
+ data_types.Concept:
457
+ _DataType(DataType.CONCEPT, MessageSerializer('concepts', data_types.Concept)),
458
+ data_types.Region:
459
+ _DataType(DataType.REGION, MessageSerializer('regions', data_types.Region)),
460
+ data_types.Frame:
461
+ _DataType(DataType.FRAME, MessageSerializer('frames', data_types.Frame)),
462
+ data_types.Audio:
463
+ _DataType(DataType.AUDIO, MessageSerializer('audio', data_types.Audio)),
464
+ data_types.Video:
465
+ _DataType(DataType.VIDEO, MessageSerializer('video', data_types.Video)),
466
+ }
467
+
468
+ _SERIALIZERS_BY_TYPE_ENUM = {dt.type: dt.serializer for dt in _DATA_TYPES.values()}
469
+
470
+
471
+ class CompatibilitySerializer(Serializer):
472
+ '''
473
+ Serialization of basic value types, used for backwards compatibility
474
+ with older models that don't have type signatures.
475
+ '''
476
+
477
+ def serialize(self, data_proto, value):
478
+ tp = _normalize_data_type(type(value))
479
+
480
+ try:
481
+ serializer = _DATA_TYPES[tp].serializer
482
+ except (KeyError, TypeError):
483
+ raise TypeError(f'serializer currently only supports basic types, got {tp}')
484
+
485
+ serializer.serialize(data_proto, value)
486
+
487
+ def deserialize(self, data_proto):
488
+ fields = [k.name for k, _ in data_proto.ListFields()]
489
+ if 'parts' in fields:
490
+ raise ValueError('serializer does not support parts')
491
+ serializers = [
492
+ serializer for serializer in _SERIALIZERS_BY_TYPE_ENUM.values()
493
+ if serializer.field_name in fields
494
+ ]
495
+ if not serializers:
496
+ raise ValueError('Returned data not recognized')
497
+ if len(serializers) != 1:
498
+ raise ValueError('Only single output supported for serializer')
499
+ serializer = serializers[0]
500
+ return serializer.deserialize(data_proto)