clarifai 11.0.5__py3-none-any.whl → 11.0.6rc2__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 (156) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  3. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  4. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  5. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  6. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  7. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  8. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  9. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  10. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  11. clarifai/cli/model.py +15 -2
  12. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  13. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  14. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  15. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  16. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  17. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  18. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  19. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  20. clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
  21. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  22. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  23. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  24. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  25. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  26. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  27. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  28. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  29. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  30. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  31. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  32. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  33. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  34. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  35. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  36. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  37. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  38. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  39. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  40. clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
  41. clarifai/models/model_serving/README.md +158 -0
  42. clarifai/models/model_serving/__init__.py +14 -0
  43. clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
  44. clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
  45. clarifai/models/model_serving/cli/__init__.py +12 -0
  46. clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  47. clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
  48. clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
  49. clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
  50. clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
  51. clarifai/models/model_serving/cli/_utils.py +53 -0
  52. clarifai/models/model_serving/cli/base.py +14 -0
  53. clarifai/models/model_serving/cli/build.py +79 -0
  54. clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
  55. clarifai/models/model_serving/cli/create.py +171 -0
  56. clarifai/models/model_serving/cli/example_cli.py +34 -0
  57. clarifai/models/model_serving/cli/login.py +26 -0
  58. clarifai/models/model_serving/cli/upload.py +183 -0
  59. clarifai/models/model_serving/constants.py +21 -0
  60. clarifai/models/model_serving/docs/cli.md +161 -0
  61. clarifai/models/model_serving/docs/concepts.md +229 -0
  62. clarifai/models/model_serving/docs/dependencies.md +11 -0
  63. clarifai/models/model_serving/docs/inference_parameters.md +139 -0
  64. clarifai/models/model_serving/docs/model_types.md +19 -0
  65. clarifai/models/model_serving/model_config/__init__.py +16 -0
  66. clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
  67. clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
  68. clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
  69. clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
  70. clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
  71. clarifai/models/model_serving/model_config/base.py +369 -0
  72. clarifai/models/model_serving/model_config/config.py +312 -0
  73. clarifai/models/model_serving/model_config/inference_parameter.py +129 -0
  74. clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -0
  75. clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -0
  76. clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -0
  77. clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -0
  78. clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -0
  79. clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -0
  80. clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -0
  81. clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -0
  82. clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -0
  83. clarifai/models/model_serving/model_config/output.py +133 -0
  84. clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
  85. clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
  86. clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
  87. clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
  88. clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
  89. clarifai/models/model_serving/model_config/triton/serializer.py +136 -0
  90. clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
  91. clarifai/models/model_serving/model_config/triton/wrappers.py +281 -0
  92. clarifai/models/model_serving/repo_build/__init__.py +14 -0
  93. clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
  94. clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
  95. clarifai/models/model_serving/repo_build/build.py +198 -0
  96. clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
  97. clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
  98. clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
  99. clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
  100. clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
  101. clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
  102. clarifai/models/model_serving/repo_build/static_files/triton/model.py +75 -0
  103. clarifai/models/model_serving/utils.py +31 -0
  104. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  105. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  106. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  107. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  108. clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
  109. clarifai/runners/deepgram_live_transcribe.py +98 -0
  110. clarifai/runners/deepgram_live_transcribe.py~ +98 -0
  111. clarifai/runners/deepgram_runner.py +131 -0
  112. clarifai/runners/deepgram_runner.py~ +130 -0
  113. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
  114. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +79 -0
  115. clarifai/runners/dockerfile_template/Dockerfile.template +53 -29
  116. clarifai/runners/example_llama2.py~ +72 -0
  117. clarifai/runners/matt_example.py +89 -0
  118. clarifai/runners/matt_example.py~ +87 -0
  119. clarifai/runners/matt_llm_example.py +129 -0
  120. clarifai/runners/matt_llm_example.py~ +128 -0
  121. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  122. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  123. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  124. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  125. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  126. clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
  127. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  128. clarifai/runners/models/model_upload.py +7 -4
  129. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  130. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  131. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  132. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  133. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  134. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  135. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  136. clarifai/runners/utils/const.py +7 -6
  137. clarifai/runners/utils/loader.py +1 -0
  138. clarifai/runners/utils/logging.py +6 -0
  139. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  140. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  141. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  142. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  143. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  144. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  145. clarifai/utils/logging.py +1 -1
  146. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  147. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  148. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  149. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  150. {clarifai-11.0.5.dist-info → clarifai-11.0.6rc2.dist-info}/METADATA +16 -27
  151. clarifai-11.0.6rc2.dist-info/RECORD +242 -0
  152. {clarifai-11.0.5.dist-info → clarifai-11.0.6rc2.dist-info}/WHEEL +1 -1
  153. clarifai-11.0.5.dist-info/RECORD +0 -100
  154. {clarifai-11.0.5.dist-info → clarifai-11.0.6rc2.dist-info}/LICENSE +0 -0
  155. {clarifai-11.0.5.dist-info → clarifai-11.0.6rc2.dist-info}/entry_points.txt +0 -0
  156. {clarifai-11.0.5.dist-info → clarifai-11.0.6rc2.dist-info}/top_level.txt +0 -0
clarifai/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "11.0.5"
1
+ __version__ = "11.0.6rc2"
clarifai/cli/model.py CHANGED
@@ -5,8 +5,7 @@ from clarifai.cli.base import cli
5
5
 
6
6
  @cli.group(['model'])
7
7
  def model():
8
- """Manage models: upload, test locally, run_locally, predict"""
9
- pass
8
+ """Manage models: upload, test locally, run locally, predict, and more"""
10
9
 
11
10
 
12
11
  @model.command()
@@ -34,6 +33,20 @@ def upload(model_path, download_checkpoints, skip_dockerfile):
34
33
  model_upload.main(model_path, download_checkpoints, skip_dockerfile)
35
34
 
36
35
 
36
+ @model.command()
37
+ @click.option(
38
+ '--model_path',
39
+ type=click.Path(exists=True),
40
+ required=True,
41
+ help='Path to the model directory.')
42
+ def download_checkpoints(model_path):
43
+ """Download checkpoints from external source to local model_path"""
44
+
45
+ from clarifai.runners.models.model_upload import ModelUploader
46
+ uploader = ModelUploader(model_path, download_validation_only=True)
47
+ uploader.download_checkpoints()
48
+
49
+
37
50
  @model.command()
38
51
  @click.option(
39
52
  '--model_path',
@@ -0,0 +1,158 @@
1
+ # Clarifai Model Serving
2
+
3
+ ## Overview
4
+
5
+ Model Serving is a part of user journey at Clarifai offers a user-friendly interface for deploying your local model into production with Clarifai, featuring:
6
+
7
+ * A convenient command-line interface (CLI)
8
+ * Easy implementation and testing in Python
9
+ * No need for MLops expertise.
10
+
11
+ ## Quickstart Guide
12
+
13
+ Quick example for deploying a `text-to-text` model
14
+
15
+ ### Initialize a Clarifai model repository
16
+
17
+ Suppose your working directory name is `your_model_dir`. Then run
18
+
19
+ ```bash
20
+ $ clarifai create model --type text-to-text --working-dir your_model_dir
21
+ $ cd your_model_dir
22
+ ```
23
+
24
+ In `your_model_dir` folder you will see essential files for deployment process
25
+
26
+ ```bash
27
+ your_model_dir
28
+ ├── clarifai_config.yaml
29
+ ├── inference.py
30
+ ├── test.py
31
+ └── requirements.txt
32
+ ```
33
+
34
+ ### Implementation
35
+
36
+ Write your code in class `InferenceModel` which is an interface between your model and Clarifai server in `inference.py`, there are 2 functions you must implement:
37
+
38
+ * `__init__`: load your model checkpoint once.
39
+ * `predict`: make prediction, called everytime when you make request from API.
40
+
41
+ For example, a complete implementation of a hf text-generation model
42
+
43
+ ```python
44
+ import os
45
+ from typing import Dict, Union
46
+ from clarifai.models.model_serving.model_config import *
47
+
48
+ import torch
49
+ from transformers import AutoTokenizer
50
+ import transformers
51
+
52
+ class InferenceModel(TextToText):
53
+ """User model inference class."""
54
+
55
+ def __init__(self) -> None:
56
+ """
57
+ Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
58
+ in this method so they are loaded only once for faster inference.
59
+ """
60
+ # current directory
61
+ self.base_path = os.path.dirname(__file__)
62
+ # where you save hf checkpoint in your working dir e.i. `your_model_dir`
63
+ model_path = os.path.join(self.base_path, "checkpoint")
64
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path)
65
+ self.pipeline = transformers.pipeline(
66
+ "text-generation",
67
+ model=model_path,
68
+ torch_dtype=torch.float16,
69
+ device_map="auto",
70
+ )
71
+
72
+ def predict(self, input_data: list,
73
+ inference_parameters: Dict[str, Union[str, float, int]]) -> list:
74
+ """ Custom prediction function for `text-to-text` (also called as `text generation`) model.
75
+
76
+ Args:
77
+ input_data (List[str]): List of text
78
+ inference_parameters (Dict[str, Union[str, float, int]]): your inference parameters
79
+
80
+ Returns:
81
+ list of TextOutput
82
+
83
+ """
84
+ output_sequences = self.pipeline(
85
+ input_data,
86
+ eos_token_id=self.tokenizer.eos_token_id,
87
+ **inference_parameters)
88
+
89
+ # wrap outputs in Clarifai defined output
90
+ return [TextOutput(each[0]) for each in output_sequences]
91
+ ```
92
+
93
+ Update dependencies in `requirements.txt`
94
+
95
+ ```
96
+ clarifai
97
+ torch=2.1.1
98
+ transformers==4.36.2
99
+ accelerate==0.26.1
100
+ ```
101
+
102
+ ### Test (optional)
103
+
104
+ > NOTE: Running `test` is also involved in `build` and `upload` command.
105
+
106
+ Test and play with your implementation by executing `test.py`.
107
+
108
+ Install pytest
109
+
110
+ ```bash
111
+ $ pip install pytest
112
+ ```
113
+
114
+ Execute test
115
+
116
+ ```bash
117
+ $ pytest test.py
118
+ ```
119
+
120
+ ### Build
121
+
122
+ Prepare for deployment step. Run:
123
+
124
+ ```bash
125
+ $ clarifai build model
126
+ ```
127
+
128
+ You will obtain `*.clarifai` file, it's simply a zip having all nessecary files in it to get your model work on Clarifai platform.
129
+
130
+ `NOTE`: you need to upload your built file to cloud storage to get direct download `url` for next step
131
+
132
+ ### Deployment
133
+
134
+ Login to Clarifai
135
+
136
+ ```bash
137
+ $ clarifai login
138
+ Get your PAT from https://clarifai.com/settings/security and pass it here: <insert your pat here>
139
+ ```
140
+
141
+ Upload
142
+
143
+ ```bash
144
+ # upload built file directly
145
+ $ clarifai upload model <your-working-dir> --user-app <your_user_id>/<your_app_id> --id <your_model_id>
146
+ # or using direct download url of cloud storage
147
+ $ clarifai upload model --url <url> --user-app <your_user_id>/<your_app_id> --id <your_model_id>
148
+ ```
149
+
150
+ ## Learn More
151
+
152
+ * [Detail Instruction](./docs/concepts.md)
153
+ * [Examples](https://github.com/Clarifai/examples/tree/main/model_upload)
154
+ * [Initialize from example](./docs/cli.md)
155
+ * [CLI usage](./docs/cli.md)
156
+ * [Inference parameters](./docs/inference_parameters.md)
157
+ * [Model Types](./docs/model_types.md)
158
+ * [Dependencies](./docs/dependencies.md)
@@ -0,0 +1,14 @@
1
+ # Copyright 2023 Clarifai, Inc.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ from .model_config import * # noqa
14
+ from .repo_build import * # noqa
@@ -0,0 +1,12 @@
1
+ # Copyright 2023 Clarifai, Inc.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
@@ -0,0 +1,53 @@
1
+ from __future__ import annotations # isort: skip
2
+ import os
3
+ import shutil
4
+ import subprocess
5
+ from typing import Dict, Union
6
+
7
+ from ..constants import (CLARIFAI_EXAMPLES_REPO, CLARIFAI_EXAMPLES_REPO_PATH,
8
+ MODEL_UPLOAD_EXAMPLE_FOLDER)
9
+
10
+
11
+ def download_examples_repo(forced_download: bool = False):
12
+
13
+ def _pull():
14
+ subprocess.run(f"git clone {CLARIFAI_EXAMPLES_REPO} {CLARIFAI_EXAMPLES_REPO_PATH}", shell=True)
15
+
16
+ if not os.path.isdir(CLARIFAI_EXAMPLES_REPO_PATH):
17
+ print(f"Download examples to {CLARIFAI_EXAMPLES_REPO_PATH}")
18
+ _pull()
19
+ else:
20
+ if forced_download:
21
+
22
+ def _rm_dir_readonly(func, path, _):
23
+ import stat
24
+ os.chmod(path, stat.S_IWRITE)
25
+ func(path)
26
+
27
+ print("Removing old examples...")
28
+ shutil.rmtree(CLARIFAI_EXAMPLES_REPO_PATH, onerror=_rm_dir_readonly)
29
+ _pull()
30
+
31
+
32
+ def list_model_upload_examples(
33
+ forced_download: bool = False) -> Dict[str, tuple[str, Union[str, None]]]:
34
+ download_examples_repo(forced_download)
35
+ model_upload_folder = MODEL_UPLOAD_EXAMPLE_FOLDER
36
+ model_upload_path = os.path.join(CLARIFAI_EXAMPLES_REPO_PATH, model_upload_folder)
37
+ examples = {}
38
+ for model_type_ex in os.listdir(model_upload_path):
39
+ _folder = os.path.join(model_upload_path, model_type_ex)
40
+ if os.path.isdir(_folder):
41
+ _walk = list(os.walk(_folder))
42
+ if len(_walk) > 0:
43
+ _, model_names, _files = _walk[0]
44
+ readme = [item for item in _files if "readme" in item.lower()]
45
+ for name in model_names:
46
+ examples.update({
47
+ f"{model_type_ex}/{name}": [
48
+ os.path.join(_folder, name),
49
+ os.path.join(_folder, readme[0]) or None
50
+ ]
51
+ })
52
+
53
+ return examples
@@ -0,0 +1,14 @@
1
+ from abc import ABC, abstractmethod
2
+ from argparse import _SubParsersAction
3
+
4
+
5
+ class BaseClarifaiCli(ABC):
6
+
7
+ @staticmethod
8
+ @abstractmethod
9
+ def register(parser: _SubParsersAction):
10
+ raise NotImplementedError()
11
+
12
+ @abstractmethod
13
+ def run(self):
14
+ raise NotImplementedError()
@@ -0,0 +1,79 @@
1
+ # Copyright 2023 Clarifai, Inc.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ """Commandline interface for model upload utils."""
14
+ import argparse
15
+ import os
16
+ import subprocess
17
+
18
+ from ..repo_build import RepositoryBuilder
19
+ from .base import BaseClarifaiCli
20
+
21
+
22
+ class BuildCli(BaseClarifaiCli):
23
+
24
+ @staticmethod
25
+ def register(parser: argparse._SubParsersAction):
26
+ parser = parser.add_parser("build", help="Build clarifai model for uploading")
27
+ sub_parser = parser.add_subparsers()
28
+
29
+ BuildModelSubCli.register(sub_parser)
30
+
31
+ parser.set_defaults(func=BuildCli)
32
+
33
+
34
+ class BuildModelSubCli(BaseClarifaiCli):
35
+
36
+ @staticmethod
37
+ def register(parser: argparse._SubParsersAction):
38
+ sub_parser = parser.add_parser("model", help="Build Clarifai model")
39
+ sub_parser.add_argument(
40
+ "path",
41
+ type=str,
42
+ nargs='?',
43
+ help="Path to working directory, default is current directory",
44
+ default=".")
45
+ sub_parser.add_argument(
46
+ "--out-path", type=str, required=False, help="Output path of built model", default=None)
47
+ sub_parser.add_argument(
48
+ "--name",
49
+ type=str,
50
+ required=False,
51
+ help="Name of built file, default is `clarifai_model_id` in config if set or `model`",
52
+ default=None)
53
+ sub_parser.add_argument(
54
+ "--no-test",
55
+ action="store_true",
56
+ help="Trigger this flag to skip testing before uploading")
57
+ sub_parser.set_defaults(func=BuildModelSubCli)
58
+
59
+ def __init__(self, args: argparse.Namespace) -> None:
60
+ self.path = args.path
61
+ self.no_test = args.no_test
62
+ self.test_path = os.path.join(self.path, "test.py")
63
+ self.output_path = args.out_path or self.path
64
+ self.serving_backend = "triton"
65
+ self.name = args.name
66
+
67
+ def run(self):
68
+
69
+ # Run test before uploading
70
+ if not self.no_test:
71
+ assert os.path.exists(
72
+ self.test_path), FileNotFoundError(f"Could not find `test.py` in {self.path}")
73
+ result = subprocess.run(f"pytest -s --log-level=INFO {self.test_path}", shell=True)
74
+ assert result.returncode == 0, "Test has failed. Please make sure no error exists in your code."
75
+
76
+ # build
77
+ print("Start building...")
78
+ RepositoryBuilder.build(
79
+ self.path, backend=self.serving_backend, output_dir=self.output_path, name=self.name)
@@ -0,0 +1,33 @@
1
+ from argparse import ArgumentParser
2
+
3
+ from .build import BuildCli
4
+ from .create import CreateCli
5
+ from .example_cli import ExampleCli
6
+ from .login import LoginCli
7
+ from .upload import UploadCli
8
+
9
+
10
+ def main():
11
+
12
+ parser = ArgumentParser("clarifai")
13
+ cmd_parser = parser.add_subparsers(help="Clarifai cli helpers")
14
+
15
+ UploadCli.register(cmd_parser)
16
+ CreateCli.register(cmd_parser)
17
+ LoginCli.register(cmd_parser)
18
+ ExampleCli.register(cmd_parser)
19
+ BuildCli.register(cmd_parser)
20
+
21
+ args = parser.parse_args()
22
+
23
+ if not hasattr(args, "func"):
24
+ parser.print_help()
25
+ exit(1)
26
+
27
+ # Run
28
+ service = args.func(args)
29
+ service.run()
30
+
31
+
32
+ if __name__ == "__main__":
33
+ main()