Create Stunning Images with the marcinkleczek/pkn Cognitive Actions

In the realm of artificial intelligence and image generation, the marcinkleczek/pkn API offers a remarkable ability to create customized images through its powerful Cognitive Actions. These actions allow developers to generate images using an inpainting mode, providing flexibility with various customizable settings such as aspect ratio, dimensions, output format, and image quality. By leveraging these pre-built actions, you can streamline the process of image generation, adding rich visual content to your applications effortlessly.
Prerequisites
Before diving into the integration of the Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform.
- Basic knowledge of making API requests, particularly how to structure headers for authentication.
Authentication typically involves including the API key in the request headers, allowing you to securely access the available actions.
Cognitive Actions Overview
Generate Customized Image
The Generate Customized Image action is designed to create images based on textual prompts and various customizable parameters. You can harness the power of inpainting to refine existing images or generate entirely new visuals from scratch.
Input
The input for this action requires a structured JSON object. Here’s the schema breakdown:
- prompt (required): The text input that guides the image generation.
- mask (optional): URI of the image mask for inpainting.
- seed (optional): An integer for reproducible results.
- image (optional): URI of the input image for image-to-image transformations.
- model (optional): Choose between "dev" and "schnell" for different performance characteristics.
- aspectRatio (optional): Sets the aspect ratio of the generated image.
- width (optional): The width of the generated image in pixels.
- height (optional): The height of the generated image in pixels.
- goFast (optional): Enable faster predictions.
- imageFormat (optional): Output image format (webp, jpg, png).
- outputCount (optional): Number of images to generate (1-4).
- imageQuality (optional): Quality of output images (0-100).
- denoisingSteps (optional): Number of steps for image generation.
- loraWeightScale (optional): Scale factor for applying main LoRA weights.
- promptInfluence (optional): Influence of the prompt in image transformations.
- additionalLoraScale (optional): Scale factor for additional LoRA weights.
- turnOffSafetyChecker (optional): Option to disable the safety checker.
- diffusionGuidanceScale (optional): Sets the guidance scale for diffusion.
Example Input:
{
"model": "dev",
"goFast": false,
"prompt": "PKN Building in the middle of eastern europe middle 70s in the middle of a day",
"aspectRatio": "1:1",
"imageFormat": "webp",
"outputCount": 1,
"imageQuality": 80,
"denoisingSteps": 28,
"imageResolution": "1",
"loraWeightScale": 1,
"promptInfluence": 0.8,
"additionalLoraScale": 1,
"diffusionGuidanceScale": 3
}
Output
Upon successfully executing the action, you will receive a response containing the generated image(s) in the specified format.
Example Output:
[
"https://assets.cognitiveactions.com/invocations/3424c78f-50d4-4ec7-b04c-1f995f27e9ad/4c1ebddf-89f7-46a9-af71-a6f14d69524b.webp"
]
The output is an array of URLs pointing to the generated image(s), allowing you to utilize them in your application.
Conceptual Usage Example (Python)
Here’s how you might call the Generate Customized Image action using a hypothetical Cognitive Actions execution endpoint:
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 = "4b311654-5c25-4821-9b7e-1723533e271e" # Action ID for Generate Customized Image
# Construct the input payload based on the action's requirements
payload = {
"model": "dev",
"goFast": False,
"prompt": "PKN Building in the middle of eastern europe middle 70s in the middle of a day",
"aspectRatio": "1:1",
"imageFormat": "webp",
"outputCount": 1,
"imageQuality": 80,
"denoisingSteps": 28,
"imageResolution": "1",
"loraWeightScale": 1,
"promptInfluence": 0.8,
"additionalLoraScale": 1,
"diffusionGuidanceScale": 3
}
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 will see how to set up the API request, populate the input payload correctly, and handle responses or errors gracefully. The action_id and input payload are explicitly defined based on the schema provided.
Conclusion
The Generate Customized Image action from the marcinkleczek/pkn API offers developers an exciting opportunity to create visually stunning images tailored to their specific needs. By mastering this action, you can enhance your applications with high-quality images generated directly from text prompts. Consider experimenting with different parameters to explore the full potential of image generation in your projects!