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.
- clarifai/__init__.py +1 -1
- clarifai/cli/__main__.py +1 -1
- clarifai/cli/base.py +144 -136
- clarifai/cli/compute_cluster.py +45 -31
- clarifai/cli/deployment.py +93 -76
- clarifai/cli/model.py +578 -180
- clarifai/cli/nodepool.py +100 -82
- clarifai/client/__init__.py +12 -2
- clarifai/client/app.py +973 -911
- clarifai/client/auth/helper.py +345 -342
- clarifai/client/auth/register.py +7 -7
- clarifai/client/auth/stub.py +107 -106
- clarifai/client/base.py +185 -178
- clarifai/client/compute_cluster.py +214 -180
- clarifai/client/dataset.py +793 -698
- clarifai/client/deployment.py +55 -50
- clarifai/client/input.py +1223 -1088
- clarifai/client/lister.py +47 -45
- clarifai/client/model.py +1939 -1717
- clarifai/client/model_client.py +525 -502
- clarifai/client/module.py +82 -73
- clarifai/client/nodepool.py +358 -213
- clarifai/client/runner.py +58 -0
- clarifai/client/search.py +342 -309
- clarifai/client/user.py +419 -414
- clarifai/client/workflow.py +294 -274
- clarifai/constants/dataset.py +11 -17
- clarifai/constants/model.py +8 -2
- clarifai/datasets/export/inputs_annotations.py +233 -217
- clarifai/datasets/upload/base.py +63 -51
- clarifai/datasets/upload/features.py +43 -38
- clarifai/datasets/upload/image.py +237 -207
- clarifai/datasets/upload/loaders/coco_captions.py +34 -32
- clarifai/datasets/upload/loaders/coco_detection.py +72 -65
- clarifai/datasets/upload/loaders/imagenet_classification.py +57 -53
- clarifai/datasets/upload/loaders/xview_detection.py +274 -132
- clarifai/datasets/upload/multimodal.py +55 -46
- clarifai/datasets/upload/text.py +55 -47
- clarifai/datasets/upload/utils.py +250 -234
- clarifai/errors.py +51 -50
- clarifai/models/api.py +260 -238
- clarifai/modules/css.py +50 -50
- clarifai/modules/pages.py +33 -33
- clarifai/rag/rag.py +312 -288
- clarifai/rag/utils.py +91 -84
- clarifai/runners/models/model_builder.py +906 -802
- clarifai/runners/models/model_class.py +370 -331
- clarifai/runners/models/model_run_locally.py +459 -419
- clarifai/runners/models/model_runner.py +170 -162
- clarifai/runners/models/model_servicer.py +78 -70
- clarifai/runners/server.py +111 -101
- clarifai/runners/utils/code_script.py +225 -187
- clarifai/runners/utils/const.py +4 -1
- clarifai/runners/utils/data_types/__init__.py +12 -0
- clarifai/runners/utils/data_types/data_types.py +598 -0
- clarifai/runners/utils/data_utils.py +387 -440
- clarifai/runners/utils/loader.py +247 -227
- clarifai/runners/utils/method_signatures.py +411 -386
- clarifai/runners/utils/openai_convertor.py +108 -109
- clarifai/runners/utils/serializers.py +175 -179
- clarifai/runners/utils/url_fetcher.py +35 -35
- clarifai/schema/search.py +56 -63
- clarifai/urls/helper.py +125 -102
- clarifai/utils/cli.py +129 -123
- clarifai/utils/config.py +127 -87
- clarifai/utils/constants.py +49 -0
- clarifai/utils/evaluation/helpers.py +503 -466
- clarifai/utils/evaluation/main.py +431 -393
- clarifai/utils/evaluation/testset_annotation_parser.py +154 -144
- clarifai/utils/logging.py +324 -306
- clarifai/utils/misc.py +60 -56
- clarifai/utils/model_train.py +165 -146
- clarifai/utils/protobuf.py +126 -103
- clarifai/versions.py +3 -1
- clarifai/workflows/export.py +48 -50
- clarifai/workflows/utils.py +39 -36
- clarifai/workflows/validate.py +55 -43
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/METADATA +16 -6
- clarifai-11.4.0.dist-info/RECORD +109 -0
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/WHEEL +1 -1
- clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/__pycache__/errors.cpython-310.pyc +0 -0
- clarifai/__pycache__/errors.cpython-311.pyc +0 -0
- clarifai/__pycache__/versions.cpython-310.pyc +0 -0
- clarifai/__pycache__/versions.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
- clarifai/client/cli/__init__.py +0 -0
- clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/base_cli.py +0 -88
- clarifai/client/cli/model_cli.py +0 -29
- clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
- clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -31
- clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -42
- clarifai/runners/dockerfile_template/Dockerfile.nim +0 -71
- clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
- clarifai/runners/models/base_typed_model.py +0 -238
- clarifai/runners/models/model_class_refract.py +0 -80
- clarifai/runners/models/model_upload.py +0 -607
- clarifai/runners/models/temp.py +0 -25
- clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
- clarifai/runners/utils/data_handler.py +0 -231
- clarifai/runners/utils/data_handler_refract.py +0 -213
- clarifai/runners/utils/data_types.py +0 -469
- clarifai/runners/utils/logger.py +0 -0
- clarifai/runners/utils/openai_format.py +0 -87
- clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
- clarifai-11.3.0rc2.dist-info/RECORD +0 -322
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/entry_points.txt +0 -0
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info/licenses}/LICENSE +0 -0
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/top_level.txt +0 -0
clarifai/client/auth/helper.py
CHANGED
@@ -22,215 +22,211 @@ ui_https_cache = {}
|
|
22
22
|
|
23
23
|
|
24
24
|
def clear_cache() -> None:
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
67
|
-
|
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
|
-
|
135
|
-
|
136
|
-
|
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
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
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
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
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
|