clarifai 10.11.0__py3-none-any.whl → 10.11.2rc1__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/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/__pycache__/errors.cpython-310.pyc +0 -0
- clarifai/__pycache__/versions.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/cli/model.py +206 -10
- clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/runner.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
- clarifai/client/dataset.py +4 -4
- clarifai/client/model.py +94 -13
- clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/datasets/upload/features.py +1 -1
- clarifai/datasets/upload/multimodal.py +2 -1
- clarifai/datasets/upload/text.py +3 -2
- clarifai/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/README.md +158 -0
- clarifai/models/model_serving/__init__.py +14 -0
- clarifai/models/model_serving/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__init__.py +12 -0
- clarifai/models/model_serving/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/_utils.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/build.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/__pycache__/create.cpython-310.pyc +0 -0
- clarifai/models/model_serving/cli/_utils.py +53 -0
- clarifai/models/model_serving/cli/base.py +14 -0
- clarifai/models/model_serving/cli/build.py +79 -0
- clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
- clarifai/models/model_serving/cli/create.py +171 -0
- clarifai/models/model_serving/cli/example_cli.py +34 -0
- clarifai/models/model_serving/cli/login.py +26 -0
- clarifai/models/model_serving/cli/upload.py +183 -0
- clarifai/models/model_serving/constants.py +21 -0
- clarifai/models/model_serving/docs/cli.md +161 -0
- clarifai/models/model_serving/docs/concepts.md +229 -0
- clarifai/models/model_serving/docs/dependencies.md +11 -0
- clarifai/models/model_serving/docs/inference_parameters.md +139 -0
- clarifai/models/model_serving/docs/model_types.md +19 -0
- clarifai/models/model_serving/model_config/__init__.py +16 -0
- clarifai/models/model_serving/model_config/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/config.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/inference_parameter.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/__pycache__/output.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/base.py +369 -0
- clarifai/models/model_serving/model_config/config.py +312 -0
- clarifai/models/model_serving/model_config/inference_parameter.py +129 -0
- clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -0
- clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -0
- clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -0
- clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -0
- clarifai/models/model_serving/model_config/output.py +133 -0
- clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/serializer.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/triton_config.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/__pycache__/wrappers.cpython-310.pyc +0 -0
- clarifai/models/model_serving/model_config/triton/serializer.py +136 -0
- clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
- clarifai/models/model_serving/model_config/triton/wrappers.py +281 -0
- clarifai/models/model_serving/repo_build/__init__.py +14 -0
- clarifai/models/model_serving/repo_build/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/models/model_serving/repo_build/__pycache__/build.cpython-310.pyc +0 -0
- clarifai/models/model_serving/repo_build/build.py +198 -0
- clarifai/models/model_serving/repo_build/static_files/__pycache__/base_test.cpython-310-pytest-7.2.0.pyc +0 -0
- clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
- clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
- clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
- clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
- clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
- clarifai/models/model_serving/repo_build/static_files/triton/model.py +75 -0
- clarifai/models/model_serving/utils.py +31 -0
- clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/rag/rag.py +6 -2
- clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/server.cpython-310.pyc +0 -0
- clarifai/runners/deepgram_live_transcribe.py +98 -0
- clarifai/runners/deepgram_live_transcribe.py~ +98 -0
- clarifai/runners/deepgram_runner.py +131 -0
- clarifai/runners/deepgram_runner.py~ +130 -0
- clarifai/runners/dockerfile_template/Dockerfile.cpu.template +31 -0
- clarifai/runners/dockerfile_template/Dockerfile.cuda.template +79 -0
- clarifai/runners/dockerfile_template/Dockerfile.template +2 -0
- clarifai/runners/example_llama2.py~ +72 -0
- clarifai/runners/matt_example.py +89 -0
- clarifai/runners/matt_example.py~ +87 -0
- clarifai/runners/matt_llm_example.py +129 -0
- clarifai/runners/matt_llm_example.py~ +128 -0
- clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_servicer.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
- clarifai/runners/models/model_run_locally.py +358 -33
- clarifai/runners/models/model_upload.py +116 -65
- clarifai/runners/server.py +2 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
- clarifai/runners/utils/const.py +42 -0
- clarifai/runners/utils/loader.py +31 -16
- clarifai/runners/utils/logging.py +6 -0
- clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
- clarifai/utils/logging.py +7 -0
- clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
- {clarifai-10.11.0.dist-info → clarifai-10.11.2rc1.dist-info}/METADATA +86 -16
- clarifai-10.11.2rc1.dist-info/RECORD +242 -0
- {clarifai-10.11.0.dist-info → clarifai-10.11.2rc1.dist-info}/WHEEL +1 -1
- clarifai-10.11.0.dist-info/RECORD +0 -99
- {clarifai-10.11.0.dist-info → clarifai-10.11.2rc1.dist-info}/LICENSE +0 -0
- {clarifai-10.11.0.dist-info → clarifai-10.11.2rc1.dist-info}/entry_points.txt +0 -0
- {clarifai-10.11.0.dist-info → clarifai-10.11.2rc1.dist-info}/top_level.txt +0 -0
@@ -3,9 +3,11 @@ import importlib.util
|
|
3
3
|
import inspect
|
4
4
|
import os
|
5
5
|
import shutil
|
6
|
+
import signal
|
6
7
|
import subprocess
|
7
8
|
import sys
|
8
9
|
import tempfile
|
10
|
+
import time
|
9
11
|
import traceback
|
10
12
|
import venv
|
11
13
|
|
@@ -24,10 +26,18 @@ class ModelRunLocally:
|
|
24
26
|
self.model_path = model_path
|
25
27
|
self.requirements_file = os.path.join(self.model_path, "requirements.txt")
|
26
28
|
|
29
|
+
# ModelUploader contains multiple useful methods to interact with the model
|
30
|
+
self.uploader = ModelUploader(self.model_path)
|
31
|
+
self.config = self.uploader.config
|
32
|
+
|
33
|
+
def _requirements_hash(self):
|
34
|
+
"""Generate a hash of the requirements file."""
|
35
|
+
with open(self.requirements_file, "r") as f:
|
36
|
+
return hashlib.md5(f.read().encode('utf-8')).hexdigest()
|
37
|
+
|
27
38
|
def create_temp_venv(self):
|
28
39
|
"""Create a temporary virtual environment."""
|
29
|
-
|
30
|
-
requirements_hash = hashlib.md5(f.read().encode('utf-8')).hexdigest()
|
40
|
+
requirements_hash = self._requirements_hash()
|
31
41
|
|
32
42
|
temp_dir = os.path.join(tempfile.gettempdir(), str(requirements_hash))
|
33
43
|
venv_dir = os.path.join(temp_dir, "venv")
|
@@ -105,33 +115,91 @@ class ModelRunLocally:
|
|
105
115
|
text=resources_pb2.Text(raw="How many people live in new york?"),
|
106
116
|
image=resources_pb2.Image(url="https://samples.clarifai.com/metro-north.jpg"),
|
107
117
|
audio=resources_pb2.Audio(url="https://samples.clarifai.com/GoodMorning.wav"),
|
118
|
+
video=resources_pb2.Video(url="https://samples.clarifai.com/beer.mp4"),
|
108
119
|
))
|
109
120
|
],
|
110
121
|
)
|
111
122
|
|
123
|
+
def _build_stream_request(self):
|
124
|
+
request = self._build_request()
|
125
|
+
for i in range(1):
|
126
|
+
yield request
|
127
|
+
|
112
128
|
def _run_model_inference(self, runner):
|
113
129
|
"""Perform inference using the runner."""
|
114
130
|
request = self._build_request()
|
131
|
+
stream_request = self._build_stream_request()
|
115
132
|
|
116
133
|
ensure_urls_downloaded(request)
|
117
|
-
|
134
|
+
predict_response = None
|
135
|
+
generate_response = None
|
136
|
+
stream_response = None
|
118
137
|
try:
|
119
|
-
|
138
|
+
predict_response = runner.predict(request)
|
139
|
+
except NotImplementedError:
|
140
|
+
logger.info("Model does not implement predict() method.")
|
120
141
|
except Exception as e:
|
121
142
|
logger.error(f"Model Prediction failed: {e}")
|
122
143
|
traceback.print_exc()
|
123
|
-
|
144
|
+
predict_response = service_pb2.MultiOutputResponse(status=status_pb2.Status(
|
124
145
|
code=status_code_pb2.MODEL_PREDICTION_FAILED,
|
125
146
|
description="Prediction failed",
|
126
147
|
details="",
|
127
148
|
internal_details=str(e),
|
128
149
|
))
|
129
150
|
|
151
|
+
if predict_response:
|
152
|
+
if predict_response.outputs[0].status.code != status_code_pb2.SUCCESS:
|
153
|
+
logger.error(f"Moddel Prediction failed: {predict_response}")
|
154
|
+
else:
|
155
|
+
logger.info(f"Model Prediction succeeded: {predict_response}")
|
156
|
+
|
157
|
+
try:
|
158
|
+
generate_response = runner.generate(request)
|
159
|
+
except NotImplementedError:
|
160
|
+
logger.info("Model does not implement generate() method.")
|
161
|
+
except Exception as e:
|
162
|
+
logger.error(f"Model Generation failed: {e}")
|
163
|
+
traceback.print_exc()
|
164
|
+
generate_response = service_pb2.MultiOutputResponse(status=status_pb2.Status(
|
165
|
+
code=status_code_pb2.MODEL_GENERATION_FAILED,
|
166
|
+
description="Generation failed",
|
167
|
+
details="",
|
168
|
+
internal_details=str(e),
|
169
|
+
))
|
170
|
+
|
171
|
+
if generate_response:
|
172
|
+
generate_first_res = next(generate_response)
|
173
|
+
if generate_first_res.outputs[0].status.code != status_code_pb2.SUCCESS:
|
174
|
+
logger.error(f"Moddel Prediction failed: {generate_first_res}")
|
175
|
+
else:
|
176
|
+
logger.info(
|
177
|
+
f"Model Prediction succeeded for generate and first response: {generate_first_res}")
|
178
|
+
|
179
|
+
try:
|
180
|
+
stream_response = runner.stream(stream_request)
|
181
|
+
except NotImplementedError:
|
182
|
+
logger.info("Model does not implement stream() method.")
|
183
|
+
except Exception as e:
|
184
|
+
logger.error(f"Model Stream failed: {e}")
|
185
|
+
traceback.print_exc()
|
186
|
+
stream_response = service_pb2.MultiOutputResponse(status=status_pb2.Status(
|
187
|
+
code=status_code_pb2.MODEL_STREAM_FAILED,
|
188
|
+
description="Stream failed",
|
189
|
+
details="",
|
190
|
+
internal_details=str(e),
|
191
|
+
))
|
192
|
+
|
193
|
+
if stream_response:
|
194
|
+
stream_first_res = next(stream_response)
|
195
|
+
if stream_first_res.outputs[0].status.code != status_code_pb2.SUCCESS:
|
196
|
+
logger.error(f"Moddel Prediction failed: {stream_first_res}")
|
197
|
+
else:
|
198
|
+
logger.info(
|
199
|
+
f"Model Prediction succeeded for stream and first response: {stream_first_res}")
|
200
|
+
|
130
201
|
def _run_test(self):
|
131
202
|
"""Test the model locally by making a prediction."""
|
132
|
-
# validate that we have checkpoints downloaded before constructing MyRunner
|
133
|
-
uploader = ModelUploader(self.model_path)
|
134
|
-
uploader.download_checkpoints()
|
135
203
|
# construct MyRunner which will call load_model()
|
136
204
|
MyRunner = self._get_model_runner()
|
137
205
|
runner = MyRunner(
|
@@ -140,13 +208,8 @@ class ModelRunLocally:
|
|
140
208
|
compute_cluster_id="n/a",
|
141
209
|
user_id="n/a",
|
142
210
|
)
|
143
|
-
|
144
211
|
# send an inference.
|
145
|
-
|
146
|
-
if response.outputs[0].status.code != status_code_pb2.SUCCESS:
|
147
|
-
logger.error(f"Moddel Prediction failed: {response}")
|
148
|
-
else:
|
149
|
-
logger.info(f"Model Prediction succeeded: {response}")
|
212
|
+
self._run_model_inference(runner)
|
150
213
|
|
151
214
|
def test_model(self):
|
152
215
|
"""Test the model by running it locally in the virtual environment."""
|
@@ -156,31 +219,258 @@ class ModelRunLocally:
|
|
156
219
|
f"import sys; sys.path.append('{os.path.dirname(os.path.abspath(__file__))}'); "
|
157
220
|
f"from model_run_locally import ModelRunLocally; ModelRunLocally('{self.model_path}')._run_test()",
|
158
221
|
]
|
222
|
+
process = None
|
159
223
|
try:
|
160
224
|
logger.info("Testing the model locally...")
|
161
|
-
subprocess.
|
162
|
-
|
225
|
+
process = subprocess.Popen(command)
|
226
|
+
# Wait for the process to complete
|
227
|
+
process.wait()
|
228
|
+
if process.returncode == 0:
|
229
|
+
logger.info("Model tested successfully!")
|
230
|
+
if process.returncode != 0:
|
231
|
+
raise subprocess.CalledProcessError(process.returncode, command)
|
163
232
|
except subprocess.CalledProcessError as e:
|
164
233
|
logger.error(f"Error testing the model: {e}")
|
165
234
|
sys.exit(1)
|
235
|
+
except Exception as e:
|
236
|
+
logger.error(f"Unexpected error: {e}")
|
237
|
+
sys.exit(1)
|
238
|
+
finally:
|
239
|
+
# After the function runs, check if the process is still running
|
240
|
+
if process and process.poll() is None:
|
241
|
+
logger.info("Process is still running. Terminating process.")
|
242
|
+
process.terminate()
|
243
|
+
try:
|
244
|
+
process.wait(timeout=5)
|
245
|
+
except subprocess.TimeoutExpired:
|
246
|
+
logger.info("Process did not terminate gracefully. Killing process.")
|
247
|
+
# Kill the process if it doesn't terminate after 5 seconds
|
248
|
+
process.kill()
|
166
249
|
|
167
250
|
# run the model server
|
168
|
-
def run_model_server(self):
|
251
|
+
def run_model_server(self, port=8080):
|
169
252
|
"""Run the Clarifai Runners's model server."""
|
170
253
|
|
171
254
|
command = [
|
172
255
|
self.python_executable, "-m", "clarifai.runners.server", "--model_path", self.model_path,
|
173
|
-
"--start_dev_server"
|
256
|
+
"--start_dev_server", "--port",
|
257
|
+
str(port)
|
174
258
|
]
|
175
259
|
try:
|
176
|
-
logger.info(
|
260
|
+
logger.info(
|
261
|
+
f"Starting model server at localhost:{port} with the model at {self.model_path}...")
|
177
262
|
subprocess.check_call(command)
|
178
|
-
logger.info("Model server started successfully
|
263
|
+
logger.info("Model server started successfully and running at localhost:{port}")
|
179
264
|
except subprocess.CalledProcessError as e:
|
180
265
|
logger.error(f"Error running model server: {e}")
|
181
266
|
self.clean_up()
|
182
267
|
sys.exit(1)
|
183
268
|
|
269
|
+
def _docker_hash(self):
|
270
|
+
"""Generate a hash of the combined requirements file and Dockefile"""
|
271
|
+
with open(self.requirements_file, "r") as f:
|
272
|
+
requirements_hash = hashlib.md5(f.read().encode('utf-8')).hexdigest()
|
273
|
+
with open(os.path.join(self.model_path, "Dockerfile"), "r") as f:
|
274
|
+
dockerfile_hash = hashlib.md5(f.read().encode('utf-8')).hexdigest()
|
275
|
+
|
276
|
+
return hashlib.md5(f"{requirements_hash}{dockerfile_hash}".encode('utf-8')).hexdigest()
|
277
|
+
|
278
|
+
def is_docker_installed(self):
|
279
|
+
"""Checks if Docker is installed on the system."""
|
280
|
+
try:
|
281
|
+
logger.info("Checking if Docker is installed...")
|
282
|
+
subprocess.run(["docker", "--version"], check=True)
|
283
|
+
return True
|
284
|
+
except subprocess.CalledProcessError:
|
285
|
+
logger.error(
|
286
|
+
"Docker is not installed! Please install Docker to run the model in a container.")
|
287
|
+
return False
|
288
|
+
|
289
|
+
def build_docker_image(
|
290
|
+
self,
|
291
|
+
image_name="model_image",
|
292
|
+
):
|
293
|
+
"""Build the docker image using the Dockerfile in the model directory."""
|
294
|
+
try:
|
295
|
+
logger.info(f"Building docker image from Dockerfile in {self.model_path}...")
|
296
|
+
|
297
|
+
# since we don't want to copy the model directory into the container, we need to modify the Dockerfile and comment out the COPY instruction
|
298
|
+
dockerfile_path = os.path.join(self.model_path, "Dockerfile")
|
299
|
+
# Read the Dockerfile
|
300
|
+
with open(dockerfile_path, 'r') as file:
|
301
|
+
lines = file.readlines()
|
302
|
+
|
303
|
+
# Comment out the COPY instruction that copies the current folder
|
304
|
+
modified_lines = []
|
305
|
+
for line in lines:
|
306
|
+
if 'COPY .' in line and '/app/model_dir/main' in line:
|
307
|
+
modified_lines.append(f'# {line}')
|
308
|
+
else:
|
309
|
+
modified_lines.append(line)
|
310
|
+
|
311
|
+
# Create a temporary directory to store the modified Dockerfile
|
312
|
+
with tempfile.TemporaryDirectory() as temp_dir:
|
313
|
+
temp_dockerfile_path = os.path.join(temp_dir, "Dockerfile.temp")
|
314
|
+
|
315
|
+
# Write the modified Dockerfile to the temporary file
|
316
|
+
with open(temp_dockerfile_path, 'w') as file:
|
317
|
+
file.writelines(modified_lines)
|
318
|
+
|
319
|
+
# Build the Docker image using the temporary Dockerfile
|
320
|
+
subprocess.check_call(
|
321
|
+
['docker', 'build', '-t', image_name, '-f', temp_dockerfile_path, self.model_path])
|
322
|
+
logger.info(f"Docker image '{image_name}' built successfully!")
|
323
|
+
except subprocess.CalledProcessError as e:
|
324
|
+
logger.info(f"Error occurred while building the Docker image: {e}")
|
325
|
+
sys.exit(1)
|
326
|
+
|
327
|
+
def docker_image_exists(self, image_name):
|
328
|
+
"""Check if the Docker image exists."""
|
329
|
+
try:
|
330
|
+
logger.info(f"Checking if Docker image '{image_name}' exists...")
|
331
|
+
subprocess.run(["docker", "inspect", image_name], check=True)
|
332
|
+
logger.info(f"Docker image '{image_name}' exists!")
|
333
|
+
return True
|
334
|
+
except subprocess.CalledProcessError:
|
335
|
+
logger.info(f"Docker image '{image_name}' does not exist!")
|
336
|
+
return False
|
337
|
+
|
338
|
+
def run_docker_container(self,
|
339
|
+
image_name,
|
340
|
+
container_name="clarifai-model-container",
|
341
|
+
port=8080,
|
342
|
+
env_vars=None):
|
343
|
+
"""Runs a Docker container from the specified image."""
|
344
|
+
try:
|
345
|
+
logger.info(f"Running Docker container '{container_name}' from image '{image_name}'...")
|
346
|
+
# Base docker run command
|
347
|
+
cmd = [
|
348
|
+
"docker", "run", "--name", container_name, '--rm', "--gpus", "all", "--network", "host"
|
349
|
+
]
|
350
|
+
# Add volume mappings
|
351
|
+
cmd.extend(["-v", f"{self.model_path}:/app/model_dir/main"])
|
352
|
+
# Add environment variables
|
353
|
+
if env_vars:
|
354
|
+
for key, value in env_vars.items():
|
355
|
+
cmd.extend(["-e", f"{key}={value}"])
|
356
|
+
# Add the image name
|
357
|
+
cmd.append(image_name)
|
358
|
+
# update the CMD to run the server
|
359
|
+
cmd.extend(
|
360
|
+
["--model_path", "/app/model_dir/main", "--start_dev_server", "--port",
|
361
|
+
str(port)])
|
362
|
+
# Run the container
|
363
|
+
process = subprocess.Popen(cmd,)
|
364
|
+
logger.info(
|
365
|
+
f"Docker container '{container_name}' is running successfully! access the model at http://localhost:{port}"
|
366
|
+
)
|
367
|
+
|
368
|
+
# Function to handle Ctrl+C (SIGINT) gracefully
|
369
|
+
def signal_handler(sig, frame):
|
370
|
+
logger.info(f"Stopping Docker container '{container_name}'...")
|
371
|
+
subprocess.run(["docker", "stop", container_name], check=True)
|
372
|
+
process.terminate()
|
373
|
+
logger.info(f"Docker container '{container_name}' stopped successfully!")
|
374
|
+
time.sleep(1)
|
375
|
+
sys.exit(0)
|
376
|
+
|
377
|
+
# Register the signal handler for SIGINT (Ctrl+C)
|
378
|
+
signal.signal(signal.SIGINT, signal_handler)
|
379
|
+
# Wait for the process to finish (keeps the container running until it's stopped)
|
380
|
+
process.wait()
|
381
|
+
except subprocess.CalledProcessError as e:
|
382
|
+
logger.info(f"Error occurred while running the Docker container: {e}")
|
383
|
+
sys.exit(1)
|
384
|
+
except Exception as e:
|
385
|
+
logger.info(f"Error occurred while running the Docker container: {e}")
|
386
|
+
sys.exit(1)
|
387
|
+
|
388
|
+
def test_model_container(self,
|
389
|
+
image_name,
|
390
|
+
container_name="clarifai-model-container",
|
391
|
+
env_vars=None):
|
392
|
+
"""Test the model inside the Docker container."""
|
393
|
+
try:
|
394
|
+
logger.info("Testing the model inside the Docker container...")
|
395
|
+
# Base docker run command
|
396
|
+
cmd = [
|
397
|
+
"docker", "run", "--name", container_name, '--rm', "--gpus", "all", "--network", "host"
|
398
|
+
]
|
399
|
+
# update the entrypoint for testing the model
|
400
|
+
cmd.extend(["--entrypoint", "python"])
|
401
|
+
# Add volume mappings
|
402
|
+
cmd.extend(["-v", f"{self.model_path}:/app/model_dir/main"])
|
403
|
+
# Add environment variables
|
404
|
+
if env_vars:
|
405
|
+
for key, value in env_vars.items():
|
406
|
+
cmd.extend(["-e", f"{key}={value}"])
|
407
|
+
# Add the image name
|
408
|
+
cmd.append(image_name)
|
409
|
+
# update the CMD to test the model inside the container
|
410
|
+
cmd.extend([
|
411
|
+
"-c",
|
412
|
+
"from clarifai.runners.models.model_run_locally import ModelRunLocally; ModelRunLocally('/app/model_dir/main')._run_test()"
|
413
|
+
])
|
414
|
+
# Run the container
|
415
|
+
subprocess.check_call(cmd)
|
416
|
+
logger.info("Model tested successfully!")
|
417
|
+
except subprocess.CalledProcessError as e:
|
418
|
+
logger.error(f"Error testing the model inside the Docker container: {e}")
|
419
|
+
sys.exit(1)
|
420
|
+
|
421
|
+
def container_exists(self, container_name="clarifai-model-container"):
|
422
|
+
"""Check if the Docker container exists."""
|
423
|
+
try:
|
424
|
+
# Run docker ps -a to list all containers (running and stopped)
|
425
|
+
result = subprocess.run(
|
426
|
+
["docker", "ps", "-a", "--filter", f"name={container_name}", "--format", "{{.Names}}"],
|
427
|
+
check=True,
|
428
|
+
capture_output=True,
|
429
|
+
text=True)
|
430
|
+
# If the container name is returned, it exists
|
431
|
+
if result.stdout.strip() == container_name:
|
432
|
+
logger.info(f"Docker container '{container_name}' exists.")
|
433
|
+
return True
|
434
|
+
else:
|
435
|
+
return False
|
436
|
+
except subprocess.CalledProcessError as e:
|
437
|
+
logger.error(f"Error occurred while checking if container exists: {e}")
|
438
|
+
return False
|
439
|
+
|
440
|
+
def stop_docker_container(self, container_name="clarifai-model-container"):
|
441
|
+
"""Stop the Docker container if it's running."""
|
442
|
+
try:
|
443
|
+
# Check if the container is running
|
444
|
+
result = subprocess.run(
|
445
|
+
["docker", "ps", "--filter", f"name={container_name}", "--format", "{{.Names}}"],
|
446
|
+
check=True,
|
447
|
+
capture_output=True,
|
448
|
+
text=True)
|
449
|
+
if result.stdout.strip() == container_name:
|
450
|
+
logger.info(f"Docker container '{container_name}' is running. Stopping it...")
|
451
|
+
subprocess.run(["docker", "stop", container_name], check=True)
|
452
|
+
logger.info(f"Docker container '{container_name}' stopped successfully!")
|
453
|
+
except subprocess.CalledProcessError as e:
|
454
|
+
logger.error(f"Error occurred while stopping the Docker container: {e}")
|
455
|
+
|
456
|
+
def remove_docker_container(self, container_name="clarifai-model-container"):
|
457
|
+
"""Remove the Docker container."""
|
458
|
+
try:
|
459
|
+
logger.info(f"Removing Docker container '{container_name}'...")
|
460
|
+
subprocess.run(["docker", "rm", container_name], check=True)
|
461
|
+
logger.info(f"Docker container '{container_name}' removed successfully!")
|
462
|
+
except subprocess.CalledProcessError as e:
|
463
|
+
logger.error(f"Error occurred while removing the Docker container: {e}")
|
464
|
+
|
465
|
+
def remove_docker_image(self, image_name):
|
466
|
+
"""Remove the Docker image."""
|
467
|
+
try:
|
468
|
+
logger.info(f"Removing Docker image '{image_name}'...")
|
469
|
+
subprocess.run(["docker", "rmi", image_name], check=True)
|
470
|
+
logger.info(f"Docker image '{image_name}' removed successfully!")
|
471
|
+
except subprocess.CalledProcessError as e:
|
472
|
+
logger.error(f"Error occurred while removing the Docker image: {e}")
|
473
|
+
|
184
474
|
def clean_up(self):
|
185
475
|
"""Clean up the temporary virtual environment."""
|
186
476
|
if os.path.exists(self.temp_dir):
|
@@ -188,18 +478,53 @@ class ModelRunLocally:
|
|
188
478
|
shutil.rmtree(self.temp_dir)
|
189
479
|
|
190
480
|
|
191
|
-
def main(model_path,
|
481
|
+
def main(model_path,
|
482
|
+
run_model_server=False,
|
483
|
+
inside_container=False,
|
484
|
+
port=8080,
|
485
|
+
keep_env=False,
|
486
|
+
keep_image=False):
|
192
487
|
|
488
|
+
if not os.environ['CLARIFAI_PAT']:
|
489
|
+
logger.error(
|
490
|
+
"CLARIFAI_PAT environment variable is not set! Please set your PAT in the 'CLARIFAI_PAT' environment variable."
|
491
|
+
)
|
492
|
+
sys.exit(1)
|
193
493
|
manager = ModelRunLocally(model_path)
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
494
|
+
manager.uploader.download_checkpoints()
|
495
|
+
if inside_container:
|
496
|
+
if not manager.is_docker_installed():
|
497
|
+
sys.exit(1)
|
498
|
+
manager.uploader.create_dockerfile()
|
499
|
+
image_tag = manager._docker_hash()
|
500
|
+
image_name = f"{manager.config['model']['id']}:{image_tag}"
|
501
|
+
container_name = manager.config['model']['id']
|
502
|
+
if not manager.docker_image_exists(image_name):
|
503
|
+
manager.build_docker_image(image_name=image_name)
|
504
|
+
try:
|
505
|
+
envs = {'CLARIFAI_PAT': os.environ['CLARIFAI_PAT'], 'CLARIFAI_USER_ID': 'n/a'}
|
506
|
+
if run_model_server:
|
507
|
+
manager.run_docker_container(
|
508
|
+
image_name=image_name, container_name=container_name, port=port, env_vars=envs)
|
509
|
+
else:
|
510
|
+
manager.test_model_container(
|
511
|
+
image_name=image_name, container_name=container_name, env_vars=envs)
|
512
|
+
finally:
|
513
|
+
if manager.container_exists(container_name):
|
514
|
+
manager.stop_docker_container(container_name)
|
515
|
+
manager.remove_docker_container(container_name=container_name)
|
516
|
+
if not keep_image:
|
517
|
+
manager.remove_docker_image(image_name)
|
518
|
+
|
519
|
+
else:
|
520
|
+
try:
|
521
|
+
use_existing_env = manager.create_temp_venv()
|
522
|
+
if not use_existing_env:
|
523
|
+
manager.install_requirements()
|
524
|
+
if run_model_server:
|
525
|
+
manager.run_model_server(port)
|
526
|
+
else:
|
527
|
+
manager.test_model()
|
528
|
+
finally:
|
529
|
+
if not keep_env:
|
530
|
+
manager.clean_up()
|