Enhance Your Applications with Image-to-Image Inference Using Lightweight-AI Model1 Actions

22 Apr 2025
Enhance Your Applications with Image-to-Image Inference Using Lightweight-AI Model1 Actions

In today's rapidly evolving landscape of artificial intelligence, the ability to manipulate and generate images programmatically opens up a wealth of opportunities for developers. The Lightweight-AI Model1 API provides a set of powerful Cognitive Actions designed for image processing, enabling you to perform sophisticated tasks like inpainting and style manipulation. This blog post will guide you through the capabilities of the Perform Image-to-Image Inference action and how to integrate it into your applications seamlessly.

Prerequisites

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

  • An API key for the Lightweight-AI platform, which facilitates access to the Cognitive Actions.
  • Basic understanding of JSON and HTTP requests, as you will need to structure your requests properly.
  • Familiarity with Python, as we will provide code examples using this language.

To authenticate your requests, you will typically include the API key in the request headers, ensuring that your application is authorized to use the Cognitive Actions.

Cognitive Actions Overview

Perform Image-to-Image Inference

The Perform Image-to-Image Inference action utilizes the flux_schnell model to perform advanced image manipulation tasks. This includes inpainting, style manipulation through Loras, and generating high-quality outputs in various formats. The action allows for reproducibility with random seeds and provides control over content quality and adherence to descriptive prompts.

Input

The input for this action requires a JSON payload structured as follows:

{
  "mask": "string (uri)",
  "seed": "integer",
  "image": "string (uri)",
  "width": "integer (default: 1024)",
  "height": "integer (default: 1024)",
  "prompt": "string (default: 'A bohemian-style female travel blogger...')",
  "loraList": ["string"],
  "loraScales": ["number"],
  "nsfwChecker": "boolean (default: false)",
  "outputFormat": "string (enum: ['webp', 'jpg', 'png'], default: 'png')",
  "guidanceScale": "number (default: 3.5)",
  "outputQuality": "integer (default: 100)",
  "promptStrength": "number (default: 0.8)",
  "numberOfOutputs": "integer (default: 1)",
  "numberOfInferenceSteps": "integer (default: 28)"
}

Example Input:

Here’s a practical example of the JSON payload needed to invoke this action:

{
  "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 invocation, the action typically returns a JSON array containing URLs to the generated images. For example:

[
  "https://assets.cognitiveactions.com/invocations/3cbf30c5-f28a-47c2-96d9-e05ef4e05665/176b6d1e-9dec-4ffc-8f0d-ab48fb805886.png"
]

This output provides the link to the processed image, ready for use in your application.

Conceptual Usage Example (Python)

Here’s how you 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 = "9591580e-f484-4d7b-be68-e828291cd47d"  # 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.",
    "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}")

In this code snippet, you replace the action ID and input payload with the appropriate values for the Perform Image-to-Image Inference action. The endpoint URL and request structure are illustrative, so make sure to adjust them according to your specific implementation.

Conclusion

The Perform Image-to-Image Inference action from the Lightweight-AI Model1 offers powerful capabilities for image manipulation, making it an excellent choice for developers looking to enhance their applications with advanced image processing features. By leveraging this action, you can achieve impressive results while maintaining control over various parameters to suit your needs.

Explore further by integrating additional actions from the Lightweight-AI suite or experimenting with different input configurations to unlock even more creative possibilities in your projects!