APIs are the way out in this era of Artificial Intelligence, where one of the significant challenges for businesses is managing the high computational requirements of AI models when integrating them into software and applications. The solution to this comes in the form of Application Programming Interfaces (APIs). APIs save you from maintenance hassles, allowing you to focus on your business logic and user experience. This means anyone can leverage these APIs to build and commercialize apps. This article will delve into Stable Diffusion APIs, a set of Generative AI models focused on image generation, which have become crucial for various applications.
We will first see Segmind API, a very efficient and effective option. These APIs have revolutionized how developers, designers, and creators approach visual content generation. We will explore some of the top 5 Stable Diffusion APIs, highlighting their features, use cases, pricing, and more.
This article was published as a part of the Data Science Blogathon.
Stable Diffusion Models are a class of generative AI models that focus on generating high-quality images. These models are designed to produce realistic, high-resolution images with various applications. They are characterized by their stability and reliability, which are helpful for tasks like image synthesis, style transfer, and data augmentation.
Stable Diffusion Models use a diffusion process to generate images, where noise is added gradually to an image until it evolves into a complex and coherent output. This process ensures the generated images are high quality and exhibit fine details.
APIs, or Application Programming Interfaces, are a set of rules and protocols that allow one software application to interact with and use the features or data of another application, service, or platform. APIs are intermediaries, enabling integration between software.
In software development, APIs provide a way for developers to access functions, services, or data from sources, including cloud services, databases, or AI models, without understanding the underlying complexities. This simplifies development and accelerates the creation of feature-rich applications.
APIs can come in forms, including RESTful APIs, gRPC APIs, and WebSocket APIs, each tailored to specific use cases. They play a critical role in modern software, enabling developers to leverage the power of third-party services and functionalities while focusing on their core application logic.
Let’s look at the top Stable Diffusion APIs on our list, starting with the user-friendly Segmind API.
Topping our list is the Segmind API, known for its ease of use and little setup. If you have experience using Standard API tools like Postman, you almost do not need to learn anything new. We will see briefly how this can be done. You can refer to this article for a detailed approach to using Segmind and Postman.
Segmind’s GenAI API provides many purposes, capabilities, and benefits. Some include image recognition in e-commerce, fashion, architecture like the DvArch, animation, background removal, etc. Segmind AI models are available through the API and playground on the website at https://www.segmind.com/models.
There are many reasons why Segmind tops our list here.
The API playground makes it very easy to use the models in seconds. To demo how easily the API playground works, we will consider the new powerful stable diffusion models called “Dreamshaper SDXL.” You only need an account, which you can set up in minutes. Go to https://www.segmind.com/ to sign up. After signing up, go to the models’ page and search for ‘Dreamshaper SDXL,’ a new release. Or follow this link https://www.segmind.com/models/sdxl1.0-dreamshaper. Once you click on the model, you see the playground. Besides the playground, you know the API call interface, as shown in the screenshot below.
To try out the above image, here is the prompt I used:
Unveiling a Cinematic Portrait: The Grumpy Cat 1.25 in a Spacesuit, Cyberpunk 2077 City Background, 2D Masterpiece by John Wilhelm, Grumpy 1.2, Cyberpunk 1.4, Photo-Realistic, Octane Render, HDR, Neon, Lens Flares, Best Quality 1.9, Active Asymmetrical Pose, Action-Packed 1.8, Trending on ArtStation, 8K, 35mm Photograph, Film, Bokeh, Professional, 4K, Highly Detailed.
The above approach is for using the playground, which is directly available on the website. The other option available is the API. This is a powerful option on Segmind. This is where you can use API calls to build your app and enjoy the Segmind model.
API calls remove the need for hosting these large and expensive models. You do not care about server maintenance or hosting. You need to have basic knowledge of API functionalities like using Postman. The whole process remains the same as regular API Calls. Assuming you use Javascript for your web app, below is the script for making a POST request on Dreamshaper SDXL. This will respond with a jpeg image.
import requests
api_key = "YOUR API-KEY"
url = "https://api.segmind.com/v1/sdxl1.0-dreamshaper"
# Request payload
data = {
"prompt": "cinematic photo of portrait of cyberpunk (the Grumpy Cat:1.25) in a spacesuit, looking with endless sadness at the universe passing by, cyberpunk 2077 city bg, 2d masterpiece by john Wilhelm, (grumpy:1.2), (cyberpunk:1.4), photo-realistic, octane render, hdr, neon, lens flares, ( best quality:1.9), active asymmetrical pose, (action-packed:1.8), trending on artstation, 8k, 35mm photograph, film, bokeh, professional, 4k, highly detailed",
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly, [deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry",
"samples": 1,
"scheduler": "UniPC",
"num_inference_steps": 35,
"guidance_scale": "7",
"seed": "1135424276",
"img_width": "1024",
"img_height": "1024",
"base64": False
}
response = requests.post(url, json=data, headers={'x-api-key': api_key})
print(response)
Segmind made every model have their API call script on their page as individual documents for ease and accessibility. All you need to do is make a POST request from Postman. Assuming you are developing with Python, you have the script for Python as seen below:
import requests
api_key = "YOUR API-KEY"
url = "https://api.segmind.com/v1/sdxl1.0-dreamshaper"
# Request payload
data = {
"prompt": "cinematic photo of portrait of cyberpunk (the Grumpy Cat:1.25) in a spacesuit, looking with endless sadness at the universe passing by, cyberpunk 2077 city bg, 2d masterpiece by john Wilhelm, (grumpy:1.2), (cyberpunk:1.4), photo-realistic, octane render, hdr, neon, lens flares, ( best quality:1.9), active asymmetrical pose, (action-packed:1.8), trending on artstation, 8k, 35mm photograph, film, bokeh, professional, 4k, highly detailed",
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly, [deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry",
"samples": 1,
"scheduler": "UniPC",
"num_inference_steps": 35,
"guidance_scale": "7",
"seed": "1135424276",
"img_width": "1024",
"img_height": "1024",
"base64": False
}
response = requests.post(url, json=data, headers={'x-api-key': api_key})
print(response)
All you need do is to change your prompt in the data, and the response you get is the generated image as a jpeg. You can explore further or look at this article for a detailed step on using Segmind with Postman, or visit the documentation or the GitHub repo for more information. Links are compiled at the end of the article. Next, we will briefly see other SD API providers.
Stability AI, a leader in open-source generative artificial intelligence, is dedicated to advancing infrastructure and research. Their open-source ethos supports language, code, audio, video, 3D content, design, and biotech. This aims to empower humanity with the potential of AI.
The Stability AI API offers a flexible approach to authentication, credits, and API calls. Users must create an account to get an API key, which is essential for accessing the API. Credits are required to use the Stability API, with new users receiving 25 free credits upon registration.
Stability AI is a bit complex for newbies since it provides more than one type of API. Apart from the regular REST, they provide gRPC API. You can check out GitHub for some technical guides found here https://github.com/Stability-AI/. You can check the documentation which provides all the API calls but to carry out a text-to-image generation, the structure looks like the below with Python language.
import base64
import os
import requests
engine_id = "stable-diffusion-xl-1024-v1-0"
api_host = os.getenv('API_HOST', 'https://api.stability.ai')
api_key = os.getenv("STABILITY_API_KEY")
if api_key is None:
raise Exception("Missing Stability API key.")
response = requests.post(
f"{api_host}/v1/generation/{engine_id}/text-to-image",
headers={
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {api_key}"
},
json={
"text_prompts": [
{
"text": "A lighthouse on a cliff"
}
],
"cfg_scale": 7,
"height": 1024,
"width": 1024,
"samples": 1,
"steps": 30,
},
)
if response.status_code != 200:
raise Exception("Non-200 response: " + str(response.text))
data = response.json()
for i, image in enumerate(data["artifacts"]):
with open(f"./out/v1_txt2img_{i}.png", "wb") as f:
f.write(base64.b64decode(image["base64"]))
For developers using other languages, there is a script for both Typescript and Go. You can check out their documentation for details. Now let us see the next API.
Hotpot.ai’s Stable Diffusion API is another one that offers multiple options to cater to diverse requirements. Designed with proprietary optimizations for enterprises and consumers offering a customizable Stable Diffusion model that is cost-effective. They allow you to generate images without any need to sign up. We will use their AI Art generator to generate an image using the first sentence of our previous prompt.
Prompt:
Create an artwork that fuses the elements of cyberpunk and futuristic space exploration.
We can easily use the ‘Style’ parameter to adjust the image. Changing to a different style gives the below image.
Pricing: Pricing for the API depends on factors like monthly volume, subscription plans, and co-marketing discounts. It’s crucial to be cautious of deals offering unlimited packages, as they may compromise reliability and latency. Hotpot.ai ensures scalability, managing millions of image generation requests while maintaining low latency and high reliability.
Accessing the API: API access is made easy with the Stable Diffusion Online Editor, which offers results identical to the API. The key difference is speed, with API users gaining access to private, more powerful servers for quicker image generation. Hotpot.ai even provides free test credits for their Stable Diffusion API, making it accessible for developers.
Additionally, Hotpot.ai keeps up with the latest Stable Diffusion versions, currently offering version 2.1 with plans to integrate Stable Diffusion XL for commercial purposes.
Let’s see the API call script for Hotpot. Hotpot.ai offers the Stable Diffusion API as budget, standard, and premium. To use the API with Python you need to first install ‘request’.
!pip3 install requests
import requests
headers = {
'Authorization': 'API_KEY_HERE',
}
# change to a full file path of the image you want to transform
body = {
'image': open('/full/path/to/image.jpg', 'rb'),
}
response = requests.post('https://api.hotpot.ai/remove-background', headers=headers, files=body)
# change to a full file path where you want to save the resulting image
with open('/full/path/to/image-nobg.jpg', 'wb') as file:
file.write(response.content)
Yeah, even if you are using old and gold PHP, you are not left out here.
<?php
$ch = curl_init();
// change to a full file path of the image you want to transform
$body = [
'image' => new CurlFile('/full/path/to/image.jpg')
];
curl_setopt($ch, CURLOPT_URL, 'https://api.hotpot.ai/remove-background');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$headers = array('Authorization: API_KEY_HERE');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// change to a full file path where you want to save the resulting image
file_put_contents('/full/path/to/image-nobg.jpg', $response);
If you need more gist on how, check the GitHub here.
ZYLA Stable Diffusion API leverages the state-of-the-art Stable Diffusion v2.1 model to produce high-quality images. This API provides advanced image generation capabilities, scalability, and a user-friendly interface. Developers, designers, and content creators can enhance their projects with stunning and realistic images. Zyla is like a webstore of APIs. We will focus on just the Stable Diffusion APIs.
Most of the APIs have a free 7-day trial. You could sign up and see if it meets your needs.
the API for AI the Image Generator API supports Postman as Segmind AI. After signing up, you get a personal API access key. This is used to authenticate with the AI Image Generator API REST API by simply including your bearer token in the Authorization header. The header variable on Postman is
Authorization
You need to sign up to access the API. After signing up, see the “Your API Access Key” and set [Required] for Bearer access_key.
Bearer access_key
Note that ZYLA is more like a web store for APIs, and SD API is just one of the collections.
This API lets you generate and edit images using the latest Stable Diffusion-based models. You can use this API for image generation pipelines, like text-to-image, ControlNet, inpainting, upscaling, and more.
They make coding very brief. Here’s an example of how to make a POST request to the Stable Diffusion API for text-to-image generation using cURL:
curl \
-X POST https://api.getimg.ai/v1/stable-diffusion/text-to-image \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"prompt":"a photo of an astronaut riding a horse on Mars"}'
You can use the above prompt in the playground to try out the model. The below sample generates 4 images at once. This is handy for generating stock photos.
You can customize the prompt to generate different images based on your requirements.
Additionally, getimg.ai offers different pricing options based on the size, steps, and pipelines you choose. For example, Stable Diffusion XL with 1024×1024 pixels and 30 steps is priced at $0.00944 per image.
You can find more information about this API, its use cases, and pricing on the getimg.ai website, and you can explore the features and models they offer for image generation and editing. You must sign up and get an API key to authenticate your requests to use the API.
Image generation is rapidly evolving, and Stable Diffusion APIs are at the forefront. Developers, designers, and content creators now have a wealth of options. These APIs offer advanced features, scalability, and many use cases, making them indispensable tools in the modern AI landscape. Employ these APIs to transform your projects and enjoy the power of cutting-edge image generation technology.
Unlock the full potential of Stable Diffusion APIs. Gain practical skills and insights to enhance your projects. Join the Course Now!
A1: Pricing for Stable Diffusion APIs can vary based on monthly volume, subscription plans, and co-marketing discounts. It’s important to be cautious of deals offering unlimited packages, as they may compromise reliability and latency.
A2: To access Stable Diffusion APIs, you typically need to create an account with the respective API provider and get an API key, which serves as the authentication mechanism. The process may include receiving free test credits upon registration.
A3: The limitations of Stable Diffusion APIs can vary by provider. Some APIs may limit usage based on the number of API calls, while others might have different usage restrictions or pricing tiers. It’s essential to review the terms and conditions of each API to understand any limitations.
A4: Different versions of Stable Diffusion models may offer varying features and capabilities. For example, version 2.1 represents a specific model iteration, while Stable Diffusion XL might be designed for commercial purposes with enhanced features. It’s advisable to check with the API provider for detailed information on version differences.
A5: To start with Stable Diffusion APIs, you should first identify your project’s specific requirements and select an API that aligns with those needs. Create an account with the chosen provider, create an API key, and explore the available documentation and resources to integrate the API effectively into your application or workflow.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
Lorem ipsum dolor sit amet, consectetur adipiscing elit,