Streamline Image and Video Generation with the ComfyUI Workflow Cognitive Action

22 Apr 2025
Streamline Image and Video Generation with the ComfyUI Workflow Cognitive Action

In the world of machine learning and media creation, the ability to generate high-quality images and videos efficiently is paramount. The ComfyUI Workflow Cognitive Action allows developers to run and customize workflows that produce stunning visuals with ease. By leveraging JSON inputs, this powerful action supports various formats and can optimize outputs by modifying parameters like seeds and prompts. In this guide, we will explore how to integrate the ComfyUI Workflow action into your applications, providing a detailed overview of its capabilities and usage.

Prerequisites

Before diving into the implementation, ensure you have the following:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • Basic knowledge of JSON and how to structure input data.
  • Familiarity with Python and sending HTTP requests.

Authentication typically involves passing your API key in the headers of your requests to authenticate your identity when calling the actions.

Cognitive Actions Overview

Execute ComfyUI Workflow

The Execute ComfyUI Workflow action allows you to run and customize any ComfyUI workflow through JSON inputs. This action can generate images or videos in popular formats, supporting various model weights and custom nodes. It facilitates optimizing workflows by modifying seeds or prompts and integrates seamlessly with the Replicate API.

Input

The input to this action is structured as a JSON object with the following fields:

  • inputFile (string, required): The URI of the input file (image, video, tar, or zip). URLs can also be included for remote files.
  • outputFormat (string, optional): The format of the output images. Options include webp, jpg, and png, with webp as the default.
  • workflowJson (string, required): Your ComfyUI workflow defined as a JSON string. Ensure it's in API format.
  • outputQuality (integer, optional): The quality of the output images, ranging from 0 to 100. Defaults to 95.
  • randomiseSeeds (boolean, optional): Whether to automatically randomize seeds. Defaults to true.
  • forceResetCache (boolean, optional): Whether to reset the ComfyUI cache before execution. Defaults to false.
  • returnTempFiles (boolean, optional): Indicates if temporary files should be returned. Defaults to false.

Example Input:

{
  "outputFormat": "webp",
  "workflowJson": "{ \"3\": { ... }}",  // Example JSON structure truncated for brevity
  "outputQuality": 80,
  "randomiseSeeds": true,
  "forceResetCache": false,
  "returnTempFiles": false
}

Output

The output from this action will typically return a URL pointing to the generated image or video based on your workflow.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/29453538-db2b-4f8c-98e0-28db4423c510/6bf2149b-a628-41d3-be3e-374bcd8153dd.webp"
]

Conceptual Usage Example (Python)

Here’s how you might structure a Python script 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 = "ec6e81d6-390c-47bd-a974-2be81ae42613"  # Action ID for Execute ComfyUI Workflow

# Construct the input payload based on the action's requirements
payload = {
    "outputFormat": "webp",
    "workflowJson": "{ \"3\": { ... }}",  # Example JSON structure truncated for brevity
    "outputQuality": 80,
    "randomiseSeeds": 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 the above Python code snippet:

  • Replace the YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key.
  • The action_id is set to the ID of the Execute ComfyUI Workflow action.
  • The payload is structured to match the required input schema, including the workflow JSON.
  • The response is handled to check for success or errors.

Conclusion

The Execute ComfyUI Workflow Cognitive Action provides a powerful way to generate images and videos tailored to your needs. By understanding the input requirements and output structure, developers can easily integrate this action into their applications, enhancing their media creation capabilities. As a next step, consider experimenting with various workflows and parameters to see how they impact your generated content!