clarifai 11.3.0rc2__py3-none-any.whl → 11.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (300) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/cli/__main__.py +1 -1
  3. clarifai/cli/base.py +144 -136
  4. clarifai/cli/compute_cluster.py +45 -31
  5. clarifai/cli/deployment.py +93 -76
  6. clarifai/cli/model.py +578 -180
  7. clarifai/cli/nodepool.py +100 -82
  8. clarifai/client/__init__.py +12 -2
  9. clarifai/client/app.py +973 -911
  10. clarifai/client/auth/helper.py +345 -342
  11. clarifai/client/auth/register.py +7 -7
  12. clarifai/client/auth/stub.py +107 -106
  13. clarifai/client/base.py +185 -178
  14. clarifai/client/compute_cluster.py +214 -180
  15. clarifai/client/dataset.py +793 -698
  16. clarifai/client/deployment.py +55 -50
  17. clarifai/client/input.py +1223 -1088
  18. clarifai/client/lister.py +47 -45
  19. clarifai/client/model.py +1939 -1717
  20. clarifai/client/model_client.py +525 -502
  21. clarifai/client/module.py +82 -73
  22. clarifai/client/nodepool.py +358 -213
  23. clarifai/client/runner.py +58 -0
  24. clarifai/client/search.py +342 -309
  25. clarifai/client/user.py +419 -414
  26. clarifai/client/workflow.py +294 -274
  27. clarifai/constants/dataset.py +11 -17
  28. clarifai/constants/model.py +8 -2
  29. clarifai/datasets/export/inputs_annotations.py +233 -217
  30. clarifai/datasets/upload/base.py +63 -51
  31. clarifai/datasets/upload/features.py +43 -38
  32. clarifai/datasets/upload/image.py +237 -207
  33. clarifai/datasets/upload/loaders/coco_captions.py +34 -32
  34. clarifai/datasets/upload/loaders/coco_detection.py +72 -65
  35. clarifai/datasets/upload/loaders/imagenet_classification.py +57 -53
  36. clarifai/datasets/upload/loaders/xview_detection.py +274 -132
  37. clarifai/datasets/upload/multimodal.py +55 -46
  38. clarifai/datasets/upload/text.py +55 -47
  39. clarifai/datasets/upload/utils.py +250 -234
  40. clarifai/errors.py +51 -50
  41. clarifai/models/api.py +260 -238
  42. clarifai/modules/css.py +50 -50
  43. clarifai/modules/pages.py +33 -33
  44. clarifai/rag/rag.py +312 -288
  45. clarifai/rag/utils.py +91 -84
  46. clarifai/runners/models/model_builder.py +906 -802
  47. clarifai/runners/models/model_class.py +370 -331
  48. clarifai/runners/models/model_run_locally.py +459 -419
  49. clarifai/runners/models/model_runner.py +170 -162
  50. clarifai/runners/models/model_servicer.py +78 -70
  51. clarifai/runners/server.py +111 -101
  52. clarifai/runners/utils/code_script.py +225 -187
  53. clarifai/runners/utils/const.py +4 -1
  54. clarifai/runners/utils/data_types/__init__.py +12 -0
  55. clarifai/runners/utils/data_types/data_types.py +598 -0
  56. clarifai/runners/utils/data_utils.py +387 -440
  57. clarifai/runners/utils/loader.py +247 -227
  58. clarifai/runners/utils/method_signatures.py +411 -386
  59. clarifai/runners/utils/openai_convertor.py +108 -109
  60. clarifai/runners/utils/serializers.py +175 -179
  61. clarifai/runners/utils/url_fetcher.py +35 -35
  62. clarifai/schema/search.py +56 -63
  63. clarifai/urls/helper.py +125 -102
  64. clarifai/utils/cli.py +129 -123
  65. clarifai/utils/config.py +127 -87
  66. clarifai/utils/constants.py +49 -0
  67. clarifai/utils/evaluation/helpers.py +503 -466
  68. clarifai/utils/evaluation/main.py +431 -393
  69. clarifai/utils/evaluation/testset_annotation_parser.py +154 -144
  70. clarifai/utils/logging.py +324 -306
  71. clarifai/utils/misc.py +60 -56
  72. clarifai/utils/model_train.py +165 -146
  73. clarifai/utils/protobuf.py +126 -103
  74. clarifai/versions.py +3 -1
  75. clarifai/workflows/export.py +48 -50
  76. clarifai/workflows/utils.py +39 -36
  77. clarifai/workflows/validate.py +55 -43
  78. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/METADATA +16 -6
  79. clarifai-11.4.0.dist-info/RECORD +109 -0
  80. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/WHEEL +1 -1
  81. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  82. clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
  83. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  84. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  85. clarifai/__pycache__/errors.cpython-311.pyc +0 -0
  86. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  87. clarifai/__pycache__/versions.cpython-311.pyc +0 -0
  88. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  89. clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
  90. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  91. clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
  92. clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  93. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  94. clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  95. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  96. clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
  97. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  98. clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
  99. clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  100. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  101. clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
  102. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  103. clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
  104. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  105. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  106. clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
  107. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  108. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  109. clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
  110. clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  111. clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  112. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  113. clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
  114. clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
  115. clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
  116. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  117. clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
  118. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  119. clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
  120. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  121. clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
  122. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  123. clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
  124. clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
  125. clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
  126. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  127. clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
  128. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  129. clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
  130. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  131. clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
  132. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  133. clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
  134. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  135. clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
  136. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  137. clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
  138. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  139. clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
  140. clarifai/client/cli/__init__.py +0 -0
  141. clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  142. clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  143. clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  144. clarifai/client/cli/base_cli.py +0 -88
  145. clarifai/client/cli/model_cli.py +0 -29
  146. clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
  147. clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
  148. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  149. clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
  150. clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
  151. clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
  152. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  153. clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
  154. clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
  155. clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
  156. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  157. clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
  158. clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
  159. clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
  160. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  161. clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
  162. clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  163. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  164. clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
  165. clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
  166. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  167. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
  168. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  169. clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
  170. clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
  171. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  172. clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
  173. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  174. clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
  175. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  176. clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
  177. clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
  178. clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
  179. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  180. clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
  181. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  182. clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
  183. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
  184. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
  185. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
  186. clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
  187. clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
  188. clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
  189. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  190. clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
  191. clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
  192. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  193. clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
  194. clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
  195. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  196. clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
  197. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  198. clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
  199. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  200. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -31
  201. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -42
  202. clarifai/runners/dockerfile_template/Dockerfile.nim +0 -71
  203. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  204. clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
  205. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  206. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  207. clarifai/runners/models/__pycache__/base_typed_model.cpython-311.pyc +0 -0
  208. clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
  209. clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
  210. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  211. clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
  212. clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
  213. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  214. clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
  215. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  216. clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
  217. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  218. clarifai/runners/models/base_typed_model.py +0 -238
  219. clarifai/runners/models/model_class_refract.py +0 -80
  220. clarifai/runners/models/model_upload.py +0 -607
  221. clarifai/runners/models/temp.py +0 -25
  222. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  223. clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  224. clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
  225. clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  226. clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
  227. clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
  228. clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
  229. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  230. clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
  231. clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
  232. clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
  233. clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
  234. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  235. clarifai/runners/utils/__pycache__/data_handler.cpython-311.pyc +0 -0
  236. clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
  237. clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
  238. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  239. clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
  240. clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
  241. clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
  242. clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
  243. clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
  244. clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
  245. clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
  246. clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
  247. clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
  248. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  249. clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
  250. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  251. clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
  252. clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
  253. clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
  254. clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
  255. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  256. clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
  257. clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
  258. clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
  259. clarifai/runners/utils/data_handler.py +0 -231
  260. clarifai/runners/utils/data_handler_refract.py +0 -213
  261. clarifai/runners/utils/data_types.py +0 -469
  262. clarifai/runners/utils/logger.py +0 -0
  263. clarifai/runners/utils/openai_format.py +0 -87
  264. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  265. clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
  266. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  267. clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
  268. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  269. clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  270. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  271. clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
  272. clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
  273. clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
  274. clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
  275. clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
  276. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  277. clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
  278. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  279. clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
  280. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  281. clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
  282. clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
  283. clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
  284. clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
  285. clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
  286. clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
  287. clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
  288. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  289. clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
  290. clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
  291. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  292. clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
  293. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  294. clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
  295. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  296. clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
  297. clarifai-11.3.0rc2.dist-info/RECORD +0 -322
  298. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/entry_points.txt +0 -0
  299. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info/licenses}/LICENSE +0 -0
  300. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/top_level.txt +0 -0
@@ -1,469 +0,0 @@
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
-
18
- def to_proto(self):
19
- raise NotImplementedError
20
-
21
- @classmethod
22
- def from_proto(cls, proto):
23
- raise NotImplementedError
24
-
25
- @classmethod
26
- def from_value(cls, value):
27
- if isinstance(value, cls):
28
- return value
29
- return cls(value)
30
-
31
- def cast(self, python_type):
32
- if python_type == self.__class__:
33
- return self
34
- raise TypeError(f'Incompatible type for {self.__class__.__name__}: {python_type}')
35
-
36
-
37
- class NamedFieldsMeta(type):
38
- """Metaclass to create NamedFields subclasses with __annotations__ when fields are specified."""
39
-
40
- def __call__(cls, *args, **kwargs):
41
- # Check if keyword arguments are types (used in type annotations)
42
- if kwargs and all(isinstance(v, type) for v in kwargs.values()):
43
- # Dynamically create a subclass with __annotations__
44
- name = f"NamedFields({', '.join(f'{k}:{v.__name__}' for k, v in kwargs.items())})"
45
- return type(name, (cls,), {'__annotations__': kwargs})
46
- else:
47
- # Create a normal instance for runtime data
48
- return super().__call__(*args, **kwargs)
49
-
50
-
51
- class NamedFields(metaclass=NamedFieldsMeta):
52
- """A class that can be used to store named fields with values."""
53
-
54
- def __init__(self, **kwargs):
55
- for key, value in kwargs.items():
56
- setattr(self, key, value)
57
-
58
- def items(self):
59
- return self.__dict__.items()
60
-
61
- def keys(self):
62
- return self.__dict__.keys()
63
-
64
- def values(self):
65
- return self.__dict__.values()
66
-
67
- def __contains__(self, key):
68
- return key in self.__dict__
69
-
70
- def __getitem__(self, key):
71
- return getattr(self, key)
72
-
73
- def __setitem__(self, key, value):
74
- setattr(self, key, value)
75
-
76
- def __repr__(self):
77
- return f"{self.__class__.__name__}({', '.join(f'{key}={value!r}' for key, value in self.__dict__.items())})"
78
-
79
- def __origin__(self):
80
- return self
81
-
82
- def __args__(self):
83
- return list(self.keys())
84
-
85
-
86
- class JSON:
87
-
88
- def __init__(self, value):
89
- self.value = value
90
-
91
- def __eq__(self, other):
92
- return self.value == other
93
-
94
- def __bool__(self):
95
- return bool(self.value)
96
-
97
- def __repr__(self):
98
- return f"JSON({self.value})"
99
-
100
- def to_json(self):
101
- return json.dumps(self.value)
102
-
103
- @classmethod
104
- def from_json(cls, json_str):
105
- return cls(json.loads(json_str))
106
-
107
- @classmethod
108
- def from_value(cls, value):
109
- return cls(value)
110
-
111
- def cast(self, python_type):
112
- if not isinstance(self.value, python_type):
113
- raise TypeError(f'Incompatible type {type(self.value)} for {python_type}')
114
- return self.value
115
-
116
-
117
- class Text(MessageData):
118
-
119
- def __init__(self, text: str, url: str = None):
120
- self.text = text
121
- self.url = url
122
-
123
- def __eq__(self, other):
124
- if isinstance(other, Text):
125
- return self.text == other.text and self.url == other.url
126
- if isinstance(other, str):
127
- return self.text == other
128
- return False
129
-
130
- def __bool__(self):
131
- return bool(self.text) or bool(self.url)
132
-
133
- def __repr__(self) -> str:
134
- return f"Text(text={self.text!r}, url={self.url!r})"
135
-
136
- def to_proto(self) -> TextProto:
137
- return TextProto(raw=self.text or '', url=self.url or '')
138
-
139
- @classmethod
140
- def from_proto(cls, proto: TextProto) -> "Text":
141
- return cls(proto.raw, proto.url or None)
142
-
143
- @classmethod
144
- def from_value(cls, value):
145
- if isinstance(value, str):
146
- return cls(value)
147
- if isinstance(value, Text):
148
- return value
149
- if isinstance(value, dict):
150
- return cls(value.get('text'), value.get('url'))
151
- raise TypeError(f'Incompatible type for Text: {type(value)}')
152
-
153
- def cast(self, python_type):
154
- if python_type == str:
155
- return self.text
156
- if python_type == Text:
157
- return self
158
- raise TypeError(f'Incompatible type for Text: {python_type}')
159
-
160
-
161
- class Concept(MessageData):
162
-
163
- def __init__(self, name: str, value: float = 1):
164
- self.id = name
165
- self.name = name
166
- self.value = value
167
-
168
- def __repr__(self) -> str:
169
- return f"Concept(id={self.id!r}, name={self.name!r}, value={self.value})"
170
-
171
- def to_proto(self):
172
- return ConceptProto(id=self.id, name=self.name, value=self.value)
173
-
174
- @classmethod
175
- def from_proto(cls, proto: ConceptProto) -> "Concept":
176
- return cls(proto.name, proto.value)
177
-
178
-
179
- class Region(MessageData):
180
-
181
- def __init__(self,
182
- proto_region: RegionProto = None,
183
- box: List[float] = None,
184
- concepts: List[Concept] = None):
185
- if proto_region is None:
186
- proto_region = RegionProto()
187
- self.proto = proto_region
188
- # use setters for init vals
189
- if box and isinstance(
190
- box, list) and len(box) == 4 and all(isinstance(val, (int, float)) for val in box):
191
- self.box = box
192
- if concepts and isinstance(concepts,
193
- list) and all(isinstance(concept, Concept) for concept in concepts):
194
- self.concepts = concepts
195
-
196
- @property
197
- def box(self) -> List[float]:
198
- bbox = self.proto.region_info.bounding_box
199
- return [bbox.left_col, bbox.top_row, bbox.right_col, bbox.bottom_row] # x1, y1, x2, y2
200
-
201
- @box.setter
202
- def box(self, value: List[float]):
203
- bbox = self.proto.region_info.bounding_box
204
- bbox.left_col, bbox.top_row, bbox.right_col, bbox.bottom_row = value
205
-
206
- @property
207
- def concepts(self) -> List[Concept]:
208
- return [Concept.from_proto(proto) for proto in self.proto.data.concepts]
209
-
210
- @concepts.setter
211
- def concepts(self, value: List[Concept]):
212
- self.proto.data.concepts.extend([concept.to_proto() for concept in value])
213
-
214
- def __repr__(self) -> str:
215
- return f"Region(box={self.box}, concepts={self.concepts})"
216
-
217
- def to_proto(self) -> RegionProto:
218
- return self.proto
219
-
220
- @classmethod
221
- def from_proto(cls, proto: RegionProto) -> "Region":
222
- return cls(proto)
223
-
224
-
225
- class Image(MessageData):
226
-
227
- def __init__(self, proto_image: ImageProto = None, url: str = None, bytes: bytes = None):
228
- if proto_image is None:
229
- proto_image = ImageProto()
230
- self.proto = proto_image
231
- # use setters for init vals
232
- if url:
233
- self.url = url
234
- if bytes:
235
- self.bytes = bytes
236
-
237
- @property
238
- def url(self) -> str:
239
- return self.proto.url
240
-
241
- @url.setter
242
- def url(self, value: str):
243
- self.proto.url = value
244
-
245
- @property
246
- def bytes(self) -> bytes:
247
- return self.proto.base64
248
-
249
- @bytes.setter
250
- def bytes(self, value: bytes):
251
- self.proto.base64 = value
252
-
253
- def __repr__(self) -> str:
254
- attrs = []
255
- if self.url:
256
- attrs.append(f"url={self.url!r}")
257
- if self.bytes:
258
- attrs.append(f"bytes=<{len(self.bytes)} bytes>")
259
- return f"Image({', '.join(attrs)})"
260
-
261
- @classmethod
262
- def from_url(cls, url: str) -> "Image":
263
- proto_image = ImageProto(url=url)
264
- return cls(proto_image)
265
-
266
- @classmethod
267
- def from_pil(cls, pil_image: PILImage.Image) -> "Image":
268
- with io.BytesIO() as output:
269
- pil_image.save(output, format="PNG")
270
- image_bytes = output.getvalue()
271
- proto_image = ImageProto(base64=image_bytes)
272
- return cls(proto_image)
273
-
274
- def to_pil(self) -> PILImage.Image:
275
- if not self.proto.base64:
276
- raise ValueError("Image has no bytes")
277
- return PILImage.open(io.BytesIO(self.proto.base64))
278
-
279
- def to_numpy(self) -> np.ndarray:
280
- return np.asarray(self.to_pil())
281
-
282
- def to_proto(self) -> ImageProto:
283
- return self.proto
284
-
285
- @classmethod
286
- def from_proto(cls, proto: ImageProto) -> "Image":
287
- return cls(proto)
288
-
289
- @classmethod
290
- def from_value(cls, value):
291
- if isinstance(value, PILImage.Image):
292
- return cls.from_pil(value)
293
- if isinstance(value, Image):
294
- return value
295
- raise TypeError(f'Incompatible type for Image: {type(value)}')
296
-
297
- def cast(self, python_type):
298
- if python_type == Image:
299
- return self
300
- if python_type in (PILImage.Image, PILImage):
301
- return self.to_pil()
302
- if python_type == np.ndarray or get_origin(python_type) == np.ndarray:
303
- return self.to_numpy()
304
- raise TypeError(f'Incompatible type for Image: {python_type}')
305
-
306
-
307
- class Audio(MessageData):
308
-
309
- def __init__(self, proto_audio: AudioProto = None, url: str = None, bytes: bytes = None):
310
- if proto_audio is None:
311
- proto_audio = AudioProto()
312
- self.proto = proto_audio
313
-
314
- if url:
315
- self.url = url
316
- if bytes:
317
- self.bytes = bytes
318
-
319
- @property
320
- def url(self) -> str:
321
- return self.proto.url
322
-
323
- @url.setter
324
- def url(self, value: str):
325
- self.proto.url = value
326
-
327
- @property
328
- def bytes(self) -> bytes:
329
- return self.proto.base64
330
-
331
- @bytes.setter
332
- def bytes(self, value: bytes):
333
- self.proto.base64 = value
334
-
335
- @classmethod
336
- def from_url(cls, url: str) -> "Audio":
337
- proto_audio = AudioProto(url=url)
338
- return cls(proto_audio)
339
-
340
- def __repr__(self) -> str:
341
- attrs = []
342
- if self.url:
343
- attrs.append(f"url={self.url!r}")
344
- if self.bytes:
345
- attrs.append(f"bytes=<{len(self.bytes)} bytes>")
346
- return f"Audio({', '.join(attrs)})"
347
-
348
- def to_proto(self) -> AudioProto:
349
- return self.proto
350
-
351
- @classmethod
352
- def from_proto(cls, proto: AudioProto) -> "Audio":
353
- return cls(proto)
354
-
355
-
356
- class Frame(MessageData):
357
-
358
- def __init__(self,
359
- proto_frame: FrameProto = None,
360
- image: Image = None,
361
- regions: List[Region] = None,
362
- time: float = None):
363
- if proto_frame is None:
364
- proto_frame = FrameProto()
365
- self.proto = proto_frame
366
- # use setters for init vals
367
- if image:
368
- self.image = image
369
- if regions:
370
- self.regions = regions
371
- if time:
372
- self.time = time
373
-
374
- @property
375
- def time(self) -> float:
376
- # TODO: time is a uint32, so this will overflow at 49.7 days
377
- # we should be using double or uint64 in the proto instead
378
- return self.proto.frame_info.time / 1000.0
379
-
380
- @time.setter
381
- def time(self, value: float):
382
- self.proto.frame_info.time = int(value * 1000)
383
-
384
- @property
385
- def image(self) -> Image:
386
- return Image.from_proto(self.proto.data.image)
387
-
388
- @image.setter
389
- def image(self, value: Image):
390
- self.proto.data.image.CopyFrom(value.to_proto())
391
-
392
- @property
393
- def regions(self) -> List[Region]:
394
- return [Region(region) for region in self.proto.data.regions]
395
-
396
- @regions.setter
397
- def regions(self, value: List[Region]):
398
- self.proto.data.regions.extend([region.proto for region in value])
399
-
400
- def to_proto(self) -> FrameProto:
401
- return self.proto
402
-
403
- @classmethod
404
- def from_proto(cls, proto: FrameProto) -> "Frame":
405
- return cls(proto)
406
-
407
-
408
- class Video(MessageData):
409
-
410
- def __init__(self, proto_video: VideoProto = None, url: str = None, bytes: bytes = None):
411
- if proto_video is None:
412
- proto_video = VideoProto()
413
- self.proto = proto_video
414
-
415
- if url:
416
- self.url = url
417
- if bytes:
418
- self.bytes = bytes
419
-
420
- @property
421
- def url(self) -> str:
422
- return self.proto.url
423
-
424
- @url.setter
425
- def url(self, value: str):
426
- self.proto.url = value
427
-
428
- @property
429
- def bytes(self) -> bytes:
430
- return self.proto.base64
431
-
432
- @bytes.setter
433
- def bytes(self, value: bytes):
434
- self.proto.base64 = value
435
-
436
- @classmethod
437
- def from_url(cls, url: str) -> "Video":
438
- proto_video = VideoProto(url=url)
439
- return cls(proto_video)
440
-
441
- def __repr__(self) -> str:
442
- attrs = []
443
- if self.url:
444
- attrs.append(f"url={self.url!r}")
445
- if self.bytes:
446
- attrs.append(f"bytes=<{len(self.bytes)} bytes>")
447
- return f"Video({', '.join(attrs)})"
448
-
449
- def to_proto(self) -> VideoProto:
450
- return self.proto
451
-
452
- @classmethod
453
- def from_proto(cls, proto: VideoProto) -> "Video":
454
- return cls(proto)
455
-
456
-
457
- def cast(value, python_type):
458
- list_type = (get_origin(python_type) == list)
459
- if isinstance(value, MessageData):
460
- return value.cast(python_type)
461
- if list_type and isinstance(value, np.ndarray):
462
- return value.tolist()
463
- if list_type and isinstance(value, list):
464
- if get_args(python_type):
465
- inner_type = get_args(python_type)[0]
466
- return [cast(item, inner_type) for item in value]
467
- if not isinstance(value, Iterable):
468
- raise TypeError(f'Expected list, got {type(value)}')
469
- return value
File without changes
@@ -1,87 +0,0 @@
1
- import time
2
- import uuid
3
-
4
- def generate_id():
5
- return f"chatcmpl-{uuid.uuid4().hex}"
6
-
7
- def format_non_streaming_response(
8
- generated_text,
9
- model="custom-model",
10
- id=None,
11
- created=None,
12
- usage=None,
13
- finish_reason="stop",
14
- ):
15
- if id is None:
16
- id = generate_id()
17
- if created is None:
18
- created = int(time.time())
19
-
20
- response = {
21
- "id": id,
22
- "object": "chat.completion",
23
- "created": created,
24
- "model": model,
25
- "choices": [
26
- {
27
- "index": 0,
28
- "message": {
29
- "role": "assistant",
30
- "content": generated_text,
31
- },
32
- "finish_reason": finish_reason,
33
- "logprobs": None,
34
- }
35
- ],
36
- }
37
-
38
- if usage is not None:
39
- response["usage"] = usage
40
-
41
- return response
42
-
43
- def format_streaming_response(
44
- generated_chunks,
45
- model="custom-model",
46
- id=None,
47
- created=None,
48
- finish_reason="stop",
49
- ):
50
- if id is None:
51
- id = generate_id()
52
- if created is None:
53
- created = int(time.time())
54
-
55
- for chunk in generated_chunks:
56
- yield {
57
- "id": id,
58
- "object": "chat.completion.chunk",
59
- "created": created,
60
- "model": model,
61
- "choices": [
62
- {
63
- "index": 0,
64
- "delta": {
65
- "content": chunk,
66
- },
67
- "finish_reason": None,
68
- "logprobs": None,
69
- }
70
- ],
71
- }
72
-
73
- # Final chunk indicating completion
74
- yield {
75
- "id": id,
76
- "object": "chat.completion.chunk",
77
- "created": created,
78
- "model": model,
79
- "choices": [
80
- {
81
- "index": 0,
82
- "delta": {},
83
- "finish_reason": finish_reason,
84
- "logprobs": None,
85
- }
86
- ],
87
- }