Enhance Your Applications with Image Generation: Integrating leosy-kingdom/nghienvy Cognitive Actions

25 Apr 2025
Enhance Your Applications with Image Generation: Integrating leosy-kingdom/nghienvy Cognitive Actions

In the world of AI-driven applications, the ability to generate high-quality images from text prompts can significantly enhance user engagement and creativity. The leosy-kingdom/nghienvy API offers a powerful Cognitive Action for generating images with inpainting capabilities. This action allows developers to create visually appealing content based on descriptive prompts, making it an excellent addition to any application that requires dynamic visual generation.

Prerequisites

Before you can start using the Cognitive Actions, make sure you have the following:

  • API Key: You will need an API key to authenticate your requests to the Cognitive Actions platform.
  • Setup: Familiarize yourself with how to make HTTP requests and handle JSON payloads in your preferred programming language.

Authentication typically involves passing your API key in the request headers, ensuring that your requests are securely processed.

Cognitive Actions Overview

Generate Image with Inpainting

The Generate Image with Inpainting action allows you to create images based on a given text prompt, with the added capability of inpainting using an image mask. This action is particularly useful for applications that require custom images while allowing users to adjust various parameters for enhanced realism.

Input

The input for this action consists of several fields, both required and optional:

  • prompt (required): A descriptive text that guides image generation.
  • mask (optional): URI of an image mask used for inpainting, overriding certain settings if provided.
  • seed (optional): A random seed for reproducibility.
  • image (optional): URI of an input image for image-to-image translation.
  • model (optional): The inference model type; either 'dev' for detailed results or 'schnell' for faster outputs.
  • width (optional): Width of the generated image in pixels.
  • height (optional): Height of the generated image in pixels.
  • megapixels (optional): Approximate number of megapixels for the output.
  • aspectRatio (optional): Desired aspect ratio of the generated image.
  • numberOfOutputs (optional): Number of image outputs to generate (1-4).
  • imageQuality (optional): JPEG quality of the output image.
  • outputFormat (optional): File format for the output images (WEBP, JPG, PNG).
  • loraIntensity (optional): Intensity of the main LoRA application.
  • additionalLora (optional): Load additional LoRA weights.
  • numberOfInferenceSteps (optional): Steps for the denoising process.

Example Input:

{
  "model": "dev",
  "prompt": "NgVy , high angle view, beautiful girl wearing white T-shirt and flared jeans, wearing black handbag, on campus, sitting down and picking flowers, sunlight, grass and flowers, bright and colorful ",
  "aspectRatio": "1:1",
  "imageQuality": 100,
  "outputFormat": "png",
  "loraIntensity": 1,
  "numberOfOutputs": 2,
  "promptIntensity": 1,
  "diffusionGuidance": 3.5,
  "numberOfInferenceSteps": 28,
  "additionalLoraIntensity": 1
}

Output

The action typically returns an array of URLs pointing to the generated images.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/e21e88d1-5351-4b54-84fa-5ee9cde24f10/330b5123-5076-4b32-b4fe-5cdc25864f77.png",
  "https://assets.cognitiveactions.com/invocations/e21e88d1-5351-4b54-84fa-5ee9cde24f10/fb3648fc-bee4-474a-8b4c-9524c1451f25.png"
]

Conceptual Usage Example (Python)

Here’s how you might use the Generate Image with Inpainting action in your Python application:

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 = "f8c35c1c-4347-4bec-bfa1-20c4e5a68c24"  # Action ID for Generate Image with Inpainting

# Construct the input payload based on the action's requirements
payload = {
    "model": "dev",
    "prompt": "NgVy , high angle view, beautiful girl wearing white T-shirt and flared jeans, wearing black handbag, on campus, sitting down and picking flowers, sunlight, grass and flowers, bright and colorful ",
    "aspectRatio": "1:1",
    "imageQuality": 100,
    "outputFormat": "png",
    "loraIntensity": 1,
    "numberOfOutputs": 2,
    "promptIntensity": 1,
    "diffusionGuidance": 3.5,
    "numberOfInferenceSteps": 28,
    "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 code snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action_id variable is set to the ID of the Generate Image with Inpainting action. The payload variable includes the necessary input parameters as specified in the input schema. The output URLs will point to the generated images.

Conclusion

The Generate Image with Inpainting action from the leosy-kingdom/nghienvy API empowers developers to create stunning visual content dynamically based on text descriptions. By leveraging this powerful tool, you can enhance your applications with unique, high-quality images tailored to user inputs.

Consider exploring additional use cases, such as integrating this action into creative tools, content creation platforms, or any application where visual content plays a crucial role. Happy coding!