Generate Stunning Images Using Dadi's Cognitive Actions

The Dadi API provides developers with powerful Cognitive Actions that leverage advanced image generation techniques. One of the standout actions is the ability to generate customized images through image-to-image transformation or inpainting. This action allows for detailed configurations including model selection, resolution, and style guidance, making it an invaluable tool for anyone looking to enhance their applications with unique visual content.
Prerequisites
Before you start integrating Dadi's Cognitive Actions into your application, ensure you have the following:
- An API key for the Dadi Cognitive Actions platform.
- Basic knowledge of JSON and how to make HTTP requests.
- Familiarity with Python for executing the example code.
Authentication typically involves passing your API key in the request headers, enabling you to securely access the Cognitive Actions.
Cognitive Actions Overview
Generate Image with Inpainting
The Generate Image with Inpainting action allows you to create customized images by applying inpainting techniques. You can specify various parameters such as model selection, aspect ratio, and output format, making it versatile for different use cases.
Input
This action requires a JSON payload that includes the following parameters:
- prompt (required): A text prompt guiding image generation (e.g., "dadi as an astronaut").
- mask (optional): URI of the image mask for inpainting. If provided, the width and height parameters are not required.
- image (optional): URI of the input image for image-to-image transformation.
- width (optional): Width of the generated image (256 to 1440).
- height (optional): Height of the generated image (256 to 1440).
- imageFormat (optional): Output format for images, defaulting to "webp".
- outputCount (optional): Number of images to generate (1 to 4).
- imageQuality (optional): Quality setting for output images (0 to 100).
- modelWeights (optional): Load LoRA weights from specified URLs.
- mainLoraScale (optional): Scale factor for LoRA influence.
- optimizeSpeed (optional): Toggle for speed-optimized predictions.
- inferenceModel (optional): Model selection for inference (e.g., "dev" or "schnell").
- imageAspectRatio (optional): Aspect ratio for generated images.
- inferenceStepCount (optional): Number of denoising steps (1 to 50).
- additionalLoraScale (optional): Adjust the scale of additional LoRA influence.
- imagePromptStrength (optional): Strength of prompt influence (0 to 1).
- approximateMegapixels (optional): Configures the approximate resolution of the generated image.
- diffusionGuidanceScale (optional): Controls the guidance scale during diffusion (0 to 10).
- deactivateSafetyChecker (optional): Option to disable the safety checker.
Example Input:
{
"prompt": "dadi as an astronaut",
"imageFormat": "webp",
"outputCount": 1,
"imageQuality": 80,
"mainLoraScale": 1,
"optimizeSpeed": false,
"inferenceModel": "dev",
"imageAspectRatio": "1:1",
"inferenceStepCount": 28,
"additionalLoraScale": 1,
"imagePromptStrength": 0.8,
"approximateMegapixels": "1",
"diffusionGuidanceScale": 3
}
Output
The output of this action will typically return a URL to the generated image. Here’s an example output:
[
"https://assets.cognitiveactions.com/invocations/c222b1c9-ae3e-4c81-a1c3-ef1d22741886/de876c58-b082-4932-a35b-b1b7e65f289e.webp"
]
Conceptual Usage Example (Python)
Here's an illustrative Python code snippet that demonstrates how to invoke the Generate Image with Inpainting action:
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 = "1003ce04-aeee-475b-9f91-5c9283651a46" # Action ID for Generate Image with Inpainting
# Construct the input payload based on the action's requirements
payload = {
"prompt": "dadi as an astronaut",
"imageFormat": "webp",
"outputCount": 1,
"imageQuality": 80,
"mainLoraScale": 1,
"optimizeSpeed": False,
"inferenceModel": "dev",
"imageAspectRatio": "1:1",
"inferenceStepCount": 28,
"additionalLoraScale": 1,
"imagePromptStrength": 0.8,
"approximateMegapixels": "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 snippet, replace "YOUR_COGNITIVE_ACTIONS_API_KEY" with your actual API key. The action_id corresponds to the action you want to execute. The payload is constructed based on the required inputs, and the response is processed to retrieve the generated image URL.
Conclusion
Integrating Dadi's Cognitive Actions can significantly enhance the visual capabilities of your applications. The Generate Image with Inpainting action provides a robust framework for generating customized images, enabling developers to explore creative possibilities. Consider leveraging this action in your projects to create unique visual content that captivates your audience. Happy coding!