Unleashing Creativity: Integrating Image Generation with Cognitive Actions in hvn-2

22 Apr 2025
Unleashing Creativity: Integrating Image Generation with Cognitive Actions in hvn-2

In today's digital landscape, the ability to generate compelling images programmatically is an invaluable asset for developers. The heavenknowswhat/hvn-2 Cognitive Actions provide powerful tools for creating images with advanced features like inpainting, customizable aspect ratios, and the use of various model weights. These pre-built actions allow developers to seamlessly integrate sophisticated image generation capabilities into their applications, enhancing user experience and creativity.

Prerequisites

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

  • API Key: You will need an API key to access the Cognitive Actions platform. This key is usually passed in the headers of your requests.
  • Basic Knowledge of JSON: Familiarity with JSON will help you structure your input and understand the output from the API effectively.

Cognitive Actions Overview

Generate Image with Inpainting and Custom Options

This action allows you to create images with advanced options, including inpainting and aspect ratio customization. By utilizing specialized models, users can optimize for either detail or speed in image generation.

Input

The input to this action requires the following fields (with additional optional parameters):

  • Required:
    • prompt: The text prompt that describes the image you want to generate.
  • Optional:
    • mask: A URI string for an image mask used in inpainting mode.
    • seed: An integer value for random seed to ensure reproducibility.
    • image: A URI for the input image when using image-to-image or inpainting modes.
    • width: An integer specifying the width of the generated image (only if aspect_ratio is set to custom).
    • height: An integer specifying the height of the generated image (only if aspect_ratio is set to custom).
    • resultFormat: The output format for the generated image (e.g., webp, jpg, png).
    • guidanceScale: A floating-point number determining the guidance for the generation process.
    • numberOfOutputs: An integer specifying how many images to generate (max 4).
    • imageAspectRatio: A string defining the aspect ratio of the generated image.
    • Additional parameters include LoRA weights, prompt strength, and more.

Here’s a practical example of the input JSON payload:

{
  "prompt": "fleet of black war mech towering over soldiers in the midst of battleground fire erupting at nighttime dramatic lighting city in background in the style of hvn2",
  "resultFormat": "webp",
  "guidanceScale": 3.5,
  "resultQuality": 90,
  "extraLoraScale": 1,
  "inferenceModel": "dev",
  "promptStrength": 0.8,
  "numberOfOutputs": 1,
  "imageAspectRatio": "9:16",
  "primaryLoraScale": 1,
  "numberOfInferenceSteps": 28
}

Output

The action typically returns a list of URLs pointing to the generated images. For instance:

[
  "https://assets.cognitiveactions.com/invocations/f7bff52d-567b-4092-aa78-7c61d5bdf342/030d58d3-c370-4ea8-81f2-2da965a5f179.webp"
]

Conceptual Usage Example (Python)

Here’s how you might call the Cognitive Actions execution endpoint 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 = "e0ff1cdc-b622-4097-a508-73ea86e60eb4"  # Action ID for Generate Image with Inpainting and Custom Options

# Construct the input payload based on the action's requirements
payload = {
    "prompt": "fleet of black war mech towering over soldiers in the midst of battleground fire erupting at nighttime dramatic lighting city in background in the style of hvn2",
    "resultFormat": "webp",
    "guidanceScale": 3.5,
    "resultQuality": 90,
    "extraLoraScale": 1,
    "inferenceModel": "dev",
    "promptStrength": 0.8,
    "numberOfOutputs": 1,
    "imageAspectRatio": "9:16",
    "primaryLoraScale": 1,
    "numberOfInferenceSteps": 28
}

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}")

Conclusion

The heavenknowswhat/hvn-2 Cognitive Actions open up a world of possibilities for developers looking to integrate advanced image generation capabilities into their applications. Whether you’re creating art, generating marketing materials, or designing custom graphics, these actions provide a powerful and flexible way to enhance your projects. Start experimenting with these features today to unlock your creative potential!