Build and Upload Models
Build and import models, including from sources like Hugging Face
The Clarifai Python SDK allows you to upload custom models easily. Whether you're working with a pre-trained model from an external source like Hugging Face, or one you've built from scratch, Clarifai allows seamless integration of your models, enabling you to take advantage of the platform’s powerful capabilities.
Once imported to our platform, your model can be utilized alongside Clarifai's vast suite of AI tools. It will be automatically deployed and ready to be evaluated, combined with other models and agent operators in a workflow, or used to serve inference requests as it is.
Let’s walk through how to build and upload a custom model to the Clarifai platform. This example model appends the phrase Hello World to any input text and also supports streaming responses.
Explore this repository for examples on uploading different model types.
Step 1: Perform Prerequisites
Sign Up and Log In
Log in to your existing Clarifai account, or sign up for a new one. If you’re creating a new account, a default application will be provided for you.
Then authenticate using the CLI:
clarifai login
The CLI will prompt for your Personal Access Token (PAT) — find it in Settings > Secrets in the left sidebar. It will auto-detect your user ID and save everything locally. You can verify with clarifai whoami.
Note: Alternatively, you can set
export CLARIFAI_PAT=YOUR_PAT_HEREas an environment variable, butclarifai loginis the recommended approach since it also configures your user ID and app context.
Install Clarifai Package
Install the latest version of the clarifai Python package. This will also install the Clarifai Command Line Interface (CLI), which we'll use for testing and uploading the model.
- Bash
pip install --upgrade clarifai
Set Up Compute (For Manual Deploy Only)
If you plan to use clarifai model deploy (recommended), you can skip this step entirely — the CLI automatically creates the compute cluster, nodepool, and deployment for you. See the Getting Started guide for the simplified flow.
If you prefer to manage compute infrastructure manually, your model requires a dedicated compute environment consisting of a cluster and a nodepool.
Note: A cluster is the foundation of your compute environment, while a nodepool is a single compute node or a group of nodes within a cluster that provides the resources your model requires.
Set Up Docker or a Virtual Environment
To test, run, and upload your model, you need to set up either a Docker container or a Python virtual environment. This ensures proper dependency management and prevents conflicts in your project.
Both options allow you to work with different Python versions. For example, you can use Python 3.11 for uploading one model and Python 3.12 for another — configured via the config.yaml file.
If Docker is installed on your system, it is highly recommended to use it for running the model. Docker provides better isolation and a fully portable environment, including for Python and system libraries.
You should ensure your local environment has sufficient memory and compute resources to handle model loading and execution, especially during testing.
Create Project Directory
You can automatically generate the required files by running the clarifai model init command in the terminal from your current directory. After the files are created, you can modify them as needed.
Create a project directory and organize your files as indicated below to fit the requirements of uploading models to the Clarifai platform.
your_model_directory/
├── 1/
│ └── model.py
├── requirements.txt
└── config.yaml
- your_model_directory/ – The root directory containing all files related to your custom model.
- 1/ – A subdirectory that holds the model file (Note that the folder is named as 1).
- model.py – Contains the code that defines your model, including loading the model and running inference.
- requirements.txt – Lists the Python dependencies required to run your model.
- config.yaml – Contains model metadata and configuration details necessary for building the model, defining compute resources, and more.
- 1/ – A subdirectory that holds the model file (Note that the folder is named as 1).
Step 2: Build a Model
Let's talk about the general steps you'd follow to upload any type of model to the Clarifai platform.
Prepare model.py
The model.py file contains the core logic for your model, including how the model is loaded and how predictions are made. This file must define a custom class that inherits from ModelClass and implements the required methods.
This is the model.py file for the custom model we want to upload:
- Python
from clarifai.runners.models.model_class import ModelClass
from typing import Iterator
class MyModel(ModelClass):
"""A custom runner that adds "Hello World" to the end of the text."""
def load_model(self):
"""Load the model here."""
@ModelClass.method
def predict(self, text1: str = "") -> str:
"""This is the method that will be called when the runner is run. It takes in an input and
returns an output.
"""
output_text = text1 + " Hello World!"
return output_text
@ModelClass.method
def generate(self, text1: str = "") -> Iterator[str]:
"""Example yielding a whole batch of streamed stuff back."""
for i in range(10): # fake something iterating generating 10 times.
output_text = text1 + f"Generate Hello World {i}"
yield output_text
@ModelClass.method
def stream(self, input_iterator: Iterator[str]) -> Iterator[str]:
"""Example yielding a whole batch of streamed stuff back."""
for i, input in enumerate(input_iterator):
output_text = input + f"Stream Hello World {i}"
yield output_text
Let’s break down what each part of the file does.
a. load_model Method
The load_model method is optional but recommended, as it prepares the model for inference by handling resource-heavy initializations. It is particularly useful for:
- One-time setup of heavy resources, such as loading trained models or initializing data transformations.
- Executing tasks during model container startup to reduce runtime latency.
- Loading essential components like tokenizers, pipelines, and other model-related assets.
Here is an example:
def load_model(self):
self.tokenizer = AutoTokenizer.from_pretrained("model/")
self.pipeline = transformers.pipeline(...)