clarifai 10.9.0__tar.gz → 10.9.1__tar.gz

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 (112) hide show
  1. {clarifai-10.9.0/clarifai.egg-info → clarifai-10.9.1}/PKG-INFO +2 -2
  2. clarifai-10.9.1/clarifai/__init__.py +1 -0
  3. clarifai-10.9.1/clarifai/runners/models/model_run_locally.py +194 -0
  4. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/models/model_upload.py +42 -27
  5. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/utils/loader.py +15 -11
  6. {clarifai-10.9.0 → clarifai-10.9.1/clarifai.egg-info}/PKG-INFO +2 -2
  7. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai.egg-info/SOURCES.txt +1 -0
  8. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai.egg-info/requires.txt +1 -1
  9. {clarifai-10.9.0 → clarifai-10.9.1}/requirements.txt +1 -1
  10. clarifai-10.9.0/clarifai/__init__.py +0 -1
  11. {clarifai-10.9.0 → clarifai-10.9.1}/LICENSE +0 -0
  12. {clarifai-10.9.0 → clarifai-10.9.1}/MANIFEST.in +0 -0
  13. {clarifai-10.9.0 → clarifai-10.9.1}/README.md +0 -0
  14. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/cli.py +0 -0
  15. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/__init__.py +0 -0
  16. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/app.py +0 -0
  17. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/auth/__init__.py +0 -0
  18. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/auth/helper.py +0 -0
  19. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/auth/register.py +0 -0
  20. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/auth/stub.py +0 -0
  21. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/base.py +0 -0
  22. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/compute_cluster.py +0 -0
  23. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/dataset.py +0 -0
  24. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/deployment.py +0 -0
  25. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/input.py +0 -0
  26. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/lister.py +0 -0
  27. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/model.py +0 -0
  28. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/module.py +0 -0
  29. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/nodepool.py +0 -0
  30. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/search.py +0 -0
  31. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/user.py +0 -0
  32. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/client/workflow.py +0 -0
  33. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/base.py +0 -0
  34. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/dataset.py +0 -0
  35. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/input.py +0 -0
  36. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/model.py +0 -0
  37. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/rag.py +0 -0
  38. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/search.py +0 -0
  39. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/constants/workflow.py +0 -0
  40. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/__init__.py +0 -0
  41. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/export/__init__.py +0 -0
  42. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/export/inputs_annotations.py +0 -0
  43. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/__init__.py +0 -0
  44. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/base.py +0 -0
  45. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/features.py +0 -0
  46. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/image.py +0 -0
  47. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/loaders/README.md +0 -0
  48. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/loaders/__init__.py +0 -0
  49. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/loaders/coco_captions.py +0 -0
  50. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/loaders/coco_detection.py +0 -0
  51. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/loaders/imagenet_classification.py +0 -0
  52. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/loaders/xview_detection.py +0 -0
  53. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/multimodal.py +0 -0
  54. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/text.py +0 -0
  55. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/datasets/upload/utils.py +0 -0
  56. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/errors.py +0 -0
  57. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/models/__init__.py +0 -0
  58. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/models/api.py +0 -0
  59. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/modules/README.md +0 -0
  60. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/modules/__init__.py +0 -0
  61. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/modules/css.py +0 -0
  62. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/modules/pages.py +0 -0
  63. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/modules/style.css +0 -0
  64. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/rag/__init__.py +0 -0
  65. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/rag/rag.py +0 -0
  66. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/rag/utils.py +0 -0
  67. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/__init__.py +0 -0
  68. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -0
  69. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -0
  70. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/models/__init__.py +0 -0
  71. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/models/base_typed_model.py +0 -0
  72. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/models/model_class.py +0 -0
  73. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/models/model_runner.py +0 -0
  74. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/models/model_servicer.py +0 -0
  75. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/server.py +0 -0
  76. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/utils/__init__.py +0 -0
  77. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/utils/data_handler.py +0 -0
  78. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/utils/data_utils.py +0 -0
  79. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/runners/utils/url_fetcher.py +0 -0
  80. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/schema/search.py +0 -0
  81. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/urls/helper.py +0 -0
  82. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/__init__.py +0 -0
  83. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/constants.py +0 -0
  84. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/evaluation/__init__.py +0 -0
  85. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/evaluation/helpers.py +0 -0
  86. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/evaluation/main.py +0 -0
  87. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/evaluation/testset_annotation_parser.py +0 -0
  88. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/logging.py +0 -0
  89. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/misc.py +0 -0
  90. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/utils/model_train.py +0 -0
  91. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/versions.py +0 -0
  92. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/workflows/__init__.py +0 -0
  93. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/workflows/export.py +0 -0
  94. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/workflows/utils.py +0 -0
  95. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai/workflows/validate.py +0 -0
  96. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai.egg-info/dependency_links.txt +0 -0
  97. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai.egg-info/entry_points.txt +0 -0
  98. {clarifai-10.9.0 → clarifai-10.9.1}/clarifai.egg-info/top_level.txt +0 -0
  99. {clarifai-10.9.0 → clarifai-10.9.1}/pyproject.toml +0 -0
  100. {clarifai-10.9.0 → clarifai-10.9.1}/setup.cfg +0 -0
  101. {clarifai-10.9.0 → clarifai-10.9.1}/setup.py +0 -0
  102. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_app.py +0 -0
  103. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_auth.py +0 -0
  104. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_data_upload.py +0 -0
  105. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_eval.py +0 -0
  106. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_misc.py +0 -0
  107. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_model_predict.py +0 -0
  108. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_model_train.py +0 -0
  109. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_modules.py +0 -0
  110. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_rag.py +0 -0
  111. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_search.py +0 -0
  112. {clarifai-10.9.0 → clarifai-10.9.1}/tests/test_stub.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: clarifai
3
- Version: 10.9.0
3
+ Version: 10.9.1
4
4
  Summary: Clarifai Python SDK
5
5
  Home-page: https://github.com/Clarifai/clarifai-python
6
6
  Author: Clarifai
@@ -20,7 +20,7 @@ Classifier: Operating System :: OS Independent
20
20
  Requires-Python: >=3.8
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENSE
23
- Requires-Dist: clarifai-grpc>=10.8.7
23
+ Requires-Dist: clarifai-grpc>=10.8.8
24
24
  Requires-Dist: clarifai-protocol>=0.0.6
25
25
  Requires-Dist: numpy>=1.22.0
26
26
  Requires-Dist: tqdm>=4.65.0
@@ -0,0 +1 @@
1
+ __version__ = "10.9.1"
@@ -0,0 +1,194 @@
1
+ import argparse
2
+ import importlib.util
3
+ import inspect
4
+ import os
5
+ import shutil
6
+ import subprocess
7
+ import sys
8
+ import tempfile
9
+ import traceback
10
+ import venv
11
+
12
+ from clarifai_grpc.grpc.api import resources_pb2, service_pb2
13
+ from clarifai_grpc.grpc.api.status import status_code_pb2, status_pb2
14
+ from clarifai_protocol import BaseRunner
15
+
16
+ from clarifai.runners.models.model_upload import ModelUploader
17
+ from clarifai.utils.logging import logger
18
+
19
+
20
+ class ModelRunLocally:
21
+
22
+ def __init__(self, model_path):
23
+ self.model_path = model_path
24
+ self.requirements_file = os.path.join(self.model_path, "requirements.txt")
25
+ self.venv_dir, self.temp_dir = self.create_temp_venv()
26
+ self.python_executable = os.path.join(self.venv_dir, "bin", "python")
27
+
28
+ def create_temp_venv(self):
29
+ """Create a temporary virtual environment."""
30
+ logger.info("Creating temporary virtual environment...")
31
+ temp_dir = tempfile.mkdtemp()
32
+ venv_dir = os.path.join(temp_dir, "venv")
33
+ venv.create(venv_dir, with_pip=True)
34
+
35
+ logger.info(f"Created temporary virtual environment at {venv_dir}")
36
+ return venv_dir, temp_dir
37
+
38
+ def install_requirements(self):
39
+ """Install the dependencies from requirements.txt and Clarifai."""
40
+ pip_executable = os.path.join(self.venv_dir, "bin", "pip")
41
+ try:
42
+ logger.info(
43
+ f"Installing requirements from {self.requirements_file}... in the virtual environment {self.venv_dir}"
44
+ )
45
+ subprocess.check_call([pip_executable, "install", "-r", self.requirements_file])
46
+ logger.info("Installing Clarifai package...")
47
+ subprocess.check_call([pip_executable, "install", "clarifai"])
48
+ logger.info("Requirements installed successfully!")
49
+ except subprocess.CalledProcessError as e:
50
+ logger.error(f"Error installing requirements: {e}")
51
+ self.clean_up()
52
+ sys.exit(1)
53
+
54
+ def _get_model_runner(self):
55
+ """Dynamically import the runner class from the model file."""
56
+
57
+ # import the runner class that to be implement by the user
58
+ runner_path = os.path.join(self.model_path, "1", "model.py")
59
+
60
+ # arbitrary name given to the module to be imported
61
+ module = "runner_module"
62
+
63
+ spec = importlib.util.spec_from_file_location(module, runner_path)
64
+ runner_module = importlib.util.module_from_spec(spec)
65
+ sys.modules[module] = runner_module
66
+ spec.loader.exec_module(runner_module)
67
+
68
+ # Find all classes in the model.py file that are subclasses of BaseRunner
69
+ classes = [
70
+ cls for _, cls in inspect.getmembers(runner_module, inspect.isclass)
71
+ if issubclass(cls, BaseRunner) and cls.__module__ == runner_module.__name__
72
+ ]
73
+
74
+ # Ensure there is exactly one subclass of BaseRunner in the model.py file
75
+ if len(classes) != 1:
76
+ raise Exception("Expected exactly one subclass of BaseRunner, found: {}".format(
77
+ len(classes)))
78
+
79
+ MyRunner = classes[0]
80
+ return MyRunner
81
+
82
+ def _build_request(self):
83
+ """Create a mock inference request for testing the model."""
84
+
85
+ uploader = ModelUploader(self.model_path)
86
+ model_version_proto = uploader.get_model_version_proto()
87
+ model_version_proto.id = "model_version"
88
+
89
+ return service_pb2.PostModelOutputsRequest(
90
+ model=resources_pb2.Model(model_version=model_version_proto),
91
+ inputs=[
92
+ resources_pb2.Input(data=resources_pb2.Data(
93
+ text=resources_pb2.Text(raw="How many people live in new york?"),
94
+ image=resources_pb2.Image(url="https://samples.clarifai.com/metro-north.jpg"),
95
+ audio=resources_pb2.Audio(url="https://samples.clarifai.com/GoodMorning.wav"),
96
+ ))
97
+ ],
98
+ )
99
+
100
+ def _run_model_inference(self, runner):
101
+ """Perform inference using the runner."""
102
+ request = self._build_request()
103
+
104
+ try:
105
+ return runner.predict(request)
106
+ except Exception as e:
107
+ logger.error(f"Model Prediction failed: {e}")
108
+ traceback.print_exc()
109
+ return service_pb2.MultiOutputResponse(status=status_pb2.Status(
110
+ code=status_code_pb2.MODEL_PREDICTION_FAILED,
111
+ description="Prediction failed",
112
+ details="",
113
+ internal_details=str(e),
114
+ ))
115
+
116
+ def _run_test(self):
117
+ """Test the model locally by making a prediction."""
118
+ # validate that we have checkpoints downloaded before constructing MyRunner
119
+ uploader = ModelUploader(self.model_path)
120
+ uploader.download_checkpoints()
121
+ # construct MyRunner which will call load_model()
122
+ MyRunner = self._get_model_runner()
123
+ runner = MyRunner(
124
+ runner_id="n/a",
125
+ nodepool_id="n/a",
126
+ compute_cluster_id="n/a",
127
+ )
128
+ runner.load_model()
129
+
130
+ # send an inference.
131
+ response = self._run_model_inference(runner)
132
+ if response.outputs[0].status.code != status_code_pb2.SUCCESS:
133
+ logger.error(f"Moddel Prediction failed: {response}")
134
+ else:
135
+ logger.info(f"Model Prediction succeeded: {response}")
136
+
137
+ def test_model(self):
138
+ """Test the model by running it locally in the virtual environment."""
139
+ command = [
140
+ self.python_executable,
141
+ "-c",
142
+ f"import sys; sys.path.append('{os.path.dirname(os.path.abspath(__file__))}'); "
143
+ f"from model_run_locally import ModelRunLocally; ModelRunLocally('{self.model_path}')._run_test()",
144
+ ]
145
+ try:
146
+ logger.info("Testing the model locally...")
147
+ subprocess.check_call(command)
148
+ logger.info("Model tested successfully!")
149
+ except subprocess.CalledProcessError as e:
150
+ logger.error(f"Error testing the model: {e}")
151
+ sys.exit(1)
152
+
153
+ # run the model server
154
+ def run_model_server(self):
155
+ """Run the Clarifai Runners's model server."""
156
+
157
+ command = [
158
+ self.python_executable, "-m", "clarifai.runners.server", "--model_path", self.model_path,
159
+ "--start_dev_server"
160
+ ]
161
+ try:
162
+ logger.info(f"Starting model server with model at {self.model_path}...")
163
+ subprocess.check_call(command)
164
+ logger.info("Model server started successfully!")
165
+ except subprocess.CalledProcessError as e:
166
+ logger.error(f"Error running model server: {e}")
167
+ self.clean_up()
168
+ sys.exit(1)
169
+
170
+ def clean_up(self):
171
+ """Clean up the temporary virtual environment."""
172
+ if os.path.exists(self.temp_dir):
173
+ logger.info("Cleaning up temporary virtual environment...")
174
+ shutil.rmtree(self.temp_dir)
175
+
176
+
177
+ def main():
178
+ parser = argparse.ArgumentParser()
179
+ parser.add_argument(
180
+ '--model_path', type=str, required=True, help='Path of the model folder to upload')
181
+ args = parser.parse_args()
182
+
183
+ model_path = args.model_path
184
+ manager = ModelRunLocally(model_path)
185
+
186
+ try:
187
+ manager.install_requirements()
188
+ manager.test_model()
189
+ finally:
190
+ manager.clean_up()
191
+
192
+
193
+ if __name__ == "__main__":
194
+ main()
@@ -10,7 +10,7 @@ from google.protobuf import json_format
10
10
  from rich import print
11
11
 
12
12
  from clarifai.client import BaseClient
13
- from clarifai.runners.utils.loader import HuggingFaceLoarder
13
+ from clarifai.runners.utils.loader import HuggingFaceLoader
14
14
  from clarifai.urls.helper import ClarifaiUrlHelper
15
15
  from clarifai.utils.logging import logger
16
16
 
@@ -59,6 +59,27 @@ class ModelUploader:
59
59
  config = yaml.safe_load(file)
60
60
  return config
61
61
 
62
+ def _validate_config_checkpoints(self):
63
+ if not self.config.get("checkpoints"):
64
+ logger.info("No checkpoints specified in the config file")
65
+ return None
66
+
67
+ assert "type" in self.config.get("checkpoints"), "No loader type specified in the config file"
68
+ loader_type = self.config.get("checkpoints").get("type")
69
+ if not loader_type:
70
+ logger.info("No loader type specified in the config file for checkpoints")
71
+ assert loader_type == "huggingface", "Only huggingface loader supported for now"
72
+ if loader_type == "huggingface":
73
+ assert "repo_id" in self.config.get("checkpoints"), "No repo_id specified in the config file"
74
+ repo_id = self.config.get("checkpoints").get("repo_id")
75
+
76
+ # prefer env var for HF_TOKEN but if not provided then use the one from config.yaml if any.
77
+ if 'HF_TOKEN' in os.environ:
78
+ hf_token = os.environ['HF_TOKEN']
79
+ else:
80
+ hf_token = self.config.get("checkpoints").get("hf_token", None)
81
+ return repo_id, hf_token
82
+
62
83
  @property
63
84
  def client(self):
64
85
  if self._client is None:
@@ -180,27 +201,9 @@ class ModelUploader:
180
201
  return f"{self.folder}.tar.gz"
181
202
 
182
203
  def download_checkpoints(self):
183
- if not self.config.get("checkpoints"):
184
- logger.info("No checkpoints specified in the config file")
185
- return
186
-
187
- assert "type" in self.config.get("checkpoints"), "No loader type specified in the config file"
188
- loader_type = self.config.get("checkpoints").get("type")
189
- if not loader_type:
190
- logger.info("No loader type specified in the config file for checkpoints")
191
- assert loader_type == "huggingface", "Only huggingface loader supported for now"
192
- if loader_type == "huggingface":
193
- assert "repo_id" in self.config.get("checkpoints"), "No repo_id specified in the config file"
194
- repo_id = self.config.get("checkpoints").get("repo_id")
195
-
196
- # prefer env var for HF_TOKEN but if not provided then use the one from config.yaml if any.
197
- if 'HF_TOKEN' in os.environ:
198
- hf_token = os.environ['HF_TOKEN']
199
- else:
200
- hf_token = self.config.get("checkpoints").get("hf_token", None)
201
- assert hf_token != 'hf_token', "The default 'hf_token' is not valid. Please provide a valid token or leave that field out of config.yaml if not needed."
202
- loader = HuggingFaceLoarder(repo_id=repo_id, token=hf_token)
203
-
204
+ repo_id, hf_token = self._validate_config_checkpoints()
205
+ if repo_id and hf_token:
206
+ loader = HuggingFaceLoader(repo_id=repo_id, token=hf_token)
204
207
  success = loader.download_checkpoints(self.checkpoint_path)
205
208
 
206
209
  if not success:
@@ -232,7 +235,7 @@ class ModelUploader:
232
235
  concepts = config.get('concepts')
233
236
  logger.info(f"Updated config.yaml with {len(concepts)} concepts.")
234
237
 
235
- def _get_model_version_proto(self):
238
+ def get_model_version_proto(self):
236
239
 
237
240
  model_version_proto = resources_pb2.ModelVersion(
238
241
  pretrained_model_config=resources_pb2.PretrainedModelConfig(),
@@ -242,8 +245,7 @@ class ModelUploader:
242
245
  model_type_id = self.config.get('model').get('model_type_id')
243
246
  if model_type_id in self.CONCEPTS_REQUIRED_MODEL_TYPE:
244
247
 
245
- loader = HuggingFaceLoarder()
246
- labels = loader.fetch_labels(self.checkpoint_path)
248
+ labels = HuggingFaceLoader.fetch_labels(self.checkpoint_path)
247
249
  # sort the concepts by id and then update the config file
248
250
  labels = sorted(labels.items(), key=lambda x: int(x[0]))
249
251
 
@@ -258,6 +260,21 @@ class ModelUploader:
258
260
  file_path = f"{self.folder}.tar.gz"
259
261
  logger.info(f"Will tar it into file: {file_path}")
260
262
 
263
+ model_type_id = self.config.get('model').get('model_type_id')
264
+ repo_id, hf_token = self._validate_config_checkpoints()
265
+
266
+ loader = HuggingFaceLoader(repo_id=repo_id, token=hf_token)
267
+
268
+ if not download_checkpoints and not loader.validate_download(self.checkpoint_path) and (
269
+ model_type_id in self.CONCEPTS_REQUIRED_MODEL_TYPE) and 'concepts' not in self.config:
270
+ logger.error(
271
+ f"Model type {model_type_id} requires concepts to be specified in the config file or download the model checkpoints to infer the concepts."
272
+ )
273
+ input("Press Enter to download the checkpoints to infer the concepts and continue...")
274
+ self.download_checkpoints()
275
+
276
+ model_version_proto = self.get_model_version_proto()
277
+
261
278
  if download_checkpoints:
262
279
  tar_cmd = f"tar --exclude=*~ -czvf {self.tar_file} -C {self.folder} ."
263
280
  else: # we don't want to send the checkpoints up even if they are in the folder.
@@ -268,8 +285,6 @@ class ModelUploader:
268
285
  os.system(tar_cmd)
269
286
  logger.info("Tarring complete, about to start upload.")
270
287
 
271
- model_version_proto = self._get_model_version_proto()
272
-
273
288
  file_size = os.path.getsize(self.tar_file)
274
289
  logger.info(f"Size of the tar is: {file_size} bytes")
275
290
 
@@ -6,7 +6,9 @@ import subprocess
6
6
  from clarifai.utils.logging import logger
7
7
 
8
8
 
9
- class HuggingFaceLoarder:
9
+ class HuggingFaceLoader:
10
+
11
+ HF_DOWNLOAD_TEXT = "The 'huggingface_hub' package is not installed. Please install it using 'pip install huggingface_hub'."
10
12
 
11
13
  def __init__(self, repo_id=None, token=None):
12
14
  self.repo_id = repo_id
@@ -14,9 +16,7 @@ class HuggingFaceLoarder:
14
16
  if token:
15
17
  try:
16
18
  if importlib.util.find_spec("huggingface_hub") is None:
17
- raise ImportError(
18
- "The 'huggingface_hub' package is not installed. Please install it using 'pip install huggingface_hub'."
19
- )
19
+ raise ImportError(self.HF_DOWNLOAD_TEXT)
20
20
  os.environ['HF_TOKEN'] = token
21
21
  subprocess.run(f'huggingface-cli login --token={os.environ["HF_TOKEN"]}', shell=True)
22
22
  except Exception as e:
@@ -27,9 +27,7 @@ class HuggingFaceLoarder:
27
27
  try:
28
28
  from huggingface_hub import snapshot_download
29
29
  except ImportError:
30
- raise ImportError(
31
- "The 'huggingface_hub' package is not installed. Please install it using 'pip install huggingface_hub'."
32
- )
30
+ raise ImportError(self.HF_DOWNLOAD_TEXT)
33
31
  if os.path.exists(checkpoint_path) and self.validate_download(checkpoint_path):
34
32
  logger.info("Checkpoints already exist")
35
33
  return True
@@ -54,20 +52,26 @@ class HuggingFaceLoarder:
54
52
 
55
53
  def validate_hf_model(self,):
56
54
  # check if model exists on HF
57
-
58
- from huggingface_hub import file_exists, repo_exists
55
+ try:
56
+ from huggingface_hub import file_exists, repo_exists
57
+ except ImportError:
58
+ raise ImportError(self.HF_DOWNLOAD_TEXT)
59
59
  return repo_exists(self.repo_id) and file_exists(self.repo_id, 'config.json')
60
60
 
61
61
  def validate_download(self, checkpoint_path: str):
62
62
  # check if model exists on HF
63
- from huggingface_hub import list_repo_files
63
+ try:
64
+ from huggingface_hub import list_repo_files
65
+ except ImportError:
66
+ raise ImportError(self.HF_DOWNLOAD_TEXT)
64
67
  checkpoint_dir_files = [
65
68
  f for dp, dn, fn in os.walk(os.path.expanduser(checkpoint_path)) for f in fn
66
69
  ]
67
70
  return (len(checkpoint_dir_files) >= len(list_repo_files(self.repo_id))) and len(
68
71
  list_repo_files(self.repo_id)) > 0
69
72
 
70
- def fetch_labels(self, checkpoint_path: str):
73
+ @staticmethod
74
+ def fetch_labels(checkpoint_path: str):
71
75
  # Fetch labels for classification, detection and segmentation models
72
76
  config_path = os.path.join(checkpoint_path, 'config.json')
73
77
  with open(config_path, 'r') as f:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: clarifai
3
- Version: 10.9.0
3
+ Version: 10.9.1
4
4
  Summary: Clarifai Python SDK
5
5
  Home-page: https://github.com/Clarifai/clarifai-python
6
6
  Author: Clarifai
@@ -20,7 +20,7 @@ Classifier: Operating System :: OS Independent
20
20
  Requires-Python: >=3.8
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENSE
23
- Requires-Dist: clarifai-grpc>=10.8.7
23
+ Requires-Dist: clarifai-grpc>=10.8.8
24
24
  Requires-Dist: clarifai-protocol>=0.0.6
25
25
  Requires-Dist: numpy>=1.22.0
26
26
  Requires-Dist: tqdm>=4.65.0
@@ -72,6 +72,7 @@ clarifai/runners/dockerfile_template/Dockerfile.cuda.template
72
72
  clarifai/runners/models/__init__.py
73
73
  clarifai/runners/models/base_typed_model.py
74
74
  clarifai/runners/models/model_class.py
75
+ clarifai/runners/models/model_run_locally.py
75
76
  clarifai/runners/models/model_runner.py
76
77
  clarifai/runners/models/model_servicer.py
77
78
  clarifai/runners/models/model_upload.py
@@ -1,4 +1,4 @@
1
- clarifai-grpc>=10.8.7
1
+ clarifai-grpc>=10.8.8
2
2
  clarifai-protocol>=0.0.6
3
3
  numpy>=1.22.0
4
4
  tqdm>=4.65.0
@@ -1,4 +1,4 @@
1
- clarifai-grpc>=10.8.7
1
+ clarifai-grpc>=10.8.8
2
2
  clarifai-protocol>=0.0.6
3
3
  numpy>=1.22.0
4
4
  tqdm>=4.65.0
@@ -1 +0,0 @@
1
- __version__ = "10.9.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes