Enhance Your Image Generation with the Zeke/Stable Diffusion Cognitive Action

In the world of AI-powered creativity, the zeke/stable-diffusion API stands out by offering a robust Cognitive Action that allows developers to generate images based on custom prompts and parameters. This action leverages a personal fork of Stable Diffusion, enhancing the image generation process with tailored settings for width, height, guidance scale, and scheduling algorithms. These pre-built actions enable developers to integrate powerful image generation capabilities into their applications effortlessly, providing improved control over output quality and adherence to user-defined prompts.
Prerequisites
Before diving into the implementation, ensure you have the following:
- An API key for the Cognitive Actions platform, which will be used for authentication.
- Basic knowledge of JSON and REST API concepts.
To authenticate your requests, you will typically need to include your API key in the request headers. This is crucial for gaining access to the Cognitive Actions service.
Cognitive Actions Overview
Generate Image With Stable Diffusion Fork
The Generate Image With Stable Diffusion Fork action allows users to create images based on specific input prompts while customizing various parameters. This action falls under the image-generation category and is ideal for applications that require dynamic image content.
Input
The input for this action consists of several fields designed to tailor the image generation process:
- seed (optional, integer): Random seed for reproducibility. Leave blank to randomize.
- width (integer): Width of the output image in pixels. Choose from predefined sizes. Default is 768.
- height (integer): Height of the output image in pixels. Default is 768.
- prompt (string): Input prompt that describes the desired content of the image. Default example: "a photo of an astronaut riding a horse on Mars."
- scheduler (string): Select the scheduling algorithm for the inference process. Default is "DPMSolverMultistep."
- guidanceScale (number): Scale factor for classifier-free guidance, ranging from 1 to 20. Default is 7.5.
- negativePrompt (optional, string): Elements to exclude from the output image.
- numberOfOutputs (integer): Number of images to generate, between 1 and 4. Default is 1.
- numberOfInferenceSteps (integer): Number of denoising steps during the inference process, affecting image quality. Default is 50.
Example Input:
{
"width": 768,
"height": 768,
"prompt": "a photo of an astronaut riding a horse on mars",
"scheduler": "DPMSolverMultistep",
"guidanceScale": 7.5,
"numberOfOutputs": 1,
"numberOfInferenceSteps": 50
}
Output
Upon successful execution, the action returns a list of URLs pointing to the generated images. Here's a typical example of the output:
Example Output:
[
"https://assets.cognitiveactions.com/invocations/97b53b73-1ed9-4054-9b47-f300879eacc1/b788cf1a-3e49-4500-8b61-630d9d613dba.png"
]
Conceptual Usage Example (Python)
Below is a conceptual Python code snippet that demonstrates how to call the Cognitive Actions execution endpoint for the image generation action. Ensure to replace the placeholder values with your actual API key and 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 = "80889653-3fb3-4ce7-bd2d-558773d24f51" # Action ID for Generate Image With Stable Diffusion Fork
# Construct the input payload based on the action's requirements
payload = {
"width": 768,
"height": 768,
"prompt": "a photo of an astronaut riding a horse on mars",
"scheduler": "DPMSolverMultistep",
"guidanceScale": 7.5,
"numberOfOutputs": 1,
"numberOfInferenceSteps": 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}
)
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:
- Replace
YOUR_COGNITIVE_ACTIONS_API_KEYwith your actual API key. - The
action_idpoints to the specific action you want to execute. - The
payloadis structured according to the input schema needed for image generation.
Conclusion
The Generate Image With Stable Diffusion Fork action empowers developers to create unique and tailored images quickly and efficiently. By leveraging customizable parameters, you can enhance your applications with dynamic visuals that align closely with user inputs. Explore further possibilities and use cases by integrating this action into your projects, and unleash the creative potential of AI-driven image generation!