Skip to main content

Programmatic Auto-Annotation Walkthrough

Use AI to help you build AI. Auto annotation uses your model predictions to label your training data


This tutorial demonstrates how auto-annotation workflows can be configured in the Clarifai API. With auto-annotation, you can use model predictions to label your inputs. Auto-annotation can help you prepare training data or assign other useful labels and metadata to your inputs.

Since models are doing most of the work of annotating your data, this enables you to speed-up and scale-up your annotation process while ensuring quality standards, typically reducing human effort of labeling data by orders of magnitude. And since this is built into our APIs, it seamlessly integrates with all the search, training, and prediction functionality of the Clarifai platform.

When a concept is predicted by a model, it is predicted with a confidence score of between 0 and 1. In this walkthrough, we will leverage that score in our workflow so that when your model predictions are confident (close to 1), you can have your data automatically labeled with that concept. When your predictions are less-than-confident, you can have your input sent to a human reviewer.

info

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

tip
  • Click here to learn how to create an auto-annotation task via the API and automatically label the inputs in your dataset.

  • Click here to learn how to carry out auto-annotation via the UI.

Create Concepts

Let's start by creating the concepts we'll use in our model. We'll create the following concepts: people, man and adult.

####################################################################################
# In this section, we set the user authentication, app ID, and the details of the
# concepts we want to create. 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 concepts
CONCEPT_ID_1 = 'peopleID'
CONCEPT_NAME_1 = 'people'

CONCEPT_ID_2 = 'manID'
CONCEPT_NAME_2 = 'man'

CONCEPT_ID_3 = 'adultID'
CONCEPT_NAME_3 = 'adult'

##########################################################################
# 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) # The userDataObject is required when using a PAT

post_concepts_response = stub.PostConcepts(
service_pb2.PostConceptsRequest(
user_app_id=userDataObject,
concepts=[
resources_pb2.Concept(id=CONCEPT_ID_1, name=CONCEPT_NAME_1),
resources_pb2.Concept(id=CONCEPT_ID_2, name=CONCEPT_NAME_2),
resources_pb2.Concept(id=CONCEPT_ID_3, name=CONCEPT_NAME_3),
]
),
metadata=metadata
)

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

Link the newly created concepts with concepts in the clarifai/main General model.

Run the code below three times; once for each concept you created previously. The concept IDs of the clarifai/main General models are as follows:

  • ai_l8TKp2h5 - the people concept;
  • ai_dxSG2s86 - the man concept;
  • ai_VPmHr5bm - the adult concept.

Your model's concept IDs are the ones you created in the previous step: peopleID, manID, and adultID.

####################################################################################
# In this section, we set the user authentication, app ID, and the details of the
# concepts we want to link. 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 link your own concepts
# Run this code three times; once for each concept you want to link
MODEL_CONCEPT_ID = 'YOUR_MODEL_CONCEPT_ID'
GENERAL_MODEL_CONCEPT_ID = 'GENERAL_MODEL_CONCEPT_ID'

##########################################################################
# 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) # The userDataObject is required when using a PAT

post_concept_relations_response = stub.PostConceptRelations(
service_pb2.PostConceptRelationsRequest(
user_app_id=userDataObject,
concept_id=MODEL_CONCEPT_ID,
concept_relations=[
resources_pb2.ConceptRelation(
object_concept=resources_pb2.Concept(id=GENERAL_MODEL_CONCEPT_ID, app_id="main"),
predicate="synonym"
)
]
),
metadata=metadata
)

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

Create a Concept Mapper Model

We're going to create a concept mapper model that translates the concepts from the General model to our new concepts. The model will map the concepts as synonyms. Hypernyms and hyponyms are supported as well.

We'll be setting the knowledge_graph_id value to be empty.

If you want to define a subset of relationships in your app to be related to each other, you can provide the knowledge_graph_id to each concept relation and then provide that knowledge_graph_id as input to this model as well, which will only follow relationships in that subset of your app's knowledge graph.

###########################################################################################
# In this section, we set the user authentication, app ID, and the details of the concept
# mapper model we want to create. 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 concept mapper model
SYNONYM_MODEL_ID = 'synonym-model-id'
MODEL_TYPE_ID = 'concept-synonym-mapper'

##########################################################################
# 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) # The userDataObject is required when using a PAT

params = Struct()
params.update({
"knowledge_graph_id": ""
})

post_models_response = stub.PostModels(
service_pb2.PostModelsRequest(
user_app_id=userDataObject,
models=[
resources_pb2.Model(
id=SYNONYM_MODEL_ID,
model_type_id=MODEL_TYPE_ID,
output_info=resources_pb2.OutputInfo(
params=params,
)
),
]
),
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)

print(post_models_response)

Create a "Greater Than" Concept Thresholder Model

This model will allow any predictions >= the concept values defined in the model to be outputted from the model.

#########################################################################################
# In this section, we set the user authentication, app ID, and the details of the model
# we want to create. 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 concept thresholder model
MODEL_ID = 'greater-than-model-id'
MODEL_TYPE_ID = 'concept-thresholder'
CONCEPT_ID_1 = 'peopleID'
CONCEPT_ID_2 = 'manID'
CONCEPT_ID_3 = 'adultID'

##########################################################################
# 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) # The userDataObject is required when using a PAT

params = Struct()
params.update({
"concept_threshold_type": "GREATER_THAN"
})

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,
output_info=resources_pb2.OutputInfo(
data=resources_pb2.Data(
concepts=[
resources_pb2.Concept(id=CONCEPT_ID_1, value=0.5),
resources_pb2.Concept(id=CONCEPT_ID_2, value=0.5),
resources_pb2.Concept(id=CONCEPT_ID_3, value=0.95),
]
),
params=params
)
),
]
),
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)

Create a "Less Than" Concept Thresholder Model

This model will allow any predictions < the concept values defined in the model to be outputted from the model.

#########################################################################################
# In this section, we set the user authentication, app ID, and the details of the model
# we want to create. 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 concept thresholder model
MODEL_ID = 'less-than-model-id'
MODEL_TYPE_ID = 'concept-thresholder'
CONCEPT_ID_1 = 'peopleID'
CONCEPT_ID_2 = 'manID'
CONCEPT_ID_3 = 'adultID'

##########################################################################
# 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) # The userDataObject is required when using a PAT

params = Struct()
params.update({
"concept_threshold_type": "LESS_THAN"
})

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,
output_info=resources_pb2.OutputInfo(
data=resources_pb2.Data(
concepts=[
resources_pb2.Concept(id=CONCEPT_ID_1, value=0.5),
resources_pb2.Concept(id=CONCEPT_ID_2, value=0.5),
resources_pb2.Concept(id=CONCEPT_ID_3, value=0.95),
]
),
params=params
)
),
]
),
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)

Create a "Write Success as Me" Annotation Writer Model

Any incoming Data object full of concepts, regions, etc. will be written by this model to the database as an annotation with ANNOTATION_SUCCESS status as if the app owner did the work themself.

#########################################################################################
# In this section, we set the user authentication, app ID, and the details of the model
# we want to create. 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 annotation writer model
ANNOTATION_USER_ID = 'ANNOTATION_USER_ID_HERE'
MODEL_ID = 'write-success-model-id'
MODEL_TYPE_ID = 'annotation-writer'

##########################################################################
# 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) # The userDataObject is required when using a PAT

params = Struct()
params.update({
"annotation_status": status_code_pb2.ANNOTATION_SUCCESS,
"annotation_user_id": ANNOTATION_USER_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,
output_info=resources_pb2.OutputInfo(
params=params
)
),
]
),
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)

Create a "Write Pending as Me" Annotation Writer Model

Any incoming Data object full of concepts, regions, etc. will be written by this model to the database as an annotation with ANNOTATION_PENDING status as if the app owner did the work themself, but needs further review. So, it is marked as pending.

#########################################################################################
# In this section, we set the user authentication, app ID, and the details of the model
# we want to create. 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 annotation writer model
ANNOTATION_USER_ID = 'ANNOTATION_USER_ID_HERE'
MODEL_ID = 'write-pending-model-id'
MODEL_TYPE_ID = 'annotation-writer'

##########################################################################
# 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) # The userDataObject is required when using a PAT

params = Struct()
params.update({
"annotation_status": status_code_pb2.ANNOTATION_PENDING,
"annotation_user_id": ANNOTATION_USER_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,
output_info=resources_pb2.OutputInfo(
params=params
)
),
]
),
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)

Create the Workflow

We will now connect all the models together into a single workflow.

Every input will be predicted by the General Embed model to generate embeddings. The output of the embed model (embeddings) will be sent to the general concept to predict concept and cluster model. Then, the concept model's output (a list of concepts with prediction values) will be sent to the concept mapper model, which maps Clarifai's concepts to the concepts within your app—people, man and adult in this case.

Then, the mapped concepts will be sent to both concept threshold models—(GREATER THAN and LESS THAN). The GREATER THAN model will filter out the concepts that are lower than the corresponding value you defined in the model and send the remaining concept list to write success as me model, which labels the input with these concepts (your app concepts only) as you with success status. You can train or search on these concepts immediately.

The LESS THAN model will filter out concepts that are higher than the corresponding value you defined in the model and send the remaining concept list to write pending as me model, which labels the input with these concepts (your app concepts only) as you with pending status.

The model IDs and model version IDs from the public clarifai/main application are fixed to the latest version at the time of this writing (use the GET /models endpoint to get an up to date list of the available models), so they are already hard-coded in the code examples below.

It's possible to use other public models or model version IDs.

###################################################################################
# In this section, we set the user authentication, app ID, and the details of the
# workflow we want to create. 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 workflow
# Note that we've also added as comments the values of most of these variables against their names in the code below

WORKFLOW_ID = 'auto-annotation-workflow-id'
NODE_ID_1 = 'general-embed'
MODEL_ID_1 = 'bbb5f41425b8468d9b7a554ff10f8581'
MODEL_VERSION_ID_1 = 'bb186755eda04f9cbb6fe32e816be104'

NODE_ID_2 = 'general-concept'
MODEL_ID_2 = 'aaa03c23b3724a16a56b629203edc62c'
MODEL_VERSION_ID_2 = 'aa7f35c01e0642fda5cf400f543e7c40'

NODE_ID_3 = 'general-cluster'
MODEL_ID_3 = 'cccbe437d6e54e2bb911c6aa292fb072'
MODEL_VERSION_ID_3 = 'cc2074cff6dc4c02b6f4e1b8606dcb54'

NODE_ID_4 = 'mapper'
SYNONYM_MODEL_ID = 'synonym-model-id'
SYNONYM_MODEL_VERSION_ID = 'YOUR_SYNONYM_MODEL_VERSION_ID'

NODE_ID_5 = 'greater-than'
GREATER_THAN_MODEL_ID = 'greater-than-model-id'
GREATER_THAN_MODEL_VERSION_ID = 'YOUR_GREATER_THAN_MODEL_VERSION_ID'

NODE_ID_6 = 'write-success'
WRITE_SUCCESS_MODEL_ID = 'write-success-model-id'
WRITE_SUCCESS_MODEL_VERSION_ID = 'YOUR_WRITE_SUCCESS_MODEL_VERSION_ID'

NODE_ID_7 = 'less-than'
LESS_THAN_MODEL_ID = 'less-than-model-id'
LESS_THAN_MODEL_VERSION_ID = 'YOUR_LESS_THAN_MODEL_VERSION_ID'

NODE_ID_8 = 'write-pending'
WRITE_PENDING_MODEL_ID = 'write-pending-model-id'
WRITE_PENDING_MODEL_VERSION_ID = 'YOUR_WRITE_PENDING_MODEL_VERSION_ID'

##########################################################################
# 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) # The userDataObject is required when using a PAT

post_workflows_response = stub.PostWorkflows(
service_pb2.PostWorkflowsRequest(
user_app_id=userDataObject,
workflows=[
resources_pb2.Workflow(
id=WORKFLOW_ID, # auto-annotation-workflow-id
nodes=[
resources_pb2.WorkflowNode(
id=NODE_ID_1, # general-embed
model=resources_pb2.Model(
id=MODEL_ID_1, # bbb5f41425b8468d9b7a554ff10f8581
model_version=resources_pb2.ModelVersion(
id=MODEL_VERSION_ID_1 # bb186755eda04f9cbb6fe32e816be104
)
)
),
resources_pb2.WorkflowNode(
id=NODE_ID_2, # general-concept
model=resources_pb2.Model(
id=MODEL_ID_2, # aaa03c23b3724a16a56b629203edc62c
model_version=resources_pb2.ModelVersion(
id=MODEL_VERSION_ID_2 # aa7f35c01e0642fda5cf400f543e7c40
)
)
),
resources_pb2.WorkflowNode(
id=NODE_ID_3, # general-cluster
model=resources_pb2.Model(
id=MODEL_ID_3, # cccbe437d6e54e2bb911c6aa292fb072
model_version=resources_pb2.ModelVersion(
id=MODEL_VERSION_ID_3 # cc2074cff6dc4c02b6f4e1b8606dcb54
)
),
),
resources_pb2.WorkflowNode(
id=NODE_ID_4, # mapper
model=resources_pb2.Model(
id=SYNONYM_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=SYNONYM_MODEL_VERSION_ID
)
),
node_inputs=[
resources_pb2.NodeInput(node_id=NODE_ID_2) # general-concept
]
),
resources_pb2.WorkflowNode(
id=NODE_ID_5, # greater-than
model=resources_pb2.Model(
id=GREATER_THAN_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=GREATER_THAN_MODEL_VERSION_ID
)
),
node_inputs=[
resources_pb2.NodeInput(node_id=NODE_ID_4) # mapper
]
),
resources_pb2.WorkflowNode(
id=NODE_ID_6, # write-success
model=resources_pb2.Model(
id=WRITE_SUCCESS_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=WRITE_SUCCESS_MODEL_VERSION_ID
)
),
node_inputs=[
resources_pb2.NodeInput(node_id=NODE_ID_5) # greater-than
]
),
resources_pb2.WorkflowNode(
id=NODE_ID_7, # less-than
model=resources_pb2.Model(
id=LESS_THAN_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=LESS_THAN_MODEL_VERSION_ID
)
),
node_inputs=[
resources_pb2.NodeInput(node_id=NODE_ID_4) # mapper
]
),
resources_pb2.WorkflowNode(
id=NODE_ID_8, # write-pending
model=resources_pb2.Model(
id=WRITE_PENDING_MODEL_ID,
model_version=resources_pb2.ModelVersion(
id=WRITE_PENDING_MODEL_VERSION_ID
)
),
node_inputs=[
resources_pb2.NodeInput(node_id=NODE_ID_7) # less-than
]
),
]
)
]
),
metadata=metadata
)

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

Make the New Workflow Your App's Default

Make this the default workflow in the app. So, it will run every time we add an input and execute the auto annotation process.

If the workflow is not the default workflow of your app, you can still use PostWorkflowResults on new inputs to check that you configured the workflow graph and your models properly, but the data will not be written to the DB. This is recommended before making it your default workflow and adding inputs to your app.

#######################################################################################
# In this section, we set the user authentication, app ID, and the ID of the workflow
# we want to make as default. 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 make your own default workflow
DEFAULT_WORKFLOW_ID = 'auto-annotation-workflow-id'

##########################################################################
# 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) # The userDataObject is required when using a PAT

patch_apps_response = stub.PatchApps(
service_pb2.PatchAppsRequest(
user_app_id=userDataObject,
action="overwrite",
apps=[
resources_pb2.App(
id=APP_ID,
default_workflow_id=DEFAULT_WORKFLOW_ID
)
]
),
metadata=metadata
)

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

Add an Image

Adding an image will trigger the default workflow.

#####################################################################################
# In this section, we set the user authentication, app ID, and the URL of the image
# we want to add. 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 add your own image
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) # The userDataObject is required when using a PAT

post_inputs_response = stub.PostInputs(
service_pb2.PostInputsRequest(
user_app_id=userDataObject,
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
url=IMAGE_URL
)
)
)
]
),
metadata=metadata
)

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

List Annotations

You can now list annotations with your user ID and see the annotations created by your workflow.

###############################################################
# 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) # The userDataObject is required when using a PAT

list_annotations_response = stub.ListAnnotations(
service_pb2.ListAnnotationsRequest(
user_app_id=userDataObject,
user_ids=[USER_ID],
list_all_annotations=True,
),
metadata=metadata
)

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

for annotation in list_annotations_response.annotations:
print(annotation)