Stable Diffusion tutorial: How to use our Stable Diffusion API

Thursday, November 10, 2022 by ezzcodeezzlife
Stable Diffusion tutorial: How to use our Stable Diffusion API

This article is to provide an overview of the API that was available for the Stable Diffusion Hackathon. Please note that this API is no longer available!

This tutorial shows you how to use our Stable Diffusion API to generate images in seconds. We created our own to ease the development of your project. You cam simply call the endpoint with your prompt, for example "A cat with a hat" and get back a link to the generated image. You can then continue working with the image. We provide a lot of different code examples in different programming languages in this guide.

Tutorial accompaniment image

Short introduction to Stable Diffusion

Stable Diffusion is a text-to-image latent diffusion model created by the researchers and engineers from CompVis, Stability AI and LAION. It's trained on 512x512 images from a subset of the LAION-5B database. This model uses a frozen CLIP ViT-L/14 text encoder to condition the model on text prompts. With its 860M UNet and 123M text encoder, the model is relatively lightweight. If you want to learn more continue reading on our Stable Diffusion tech page here.

Lets get started

You can call the API from your Python project with the help of the requests module like this:

import requests

url = "https://api.newnative.ai/stable-diffusion?prompt=futuristic spce station, 4k digital art"

response = requests.request("GET", url)
data = response.json()
print(data["image_url"])

# https://storage.googleapis.com/lablab-static-eu/stable-diffusion/futuristic%20spce%20station%2C%204k%20digital%20artbd8250880a904de79fac1a5f5f6fb3ed.png

If you need the image on your local disk you can use the following code to download it from the URL (Python)

import urllib.request

urllib.request.urlretrieve("https://storage.googleapis.com/lablab-static-eu/stable-diffusion/futuristic%20space%20station.png", "local-filename.jpg")

In Javascript you can call the API with this code:

var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};

fetch("https://api.newnative.ai/stable-diffusion?prompt=futuristic spce station, 4k digital art", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

and how to call the endpoint in C#:

var client = new RestClient("https://api.newnative.ai/stable-diffusion?prompt=futuristic spce station, 4k digital art");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

If you are building a flutter app you might want to use Dart to call the API. Here is how you can do it:

var request = http.Request('GET', Uri.parse('https://api.newnative.ai/stable-diffusion?prompt=futuristic spce station, 4k digital art'));


http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

Note that you can change the actual prompt to whatever you want in the URL parameter.

We hope this helps you to get started with our Stable Diffusion API. If you have any questions or feedback, please let us know in the Lablab.ai Discord.

Thank you! If you enjoyed this tutorial you can find more and continue reading on our tutorial page - Fabian Stehle, Data Science Intern at New Native

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events