Execute ComfyUI Workflows Effortlessly with Cognitive Actions

22 Apr 2025
Execute ComfyUI Workflows Effortlessly with Cognitive Actions

Integrating advanced image processing capabilities into your applications can be a daunting task, but the fofr/any-comfyui-workflow provides a powerful set of Cognitive Actions to simplify this process. With these pre-built actions, developers can execute complex workflows using the ComfyUI framework, enabling functionalities like image-to-image conversion, video processing, and more. This article will guide you through the various features of the Execute ComfyUI Workflow action, helping you leverage its power in your applications seamlessly.

Prerequisites

Before diving into the integration of Cognitive Actions, ensure you have the following:

  • An API key for the Cognitive Actions platform, which will be required for authentication purposes.
  • Familiarity with JSON format as the payload for actions will be structured in JSON.
  • Basic knowledge of HTTP requests, as you will be making calls to the Cognitive Actions endpoint.

Authentication typically involves passing your API key in the headers of your requests to verify your identity and permissions.

Cognitive Actions Overview

Execute ComfyUI Workflow

The Execute ComfyUI Workflow action allows you to execute any ComfyUI workflow using the provided API JSON. This operation leverages the ComfyUI framework to perform tasks like image-to-image conversion and other processing tasks by setting up a custom workflow with API settings. It includes features such as input file handling, output format specification, and randomization of seeds for various operations.

Input

The input for this action is structured as follows:

{
  "inputFile": "string (uri)", // Input image, video, tar or zip file
  "outputFormat": "string (enum)", // Options: 'webp', 'jpg', 'png'
  "workflowJson": "string", // The ComfyUI workflow as a JSON string or URL
  "outputQuality": "integer", // Quality of output images (0-100)
  "randomizeSeeds": "boolean", // Automatically randomize seed values
  "forceResetCache": "boolean", // Reset the ComfyUI cache before running
  "returnTempFiles": "boolean" // Return any temporary files
}

Example Input:

Here’s an example of how the input JSON payload might look:

{
  "outputFormat": "webp",
  "workflowJson": "{\n  \"3\": {\n    \"inputs\": {\n      \"seed\": 156680208700286,\n      \"steps\": 10,\n      \"cfg\": 2.5,\n      \"sampler_name\": \"dpmpp_2m_sde\",\n      \"scheduler\": \"karras\",\n      \"denoise\": 1,\n      \"model\": [\n        \"4\",\n        0\n      ],\n      \"positive\": [\n        \"6\",\n        0\n      ],\n      \"negative\": [\n        \"7\",\n        0\n      ],\n      \"latent_image\": [\n        \"5\",\n        0\n      ]\n    },\n    \"class_type\": \"KSampler\",\n    \"_meta\": {\n      \"title\": \"KSampler\"\n    }\n  }, ... }", 
  "outputQuality": 80,
  "randomizeSeeds": true,
  "forceResetCache": false,
  "returnTempFiles": false
}

Output

The output of this action typically returns a URL to the processed file.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/c234b0a0-d847-4dbf-932a-8316e57c5cff/0ed435ef-ba76-494f-bfd9-9417c038a8fa.webp"
]

Conceptual Usage Example (Python)

Below is a conceptual Python code snippet demonstrating how to call the Execute ComfyUI Workflow action:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "704fc9a4-7ed8-413a-af81-fd10c38e1516" # Action ID for Execute ComfyUI Workflow

# Construct the input payload based on the action's requirements
payload = {
    "outputFormat": "webp",
    "workflowJson": "{\n  \"3\": { ... }",  # Replace with actual JSON workflow
    "outputQuality": 80,
    "randomizeSeeds": true,
    "forceResetCache": false,
    "returnTempFiles": false
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload} # Hypothetical structure
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this code snippet, replace the action ID and input payload with your specific requirements. The endpoint URL and request structure used are illustrative, focusing on how to structure the input JSON correctly.

Conclusion

The Execute ComfyUI Workflow action provides a powerful and flexible way to integrate image and video processing capabilities into your applications. By leveraging the ComfyUI framework, developers can automate complex workflows with ease, enhancing their applications' functionality while saving development time. Explore the possibilities and consider how these Cognitive Actions can streamline your projects!