Generate Stunning Images with Cognitive Actions for Inpainting and Customization

21 Apr 2025
Generate Stunning Images with Cognitive Actions for Inpainting and Customization

In this blog post, we will explore the capabilities of the Cognitive Actions associated with the idorgham/dorgham-lora-hr specification. This set of actions empowers developers to generate images using inpainting techniques with a variety of customizable parameters. These pre-built actions simplify the integration of advanced image generation into your applications, allowing you to focus on creativity and functionality.

Prerequisites

Before you dive into using the Cognitive Actions, ensure that you have:

  • An API key for the Cognitive Actions platform.
  • Familiarity with how to make HTTP requests and handle JSON data.
  • Basic understanding of Python programming for implementing the provided code examples.

To authenticate your requests, you will typically include your API key in the request headers, ensuring secure access to the Cognitive Actions services.

Cognitive Actions Overview

Generate Image with Inpainting

Description: This action allows you to generate images using inpainting techniques, offering customizable parameters including model selection, guidance scale, prompt strength, and output format. This action supports fast prediction modes and detailed customization through various options.

Category: Image Generation

Input

The input for this action requires a JSON object with the following schema:

  • Required:
    • prompt (string): The prompt for the generated image. Including a trigger_word used during training can enhance results.
  • Optional:
    • mask (string): URI of the image mask for inpainting mode (overrides width and height).
    • seed (integer): Sets a random seed for reproducibility.
    • image (string): URI of the input image for image-to-image or inpainting mode (overrides width and height).
    • model (string): Selects the inference model (options: dev, schnell).
    • width (integer): Width of the generated image (if aspect ratio is custom).
    • height (integer): Height of the generated image (if aspect ratio is custom).
    • goFast (boolean): Enables a model optimized for speed.
    • imageFormat (string): Format of the output image (options: webp, jpg, png).
    • outputCount (integer): Number of outputs to generate.
    • guidanceScale (number): Adjusts the guidance scale during the diffusion process.
    • outputQuality (integer): Quality of the output images.
    • extraLora (string): Load additional LoRA weights.
    • loraScale (number): Strength of the main LoRA application.
    • extraLoraScale (number): Intensity of additional LoRA.
    • inferenceSteps (integer): Number of denoising steps.
    • promptStrength (number): Defines the prompt strength in image-to-image mode.
    • approxMegapixels (string): Approximate megapixels for generated image.
    • imageAspectRatio (string): Sets the aspect ratio for the generated image.
    • safetyCheckerDisabled (boolean): Disables the safety checker for generated images.

Example Input:

{
  "model": "dev",
  "goFast": false,
  "prompt": "photo of mr Dorgham wearing sunglasses",
  "loraScale": 1,
  "imageFormat": "webp",
  "outputCount": 1,
  "guidanceScale": 3,
  "outputQuality": 80,
  "extraLoraScale": 1,
  "inferenceSteps": 28,
  "promptStrength": 0.8,
  "approxMegapixels": "1",
  "imageAspectRatio": "1:1"
}

Output

The action typically returns a JSON array with URLs of the generated images.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/f162d5e2-3517-4f7b-a074-6c46387b3a2d/06690543-1bce-407a-9826-db2f5a7edce9.webp"
]

Conceptual Usage Example (Python)

Here’s how you can implement this action in Python:

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 = "49492937-2be2-4e68-9188-c3ba82d80d8a"  # Action ID for Generate Image with Inpainting

# Construct the input payload based on the action's requirements
payload = {
    "model": "dev",
    "goFast": False,
    "prompt": "photo of mr Dorgham wearing sunglasses",
    "loraScale": 1,
    "imageFormat": "webp",
    "outputCount": 1,
    "guidanceScale": 3,
    "outputQuality": 80,
    "extraLoraScale": 1,
    "inferenceSteps": 28,
    "promptStrength": 0.8,
    "approxMegapixels": "1",
    "imageAspectRatio": "1: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 code snippet, you replace the endpoint and API key with your own. The input payload is structured according to the action's requirements, and the output is printed in a readable format.

Conclusion

The Cognitive Actions for the idorgham/dorgham-lora-hr specification provide developers with powerful tools for image generation and customization. By leveraging the capabilities of inpainting and various adjustable parameters, you can create stunning visuals tailored to your specific needs.

Consider experimenting with different prompts and settings to explore the full potential of these actions, and integrate them into your applications to enhance user experiences. Happy coding!