Skip to content

General ComfyUI Workflow Invoke

Ryu Xin edited this page Feb 22, 2024 · 4 revisions

Preparation

Open ComfyUI, click on settings (highlighted button in the image below):

image

In the popup interface, check the 'Enable Developer Mode' option, as shown below:

image

When Developer Mode is enabled, a 'Save (API Format)' button will appear on the management panel.

After editing the workflow in ComfyUI, you can click on 'Save' and then 'Save (API Format)' buttons respectively. This will save and download workflow.json and workflow-api.json files. The former is the definition file of your edited workflow, while the latter is the data file containing variables used in the workflow settings.

Example of starting a ComfyUI workflow

Sample code URL: https://github.com/hiforce/pixel-force-open-client/blob/main/pixel-force-open-sample/src/main/java/hiforce/pixel/open/sample/general/GeneralPromptSample01.java

public class GeneralPromptSample01 extends BaseSample {

    public static void main(String[] args) {
        GeneralPromptSample01 sample = new GeneralPromptSample01();
        sample.run();
    }

    @Override
    public void execute() throws Exception {
        ResourceResult result = getFirstAvailableResource();
        String resourceId = result.getResources().get(0).getId();

        String workflow = getTextFromResource("/general/01-default-workflow.json");
        String workflowApi = getTextFromResource("/general/01-default-workflow-api.json");
        GeneralPromptClientRequest request = new GeneralPromptClientRequest();
        request.setWorkflow(workflow);
        request.setWorkflowAPI(workflowApi);

        InvokeResult invokeResult = PixelForceClient.getInstance().generalPrompt(request);
        if (!invokeResult.getStatus().equals(ApiStatusEnum.SUCCESS.getCode())) {
            throw new RuntimeException("Failed to invoke General Prompt Workflow");
        }
        System.out.println("General Prompt invoke result:" + JSON.toJSONString(invokeResult));
        waitAndQueryTaskExecuteResult(invokeResult, resourceId);
    }
}

In the sample code, the 01-default-workflow.json test workflow file will be loaded from the resources, along with the data file 01-default-workflow-api.json used to drive the workflow. In the BaseSample base class, you need to set the Access Key and Access Secret required for API calls. For information on how to obtain the Access Key and Access Secret, please refer to this video: https://www.youtube.com/watch?v=5bj3YOE-9vE&t=63s

The flowchart of the sample code is as follows (default workflow):

image

When we run the sample code, we can see the following output:

image

Clicking on the image link will allow you to see the generated visual results:

image