Greetings Clarifai-ers! If you're thinking about using our software or have already had the pleasure of dabbling with it, you've probably encountered this "Workflows" term in some form or another. I'm here to clear the air for you so that all of it makes sense and that you get the most use out of them!
App Workflows vs. Base Workflows
The first thing that we need to clarify (no pun intended...ok fine it was) is the difference between these two workflows types, as they serve very different functions.
- An application workflow is essentially used when you want to call up to 5 models at once (including custom models) to retrieve all of the output in one, singular response, either via the User Interface or our API. Pretty nifty right? If you're using the API, this will require only one line of code versus X lines for the X models that you're using.
- A base workflow, on the other hand, is a base model that your application will use to optimize any of your custom models and visual search results via all of the base model's pre-existing knowledge. Thus, you will only need to worry about this if you are doing Custom Training or Visual Search.
Let's delve into these two items a little bit closer.
Application Workflows: Explained Further
An application workflow can be used to call up to 5 models at once (public + custom), and as the name suggests, it is always tied to a particular application in your account.
Setting it up
From your applications page here, you'll see something like the following:
Great! Now you can either create a new application (top right) or click on an existing one. Doing either will take you to that application's specific page:
...and to access your workflows, all you need to do is go to the App Workflows section in the above screenshot (located in the left sidebar). By default you won't have any at first so you'll need to click on the "Create Workflow" button, which will lead you here:
Here you can add up to 5 models to call at once, and you can have as many of these (workflows, that is) as you'd like. Note that if you're adding a custom model to any of them, you can only use a version of the model up until that point, and any future changes to the model will not be incorporated.
Make sure to link every model in the workflow:
Predicting
Now that you've created a workflow you'll want to use it of course! At this moment, you will only be able do this via our API. Via the API, you can retrieve your workflow results with one of our supported languages (or make a REST call with your favorite language). Just make sure to replace "workflow-id" with your own.
Python gRPC
USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the portal under Authentification
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'
# Change these to make your own predictions
WORKFLOW_ID = 'my-custom-workflow'
IMAGE_URL = 'https://samples.clarifai.com/featured-models/ocr-woman-holding-sold-sign.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_workflow_results_response = stub.PostWorkflowResults(
service_pb2.PostWorkflowResultsRequest(
user_app_id=userDataObject,
workflow_id=WORKFLOW_ID,
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(
url=IMAGE_URL
)
)
)
]
),
metadata=metadata
)
if post_workflow_results_response.status.code != status_code_pb2.SUCCESS:
print(post_workflow_results_response.status)
raise Exception("Post workflow results failed, status: " + post_workflow_results_response.status.description)
# We'll get one WorkflowResult for each input we used above. Because of one input, we have here one WorkflowResult
results = post_workflow_results_response.results[0]
# Each model we have in the workflow will produce its output
for output in results.outputs:
model = output.model
print("Output for the model: `%s`" % model.id)
i = 0
while(i < len(output.data.regions)):
print(output.data.regions[i].data.text.raw)
i += 1
Javascript (via REST call)
const USER_ID = "YOUR_USER_ID_HERE";
// Your PAT (Personal Access Token) can be found in the portal under Authentification
const PAT = "YOUR_PAT_HERE";
const APP_ID = "YOUR_APP_ID_HERE";
// Change these to make your own predictions
const WORKFLOW_ID = "my-custom-workflow";
const IMAGE_URL = "https://samples.clarifai.com/featured-models/ocr-woman-holding-sold-sign.jpg";
///////////////////////////////////////////////////////////////////////////////////
// YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
///////////////////////////////////////////////////////////////////////////////////
const raw = JSON.stringify({
"user_app_id": {
"user_id": USER_ID,
"app_id": APP_ID
},
"inputs": [{
"data": {
"image": {
"url": IMAGE_URL
}
}
}]
});
const requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + PAT
},
body: raw
};
fetch(`https://api.clarifai.com/v2/workflows/${WORKFLOW_ID}/results`, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
You can explore more code snippets for Workflow Preditions in our docs.
And voila! That's about it for application workflows.
Base Workflows: Explained Further
Unlike Application Workflows, there isn't a whole lot that you need to know or do for your base workflow setup, but let's go through them briefly.
When you first create an application you will be presented with the following screen, with the Base Workflow menu at the bottom:
You'll notice that when you click the dropdown menu you'll only see General, Food, NSFW, Moderation, Wedding, Travel and Face as options, and these are indeed the only seven available for this functionality.
A few things about this:
- If you are not creating any custom models (i.e. you are only using our 17 public ones) or doing visual searches within your application then you won't need to worry about what you choose here
- If you are using a custom model or visual search, then you will want to pick one of the six options that most closely associates to your model and/or images. For example, if you're training a model on food, then you'll want to pick the food one. Interior rooms of a house - pick the travel one. And if you're ever unsure of what to choose, it's always a safe bet to pick the General option as it contains over 11,000 broad concepts (including food and rooms of a house!).
- Your base model will not alter the concepts that you create in your custom model. It only exists to make your model more optimal by combining the base model's knowledge with yours.
- Once you select a Base Model, it cannot be changed. Ever. So make sure you know which one you'll want!
And that's pretty much it!
Go out there and make some great workflows Clari-fam!