You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide will walk you through the process of uploading custom models to the Clarifai platform, leveraging pre-built model examples for different tasks. You'll also learn to customize models and adjust configurations for deployment.
8
8
9
+
## Contents
10
+
11
+
-[Clarifai Model Upload](#clarifai-model-upload)
12
+
-[Contents](#contents)
13
+
-[Available Model Examples](#available-model-examples)
Clarifai provides a collection of pre-built model examples designed for different tasks. You can leverage these examples to streamline the model upload and deployment process.
11
48
@@ -100,8 +137,21 @@ checkpoints:
100
137
type: "huggingface"
101
138
repo_id: "meta-llama/Meta-Llama-3-8B-Instruct"
102
139
hf_token: "your_hf_token" # Required for private models
140
+
when: "runtime"
103
141
```
104
142
143
+
> `when` in the checkpoints section defines when checkpoints for the model should be downloaded and stored. The `when` parameter must be one of `['upload', 'build', 'runtime']`.
144
+
>
145
+
> * **runtime**: Downloads checkpoints at runtime when loading the model in the `load_model` method.
146
+
> * **build**: Downloads checkpoints at build time, while the image is being built.
147
+
> * **upload**: Downloads checkpoints before uploading the model.
148
+
>
149
+
> For larger models, we highly recommend downloading checkpoints at `runtime`. Downloading them during the `upload` or `build` stages can significantly increase the Docker image size, leading to longer upload times and increased cold start time for the model. while downloading checkpoints at `runtime` has some advantages:
150
+
>
151
+
> * Smaller image sizes
152
+
> * Faster build times
153
+
> * Faster pushes and inference on Clarifai platform
154
+
105
155
#### Model concepts/ labels
106
156
> Important: This section is necessary if your model outputs concepts/labels (e.g., for classification or detection) and is not directly loaded from Hugging Face.
107
157
@@ -124,38 +174,101 @@ concepts:
124
174
### Step 2: Define dependencies in requirements.txt
125
175
List all required Python dependencies for your model in `requirements.txt` file. This ensures that all necessary libraries are installed in the runtime environment
126
176
177
+
178
+
> If your model requires `Torch`, we provide optimized pre-built Torch images as the base for machine learning and inference tasks. These images include all necessary dependencies, ensuring efficient execution.
179
+
>
180
+
>
181
+
> The available pre-built Torch images are:
182
+
> • `2.4.1-py3.11-cuda124`: Based on PyTorch 2.4.1, Python 3.11, and CUDA 12.4.
183
+
> • `2.5.1-py3.11-cuda124`: Based on PyTorch 2.5.1, Python 3.11, and CUDA 12.4.
184
+
> • `2.4.1-py3.12-cuda124`: Based on PyTorch 2.4.1, Python 3.12, and CUDA 12.4.
185
+
> • `2.5.1-py3.12-cuda124`: Based on PyTorch 2.5.1, Python 3.12, and CUDA 12.4.
186
+
>
187
+
> To use a specific Torch version, define it in your `requirements.txt` file like this: `torch==2.5.1` This ensures the correct pre-built image is pulled from Clarifai's container registry, ensuring the correct environment is used. This minimizes cold start times and speeds up model uploads and runtime execution — avoiding the overhead of building images from scratch or pulling and configuring them from external sources.
188
+
> We recommend using either `torch==2.5.1` or `torch==2.4.1`. If your model requires a different Torch version, you can specify it in requirements.txt, but this may slightly increase the model upload time.
189
+
127
190
### Step 3: Prepare the `model.py` File
128
191
129
-
The `model.py` file contains the logic for your model, including how the model is loaded and how predictions are made. This file must implement a class that inherits from `ModelRunner`, which should define the following methods:
192
+
The `model.py` file contains the logic for your model, including how the model is loaded and how predictions are made. This file must implement a class that inherits from `ModelClass`.
193
+
194
+
### Core Model Class Structure**
195
+
196
+
To define a custom model, you need to create a class that inherits from **ModelClass** and implements the **load\_model** method and at least one method decorated with **@ModelClass.method** to define prediction endpoints
197
+
198
+
> Each parameter of the Class method must be annotated with a type. The method's return type must also be annotated. The supported types are described [here](SUPPORTED_DATATYPE.md):
130
199
131
200
#### Example Class Structure:
132
201
133
202
```python
134
203
from clarifai.runners.models.model_class import ModelClass
135
-
from clarifai.runners.models.model_builder import ModelBuilder
204
+
from clarifai.runners.utils.data_types import Stream, Text
205
+
206
+
207
+
class MyModel(ModelClass):
208
+
"""A custom runner that adds "Hello World" to the end of the text."""
136
209
137
-
class YourCustomModel(ModelClass):
138
210
def load_model(self):
139
-
'''Initialize and load the model here'''
140
-
pass
211
+
"""Load the model here."""
212
+
213
+
@ModelClass.method
214
+
def predict(self, text1: Text = "") -> Text:
215
+
"""This is the method that will be called when the runner is run. It takes in an input and
216
+
returns an output.
217
+
"""
218
+
219
+
output_text = text1.text + "Hello World"
220
+
221
+
return Text(output_text)
222
+
223
+
@ModelClass.method
224
+
def generate(self, text1: Text = Text("")) -> Stream[Text]:
225
+
"""Example yielding a whole batch of streamed stuff back."""
226
+
227
+
for i in range(10): # fake something iterating generating 10 times.
228
+
output_text = text1.text + f"Generate Hello World {i}"
* **load_model()**: Loads the model, similar to an initialization step.
156
-
* **predict(input_data)**: Handles the core logic for making a prediction with the model. It processes input data and returns a output reponse.
157
-
* **generate(input_data)**: Returns output in a streaming manner (if applicable).
158
-
* **stream(input_data)**: Manages streaming input and output (for more advanced use cases).
270
+
#### [Supported Input and Output Data Types](SUPPORTED_DATATYPE.md)
271
+
Clarifai's model framework supports rich data typing for both inputs and outputs. [Here](SUPPORTED_DATATYPE.md) is a comprehensive guide to supported types with usage examples.
159
272
160
273
### Step 4: Test Model locally
161
274
@@ -171,14 +284,12 @@ This method runs the model locally and sends a sample request to verify that the
171
284
172
285
This method runs the model locally and starts a local gRPC server at \`https://localhost:{port}\`, Once the server is running, you can perform inference on the model via the Clarifai client SDK
173
286
174
-
175
287
You can test the model within a Docker container or a Python virtual environment.
176
288
177
289
> **Recommendation:** If Docker is installed on your system, it is highly recommended to use Docker for testing or running the model, as it provides better isolation and avoids dependency conflicts.
178
290
179
291
1. ### Testing the Model
180
292
181
-
182
293
#### Testing the Model in a Container
183
294
184
295
```python
@@ -250,7 +361,7 @@ clarifai model upload --model_path {model_directory_path}
250
361
This command builds the model Docker image based on the specified compute resources and uploads it to the Clarifai platform.
251
362
252
363
253
-
### Model Prediction
364
+
### Step 6: Model Prediction
254
365
255
366
Once the model is uploaded, you can easily make the prediction to the model using Clarifai SDK.
256
367
@@ -260,35 +371,37 @@ Once the model is uploaded, you can easily make the prediction to the model usin
260
371
261
372
```python
262
373
from clarifai.client.model import Model
263
-
model = Model("url") # Example URL: https://clarifai.com/stepfun-ai/ocr/models/got-ocr-2_0
374
+
model = Model("url", , compute_cluster_id="compute_cluster_id", nodepool_id="nodepool_id")
264
375
or
265
-
model = Model(model_id='model_id', user_id='user_id', app_id='app_id')
0 commit comments