clarifai 11.7.5__py3-none-any.whl → 11.7.5rc1__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 (231) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
  3. clarifai/__pycache__/__init__.cpython-312.pyc +0 -0
  4. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  5. clarifai/__pycache__/errors.cpython-311.pyc +0 -0
  6. clarifai/__pycache__/errors.cpython-39.pyc +0 -0
  7. clarifai/__pycache__/versions.cpython-311.pyc +0 -0
  8. clarifai/__pycache__/versions.cpython-39.pyc +0 -0
  9. clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
  10. clarifai/cli/__pycache__/__init__.cpython-39.pyc +0 -0
  11. clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
  12. clarifai/cli/__pycache__/base.cpython-39.pyc +0 -0
  13. clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  14. clarifai/cli/__pycache__/compute_cluster.cpython-39.pyc +0 -0
  15. clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
  16. clarifai/cli/__pycache__/deployment.cpython-39.pyc +0 -0
  17. clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
  18. clarifai/cli/__pycache__/model.cpython-39.pyc +0 -0
  19. clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
  20. clarifai/cli/__pycache__/nodepool.cpython-39.pyc +0 -0
  21. clarifai/cli/__pycache__/pipeline.cpython-311.pyc +0 -0
  22. clarifai/cli/__pycache__/pipeline_step.cpython-311.pyc +0 -0
  23. clarifai/cli/base.py +2 -2
  24. clarifai/cli/model.py +261 -139
  25. clarifai/cli/model_templates.py +243 -0
  26. clarifai/cli/pipeline.py +31 -11
  27. clarifai/cli/pipeline_step_templates.py +64 -0
  28. clarifai/cli/templates/__pycache__/__init__.cpython-311.pyc +0 -0
  29. clarifai/cli/templates/__pycache__/pipeline_templates.cpython-311.pyc +0 -0
  30. clarifai/cli/templates/pipeline_templates.py +34 -28
  31. clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
  32. clarifai/client/__pycache__/__init__.cpython-312.pyc +0 -0
  33. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  34. clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
  35. clarifai/client/__pycache__/app.cpython-312.pyc +0 -0
  36. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  37. clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
  38. clarifai/client/__pycache__/base.cpython-39.pyc +0 -0
  39. clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  40. clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
  41. clarifai/client/__pycache__/dataset.cpython-39.pyc +0 -0
  42. clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
  43. clarifai/client/__pycache__/deployment.cpython-39.pyc +0 -0
  44. clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
  45. clarifai/client/__pycache__/input.cpython-39.pyc +0 -0
  46. clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
  47. clarifai/client/__pycache__/lister.cpython-39.pyc +0 -0
  48. clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
  49. clarifai/client/__pycache__/model.cpython-39.pyc +0 -0
  50. clarifai/client/__pycache__/model_client.cpython-311.pyc +0 -0
  51. clarifai/client/__pycache__/model_client.cpython-39.pyc +0 -0
  52. clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
  53. clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
  54. clarifai/client/__pycache__/pipeline.cpython-311.pyc +0 -0
  55. clarifai/client/__pycache__/pipeline_step.cpython-311.pyc +0 -0
  56. clarifai/client/__pycache__/runner.cpython-311.pyc +0 -0
  57. clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
  58. clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
  59. clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
  60. clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
  61. clarifai/client/auth/__pycache__/__init__.cpython-39.pyc +0 -0
  62. clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
  63. clarifai/client/auth/__pycache__/helper.cpython-39.pyc +0 -0
  64. clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
  65. clarifai/client/auth/__pycache__/register.cpython-39.pyc +0 -0
  66. clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
  67. clarifai/client/auth/__pycache__/stub.cpython-39.pyc +0 -0
  68. clarifai/client/pipeline.py +20 -4
  69. clarifai/client/user.py +172 -0
  70. clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
  71. clarifai/constants/__pycache__/base.cpython-39.pyc +0 -0
  72. clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
  73. clarifai/constants/__pycache__/dataset.cpython-39.pyc +0 -0
  74. clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
  75. clarifai/constants/__pycache__/input.cpython-39.pyc +0 -0
  76. clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
  77. clarifai/constants/__pycache__/model.cpython-39.pyc +0 -0
  78. clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
  79. clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
  80. clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
  81. clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
  82. clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  83. clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
  84. clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
  85. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
  86. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-39.pyc +0 -0
  87. clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
  88. clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
  89. clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
  90. clarifai/datasets/upload/__pycache__/base.cpython-39.pyc +0 -0
  91. clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
  92. clarifai/datasets/upload/__pycache__/features.cpython-39.pyc +0 -0
  93. clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
  94. clarifai/datasets/upload/__pycache__/image.cpython-39.pyc +0 -0
  95. clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
  96. clarifai/datasets/upload/__pycache__/multimodal.cpython-39.pyc +0 -0
  97. clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
  98. clarifai/datasets/upload/__pycache__/text.cpython-39.pyc +0 -0
  99. clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
  100. clarifai/datasets/upload/__pycache__/utils.cpython-39.pyc +0 -0
  101. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
  102. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
  103. clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
  104. clarifai/models/model_serving/README.md +158 -0
  105. clarifai/models/model_serving/__init__.py +14 -0
  106. clarifai/models/model_serving/cli/__init__.py +12 -0
  107. clarifai/models/model_serving/cli/_utils.py +53 -0
  108. clarifai/models/model_serving/cli/base.py +14 -0
  109. clarifai/models/model_serving/cli/build.py +79 -0
  110. clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
  111. clarifai/models/model_serving/cli/create.py +171 -0
  112. clarifai/models/model_serving/cli/example_cli.py +34 -0
  113. clarifai/models/model_serving/cli/login.py +26 -0
  114. clarifai/models/model_serving/cli/upload.py +179 -0
  115. clarifai/models/model_serving/constants.py +21 -0
  116. clarifai/models/model_serving/docs/cli.md +161 -0
  117. clarifai/models/model_serving/docs/concepts.md +229 -0
  118. clarifai/models/model_serving/docs/dependencies.md +11 -0
  119. clarifai/models/model_serving/docs/inference_parameters.md +139 -0
  120. clarifai/models/model_serving/docs/model_types.md +19 -0
  121. clarifai/models/model_serving/model_config/__init__.py +16 -0
  122. clarifai/models/model_serving/model_config/base.py +369 -0
  123. clarifai/models/model_serving/model_config/config.py +312 -0
  124. clarifai/models/model_serving/model_config/inference_parameter.py +129 -0
  125. clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -0
  126. clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -0
  127. clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -0
  128. clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -0
  129. clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -0
  130. clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -0
  131. clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -0
  132. clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -0
  133. clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -0
  134. clarifai/models/model_serving/model_config/output.py +133 -0
  135. clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
  136. clarifai/models/model_serving/model_config/triton/serializer.py +136 -0
  137. clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
  138. clarifai/models/model_serving/model_config/triton/wrappers.py +281 -0
  139. clarifai/models/model_serving/repo_build/__init__.py +14 -0
  140. clarifai/models/model_serving/repo_build/build.py +198 -0
  141. clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
  142. clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
  143. clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
  144. clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
  145. clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
  146. clarifai/models/model_serving/repo_build/static_files/triton/model.py +75 -0
  147. clarifai/models/model_serving/utils.py +23 -0
  148. clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
  149. clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
  150. clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
  151. clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
  152. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  153. clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
  154. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  155. clarifai/runners/models/__pycache__/dummy_openai_model.cpython-311.pyc +0 -0
  156. clarifai/runners/models/__pycache__/mcp_class.cpython-311.pyc +0 -0
  157. clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
  158. clarifai/runners/models/__pycache__/model_builder.cpython-39.pyc +0 -0
  159. clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
  160. clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
  161. clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
  162. clarifai/runners/models/__pycache__/model_servicer.cpython-311.pyc +0 -0
  163. clarifai/runners/models/__pycache__/openai_class.cpython-311.pyc +0 -0
  164. clarifai/runners/models/base_typed_model.py +238 -0
  165. clarifai/runners/models/model_builder.py +274 -10
  166. clarifai/runners/models/model_runner.py +93 -76
  167. clarifai/runners/models/model_upload.py +607 -0
  168. clarifai/runners/pipeline_steps/__pycache__/__init__.cpython-311.pyc +0 -0
  169. clarifai/runners/pipeline_steps/__pycache__/pipeline_step_builder.cpython-311.pyc +0 -0
  170. clarifai/runners/pipeline_steps/pipeline_step_builder.py +10 -1
  171. clarifai/runners/pipelines/__pycache__/__init__.cpython-311.pyc +0 -0
  172. clarifai/runners/pipelines/__pycache__/pipeline_builder.cpython-311.pyc +0 -0
  173. clarifai/runners/server.py +1 -1
  174. clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  175. clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  176. clarifai/runners/utils/__pycache__/code_script.cpython-311.pyc +0 -0
  177. clarifai/runners/utils/__pycache__/code_script.cpython-39.pyc +0 -0
  178. clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
  179. clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
  180. clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
  181. clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
  182. clarifai/runners/utils/__pycache__/method_signatures.cpython-311.pyc +0 -0
  183. clarifai/runners/utils/__pycache__/model_utils.cpython-311.pyc +0 -0
  184. clarifai/runners/utils/__pycache__/openai_convertor.cpython-311.pyc +0 -0
  185. clarifai/runners/utils/__pycache__/pipeline_validation.cpython-311.pyc +0 -0
  186. clarifai/runners/utils/__pycache__/serializers.cpython-311.pyc +0 -0
  187. clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
  188. clarifai/runners/utils/data_handler.py +231 -0
  189. clarifai/runners/utils/data_types/__pycache__/__init__.cpython-311.pyc +0 -0
  190. clarifai/runners/utils/data_types/__pycache__/__init__.cpython-39.pyc +0 -0
  191. clarifai/runners/utils/data_types/__pycache__/data_types.cpython-311.pyc +0 -0
  192. clarifai/runners/utils/data_types/__pycache__/data_types.cpython-39.pyc +0 -0
  193. clarifai/runners/utils/data_types.py +471 -0
  194. clarifai/runners/utils/loader.py +24 -4
  195. clarifai/runners/utils/temp.py +59 -0
  196. clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
  197. clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
  198. clarifai/urls/__pycache__/helper.cpython-39.pyc +0 -0
  199. clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  200. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  201. clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
  202. clarifai/utils/__pycache__/cli.cpython-39.pyc +0 -0
  203. clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
  204. clarifai/utils/__pycache__/config.cpython-39.pyc +0 -0
  205. clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
  206. clarifai/utils/__pycache__/constants.cpython-39.pyc +0 -0
  207. clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
  208. clarifai/utils/__pycache__/logging.cpython-39.pyc +0 -0
  209. clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
  210. clarifai/utils/__pycache__/misc.cpython-39.pyc +0 -0
  211. clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
  212. clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
  213. clarifai/utils/__pycache__/protobuf.cpython-39.pyc +0 -0
  214. clarifai/utils/__pycache__/secrets.cpython-311.pyc +0 -0
  215. clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
  216. clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
  217. clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
  218. clarifai/utils/evaluation/__pycache__/testset_annotation_parser.cpython-311.pyc +0 -0
  219. clarifai/utils/misc.py +20 -0
  220. clarifai/utils/secrets.py +96 -1
  221. clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
  222. clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
  223. clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
  224. clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
  225. {clarifai-11.7.5.dist-info → clarifai-11.7.5rc1.dist-info}/METADATA +1 -1
  226. clarifai-11.7.5rc1.dist-info/RECORD +339 -0
  227. {clarifai-11.7.5.dist-info → clarifai-11.7.5rc1.dist-info}/WHEEL +1 -1
  228. clarifai-11.7.5.dist-info/RECORD +0 -129
  229. {clarifai-11.7.5.dist-info → clarifai-11.7.5rc1.dist-info}/entry_points.txt +0 -0
  230. {clarifai-11.7.5.dist-info → clarifai-11.7.5rc1.dist-info}/licenses/LICENSE +0 -0
  231. {clarifai-11.7.5.dist-info → clarifai-11.7.5rc1.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
@@ -72,6 +72,17 @@ class HuggingFaceLoader:
72
72
  else:
73
73
  self.ignore_patterns = ignore_file_patterns
74
74
 
75
+ repo_files_to_download = self.get_repo_files_list(
76
+ allowed_file_patterns=allowed_file_patterns,
77
+ ignore_file_patterns=self.ignore_patterns,
78
+ )
79
+ total_size = self.get_huggingface_checkpoint_total_size(
80
+ self.repo_id, checkpoint_files_list=repo_files_to_download
81
+ )
82
+ total_size = total_size / (1024**2)
83
+ logger.info(f"Total download size: {total_size:.2f} MB")
84
+
85
+ logger.info("Downloading model checkpoints...")
75
86
  snapshot_download(
76
87
  repo_id=self.repo_id,
77
88
  local_dir=checkpoint_path,
@@ -134,9 +145,7 @@ class HuggingFaceLoader:
134
145
  else:
135
146
  return repo_exists(self.repo_id)
136
147
 
137
- def validate_download(
138
- self, checkpoint_path: str, allowed_file_patterns: list, ignore_file_patterns: list
139
- ):
148
+ def get_repo_files_list(self, allowed_file_patterns: list, ignore_file_patterns: list):
140
149
  # check if model exists on HF
141
150
  try:
142
151
  from huggingface_hub import list_repo_files
@@ -169,6 +178,14 @@ class HuggingFaceLoader:
169
178
  return any(fnmatch.fnmatch(file_path, pattern) for pattern in patterns)
170
179
 
171
180
  repo_files = [f for f in repo_files if not should_ignore(f)]
181
+ return repo_files
182
+
183
+ def validate_download(
184
+ self, checkpoint_path: str, allowed_file_patterns: list, ignore_file_patterns: list
185
+ ):
186
+ repo_files = self.get_repo_files_list(
187
+ allowed_file_patterns=allowed_file_patterns, ignore_file_patterns=ignore_file_patterns
188
+ )
172
189
 
173
190
  # Check if downloaded files match the files we expect (ignoring ignored patterns)
174
191
  checkpoint_dir_files = []
@@ -258,13 +275,14 @@ class HuggingFaceLoader:
258
275
  return labels
259
276
 
260
277
  @staticmethod
261
- def get_huggingface_checkpoint_total_size(repo_name):
278
+ def get_huggingface_checkpoint_total_size(repo_name, checkpoint_files_list=None):
262
279
  """
263
280
  Fetches the JSON data for a Hugging Face model using the API with `?blobs=true`.
264
281
  Calculates the total size from the JSON output.
265
282
 
266
283
  Args:
267
284
  repo_name (str): The name of the model on Hugging Face Hub. e.g. "casperhansen/llama-3-8b-instruct-awq"
285
+ checkpoint_files_list (list, optional): A list of specific files to include in the size calculation. If None, all files are included.
268
286
 
269
287
  Returns:
270
288
  int: The total size in bytes.
@@ -282,6 +300,8 @@ class HuggingFaceLoader:
282
300
 
283
301
  total_size = 0
284
302
  for file in data['siblings']:
303
+ if checkpoint_files_list and (file['rfilename'] not in checkpoint_files_list):
304
+ continue
285
305
  total_size += file['size']
286
306
  return total_size
287
307
  except Exception as e:
@@ -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
+
clarifai/utils/misc.py CHANGED
@@ -221,6 +221,26 @@ def clone_github_repo(repo_url, target_dir, github_pat=None, branch=None):
221
221
  return False
222
222
 
223
223
 
224
+ def get_list_of_files_to_download(
225
+ downloader, owner, repo, folder_path, branch, files_to_download, depth=1
226
+ ):
227
+ data = downloader.get_folder_contents(owner, repo, folder_path, branch=branch)
228
+ for point in data:
229
+ if (point['type'] == 'dir') and (depth > 0):
230
+ files_to_download = get_list_of_files_to_download(
231
+ downloader,
232
+ owner,
233
+ repo,
234
+ point['path'],
235
+ branch,
236
+ files_to_download,
237
+ depth=(depth - 1),
238
+ )
239
+ else:
240
+ files_to_download.append(point['path'])
241
+ return files_to_download
242
+
243
+
224
244
  class GitHubDownloader:
225
245
  def __init__(
226
246
  self, max_retries: int = 3, backoff_factor: float = 0.3, github_token: str = None