Unlock Creativity: Integrate AI Image Generation with paappraiser/looney Cognitive Actions

In the digital age, the ability to generate images from text prompts has become an exciting frontier for developers. The paappraiser/looney Cognitive Actions provide a powerful API to generate and refine AI images based on customizable prompts. These pre-built actions allow developers to integrate advanced image generation capabilities into their applications with ease. By leveraging the sophistication of machine learning models, you can create captivating images tailored to your specifications, whether for artistic endeavors, marketing materials, or social media content.
Prerequisites
Before you start working with the Cognitive Actions, ensure you have the following:
- An API key for the Cognitive Actions platform to authenticate your requests.
- Basic knowledge of JSON format for crafting input payloads.
- Familiarity with making HTTP requests in your programming environment of choice.
You will pass your API key in the headers of your requests to authenticate and access the functionality of the Cognitive Actions.
Cognitive Actions Overview
Generate and Refine AI Images
The Generate and Refine AI Images action allows you to create images using a text prompt while providing options for refinement, inpainting, and application of LoRA (Low-Rank Adaptation). This action supports various scheduler algorithms, enabling you to control output dimensions, the number of images generated, and the application of watermarks.
Input:
The following schema outlines the required and optional fields for this action:
{
"mask": "string (uri, optional)",
"seed": "integer (optional)",
"image": "string (uri, optional)",
"width": "integer (default: 1024)",
"height": "integer (default: 1024)",
"prompt": "string (default: 'An astronaut riding a rainbow unicorn')",
"loraScale": "number (default: 0.6, range: 0 to 1)",
"numOutputs": "integer (default: 1, range: 1 to 4)",
"loraWeights": "string (optional)",
"refineSteps": "integer (optional)",
"refineStyle": "string (default: 'no_refiner', options: ['no_refiner', 'expert_ensemble_refiner', 'base_image_refiner'])",
"guidanceScale": "number (default: 7.5, range: 1 to 50)",
"highNoiseFrac": "number (default: 0.8, range: 0 to 1)",
"applyWatermark": "boolean (default: true)",
"negativePrompt": "string (default: '')",
"promptStrength": "number (default: 0.8, range: 0 to 1)",
"schedulingMethod": "string (default: 'K_EULER', options: ['DDIM', 'DPMSolverMultistep', 'HeunDiscrete', 'KarrasDPM', 'K_EULER_ANCESTRAL', 'K_EULER', 'PNDM'])",
"numInferenceSteps": "integer (default: 50, range: 1 to 500)",
"disableSafetyChecker": "boolean (default: false)"
}
Example Input:
Here's a sample JSON payload that illustrates how to structure your request:
{
"width": 640,
"height": 480,
"prompt": "photograph of TOK. new york City street, broadway, at night time, cartoon style, neon lights, bars, musicals",
"loraScale": 0.6,
"numOutputs": 1,
"refineStyle": "no_refiner",
"guidanceScale": 7.5,
"highNoiseFrac": 0.8,
"applyWatermark": true,
"negativePrompt": "realistic",
"promptStrength": 0.8,
"schedulingMethod": "K_EULER",
"numInferenceSteps": 50
}
Output:
The action typically returns a list of generated image URLs. Here’s an example of what you might receive:
[
"https://assets.cognitiveactions.com/invocations/4c74f2cd-7bc0-46dd-8ef7-47cace4b959f/8dd350c0-b46f-4c3d-a1be-d7388dbb7ec7.png"
]
Conceptual Usage Example (Python):
Below is a conceptual Python code snippet demonstrating how to invoke the Generate and Refine AI Images action using the Cognitive Actions API:
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 = "dcdb5e36-e4ee-40f6-aa8d-44bc668ec383" # Action ID for Generate and Refine AI Images
# Construct the input payload based on the action's requirements
payload = {
"width": 640,
"height": 480,
"prompt": "photograph of TOK. new york City street, broadway, at night time, cartoon style, neon lights, bars, musicals",
"loraScale": 0.6,
"numOutputs": 1,
"refineStyle": "no_refiner",
"guidanceScale": 7.5,
"highNoiseFrac": 0.8,
"applyWatermark": true,
"negativePrompt": "realistic",
"promptStrength": 0.8,
"schedulingMethod": "K_EULER",
"numInferenceSteps": 50
}
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, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action ID for Generate and Refine AI Images is included, and the input payload is structured to match the expected schema.
Conclusion
The paappraiser/looney Cognitive Actions provide an innovative way to harness the power of AI for image generation and refinement. By utilizing these actions, developers can create unique visual content tailored to their application needs. Whether you're looking to generate artistic images or enhance existing visuals, the capabilities offered by this API are extensive and versatile. Start integrating these Cognitive Actions into your projects today and unlock a new level of creativity!