Transform Your Images with Image-to-Image Inference Using the lightweight-ai/model1 Actions

22 Apr 2025
Transform Your Images with Image-to-Image Inference Using the lightweight-ai/model1 Actions

In the rapidly evolving world of artificial intelligence and image processing, the lightweight-ai/model1 API provides developers with powerful tools to manipulate and enhance images. One of the standout features of this API is the Cognitive Actions it offers, particularly the ability to perform image-to-image inference with inpainting capabilities. This allows developers to modify specific areas of an image while retaining the original context, enabling a plethora of creative applications.

In this article, we'll explore how to leverage these Cognitive Actions effectively, focusing on the action available in the lightweight-ai/model1 spec.

Prerequisites

Before you begin integrating the Cognitive Actions into your applications, ensure you have the following:

  • An API key for the Cognitive Actions platform, which you will use for authentication.
  • Familiarity with making HTTP requests and handling JSON payloads in your preferred programming language.

Authentication typically involves including the API key in the headers of your requests. Here’s a conceptual example of how you might structure it:

headers = {
    "Authorization": f"Bearer {YOUR_API_KEY}",
    "Content-Type": "application/json"
}

Cognitive Actions Overview

Perform Image-to-Image Inference with Flux Schnell

This action utilizes the flux_schnell model to perform image-to-image inference, allowing for advanced modifications through inpainting capabilities. With a range of customizable parameters, this action ensures high-quality results tailored to your specific needs.

Input

The following is the input schema required for this action:

  • mask (string, required): URI of the mask image for inpainting. White areas (255) will be modified, while black areas (0) will be preserved.
  • seed (integer, optional): Seed for reproducible image generation.
  • image (string, required): URI of the base image to modify.
  • width (integer, optional): Width of the output image (default: 1024).
  • height (integer, optional): Height of the output image (default: 1024).
  • prompt (string, optional): Description guiding the image generation (default: "A bohemian-style female travel blogger...").
  • loraModels (array, optional): List of Lora models to apply.
  • imageFormat (string, optional): Output image format ("webp", "jpg", "png"; default: "png").
  • guidanceScale (number, optional): Influence of the guidance on generation (default: 3.5).
  • outputQuality (integer, optional): Quality of the output image (0-100; not applicable for 'png', default: 100).
  • promptStrength (number, optional): Level of modification to the original image (0-1; default: 0.8).
  • numberOfOutputs (integer, optional): Number of generated images (1-4; default: 1).
  • inferenceStepsCount (integer, optional): Steps used during inference (1-50; default: 28).
  • loraIntensityScales (array, optional): Intensity scales for corresponding Lora models.

Example Input:

{
  "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.",
  "imageFormat": "png",
  "outputQuality": 100,
  "promptStrength": 0.8,
  "numberOfOutputs": 1,
  "inferenceStepsCount": 4
}

Output

The action typically returns a list of URIs for the generated output images. For example:

[
  "https://assets.cognitiveactions.com/invocations/56330bcf-ac32-4b82-aeb9-6cb24e71b6ae/dadb8c36-4ec4-4943-89f0-84efd08702f2.png"
]

This URI points to the resulting image generated based on the input parameters.

Conceptual Usage Example (Python)

Here’s how you might call this action using Python, focusing on structuring the input payload correctly:

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 = "e83815ca-d599-49c5-971b-12c9eefd02d7"  # Action ID for Perform Image-to-Image Inference

# 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.",
    "imageFormat": "png",
    "outputQuality": 100,
    "promptStrength": 0.8,
    "numberOfOutputs": 1,
    "inferenceStepsCount": 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}")

Explanation of the Python Code Snippet:

  • You start by importing the necessary libraries and defining your API key and endpoint.
  • The action_id is set to target the specific action for image inference.
  • The payload is constructed following the input schema, ensuring all required parameters are included.
  • The request is sent to the hypothetical execution endpoint, and the response is handled gracefully, printing the output or any potential errors.

Conclusion

The lightweight-ai/model1 API opens up exciting possibilities for developers looking to manipulate images creatively. By utilizing the image-to-image inference action, you can perform complex modifications with high-quality results, all while enjoying a customizable experience.

Consider exploring additional use cases, such as enhancing photo editing applications or creating unique artistic filters, to fully leverage the capabilities of these Cognitive Actions. Happy coding!