Enhance Your Applications with Img2Img Inference Using Lightweight AI's Cognitive Actions

22 Apr 2025
Enhance Your Applications with Img2Img Inference Using Lightweight AI's Cognitive Actions

In the world of image processing, the ability to transform images through inference has opened up new creative avenues for developers. The Lightweight AI Model 1 offers a powerful Cognitive Action that enables developers to perform image-to-image (img2img) inference. This action allows for seamless inpainting and style applications driven by text prompts, which can significantly enhance user experiences in various applications, from graphic design tools to content creation platforms.

By utilizing pre-built Cognitive Actions, developers can save time and resources while leveraging advanced AI capabilities without needing extensive machine learning expertise.

Prerequisites

To get started with the Cognitive Actions under the Lightweight AI Model 1, you will need to have:

  • A valid API key for the Cognitive Actions platform.
  • Basic familiarity with making HTTP requests, as you will be sending JSON payloads to the Cognitive Actions endpoint.

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

Cognitive Actions Overview

Perform Img2Img Inference with Flux Schnell

The Perform Img2Img Inference with Flux Schnell action is designed for image processing tasks where you can modify existing images based on specific prompts. This action supports inpainting, allowing you to fill or alter parts of an image while controlling various parameters, such as output quality and format.

Input

The input for this action is structured as follows:

{
  "mask": "uri_to_mask_image",
  "seed": 42,
  "image": "uri_to_base_image",
  "width": 1024,
  "height": 1024,
  "prompt": "A fluffy, orange tabby cat curled up asleep in a sunbeam streaming through a window, its soft fur glowing with the warmth of the light; highly detailed 8K UHD photorealistic rendering, natural lighting, warm and inviting atmosphere, focus on softness and texture.",
  "loraList": [],
  "nsfwChecker": false,
  "outputFormat": "png",
  "guidanceScale": 3.5,
  "outputQuality": 100,
  "promptStrength": 0.8,
  "loraScaleValues": [],
  "numberOfOutputs": 1,
  "numberOfInferenceSteps": 4
}
  • Required Fields:
    • mask: URI of the mask image for inpainting.
    • image: URI of the base image to be modified.
    • prompt: Text prompt guiding the image generation.
  • Optional Fields:
    • seed: Random seed for reproducibility.
    • width & height: Dimensions of the output image.
    • loraList: List of Lora configurations to be applied.
    • nsfwChecker: Flag to enable NSFW content filtering.
    • outputFormat: Desired output image format (default: "png").
    • guidanceScale: Scale factor for prompt influence.
    • outputQuality: Quality setting for the output image.
    • promptStrength: Influence of the prompt on image generation.
    • numberOfOutputs: Number of images to generate.
    • numberOfInferenceSteps: Steps for the inference process.

Example Input

Here’s an example of a JSON payload you might send:

{
  "width": 1024,
  "height": 1024,
  "prompt": "A fluffy, orange tabby cat curled up asleep in a sunbeam streaming through a window, its soft fur glowing with the warmth of the light; highly detailed 8K UHD photorealistic rendering, natural lighting, warm and inviting atmosphere, focus on softness and texture.",
  "outputFormat": "png",
  "outputQuality": 100,
  "promptStrength": 0.8,
  "numberOfOutputs": 1,
  "numberOfInferenceSteps": 4
}

Output

Upon successful execution, the action returns the following output, which includes the URI of the generated image:

[
  "https://assets.cognitiveactions.com/invocations/63959c05-0f97-4af0-ad55-4d4ec39d1f4f/697d635c-cc72-492d-9cf4-5328ff6a1a28.png"
]

This output URI points to the resulting image after processing.

Conceptual Usage Example (Python)

Here’s how a developer might invoke this action 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 = "1e5b9830-e235-4c27-92ba-4ad5e41b39fc" # Action ID for Perform Img2Img Inference with Flux Schnell

# Construct the input payload based on the action's requirements
payload = {
    "width": 1024,
    "height": 1024,
    "prompt": "A fluffy, orange tabby cat curled up asleep in a sunbeam streaming through a window, its soft fur glowing with the warmth of the light; highly detailed 8K UHD photorealistic rendering, natural lighting, warm and inviting atmosphere, focus on softness and texture.",
    "outputFormat": "png",
    "outputQuality": 100,
    "promptStrength": 0.8,
    "numberOfOutputs": 1,
    "numberOfInferenceSteps": 4
}

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

This code snippet demonstrates how to set up the necessary headers, construct the input payload, and handle the API response effectively.

Conclusion

The Perform Img2Img Inference with Flux Schnell Cognitive Action from the Lightweight AI Model 1 empowers developers to create stunning image modifications with ease. By leveraging the capabilities of this action, you can enrich your applications, enhance user creativity, and provide unique visual experiences. Explore further use cases, integrate this action into your projects, and unleash the power of AI-driven image processing today!