September 14, 2023

Developer Preview: Clarifai Python SDK

Table of Contents:

We’re excited to announce our new Python SDK - https://github.com/Clarifai/clarifai-python for Clarifai, launching in developer preview.

While you can interact with Clarifai with our powerful gRPC API, we also recognize that Python is the most common programming language used by developers, data scientists, and the machine learning community at large, and prefer to use in an object-oriented way. We aim to provide the functionality of the platform, accessible with one or fewer lines of code.

With the new Python SDK, you can now:

  • Simplify your data import. Import datasets along with annotations.
  • Interact with the API in an object-oriented way
  • Create apps, inputs, and datasets, and consume model predictions.
  • Consume models and workflows from the Clarifai community with ease.

We’d love for you to participate in the open beta and provide feedback. Sign up and setup your PAT token, you can access the Python SDK directly today via pip.

pip install -U clarifai

You can also learn more about the Python SDK and API Reference our docs.

Signup and get your PAT Token

The SDK uses PAT token for authentication.  To create a new PAT, log in to the portal, navigate to the upper right-hand section of the navigation bar, and click your user’s profile icon.

Select the Security settings option on the drop-down list.

 

On the ensuing Security page, click the Create Personal Access Token button.


On the command line on your computer, export your PAT as an environment variable

export CLARIFAI_PAT={your personal access token}

Building an AI App with Clarifai-python SDK

Initializing the client

# Note: CLARIFAI_PAT must be set as env variable.
from clarifai.client.user import User
client = User(user_id="user_id")

Get Started by uploading your data

Creating an app in Clarifai

# Create app
app = client.create_app(app_id="demo_app", base_workflow="Universal")
view raw create_app.py hosted with ❤ by GitHub

Dataset Upload

#upload text from csv
dataset = app.create_dataset(dataset_id="demo_dataset")
dataset.upload_from_csv(csv_path='csv_path', input_type='text', csv_type='raw', labels=True)

For more examples, see https://github.com/Clarifai/examples/tree/main/datasets/upload

Customizing Model Inference Output

# Customizing Model Inference Output
from clarifai.client.model import Model
model = Model(user_id="user_id", app_id="app_id", model_id="model_id",
output_config={"min_value": 0.98}) # Return predictions having prediction confidence > 0.98
model_prediction = model.predict_by_filepath(filepath="local_filepath", input_type="text")

Exploring Community Models

Explore and choose state-of-art Vision, Language, and Generative AI models here.

Copy the URL of the selected model as above and start predicting from the models within the Clarifai Community.

Model Predict

# Model Predict
from clarifai.client.model import Model
model_prediction = Model("https://clarifai.com/meta/Llama-2/models/llama2-13b-chat").predict_by_bytes(b"Write a tweet on future of AI", "text")
print(model_prediction)
Additionally, you can list the models from the community with the below snippet:

# List all models in community filtered by model_type, description
from clarifai.client.app import App
all_llm_community_models = App().list_models(filter_by={"query": "LLM",
"model_type_id": "text-to-text"}, only_in_app=False)
print(all_llm_community_models)
view raw list_models.py hosted with ❤ by GitHub

Users of clarifai-python-utils, take notice that the repository is deprecated, and clarifai-python-grpc will still be available with granular API access. For more information on gRPC / HTTP clients, refer to the docs here.

What's Next

Try it out inside the Clarifai platform!

If you haven't already signed up for Clarifai, you're missing out on a lot of cool AI features.

More features are in progress, and we are working on adding Workflow creation with YAML, MLOps processes, and more exciting utilities. If you want to see any feature within the SDK, please reach out to us on Clarifai Discord!