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/utils/misc.py
CHANGED
@@ -8,95 +8,99 @@ from clarifai_grpc.grpc.api.status import status_code_pb2
|
|
8
8
|
from clarifai.errors import UserError
|
9
9
|
|
10
10
|
RETRYABLE_CODES = [
|
11
|
-
status_code_pb2.MODEL_DEPLOYING,
|
12
|
-
status_code_pb2.
|
11
|
+
status_code_pb2.MODEL_DEPLOYING,
|
12
|
+
status_code_pb2.MODEL_LOADING,
|
13
|
+
status_code_pb2.MODEL_BUSY_PLEASE_RETRY,
|
13
14
|
]
|
14
15
|
|
15
16
|
DEFAULT_CONFIG = f'{os.environ["HOME"]}/.config/clarifai/config'
|
16
17
|
|
17
18
|
|
18
19
|
def status_is_retryable(status_code: int) -> bool:
|
19
|
-
|
20
|
-
|
20
|
+
"""Check if a status code is retryable."""
|
21
|
+
return status_code in RETRYABLE_CODES
|
21
22
|
|
22
23
|
|
23
24
|
class Chunker:
|
24
|
-
|
25
|
+
"""Split an input sequence into small chunks."""
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def __init__(self, seq: List, size: int) -> None:
|
28
|
+
self.seq = seq
|
29
|
+
self.size = size
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def chunk(self) -> List[List]:
|
32
|
+
"""Chunk input sequence."""
|
33
|
+
return [self.seq[pos : pos + self.size] for pos in range(0, len(self.seq), self.size)]
|
33
34
|
|
34
35
|
|
35
36
|
class BackoffIterator:
|
36
|
-
|
37
|
+
"""Iterator that returns a sequence of backoff values."""
|
37
38
|
|
38
|
-
|
39
|
-
|
39
|
+
def __init__(self, count=0):
|
40
|
+
self.count = count
|
40
41
|
|
41
|
-
|
42
|
-
|
42
|
+
def __iter__(self):
|
43
|
+
return self
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
def __next__(self):
|
46
|
+
self.count += 1
|
47
|
+
return 0.1 * (1.3**self.count)
|
47
48
|
|
48
49
|
|
49
50
|
def get_from_dict_or_env(key: str, env_key: str, **data) -> str:
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
"""Get a value from a dictionary or an environment variable."""
|
52
|
+
if key in data and data[key]:
|
53
|
+
return data[key]
|
54
|
+
else:
|
55
|
+
return get_from_env(key, env_key)
|
55
56
|
|
56
57
|
|
57
58
|
def get_from_env(key: str, env_key: str) -> str:
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
"""Get a value from a dictionary or an environment variable."""
|
60
|
+
if env_key in os.environ and os.environ[env_key]:
|
61
|
+
return os.environ[env_key]
|
62
|
+
else:
|
63
|
+
raise UserError(
|
64
|
+
f"Did not find `{key}`, please add an environment variable"
|
65
|
+
f" `{env_key}` which contains it, or pass"
|
66
|
+
f" `{key}` as a named parameter."
|
67
|
+
)
|
65
68
|
|
66
69
|
|
67
|
-
def concept_relations_accumulation(
|
68
|
-
|
69
|
-
|
70
|
+
def concept_relations_accumulation(
|
71
|
+
relations_dict: Dict[str, Any], subject_concept: str, object_concept: str, predicate: str
|
72
|
+
) -> Dict[str, Any]:
|
73
|
+
"""Append the concept relation to relations dict based on its predicate.
|
70
74
|
|
71
75
|
Args:
|
72
76
|
relations_dict (dict): A dict of concept relations info.
|
73
77
|
"""
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
78
|
+
if predicate == 'hyponym':
|
79
|
+
if object_concept in relations_dict:
|
80
|
+
relations_dict[object_concept].append(subject_concept)
|
81
|
+
else:
|
82
|
+
relations_dict[object_concept] = [subject_concept]
|
83
|
+
elif predicate == 'hypernym':
|
84
|
+
if subject_concept in relations_dict:
|
85
|
+
relations_dict[subject_concept].append(object_concept)
|
86
|
+
else:
|
87
|
+
relations_dict[subject_concept] = [object_concept]
|
82
88
|
else:
|
83
|
-
|
84
|
-
|
85
|
-
relations_dict
|
86
|
-
relations_dict[subject_concept] = []
|
87
|
-
return relations_dict
|
89
|
+
relations_dict[object_concept] = []
|
90
|
+
relations_dict[subject_concept] = []
|
91
|
+
return relations_dict
|
88
92
|
|
89
93
|
|
90
94
|
def get_uuid(val: int) -> str:
|
91
|
-
|
92
|
-
|
95
|
+
"""Generates a UUID."""
|
96
|
+
return uuid.uuid4().hex[:val]
|
93
97
|
|
94
98
|
|
95
99
|
def clean_input_id(input_id: str) -> str:
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
100
|
+
"""Clean input_id string into a valid input ID"""
|
101
|
+
input_id = re.sub('[., /]+', '_', input_id)
|
102
|
+
input_id = re.sub('[_]+', '_', input_id)
|
103
|
+
input_id = re.sub('[-]+', '-', input_id)
|
104
|
+
input_id = input_id.lower().strip('_-')
|
105
|
+
input_id = re.sub('[^a-z0-9-_]+', '', input_id)
|
106
|
+
return input_id
|
clarifai/utils/model_train.py
CHANGED
@@ -7,156 +7,175 @@ from google.protobuf.struct_pb2 import Struct
|
|
7
7
|
|
8
8
|
|
9
9
|
def response_to_templates(response: MultiModelTypeResponse, model_type_id: str) -> List[str]:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
params
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
params["train_params"][_path[-1]] = modeltypefield['defaultValue']
|
49
|
-
except Exception:
|
50
|
-
params["train_params"][_path[-1]] = None
|
51
|
-
if _path[0] == 'output_info':
|
52
|
-
params["inference_params"] = dict()
|
53
|
-
try:
|
54
|
-
params["inference_params"][_path[-1]] = modeltypefield['defaultValue']
|
55
|
-
except Exception:
|
56
|
-
params["inference_params"][_path[-1]] = None
|
57
|
-
if _path[0] == 'output_info' and _path[1] == 'output_config':
|
58
|
-
params["concepts_mutually_exclusive"] = (modeltypefield['defaultValue'])
|
59
|
-
else:
|
60
|
-
if 'modelTypeEnumOptions' in modeltypefield.keys():
|
61
|
-
#check given template is valid
|
62
|
-
all_templates = [template['id'] for template in modeltypefield['modelTypeEnumOptions']]
|
63
|
-
if template not in all_templates:
|
64
|
-
raise ValueError(f"Invalid template {template} for model type {model_type_id}. "
|
65
|
-
f"Valid templates are {all_templates}")
|
66
|
-
for modeltypeenum in modeltypefield['modelTypeEnumOptions']:
|
67
|
-
#finding the given template
|
68
|
-
if modeltypeenum['id'] == template:
|
69
|
-
params['train_params']["template"] = modeltypeenum['id']
|
70
|
-
#iterate through the template fields
|
71
|
-
for modeltypeenumfield in modeltypeenum['modelTypeFields']:
|
72
|
-
if "internalOnly" in modeltypeenumfield.keys():
|
10
|
+
"""Converts the response from the API to a list of templates for the given model type id."""
|
11
|
+
dict_response = MessageToDict(response)
|
12
|
+
templates = []
|
13
|
+
for model_type in dict_response['modelTypes']:
|
14
|
+
if model_type['id'] == model_type_id:
|
15
|
+
for modeltypefield in model_type['modelTypeFields']:
|
16
|
+
if modeltypefield['path'].split('.')[-1] == "template":
|
17
|
+
templates = [
|
18
|
+
template['id'] for template in modeltypefield['modelTypeEnumOptions']
|
19
|
+
]
|
20
|
+
return templates
|
21
|
+
|
22
|
+
|
23
|
+
def response_to_model_params(
|
24
|
+
response: MultiModelTypeResponse, model_type_id: str, template: str = None
|
25
|
+
) -> Dict[str, Any]:
|
26
|
+
"""Converts the response from the API to a dictionary of model params for the given model type id."""
|
27
|
+
dict_response = MessageToDict(response)
|
28
|
+
params = {}
|
29
|
+
if model_type_id != "clusterer":
|
30
|
+
params["dataset_id"] = ""
|
31
|
+
params["dataset_version_id"] = ""
|
32
|
+
if model_type_id not in ["clusterer", "text-to-text"]:
|
33
|
+
params["concepts"] = []
|
34
|
+
params["train_params"] = dict()
|
35
|
+
|
36
|
+
for model_type in dict_response['modelTypes']:
|
37
|
+
if model_type['id'] == model_type_id:
|
38
|
+
# iterate through the model type fields
|
39
|
+
for modeltypefield in model_type['modelTypeFields']:
|
40
|
+
_path = modeltypefield['path'].split('.')
|
41
|
+
# removing the fields which are not required
|
42
|
+
if (
|
43
|
+
(_path[0] in ["'eval_info'"])
|
44
|
+
or (_path[1] in ["dataset", "data"])
|
45
|
+
or (_path[-1] in ["dataset_id", "dataset_version_id"])
|
46
|
+
or ("internalOnly" in modeltypefield.keys())
|
47
|
+
):
|
73
48
|
continue
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
49
|
+
# checking the template model type fields
|
50
|
+
if _path[-1] != "template":
|
51
|
+
if _path[0] == 'train_info':
|
52
|
+
try:
|
53
|
+
params["train_params"][_path[-1]] = modeltypefield['defaultValue']
|
54
|
+
except Exception:
|
55
|
+
params["train_params"][_path[-1]] = None
|
56
|
+
if _path[0] == 'output_info':
|
57
|
+
params["inference_params"] = dict()
|
58
|
+
try:
|
59
|
+
params["inference_params"][_path[-1]] = modeltypefield['defaultValue']
|
60
|
+
except Exception:
|
61
|
+
params["inference_params"][_path[-1]] = None
|
62
|
+
if _path[0] == 'output_info' and _path[1] == 'output_config':
|
63
|
+
params["concepts_mutually_exclusive"] = modeltypefield['defaultValue']
|
64
|
+
elif 'modelTypeEnumOptions' in modeltypefield.keys():
|
65
|
+
# check given template is valid
|
66
|
+
all_templates = [
|
67
|
+
template['id'] for template in modeltypefield['modelTypeEnumOptions']
|
68
|
+
]
|
69
|
+
if template not in all_templates:
|
70
|
+
raise ValueError(
|
71
|
+
f"Invalid template {template} for model type {model_type_id}. "
|
72
|
+
f"Valid templates are {all_templates}"
|
73
|
+
)
|
74
|
+
for modeltypeenum in modeltypefield['modelTypeEnumOptions']:
|
75
|
+
# finding the given template
|
76
|
+
if modeltypeenum['id'] == template:
|
77
|
+
params['train_params']["template"] = modeltypeenum['id']
|
78
|
+
# iterate through the template fields
|
79
|
+
for modeltypeenumfield in modeltypeenum['modelTypeFields']:
|
80
|
+
if "internalOnly" in modeltypeenumfield.keys():
|
81
|
+
continue
|
82
|
+
try:
|
83
|
+
params["train_params"][
|
84
|
+
modeltypeenumfield['path'].split('.')[-1]
|
85
|
+
] = modeltypeenumfield['defaultValue']
|
86
|
+
except Exception:
|
87
|
+
params["train_params"][
|
88
|
+
modeltypeenumfield['path'].split('.')[-1]
|
89
|
+
] = None
|
90
|
+
# custom config
|
91
|
+
if "custom_config" in params['train_params'].keys():
|
92
|
+
# Write the content to the file
|
93
|
+
file_path = params['train_params']['template'] + ".py"
|
94
|
+
with open(file_path, "w") as script_file:
|
95
|
+
script_file.write(params['train_params']['custom_config'])
|
96
|
+
params['train_params']['custom_config'] = file_path
|
97
|
+
|
98
|
+
return params
|
88
99
|
|
89
100
|
|
90
101
|
def params_parser(params_dict: dict, concepts: List = None) -> Dict[str, Any]:
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
102
|
+
"""Converts the params dictionary to a dictionary of model specific params for the given model"""
|
103
|
+
# dict parser
|
104
|
+
train_dict = {}
|
105
|
+
train_dict["train_info"] = dict()
|
106
|
+
train_dict["output_info"] = dict()
|
107
|
+
|
108
|
+
train_dict["train_info"]['params'] = Struct()
|
109
|
+
if 'custom_config' in params_dict['train_params'].keys():
|
110
|
+
# Open and read the Python file
|
111
|
+
with open(params_dict['train_params']['custom_config'], 'r') as python_file:
|
112
|
+
custom_config = python_file.read()
|
113
|
+
params_dict['train_params']['custom_config'] = custom_config
|
114
|
+
|
115
|
+
train_dict["train_info"]['params'].update(params_dict["train_params"])
|
116
|
+
if 'dataset_id' in params_dict.keys():
|
117
|
+
train_dict["train_info"]['params']['dataset_id'] = params_dict['dataset_id']
|
118
|
+
train_dict["train_info"]['params']['dataset_version_id'] = params_dict[
|
119
|
+
'dataset_version_id'
|
120
|
+
]
|
121
|
+
train_dict['train_info'] = resources_pb2.TrainInfo(**train_dict['train_info'])
|
122
|
+
|
123
|
+
if 'concepts' in params_dict.keys():
|
124
|
+
assert set(params_dict["concepts"]).issubset(concepts), (
|
125
|
+
"Invalid concept IDs. Available concepts in the app are {}".format(concepts)
|
126
|
+
)
|
127
|
+
train_dict["output_info"]['data'] = resources_pb2.Data(
|
128
|
+
concepts=[
|
129
|
+
resources_pb2.Concept(id=concept_id) for concept_id in params_dict["concepts"]
|
130
|
+
]
|
131
|
+
)
|
132
|
+
if 'inference_params' in params_dict.keys():
|
133
|
+
train_dict["output_info"]['params'] = Struct()
|
134
|
+
train_dict['output_info']['params'].update(params_dict["inference_params"])
|
135
|
+
if 'concepts_mutually_exclusive' in params_dict.keys():
|
136
|
+
train_dict['output_info']['output_config'] = resources_pb2.OutputConfig(
|
137
|
+
concepts_mutually_exclusive=params_dict['concepts_mutually_exclusive']
|
138
|
+
)
|
139
|
+
train_dict['output_info'] = resources_pb2.OutputInfo(**train_dict['output_info'])
|
140
|
+
|
141
|
+
return train_dict
|
142
|
+
|
143
|
+
|
144
|
+
def response_to_param_info(
|
145
|
+
response: MultiModelTypeResponse, model_type_id: str, param: str, template: str = None
|
146
|
+
) -> Dict[str, Any]:
|
147
|
+
"""Converts the response from the API to a dictionary of model param info for the given model type id."""
|
148
|
+
dict_response = MessageToDict(response)
|
149
|
+
for model_type in dict_response['modelTypes']:
|
150
|
+
if model_type['id'] == model_type_id:
|
151
|
+
# iterate through the model type fields
|
152
|
+
for modeltypefield in model_type['modelTypeFields']:
|
153
|
+
if modeltypefield['path'].split('.')[-1] == param:
|
154
|
+
if param == 'template':
|
155
|
+
del modeltypefield['placeholder']
|
156
|
+
del modeltypefield['modelTypeEnumOptions']
|
157
|
+
return modeltypefield
|
158
|
+
modeltypefield['param'] = modeltypefield.pop('path').split('.')[-1]
|
159
|
+
del modeltypefield['placeholder']
|
160
|
+
return modeltypefield
|
161
|
+
# checking the template model type fields
|
162
|
+
if modeltypefield['path'].split('.')[-1] == "template":
|
163
|
+
for modeltypeenum in modeltypefield['modelTypeEnumOptions']:
|
164
|
+
if modeltypeenum['id'] == template:
|
165
|
+
# iterate through the template fields
|
166
|
+
for modeltypeenumfield in modeltypeenum['modelTypeFields']:
|
167
|
+
if modeltypeenumfield['path'].split('.')[-1] == param:
|
168
|
+
modeltypeenumfield['param'] = modeltypeenumfield.pop(
|
169
|
+
'path'
|
170
|
+
).split('.')[-1]
|
171
|
+
del modeltypeenumfield['placeholder']
|
172
|
+
return modeltypeenumfield
|
154
173
|
|
155
174
|
|
156
175
|
def find_and_replace_key(nested_dict: Dict, target_key: str, replacement_value: Any) -> None:
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
176
|
+
"""Finds and replaces the target key with the replacement value in the nested dictionary."""
|
177
|
+
for key, value in nested_dict.items():
|
178
|
+
if key == target_key:
|
179
|
+
nested_dict[key] = replacement_value
|
180
|
+
elif isinstance(value, dict):
|
181
|
+
find_and_replace_key(value, target_key, replacement_value)
|