Enhance Your Application with Image Generation: Using swetabhsaurav/4-in-a-row Cognitive Actions

23 Apr 2025
Enhance Your Application with Image Generation: Using swetabhsaurav/4-in-a-row Cognitive Actions

In today's digital landscape, the ability to generate and manipulate images programmatically is a powerful capability. The swetabhsaurav/4-in-a-row API offers a robust Cognitive Action that allows developers to create images using inpainting techniques. This action transforms or enhances images based on provided prompts and customizable parameters, making it a valuable tool for applications involving graphics, gaming, or creative content generation.

Prerequisites

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

  • An API key for the Cognitive Actions platform.
  • Familiarity with making HTTP requests and handling JSON data.

Authentication typically involves passing your API key in the request headers, allowing you to securely access the Cognitive Actions.

Cognitive Actions Overview

Generate Image Using Inpainting

Description:
This action utilizes a specified model to transform or inpaint images based on a given prompt. Developers can customize various parameters such as image dimensions, aspect ratios, and quality. The dev model is optimized for longer inference, while the schnell model offers efficient processing.

Category: Image Processing

Input: The input schema for this action consists of several fields, some required and others optional. Here’s a breakdown:

  • prompt (required): The text prompt for generating the image.
  • model (optional): Select between dev or schnell (default is dev).
  • outputCount (optional): Specifies the number of output images to generate (default is 1, max is 4).
  • mainLoraScale (optional): Controls the application strength of the main LoRA (default is 1).
  • imageAspectRatio (optional): Specifies the aspect ratio for the generated image (default is 1:1).
  • imageOutputFormat (optional): Sets the format of the output image (default is webp).
  • imageOutputQuality (optional): Sets the quality level for output images (default is 80).
  • inferenceStepCount (optional): Defines the number of denoising steps (default is 28).

Example Input:

{
  "model": "dev",
  "prompt": "an image of BOLACON kept on a table. BOLACON is a 4 in a row game. make sure you generate image using trained images and the color, dimension must be same as trained image.",
  "outputCount": 1,
  "mainLoraScale": 0.97,
  "imageAspectRatio": "1:1",
  "imageOutputFormat": "webp",
  "imageOutputQuality": 90,
  "inferenceStepCount": 32
}

Output: The action typically returns a URI to the generated image. Here’s an example of what the output might look like:

[
  "https://assets.cognitiveactions.com/invocations/f02c8540-aaac-425c-af94-abf0e48cae11/ad0d8b14-8005-43ea-ac7c-915e0792b415.webp"
]

Conceptual Usage Example (Python): Here’s how you might structure a request to the Cognitive Actions API using 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 = "9e2457f6-1550-4ea3-b450-ba4ac9cb9774" # Action ID for Generate Image Using Inpainting

# Construct the input payload based on the action's requirements
payload = {
    "model": "dev",
    "prompt": "an image of BOLACON kept on a table. BOLACON is a 4 in a row game. make sure you generate image using trained images and the color, dimension must be same as trained image.",
    "outputCount": 1,
    "mainLoraScale": 0.97,
    "imageAspectRatio": "1:1",
    "imageOutputFormat": "webp",
    "imageOutputQuality": 90,
    "inferenceStepCount": 32
}

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 payload is structured according to the action's input requirements, and you can expect the result to contain the generated image URI.

Conclusion

The Generate Image Using Inpainting action from the swetabhsaurav/4-in-a-row API empowers developers to create customized images programmatically. By leveraging the various parameters, you can refine the outputs to suit your application's needs, whether for gaming, content creation, or any other use case that requires dynamic image generation.

Explore the capabilities of this action and consider how it can enhance your next project!