Skip to main content

Create, Train, Get, Update, Delete

Learn how to create, train, get, update, delete, predict, and search your models


info

The initialization code used in the following examples is outlined in detail on the client installation page.

Create

Create a Model

To create a model, you need to specify the model's ID.

If you want to specify the type of model you want to create, you need to provide its ID using the model_type_id parameter⁠—though specifying the model type ID is optional. You can call the ListModelTypes endpoint to learn more about the model types we offer.

Below is an example of how you would create an embedding-classifier (Transfer Learning Classifier) model. It's also the default model type created if you do not specify the model_type_id.

tip

PostModels will create new models but not create new model versions. This means trainable models that have not yet been trained will require the additional step of calling the PostModelVersions endpoint, while providing the *_info fields in the model version—to affect training.

##########################################################################################
# In this section, we set the user authentication, app ID, model ID, and model type ID.
# Change these strings to run your own example.
#########################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to create your own model
MODEL_ID = 'petsID'
MODEL_TYPE_ID = 'embedding-classifier'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

post_models_response = stub.PostModels(
service_pb2.PostModelsRequest(
user_app_id=userDataObject,
models=[
resources_pb2.Model(
id=MODEL_ID,
model_type_id=MODEL_TYPE_ID
)
]
),
metadata=metadata
)

if post_models_response.status.code != status_code_pb2.SUCCESS:
print(post_models_response.status)
raise Exception("Post models failed, status: " + post_models_response.status.description)

Train

Train a Model

When you train an embedding-classifier model, you are telling the system to look at successfully indexed inputs with concepts you've provided and learn from them. This train operation is asynchronous. It may take a few seconds for your model to be fully trained and ready.

However, if training other types of models, such as the deep fine-tuned models, the train operation can take much longer and does not necessarily need indexed inputs.

You can repeat this operation as often as you like. By adding more images with concepts and training, you can get the model to predict exactly how you want it to.

tip
  • The PostModelVersions endpoint kicks off training and creates a new model version. You can also add concepts to a model when creating the model version—and only if the model type supports it as defined in the model type parameters.

  • You can use the PostModelVersions endpoint to give information specific to a model version. All the *_info fields—such as output_info, input_info, train_info, and import_info—are available on this endpoint.

  • You cannot remove the training concepts from a model version. However, you can edit the additional OutputInfo.Params concept options if they are defined in the model type.

  • When training an embedding-classifier, you could specify the enrich_dataset variable inside modelVersion.TrainInfo.Params of the PostModelVersions endpoint. It lets you enrich the model with supplemental data from pre-built datasets of negative embeddings, which improves the model's accuracy. It has two options:

    • Automatic (default) means that if there are negative embeddings for a base model, we will use them—and we won’t use them if they’re not available.
    • Disabled means that we should not use the negative embeddings whether they are available or not.
########################################################################################
# In this section, we set the user authentication, app ID, model ID, and concept ID.
# Change these strings to run your own example.
########################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to train your own model
MODEL_ID = 'petsID'
CONCEPT_ID = 'boscoe'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

post_model_versions = stub.PostModelVersions(
service_pb2.PostModelVersionsRequest(
user_app_id=userDataObject,
model_id=MODEL_ID,
model_versions=[
resources_pb2.ModelVersion(
output_info=resources_pb2.OutputInfo(
data=resources_pb2.Data(
concepts=[resources_pb2.Concept(id=CONCEPT_ID, value=1)] # 1 means true, this concept is present
),
)
)]
),
metadata=metadata
)

if post_model_versions.status.code != status_code_pb2.SUCCESS:
print(post_model_versions.status)
raise Exception("Post models versions failed, status: " + post_model_versions.status.description)

Get

List Model Types

Learn about the available model types and their hyperparameters. This endpoint lets you list all the possible models that are creatable (when creatable=true) or generally in the platform (the other ones have creatable=false).

################################################################
# In this section, we set the user authentication and app ID.
# Change these strings to run your own example.
################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

response = stub.ListModelTypes(
service_pb2.ListModelTypesRequest(
user_app_id=userDataObject
),
metadata=metadata
)

if response.status.code != status_code_pb2.SUCCESS:
print(response.status)
raise Exception("List models failed, status: " + response.status.description)

for model_type in response.model_types:
print(model_type)

List Models

Below is an example of how to get a list of all the models you've created in your app.

info

The ListModels endpoint will return details of all the models in your app, alongside the details of their latest versions.

tip

If you want to get a list of the models not within the scope of your app, you need to use your PAT while specifying the user_id of their owner and the app_id of the application that you’re accessing. For example, to get Clarifai's models in the main app, you need to use your PAT while specifying Clarifai's user_id as "clarifai" and app_id as "main" in the request.

#################################################################
# In this section, we set the user authentication and app ID.
# Change these strings to run your own example.
#################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

list_models_response = stub.ListModels(
service_pb2.ListModelsRequest(
user_app_id=userDataObject
),
metadata=metadata
)

if list_models_response.status.code != status_code_pb2.SUCCESS:
print(list_models_response.status)
raise Exception("List models failed, status: " + list_models_response.status.description)

for model in list_models_response.models:
print(model)

Get Model by ID

All models have unique IDs. You can get a specific model by its ID.

################################################################################
# In this section, we set the user authentication, app ID, and the model ID we
# want to get its details. Change these strings to run your own example.
################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change to your own model ID
MODEL_ID = 'petsID'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

get_model_response = stub.GetModel(
service_pb2.GetModelRequest(
user_app_id=userDataObject,
model_id=MODEL_ID
),
metadata=metadata
)

if get_model_response.status.code != status_code_pb2.SUCCESS:
print(get_model_response.status)
raise Exception("Get model failed, status: " + get_model_response.status.description)

model = get_model_response.model
print(model)

Get Model Output Info by ID

The output info of a model lists the concepts contained in the latest version of the model—unless a particular version is specified.

note

The GetModelOutputInfo endpoint does not support pagination. If you want to split your results into pages, use the below-described ListModelConcepts endpoint.

###############################################################################
# In this section, we set the user authentication, app ID, and the model ID we
# want to get its output info. Change these strings to run your own example.
###############################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change to your own model ID
MODEL_ID = 'petsID'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

get_model_response = stub.GetModelOutputInfo(
service_pb2.GetModelRequest(
user_app_id=userDataObject,
model_id=MODEL_ID
),
metadata=metadata
)

if get_model_response.status.code != status_code_pb2.SUCCESS:
print(get_model_response.status)
raise Exception("Get model failed, status: " + get_model_response.status.description)

model = get_model_response.model
print(model)

List Model Concepts

Apart from the GetModelOutputInfo endpoint, you can also use the ListModelConcepts endpoint to list the concepts in your model.

A major difference between the two is that the ListModelConcepts endpoint supports pagination, which lets you easily list concepts instead of displaying all of them at once.

info

If you are using any of the Clarifai gRPC clients, the ListModelConcepts endpoint is only available from release 8.10.0.

###########################################################################
# In this section, we set the user authentication and model ID.
# Change these strings to run your own example.
###########################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to whatever model you want to retrieve its concepts
MODEL_ID = 'YOUR_MODEL_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

list_model_concepts_response = stub.ListModelConcepts(
service_pb2.ListModelConceptsRequest(
user_app_id=userDataObject,
model_id=MODEL_ID
),
metadata=metadata
)

if list_model_concepts_response.status.code != status_code_pb2.SUCCESS:
print(list_model_concepts_response.status)
raise Exception("List model concept failed, status: " + list_model_concepts_response.status.description)

print(list_model_concepts_response)

List Model Versions

Every time you train a model, it creates a new version. You can list all the versions created.

###############################################################################
# In this section, we set the user authentication, app ID, and the model ID we
# want to list its versions. Change these strings to run your own example.
###############################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change to your own model ID
MODEL_ID = 'petsID'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

list_model_versions_response = stub.ListModelVersions(
service_pb2.ListModelVersionsRequest(
user_app_id=userDataObject,
model_id=MODEL_ID
),
metadata=metadata
)

if list_model_versions_response.status.code != status_code_pb2.SUCCESS:
print(list_model_versions_response.status)
raise Exception("List model versions failed, status: " + list_model_versions_response.status.description)

for model_version in list_model_versions_response.model_versions:
print(model_version)

Get Model Version by ID

To get the details of a specific model version, you must provide the model_id as well as the version_id parameters. You can inspect the model version status to determine if your model is trained or still training.

#########################################################################################
# In this section, we set the user authentication, app ID, and the ID and version
# of the model we want to get its details. Change these strings to run your own example.
#########################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to your own model ID and version
MODEL_ID = 'petsID'
MODEL_VERSION_ID = 'daf51b311c8644a486bbbe57c0680bc2'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

get_model_version_response = stub.GetModelVersion(
service_pb2.GetModelVersionRequest(
user_app_id=userDataObject,
model_id=MODEL_ID,
version_id=MODEL_VERSION_ID
),
metadata=metadata
)

if get_model_version_response.status.code != status_code_pb2.SUCCESS:
print(get_model_version_response.status)
raise Exception("Get model version failed, status: " + get_model_version_response.status.description)

model_version = get_model_version_response.model_version
print(model_version)

Update

info
  • The PatchModels endpoint allows you to patch only the model level fields, and nothing in the model version. It only updates things like name, description, notes, and other metadata type information field you may have.
  • The PatchModelVersions endpoint allows you to change most of the model version fields like gettable, metadata, license, description, notes, and output_info (not including concepts).

Update Model Name

Let's use the PatchModels endpoint to change the model name to newname.

#####################################################################################################
# In this section, we set the user authentication, app ID, and how we want to update the model.
# Change these strings to run your own example.
#####################################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to update your own model
MODEL_ID = 'petsID'
MODEL_NAME = 'newname'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

patch_models_response = stub.PatchModels(
service_pb2.PatchModelsRequest(
user_app_id=userDataObject,
action="overwrite",
models=[
resources_pb2.Model(
id=MODEL_ID,
name=MODEL_NAME
)
]
),
metadata=metadata
)

if patch_models_response.status.code != status_code_pb2.SUCCESS:
print(patch_models_response.status)
raise Exception("Patch models failed, status: " + patch_models_response.status.description)

Update Model Configuration

Let's use the PatchModelVersions endpoint to configure the minimum probability threshold for the outputs we want to view from the model. We can modify the min_value parameter available for this model type to filter the outputs to see only the concepts with a probability score of 0.95 or higher.

tip

For embedding-classifiers, the min_value parameter falls within the range of 0 to 1, with a step size of 0.01. For most of the other model types, it falls within the range of 0 to 100, with a step size of 0.1.

#####################################################################################################
# In this section, we set the user authentication, app ID, and how we want to update the model.
# Change these strings to run your own example.
#####################################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to update your own model
MODEL_ID = 'petsID'
MODEL_VERSION_ID = 'b0b89c973e5d4b6d9599ce13da04b894'
MINIMUM_VALUE = 0.95

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
from google.protobuf.struct_pb2 import Struct

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

params = Struct()
params.update({
"min_value": MINIMUM_VALUE
})

patch_models_response = stub.PatchModelVersions(
service_pb2.PatchModelVersionsRequest(
user_app_id=userDataObject,
action="overwrite",
model_id=MODEL_ID,
model_versions=[
resources_pb2.ModelVersion(
id=MODEL_VERSION_ID,
output_info=resources_pb2.OutputInfo(
params=params
)
)
]

),
metadata=metadata
)

if patch_models_response.status.code != status_code_pb2.SUCCESS:
print(patch_models_response.status)
raise Exception("Patch models failed, status: " + patch_models_response.status.description)

Delete

Delete a Model

You can delete a model by using the model_id parameter.

##########################################################################
# In this section, we set the user authentication, app ID, and model ID.
# Change these strings to run your own example.
##########################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change this to your own model ID
MODEL_ID = 'petsID'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

delete_model_response = stub.DeleteModel(
service_pb2.DeleteModelRequest(
user_app_id=userDataObject,
model_id=MODEL_ID
),
metadata=metadata
)

if delete_model_response.status.code != status_code_pb2.SUCCESS:
print(delete_model_response.status)
raise Exception("Delete model inputs failed, status: " + delete_model_response.status.description)

Delete a Model Version

You can also delete a specific version of a model with the model_id and version_id parameters.

##################################################################################
# In this section, we set the user authentication, app ID, and the ID and version
# of the model we want to delete. Change these strings to run your own example.
##################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to your own model ID and version
MODEL_ID = 'petsID'
MODEL_VERSION_ID = '9641cbbb019b424db17e593b48ebc7ff'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

delete_model_version_response = stub.DeleteModelVersion(
service_pb2.DeleteModelVersionRequest(
user_app_id=userDataObject,
model_id=MODEL_ID,
version_id=MODEL_VERSION_ID
),
metadata=metadata
)

if delete_model_version_response.status.code != status_code_pb2.SUCCESS:
print(delete_model_version_response.status)
raise Exception("Delete model version failed, status: " + delete_model_version_response.status.description)

Delete all Models

If you would like to delete all models associated with an application, you can also do that.

caution

Please proceed with extreme caution as deleted models cannot be recovered.

################################################################
# In this section, we set the user authentication and app ID.
# Change these strings to run your own example.
################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

delete_models_response = stub.DeleteModels(
service_pb2.DeleteModelsRequest(
user_app_id=userDataObject,
delete_all=True
),
metadata=metadata
)

if delete_models_response.status.code != status_code_pb2.SUCCESS:
print(delete_models_response.status)
raise Exception("Delete models failed, status: " + delete_models_response.status.description)

Predict

Predict With a Model

Once you have trained a model, you are ready to use the new model to make predictions. The predictions returned will only contain the concepts that you told it to see.

####################################################################################
# In this section, we set the user authentication, app ID, model ID, model version,
# and image URL. Change these strings to run your own example.
####################################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to make your own predictions
MODEL_ID = 'petsID'
MODEL_VERSION = '8efft4567a9d40c7b84ecfd664ac603d'
IMAGE_URL = 'https://samples.clarifai.com/metro-north.jpg'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

post_model_outputs_response = stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject,
model_id=MODEL_ID,
version_id=MODEL_VERSION, # This is optional. Defaults to the latest model version.
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
url=IMAGE_URL
)
)
)
]
),
metadata=metadata
)

if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
print(post_model_outputs_response.status)
raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)

# Since we have one input, one output will exist here.
output = post_model_outputs_response.outputs[0]

print("Predicted concepts:")
for concept in output.data.concepts:
print("%s %.2f" % (concept.name, concept.value))

Search Models by Name and Type

You can search all your models by name and type of model.

################################################################
# In this section, we set the user authentication and app ID.
# Change these strings to run your own example.
#################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID)

post_models_searches_response = stub.PostModelsSearches(
service_pb2.PostModelsSearchesRequest(
user_app_id=userDataObject,
model_query=resources_pb2.ModelQuery(
name="gen*", # This supports wild card queries like "gen*" to match "general" as an example
model_type_id="concept"
)
),
metadata=metadata
)

if post_models_searches_response.status.code != status_code_pb2.SUCCESS:
print(post_models_searches_response.status)
raise Exception("Post models searches failed, status: " + post_models_searches_response.status.description)

for model in post_models_searches_response.models:
print(model)