Create, Get, Update, Delete

Create

To create a new custom workflow, specify a list of model IDs that are to be included in the workflow. Each model ID also requires a specific model version ID, since a model can have several versions.

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

MultiWorkflowResponse postWorkflowsResponse = stub.postWorkflows(
    PostWorkflowsRequest.newBuilder().addWorkflows(
        Workflow.newBuilder()
            .setId("my-custom-workflow")
            .addNodes(
                WorkflowNode.newBuilder()
                    .setId("food-concepts")
                    .setModel(
                        Model.newBuilder()
                            .setId("bd367be194cf45149e75f01d59f77ba7")
                            .setModelVersion(ModelVersion.newBuilder().setId("dfebc169854e429086aceb8368662641"))
                    )
            )
            .addNodes(
                WorkflowNode.newBuilder()
                    .setId("general-concepts")
                    .setModel(
                        Model.newBuilder()
                            .setId("aaa03c23b3724a16a56b629203edc62c")
                            .setModelVersion(ModelVersion.newBuilder().setId("aa9ca48295b37401f8af92ad1af0d91d"))
                    )
            )
    ).build()
);

if (postWorkflowsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("Post workflows failed, status: " + postWorkflowsResponse.getStatus());
}

Workflow Predict

Predict using a workflow. The response will contain the predictions each model in the workflow returns for the input.

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

PostWorkflowResultsResponse postWorkflowResultsResponse = stub.postWorkflowResults(
    PostWorkflowResultsRequest.newBuilder()
        .setWorkflowId("my-custom-workflow")
        .addInputs(
            Input.newBuilder().setData(
                Data.newBuilder().setImage(
                    Image.newBuilder()
                        .setUrl("https://portal.clarifai.com/cms-assets/20180320212157/food-001.jpg")
                )
            )
        ).build()
);

if (postWorkflowResultsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("Post workflow results failed, status: " + postWorkflowResultsResponse.getStatus());
}

// Since we have one input, one output will exist here.
WorkflowResult results = postWorkflowResultsResponse.getResults(0);

// One output is present for each model in the workflow.
for (Output output : results.getOutputsList()) {
    System.out.println("Predicted concepts for model: " + output.getModel().getName());
    for (Concept concept : output.getData().getConceptsList()) {
        System.out.printf("%s %.2f%n", concept.getName(), concept.getValue());
    }
    System.out.println();
}

Get

Get all workflows in an app

Return all custom workflows in your app.

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

MultiWorkflowResponse listWorkflowsResponse = stub.listWorkflows(ListWorkflowsRequest.newBuilder().build());

if (listWorkflowsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("List workflows failed, status: " + listWorkflowsResponse.getStatus());
}

for (Workflow workflow : listWorkflowsResponse.getWorkflowsList()) {
    System.out.println("The workflow " + workflow.getId() + " consists of these models:");
    for (WorkflowNode workflowNode : workflow.getNodesList()) {
        Model model = workflowNode.getModel();
        System.out.println(model.getId());
    }
    System.out.println();
}

Get a workflow by a specific ID

Returns information about a specific workflow.

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

SingleWorkflowResponse getWorkflowResponse = stub.getWorkflow(
    GetWorkflowRequest.newBuilder()
        .setWorkflowId("food-and-general")
        .build()
);

if (getWorkflowResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("Get workflow failed, status: " + getWorkflowResponse.getStatus());
}

Workflow workflow = getWorkflowResponse.getWorkflow();

System.out.println("The workflow consists of these models:");
for (WorkflowNode workflowNode : workflow.getNodesList()) {
    Model model = workflowNode.getModel();
    System.out.println(model.getId());
}

Update

Patch workflow

Ability to change the workflow, that is to change the models of which the workflow consists.

Possible actions are "overwrite", "merge" and "remove".

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

MultiWorkflowResponse patchWorkflowsResponse = stub.patchWorkflows(
    PatchWorkflowsRequest.newBuilder()
        .setAction("overwrite")
        .addWorkflows(
            Workflow.newBuilder()
                .setId("my-custom-workflow")
                .addNodes(
                    WorkflowNode.newBuilder()
                        .setId("travel-concepts")
                        .setModel(
                            Model.newBuilder()
                                .setId("ccc28c313d69466f836ab83287a54ed9")
                                .setModelVersion(ModelVersion.newBuilder().setId("cce28c313d69466f836ab83287a54ed9"))
                        )
                )
                .addNodes(
                    WorkflowNode.newBuilder()
                        .setId("nsfw-concepts")
                        .setModel(
                            Model.newBuilder()
                                .setId("ccc76d86d2004ed1a38ba0cf39ecb4b1")
                                .setModelVersion(ModelVersion.newBuilder().setId("cc76a92beaeb4d8495a58ba197998158"))
                        )
                )
                .addNodes(
                    WorkflowNode.newBuilder()
                        .setId("wedding-concepts")
                        .setModel(
                            Model.newBuilder()
                                .setId("c386b7a870114f4a87477c0824499348")
                                .setModelVersion(ModelVersion.newBuilder().setId("787cc9a002164250800598d36b072384"))
                        )
                )
        ).build()
);

if (patchWorkflowsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("Patch workflows failed, status: " + patchWorkflowsResponse.getStatus());
}

Delete

Delete workflow by ID

Delete a specific workflow.

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

BaseResponse deleteWorkflowResponse = stub.deleteWorkflow(
    DeleteWorkflowRequest.newBuilder()
        .setWorkflowId("my-custom-workflow")
        .build()
);

if (deleteWorkflowResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("Delete workflow failed, status: " + deleteWorkflowResponse.getStatus());
}

Delete all workflows

Deletes all custom workflows.

Note: instead of "delete_all" it's possible to specify a list of workflow IDs to be deleted, using the ids field.

import com.clarifai.grpc.api.*;
import com.clarifai.grpc.api.status.*;

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

BaseResponse deleteWorkflowsResponse = stub.deleteWorkflows(
    DeleteWorkflowsRequest.newBuilder()
        .setDeleteAll(true)
        .build()
);

if (deleteWorkflowsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
    throw new RuntimeException("Delete workflows failed, status: " + deleteWorkflowsResponse.getStatus());
}

Last updated