Generate Stunning Images with codelace/forflux2 Cognitive Actions

22 Apr 2025
Generate Stunning Images with codelace/forflux2 Cognitive Actions

In the realm of image generation, the codelace/forflux2 API offers powerful Cognitive Actions that enable developers to create detailed images using prompts and image masks. These pre-built actions simplify the integration of advanced image generation capabilities into applications, allowing for customization in dimensions, quality, and formats. Whether you’re looking to create artwork, design elements, or other visual content, the Cognitive Actions in this spec can help streamline your workflow.

Prerequisites

To start using the Cognitive Actions provided by the codelace/forflux2 API, ensure you have the following:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • A basic understanding of JSON format for structuring your requests.

Authentication typically involves including your API key in the headers of your HTTP requests.

Cognitive Actions Overview

Generate Image with Inpainting

The Generate Image with Inpainting action is designed to produce detailed images based on a specified prompt and an optional image mask. This action provides the flexibility to customize the generated images in aspects such as dimensions, quality, and output format. You can choose between two models: 'dev' for optimal results and 'schnell' for faster output.

Input

The input for this action requires the following fields:

  • prompt (required): A textual description of the desired image, e.g., "cryptopunk".
  • mask (optional): A URI for an image mask to use in inpainting mode.
  • image (optional): A URI for an input image, applicable in image-to-image or inpainting mode.
  • width (optional): The width of the generated image in pixels, ranging from 256 to 1440.
  • height (optional): The height of the generated image in pixels, also ranging from 256 to 1440.
  • outputCount (optional): Number of images to generate (1 to 4).
  • promptStrength (optional): Emphasis on the prompt in image-to-image mode (0 to 1).
  • generationModel (optional): Select either 'dev' or 'schnell' for inference.
  • imageAspectRatio (optional): Desired aspect ratio for the output image.
  • imageOutputFormat (optional): Output image format (e.g., webp, jpg, png).
  • imageOutputQuality (optional): Quality level for output images (0 to 100).

Example Input:

{
  "prompt": "cryptopunk",
  "outputCount": 1,
  "promptStrength": 0,
  "generationModel": "schnell",
  "imageAspectRatio": "1:1",
  "imageOutputFormat": "png",
  "mainLoraIntensity": 1,
  "imageOutputQuality": 80,
  "inferenceStepsCount": 15,
  "enableFastGeneration": false,
  "approximateMegapixels": "1",
  "diffusionGuidanceScale": 3,
  "additionalLoraIntensity": 1
}

Output

The action returns a list of URLs pointing to the generated images. Here’s a sample output:

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/b343fc08-eafe-42b3-95ea-e0b4d32a8f8b/f2adf8d0-a6f4-48e6-8be0-b555829c36e8.png"
]

Conceptual Usage Example (Python)

Here's a conceptual Python snippet demonstrating how to call the Generate Image with Inpainting 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 = "316f7430-c809-4f5a-bf1a-27f97bf9d5f8" # Action ID for Generate Image with Inpainting

# Construct the input payload based on the action's requirements
payload = {
    "prompt": "cryptopunk",
    "outputCount": 1,
    "promptStrength": 0,
    "generationModel": "schnell",
    "imageAspectRatio": "1:1",
    "imageOutputFormat": "png",
    "mainLoraIntensity": 1,
    "imageOutputQuality": 80,
    "inferenceStepsCount": 15,
    "enableFastGeneration": False,
    "approximateMegapixels": "1",
    "diffusionGuidanceScale": 3,
    "additionalLoraIntensity": 1
}

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 example, replace the API key and endpoint with your actual credentials. The action ID and input payload are structured according to the requirements specified above.

Conclusion

The codelace/forflux2 Cognitive Actions facilitate advanced image generation through simple API calls. With the ability to customize prompts, dimensions, and output formats, developers can easily integrate these capabilities into their applications. Explore the possibilities of image creation and enhance your projects with the power of Cognitive Actions!