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
@@ -22,215 +22,211 @@ ui_https_cache = {}
22
22
 
23
23
 
24
24
  def clear_cache() -> None:
25
- """Clears the cache."""
26
- base_https_cache.clear()
27
- ui_https_cache.clear()
25
+ """Clears the cache."""
26
+ base_https_cache.clear()
27
+ ui_https_cache.clear()
28
28
 
29
29
 
30
30
  def https_cache(cache: dict, url: str) -> str:
31
- """This is a helper function to cache whether a url is http or https."""
32
- HTTPS = True
33
- HTTP = False
34
-
35
- # If http or https is provided, we trust that it is correct.
36
- # Note: this always stores the url without http:// or https://
37
- if url.startswith("https://"):
38
- url = url.replace("https://", "")
39
- cache[url] = HTTPS
40
- elif url.startswith("http://"):
41
- url = url.replace("http://", "")
42
- cache[url] = HTTP
43
- elif url not in cache:
44
- # We know our endpoints are https.
45
- host_name = urlparse(url).hostname
46
- if host_name and host_name.endswith(".clarifai.com"):
47
- cache[url] = HTTPS
48
- else: # need to test it.
49
- try: # make request to https endpoint.
50
- urllib.request.urlopen("https://%s/v2/auth/methods" % url, timeout=1)
51
- cache[url] = HTTPS # cache it.
52
- except Exception as e:
53
- if "SSL" in str(e): # if ssl error then we know it's http.
54
- cache[url] = HTTP
55
- # For http urls we need host:port format.
56
- if ":" not in url:
57
- raise Exception("When providing an insecure url it must have both host:port format")
58
- else:
59
- raise Exception("Could not get a valid response from url: %s, is the API running there?"
60
- % url) from e
61
- return url
31
+ """This is a helper function to cache whether a url is http or https."""
32
+ HTTPS = True
33
+ HTTP = False
34
+
35
+ # If http or https is provided, we trust that it is correct.
36
+ # Note: this always stores the url without http:// or https://
37
+ if url.startswith("https://"):
38
+ url = url.replace("https://", "")
39
+ cache[url] = HTTPS
40
+ elif url.startswith("http://"):
41
+ url = url.replace("http://", "")
42
+ cache[url] = HTTP
43
+ elif url not in cache:
44
+ # We know our endpoints are https.
45
+ host_name = urlparse(url).hostname
46
+ if host_name and host_name.endswith(".clarifai.com"):
47
+ cache[url] = HTTPS
48
+ else: # need to test it.
49
+ try: # make request to https endpoint.
50
+ urllib.request.urlopen("https://%s/v2/auth/methods" % url, timeout=1)
51
+ cache[url] = HTTPS # cache it.
52
+ except Exception as e:
53
+ if "SSL" in str(e): # if ssl error then we know it's http.
54
+ cache[url] = HTTP
55
+ # For http urls we need host:port format.
56
+ if ":" not in url:
57
+ raise Exception(
58
+ "When providing an insecure url it must have both host:port format"
59
+ )
60
+ else:
61
+ raise Exception(
62
+ "Could not get a valid response from url: %s, is the API running there?"
63
+ % url
64
+ ) from e
65
+ return url
62
66
 
63
67
 
64
68
  class ClarifaiAuthHelper:
69
+ def __init__(
70
+ self,
71
+ user_id: str = "",
72
+ app_id: str = "",
73
+ pat: str = "",
74
+ token: str = "",
75
+ base: str = DEFAULT_BASE,
76
+ ui: str = DEFAULT_UI,
77
+ root_certificates_path: str = None,
78
+ validate: bool = True,
79
+ ):
80
+ """
81
+ A helper to get the authorization information needed to make API calls with the grpc
82
+ client to a specific app using a personal access token.
83
+
84
+ There are classmethods to make this object easily from either query_params provided by streamlit or from env vars.
65
85
 
66
- def __init__(self,
67
- user_id: str = "",
68
- app_id: str = "",
69
- pat: str = "",
70
- token: str = "",
71
- base: str = DEFAULT_BASE,
72
- ui: str = DEFAULT_UI,
73
- root_certificates_path: str = None,
74
- validate: bool = True):
75
- """
76
- A helper to get the authorization information needed to make API calls with the grpc
77
- client to a specific app using a personal access token.
78
-
79
- There are classmethods to make this object easily from either query_params provided by streamlit or from env vars.
80
-
81
- Note: only one of personal access token (pat) or a session token (token) can be provided.
82
- Always use PATs in your code and never session tokens, those are only provided internal UI code.
83
-
84
- Args:
85
- user_id: a user id who owns the resource you want to make calls to.
86
- app_id: an app id for the application that owns the resource you want to interact with
87
- pat: a personal access token.
88
- token: a session token (internal use only, always use a PAT).
89
- base: a url to the API endpoint to hit. Examples include api.clarifai.com,
90
- https://api.clarifai.com (default), https://host:port, http://host:port, host:port (will be treated as http, not https). It's highly recommended to include the http:// or https:// otherwise we need to check the endpoint to determine if it has SSL during this __init__
91
- ui: a url to the UI. Examples include clarifai.com,
92
- https://clarifai.com (default), https://host:port, http://host:port, host:port (will be treated as http, not https). It's highly recommended to include the http:// or https:// otherwise we need to check the endpoint to determine if it has SSL during this __init__
93
- root_certificates_path: path to the root certificates file. This is only used for grpc secure channels.
94
- validate: whether to validate the inputs. This is useful for overriding vars then validating
95
- """
96
-
97
- self.user_id = user_id
98
- self.app_id = app_id
99
- self._pat = pat
100
- self._token = token
101
- self._root_certificates_path = root_certificates_path
102
-
103
- self.set_base(base)
104
- self.set_ui(ui)
105
- if validate:
106
- self.validate()
107
-
108
- def validate(self):
109
- if self.user_id == "":
110
- raise Exception(
111
- "Need 'user_id' to not be empty in the query params or user CLARIFAI_USER_ID env var")
112
- if self._pat != "" and self._token != "":
113
- raise Exception(
114
- "A personal access token OR a session token need to be provided, but you cannot provide both."
115
- )
116
- elif self._pat == "" and self._token == "":
117
- raise Exception(
118
- "Need 'pat' or 'token' in the query params or use one of the CLARIFAI_PAT or CLARIFAI_SESSION_TOKEN env vars"
119
- )
120
- if (self._root_certificates_path) and (not os.path.exists(self._root_certificates_path)):
121
- raise Exception("Root certificates path %s does not exist" % self._root_certificates_path)
122
-
123
- @classmethod
124
- def from_streamlit(cls, st: Any) -> "ClarifaiAuthHelper":
125
- """ This is a convenient method to check the environment variables first to see if there are
126
- required variables for auth, then override them with any additional query parameters that may
127
- have been passed in.
128
-
129
- Note: if a .streamlit/secrets.toml is present then st.secrets will auto populate the
130
- corresponding environment variables and we will pick them up from there, OVERWRITING whatever
131
- matching env var that may already be present.
86
+ Note: only one of personal access token (pat) or a session token (token) can be provided.
87
+ Always use PATs in your code and never session tokens, those are only provided internal UI code.
132
88
 
133
89
  Args:
134
- st: the streamlit package typically as: 'import streamlit as st'
135
- Returns:
136
- auth: this class instantiated
90
+ user_id: a user id who owns the resource you want to make calls to.
91
+ app_id: an app id for the application that owns the resource you want to interact with
92
+ pat: a personal access token.
93
+ token: a session token (internal use only, always use a PAT).
94
+ base: a url to the API endpoint to hit. Examples include api.clarifai.com,
95
+ https://api.clarifai.com (default), https://host:port, http://host:port, host:port (will be treated as http, not https). It's highly recommended to include the http:// or https:// otherwise we need to check the endpoint to determine if it has SSL during this __init__
96
+ ui: a url to the UI. Examples include clarifai.com,
97
+ https://clarifai.com (default), https://host:port, http://host:port, host:port (will be treated as http, not https). It's highly recommended to include the http:// or https:// otherwise we need to check the endpoint to determine if it has SSL during this __init__
98
+ root_certificates_path: path to the root certificates file. This is only used for grpc secure channels.
99
+ validate: whether to validate the inputs. This is useful for overriding vars then validating
100
+ """
101
+
102
+ self.user_id = user_id
103
+ self.app_id = app_id
104
+ self._pat = pat
105
+ self._token = token
106
+ self._root_certificates_path = root_certificates_path
107
+
108
+ self.set_base(base)
109
+ self.set_ui(ui)
110
+ if validate:
111
+ self.validate()
112
+
113
+ def validate(self):
114
+ if self.user_id == "":
115
+ raise Exception(
116
+ "Need 'user_id' to not be empty in the query params or user CLARIFAI_USER_ID env var"
117
+ )
118
+ if self._pat != "" and self._token != "":
119
+ raise Exception(
120
+ "A personal access token OR a session token need to be provided, but you cannot provide both."
121
+ )
122
+ elif self._pat == "" and self._token == "":
123
+ raise Exception(
124
+ "Need 'pat' or 'token' in the query params or use one of the CLARIFAI_PAT or CLARIFAI_SESSION_TOKEN env vars"
125
+ )
126
+ if (self._root_certificates_path) and (not os.path.exists(self._root_certificates_path)):
127
+ raise Exception(
128
+ "Root certificates path %s does not exist" % self._root_certificates_path
129
+ )
130
+
131
+ @classmethod
132
+ def from_streamlit(cls, st: Any) -> "ClarifaiAuthHelper":
133
+ """This is a convenient method to check the environment variables first to see if there are
134
+ required variables for auth, then override them with any additional query parameters that may
135
+ have been passed in.
136
+
137
+ Note: if a .streamlit/secrets.toml is present then st.secrets will auto populate the
138
+ corresponding environment variables and we will pick them up from there, OVERWRITING whatever
139
+ matching env var that may already be present.
140
+
141
+ Args:
142
+ st: the streamlit package typically as: 'import streamlit as st'
143
+ Returns:
144
+ auth: this class instantiated
145
+ """
146
+ # start with the env vars (potentially loaded from secrets.toml)
147
+ # Don't validate yet as we'll layer on the query params next.
148
+ auth = ClarifaiAuthHelper.from_env(validate=False)
149
+
150
+ # Then add in the query params.
151
+ try:
152
+ if st.query_params:
153
+ auth.add_streamlit_query_params(st.query_params)
154
+ else:
155
+ auth.add_streamlit_query_params(st.session_state)
156
+ except Exception as e:
157
+ st.error(e)
158
+ st.stop()
159
+ raise e
160
+
161
+ # Then validate.
162
+ try:
163
+ auth.validate()
164
+ except Exception as e:
165
+ st.error(e)
166
+ st.stop()
167
+ raise e
168
+
169
+ return auth
170
+
171
+ @classmethod
172
+ def from_streamlit_query_params(cls, query_params: Any = "") -> "ClarifaiAuthHelper":
173
+ """Initialize from streamlit queryparams. The following things will be looked for:
174
+ user_id: as 'user_id' in query_params
175
+ app_id: as 'app_id' in query_params
176
+ one of:
177
+ token: as 'token' in query_params
178
+ pat: as 'pat' in query_params
179
+ optionally:
180
+ base: as 'base' in query_params.
181
+ ui: as 'ui' in query_params.
182
+
137
183
  """
138
- # start with the env vars (potentially loaded from secrets.toml)
139
- # Don't validate yet as we'll layer on the query params next.
140
- auth = ClarifaiAuthHelper.from_env(validate=False)
141
-
142
- # Then add in the query params.
143
- try:
144
- auth.add_streamlit_query_params(dict(st.query_params))
145
- except Exception as e:
146
- st.error(e)
147
- st.stop()
148
- raise e
149
-
150
- # Then validate.
151
- try:
152
- auth.validate()
153
- except Exception as e:
154
- st.error(e)
155
- st.stop()
156
- raise e
157
-
158
- return auth
159
-
160
- @classmethod
161
- def from_streamlit_query_params(cls, query_params: Any = "") -> "ClarifaiAuthHelper":
162
- """Initialize from streamlit queryparams. The following things will be looked for:
163
- user_id: as 'user_id' in query_params
164
- app_id: as 'app_id' in query_params
165
- one of:
166
- token: as 'token' in query_params
167
- pat: as 'pat' in query_params
168
- optionally:
169
- base: as 'base' in query_params.
170
- ui: as 'ui' in query_params.
171
-
172
- """
173
-
174
- # Setup an empty one (not from env).
175
- auth = ClarifaiAuthHelper("", "", "", "", validate=False)
176
-
177
- # Then add in the query params.
178
- auth.add_streamlit_query_params(query_params)
179
-
180
- # Then validate.
181
- auth.validate()
182
-
183
- return auth
184
-
185
- def add_streamlit_query_params(self, query_params: Any = "") -> "ClarifaiAuthHelper":
186
- """Initialize from streamlit queryparams. The following things will be looked for:
187
- user_id: as 'user_id' in query_params
188
- app_id: as 'app_id' in query_params
189
- one of:
190
- token: as 'token' in query_params
191
- pat: as 'pat' in query_params
192
- optionally:
193
- base: as 'base' in query_params.
194
-
195
- Args:
196
- query_params: the streamlit.experimental_get_query_params() response or an empty dict to fall
197
- back to using env vars.
198
- """
199
- error_description = """
200
- Please check the following required query params are in the url:
201
- - 'user_id': the user ID accessing the module.
202
- - 'app_id': the app the module is being accessed from.
203
- - 'token' or 'pat': to authenticate the calling user with a session token or personal access token.
204
-
205
- Additionally, these optional params are supported:
206
- - 'base': the base domain for the API such as https://api.clarifai.com
207
- - 'ui': the overall UI domain for redirects such as https://clarifai.com
208
- """
209
-
210
- if query_params == "": # empty response from streamlit
211
- query_params = {}
212
- for k in ["user_id", "app_id", "token", "pat"]:
213
- if k in query_params and len(query_params[k]) != 1:
214
- err_str = "There should only be 1 query param value for key '%s'" % k
215
- raise Exception(err_str + error_description)
216
- if "user_id" in query_params:
217
- self.user_id = query_params["user_id"][0]
218
- if "app_id" in query_params:
219
- self.app_id = query_params["app_id"][0]
220
- if "token" in query_params:
221
- self._token = query_params["token"][0]
222
- if "pat" in query_params:
223
- self._pat = query_params["pat"][0]
224
- if "base" in query_params:
225
- self.set_base(query_params["base"][0])
226
- if "ui" in query_params:
227
- self.set_ui(query_params["ui"][0])
228
- if "root_certificates_path" in query_params:
229
- self._root_certificates_path = query_params["root_certificates_path"][0]
230
-
231
- @classmethod
232
- def from_env(cls, validate: bool = True) -> "ClarifaiAuthHelper":
233
- """Will look for the following env vars:
184
+
185
+ # Setup an empty one (not from env).
186
+ auth = ClarifaiAuthHelper("", "", "", "", validate=False)
187
+
188
+ # Then add in the query params.
189
+ auth.add_streamlit_query_params(query_params)
190
+
191
+ # Then validate.
192
+ auth.validate()
193
+
194
+ return auth
195
+
196
+ def add_streamlit_query_params(self, query_params: Any = "") -> "ClarifaiAuthHelper":
197
+ """Initialize from streamlit queryparams. The following things will be looked for:
198
+ user_id: as 'user_id' in query_params
199
+ app_id: as 'app_id' in query_params
200
+ one of:
201
+ token: as 'token' in query_params
202
+ pat: as 'pat' in query_params
203
+ optionally:
204
+ base: as 'base' in query_params.
205
+
206
+ Args:
207
+ query_params: the streamlit.query_params response or streamlit.session_state.
208
+ """
209
+
210
+ if query_params == "": # empty response from streamlit
211
+ query_params = {}
212
+ if "user_id" in query_params:
213
+ self.user_id = query_params["user_id"]
214
+ if "app_id" in query_params:
215
+ self.app_id = query_params["app_id"]
216
+ if "token" in query_params:
217
+ self._token = query_params["token"]
218
+ if "pat" in query_params:
219
+ self._pat = query_params["pat"]
220
+ if "base" in query_params:
221
+ self.set_base(query_params["base"])
222
+ if "ui" in query_params:
223
+ self.set_ui(query_params["ui"])
224
+ if "root_certificates_path" in query_params:
225
+ self._root_certificates_path = query_params["root_certificates_path"]
226
+
227
+ @classmethod
228
+ def from_env(cls, validate: bool = True) -> "ClarifaiAuthHelper":
229
+ """Will look for the following env vars:
234
230
  user_id: CLARIFAI_USER_ID env var.
235
231
  app_id: CLARIFAI_APP_ID env var.
236
232
  one of:
@@ -238,146 +234,153 @@ Additionally, these optional params are supported:
238
234
  pat: CLARIFAI_PAT env var.
239
235
  base: CLARIFAI_API_BASE env var.
240
236
  root_certificates_path: CLARIFAI_ROOT_CERTIFICATES_PATH env var.
241
- """
242
- user_id = os.environ.get("CLARIFAI_USER_ID", "")
243
- app_id = os.environ.get("CLARIFAI_APP_ID", "")
244
- token = os.environ.get(CLARIFAI_SESSION_TOKEN_ENV_VAR, "")
245
- pat = os.environ.get(CLARIFAI_PAT_ENV_VAR, "")
246
- base = os.environ.get("CLARIFAI_API_BASE", DEFAULT_BASE)
247
- ui = os.environ.get("CLARIFAI_UI", DEFAULT_UI)
248
- root_certificates_path = os.environ.get("CLARIFAI_ROOT_CERTIFICATES_PATH", None)
249
- return cls(user_id, app_id, pat, token, base, ui, root_certificates_path, validate)
250
-
251
- def get_user_app_id_proto(
252
- self,
253
- user_id: str = None,
254
- app_id: str = None,
255
- ) -> resources_pb2.UserAppIDSet:
256
- """Get the gRPC metadata that contains either the session token or the PAT to use.
257
-
258
- Args:
259
- user_id: optional user_id to override the default
260
- app_id: optional app_id to override the default.
261
-
262
- Returns:
263
- metadata: the metadata need to send with all grpc API calls in the API client.
264
- """
265
- user_id = self.user_id if user_id is None else user_id
266
- app_id = self.app_id if app_id is None else app_id
267
- return resources_pb2.UserAppIDSet(user_id=user_id, app_id=app_id)
268
-
269
- @property
270
- def metadata(self):
271
- """Get the gRPC metadata that contains either the session token or the PAT to use.
272
-
273
- Returns:
274
- metadata: the metadata need to send with all grpc API calls in the API client.
275
- """
276
- if self._pat != "":
277
- return (("authorization", "Key %s" % self._pat), (REQUEST_ID_PREFIX_HEADER,
278
- REQUEST_ID_PREFIX))
279
- elif self._token != "":
280
- return (("x-clarifai-session-token", self._token), (REQUEST_ID_PREFIX_HEADER,
281
- REQUEST_ID_PREFIX))
282
- else:
283
- raise Exception("'token' or 'pat' needed to be provided in the query params or env vars.")
284
-
285
- def get_stub(self) -> service_pb2_grpc.V2Stub:
286
- """Get the API gRPC stub using the right channel based on the API endpoint base.
287
-
288
- Returns:
289
- stub: The service_pb2_grpc.V2Stub stub for the API.
290
- """
291
- if self._base not in base_https_cache:
292
- raise Exception("Cannot determine if base %s is https" % self._base)
293
-
294
- https = base_https_cache[self._base]
295
- if https:
296
- channel = ClarifaiChannel.get_grpc_channel(
297
- base=self._base, root_certificates_path=self._root_certificates_path)
298
- else:
299
- if self._base.find(":") >= 0:
300
- host, port = self._base.split(":")
301
- else:
302
- host = self._base
303
- port = 80
304
- channel = ClarifaiChannel.get_insecure_grpc_channel(base=host, port=port)
305
- stub = service_pb2_grpc.V2Stub(channel)
306
- return stub
307
-
308
- @property
309
- def ui(self) -> str:
310
- """ Return the domain for the UI. """
311
- if self._ui not in ui_https_cache:
312
- raise Exception("Cannot determine if ui %s is https" % self._ui)
313
- https = ui_https_cache[self._ui]
314
- if https:
315
- if not self._ui.startswith("https://"):
316
- return "https://" + self._ui
317
- return self._ui
318
- if not self._ui.startswith("http://"):
319
- return "http://" + self._ui
320
- return self._ui
321
-
322
- def set_base(self, base: str):
323
- """ Set the base domain for the API. """
324
- base = DEFAULT_BASE if base is None else base
325
- self._base = https_cache(base_https_cache, base)
326
-
327
- def set_ui(self, ui: str):
328
- """ Set the domain for the UI. """
329
- self._ui = https_cache(ui_https_cache, ui)
330
-
331
- @property
332
- def base(self) -> str:
333
- """ Return the base domain for the API. """
334
- if self._base not in base_https_cache:
335
- raise Exception("Cannot determine if base %s is https" % self._base)
336
- https = base_https_cache[self._base]
337
- if https:
338
- if not self._base.startswith("https://"):
339
- return "https://" + self._base
340
- return self._base
341
- if not self._base.startswith("http://"):
342
- return "http://" + self._base
343
- return self._base
344
-
345
- @property
346
- def pat(self) -> str:
347
- """ Return the personal access token. """
348
- return self._pat
349
-
350
- def __str__(self):
351
- return "ClarifaiAuthHelper:\n- base: %s\n- user_id: %s\n- app_id: %s\n" % (
352
- self._base,
353
- self.user_id,
354
- self.app_id,
355
- )
356
-
357
- @classmethod
358
- def required_env_vars(cls):
359
- """ Return the list of the required environment variables. """
360
- return ["CLARIFAI_USER_ID", "CLARIFAI_APP_ID", "CLARIFAI_PAT"]
361
-
362
- @classmethod
363
- def validate_secrets_dict(cls, toml_dict: Dict[str, Any]):
364
- """ Validate the secrets.toml file has been filled with non-empty values for all the auth
365
- parameters that are present.
366
-
367
- We don't load the file here so that we don't need the tomli package dependency. You can simply
368
- do:
369
- import tomli
370
-
371
- d = tomli.load(open("secrets.toml"))
372
- ClarifaiAuthHelper.validate_secrets_dict(d)
373
-
374
- """
375
- # We don't validate the bases because they have sensible defaults
376
- auth_keys = cls.required_env_vars()
377
-
378
- for k, v in toml_dict.items():
379
- if k in auth_keys:
380
- if v == "":
381
- raise Exception("'%s' in secrets.toml cannot be empty" % k)
382
- # for all the keys that are not present, they have a non empty value.
383
- return True
237
+ """
238
+ user_id = os.environ.get("CLARIFAI_USER_ID", "")
239
+ app_id = os.environ.get("CLARIFAI_APP_ID", "")
240
+ token = os.environ.get(CLARIFAI_SESSION_TOKEN_ENV_VAR, "")
241
+ pat = os.environ.get(CLARIFAI_PAT_ENV_VAR, "")
242
+ base = os.environ.get("CLARIFAI_API_BASE", DEFAULT_BASE)
243
+ ui = os.environ.get("CLARIFAI_UI", DEFAULT_UI)
244
+ root_certificates_path = os.environ.get("CLARIFAI_ROOT_CERTIFICATES_PATH", None)
245
+ return cls(user_id, app_id, pat, token, base, ui, root_certificates_path, validate)
246
+
247
+ def get_user_app_id_proto(
248
+ self,
249
+ user_id: str = None,
250
+ app_id: str = None,
251
+ ) -> resources_pb2.UserAppIDSet:
252
+ """Get the gRPC metadata that contains either the session token or the PAT to use.
253
+
254
+ Args:
255
+ user_id: optional user_id to override the default
256
+ app_id: optional app_id to override the default.
257
+
258
+ Returns:
259
+ metadata: the metadata need to send with all grpc API calls in the API client.
260
+ """
261
+ user_id = self.user_id if user_id is None else user_id
262
+ app_id = self.app_id if app_id is None else app_id
263
+ return resources_pb2.UserAppIDSet(user_id=user_id, app_id=app_id)
264
+
265
+ @property
266
+ def metadata(self):
267
+ """Get the gRPC metadata that contains either the session token or the PAT to use.
268
+
269
+ Returns:
270
+ metadata: the metadata need to send with all grpc API calls in the API client.
271
+ """
272
+ if self._pat != "":
273
+ return (
274
+ ("authorization", "Key %s" % self._pat),
275
+ (REQUEST_ID_PREFIX_HEADER, REQUEST_ID_PREFIX),
276
+ )
277
+ elif self._token != "":
278
+ return (
279
+ ("x-clarifai-session-token", self._token),
280
+ (REQUEST_ID_PREFIX_HEADER, REQUEST_ID_PREFIX),
281
+ )
282
+ else:
283
+ raise Exception(
284
+ "'token' or 'pat' needed to be provided in the query params or env vars."
285
+ )
286
+
287
+ def get_stub(self) -> service_pb2_grpc.V2Stub:
288
+ """Get the API gRPC stub using the right channel based on the API endpoint base.
289
+
290
+ Returns:
291
+ stub: The service_pb2_grpc.V2Stub stub for the API.
292
+ """
293
+ if self._base not in base_https_cache:
294
+ raise Exception("Cannot determine if base %s is https" % self._base)
295
+
296
+ https = base_https_cache[self._base]
297
+ if https:
298
+ channel = ClarifaiChannel.get_grpc_channel(
299
+ base=self._base, root_certificates_path=self._root_certificates_path
300
+ )
301
+ else:
302
+ if self._base.find(":") >= 0:
303
+ host, port = self._base.split(":")
304
+ else:
305
+ host = self._base
306
+ port = 80
307
+ channel = ClarifaiChannel.get_insecure_grpc_channel(base=host, port=port)
308
+ stub = service_pb2_grpc.V2Stub(channel)
309
+ return stub
310
+
311
+ @property
312
+ def ui(self) -> str:
313
+ """Return the domain for the UI."""
314
+ if self._ui not in ui_https_cache:
315
+ raise Exception("Cannot determine if ui %s is https" % self._ui)
316
+ https = ui_https_cache[self._ui]
317
+ if https:
318
+ if not self._ui.startswith("https://"):
319
+ return "https://" + self._ui
320
+ return self._ui
321
+ if not self._ui.startswith("http://"):
322
+ return "http://" + self._ui
323
+ return self._ui
324
+
325
+ def set_base(self, base: str):
326
+ """Set the base domain for the API."""
327
+ base = DEFAULT_BASE if base is None else base
328
+ self._base = https_cache(base_https_cache, base)
329
+
330
+ def set_ui(self, ui: str):
331
+ """Set the domain for the UI."""
332
+ self._ui = https_cache(ui_https_cache, ui)
333
+
334
+ @property
335
+ def base(self) -> str:
336
+ """Return the base domain for the API."""
337
+ if self._base not in base_https_cache:
338
+ raise Exception("Cannot determine if base %s is https" % self._base)
339
+ https = base_https_cache[self._base]
340
+ if https:
341
+ if not self._base.startswith("https://"):
342
+ return "https://" + self._base
343
+ return self._base
344
+ if not self._base.startswith("http://"):
345
+ return "http://" + self._base
346
+ return self._base
347
+
348
+ @property
349
+ def pat(self) -> str:
350
+ """Return the personal access token."""
351
+ return self._pat
352
+
353
+ def __str__(self):
354
+ return "ClarifaiAuthHelper:\n- base: %s\n- user_id: %s\n- app_id: %s\n" % (
355
+ self._base,
356
+ self.user_id,
357
+ self.app_id,
358
+ )
359
+
360
+ @classmethod
361
+ def required_env_vars(cls):
362
+ """Return the list of the required environment variables."""
363
+ return ["CLARIFAI_USER_ID", "CLARIFAI_APP_ID", "CLARIFAI_PAT"]
364
+
365
+ @classmethod
366
+ def validate_secrets_dict(cls, toml_dict: Dict[str, Any]):
367
+ """Validate the secrets.toml file has been filled with non-empty values for all the auth
368
+ parameters that are present.
369
+
370
+ We don't load the file here so that we don't need the tomli package dependency. You can simply
371
+ do:
372
+ import tomli
373
+
374
+ d = tomli.load(open("secrets.toml"))
375
+ ClarifaiAuthHelper.validate_secrets_dict(d)
376
+
377
+ """
378
+ # We don't validate the bases because they have sensible defaults
379
+ auth_keys = cls.required_env_vars()
380
+
381
+ for k, v in toml_dict.items():
382
+ if k in auth_keys:
383
+ if v == "":
384
+ raise Exception("'%s' in secrets.toml cannot be empty" % k)
385
+ # for all the keys that are not present, they have a non empty value.
386
+ return True