Quickstart
Stable Diffusion is a deep learning, text-to-image model released in 2022. It is primarily used to generate detailed images from text descriptions, it also be applied to other jobs such as inpainting, outpainting, and image-to-image translations.
DAN API
package full capabilities of Stable Diffusion on the decentralized cloud. Using DAN API
, you can build a Stable Diffusion application with:
- Zero GPU
- Zero Stable Diffusion runtime setup
- Zero Models and LoRAs download
Introduction
In this tutorial, you will use DAN API
to generate images step by step. And you will learn the key concepts and techniques including:
- Prompt
- Sampling method
- Model
- CFG Scale
- Sampling steps
- LoRA
- Negative prompt
Let's start with txt2img
API, the final goal is to generate a stunning
image as below.
Generate image from prompt
Great! Now that you have found a good prompt, you are ready to generate your image. We have provided some code to help you get started - please follow the instructions below to copy the code and run the application.
0. Setup
- macOS
- Windows
If you are using a Mac system, you can open the Terminal
and enter the following command to install the necessary software and dependencies:
brew install curl
brew install jq
Note: If you have not installed brew
before, you can refer to this tutorial to install brew
: https://brew.sh/
You can enter the following command to check if curl
and jq
are installed successfully.
curl --version
jq --version
If a version number is returned, it means that they have been installed successfully. Please proceed to the next step of the tutorial.
If you are using a Windows system, please refer to the following two articles to install the curl
and jq
tools respectively:
- https://linuxhint.com/install-use-curl-windows/
- https://bobbyhadz.com/blog/install-and-use-jq-on-windows
You can enter the following command on PowerShell
to check if curl
and jq
are installed successfully.
curl --version
jq --version
If a version number is returned, it means that they have been installed successfully. Please proceed to the next step of the tutorial.
1. Generate the first image
Please copy the following command and paste it in your terminal to run:
curl -L -X POST 'https://api.opendan.ai/api/sd/txt2img' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer sk-RIO8FZ8XHXSWWTHRPRCLW3HESYQCQDVBQQVFGCB3MU4VMXJ4' \
--data-raw '{
"prompt": "masterpiece, best quality, prefect face, shiny skin, cyberpunk",
"sampler_name": "DPM++ SDE Karras",
"width": 600,
"height": 800,
"model": "3a17d0deffa4592fd91c711a798031a258ab44041809ade8b4591c0225ea9401"
}' \
| jq '.data.images[0]' | tr -d '\"' | tr -d '\\' | base64 -d > image.png
here is a brief explanation of the parameters used in the command:
- prompt: provides a text prompt that describes the desired contents or features of the generated image.
- sampler_name: The name of the sampling algorithm used. On the API page, we provide a list of supported sampling methods.
- model: specifies the name of the language model to use for image generation.On the API page, we provide a list of supported model.
- width & height: sets the size of the generated image(s) in pixels.
The command above will generate an image file called image.png
in the directory where you run the command. The resulting image may look similar to the one shown below:
Although we have generated an image, the quality of the image is not satisfactory. In the following steps, we will optimize the parameters to obtain a better image.
2. Improve image-word similarity
Please continue to copy the following command and paste it in your terminal to run:
curl -L -X POST 'https://api.opendan.ai/api/sd/txt2img' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer sk-RIO8FZ8XHXSWWTHRPRCLW3HESYQCQDVBQQVFGCB3MU4VMXJ4' \
--data-raw '{
"prompt": "masterpiece, best quality, prefect face, shiny skin, cyberpunk",
"sampler_name": "DPM++ SDE Karras",
"width": 600,
"height": 800,
"model": "3a17d0deffa4592fd91c711a798031a258ab44041809ade8b4591c0225ea9401",
"cfg_scale": 7
}' \
| jq '.data.images[0]' | tr -d '\"' | tr -d '\\' | base64 -d > image.png
This time, we have added the following parameters:
- cfg_scale: Classifier Free Guidance scale is a parameter to control how much the model should respect your prompt.Recommendation: Starts with 7. Increase if you want it to follow your prompt more
The resulting image may look similar to the one shown below:
3. Improve the quality
Please continue to copy the following command and paste it in your terminal to run:
curl -L -X POST 'https://api.opendan.ai/api/sd/txt2img' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer sk-RIO8FZ8XHXSWWTHRPRCLW3HESYQCQDVBQQVFGCB3MU4VMXJ4' \
--data-raw '{
"prompt": "masterpiece, best quality, prefect face, shiny skin, cyberpunk",
"sampler_name": "DPM++ SDE Karras",
"width": 600,
"height": 800,
"model": "3a17d0deffa4592fd91c711a798031a258ab44041809ade8b4591c0225ea9401",
"cfg_scale": 7,
"steps": 20
}' \
| jq '.data.images[0]' | tr -d '\"' | tr -d '\\' | base64 -d > image.png
This time, we have added the following parameters:
- steps: Means sampling steps.Quality improves as the sampling step increases.Although the image will still change subtly when stepping through to higher values, it will become different but not necessarily higher quality.Recommendation: 20 steps. Adjust to higher if you suspect quality is low.
The resulting image may look similar to the one shown below:
4. Use loRAs
Please continue to copy the following command and paste it in your terminal to run:
curl -L -X POST 'https://api.opendan.ai/api/sd/txt2img' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer sk-RIO8FZ8XHXSWWTHRPRCLW3HESYQCQDVBQQVFGCB3MU4VMXJ4' \
--data-raw '{
"prompt": "masterpiece, best quality, prefect face, shiny skin, cyberpunk",
"sampler_name": "DPM++ SDE Karras",
"width": 600,
"height": 800,
"model": "3a17d0deffa4592fd91c711a798031a258ab44041809ade8b4591c0225ea9401",
"cfg_scale": 7,
"steps": 20,
"loras": [
["62efe75048d55a096a238c6e8c4e12d61b36bf59e388a90589335f750923954c", 0.6],
["3e5d8fe726b4c0f1e7f0905f32ea3d1c9ce89a54028209e8179d64d323048dac", 0.7]
]
}' \
| jq '.data.images[0]' | tr -d '\"' | tr -d '\\' | base64 -d > image.png
This time, we have added the following parameters:
- loras: LoRA models are small modifiers of checkpoint models.Mixing multiple LoRA models can have a stacking effect.On the API page, we provide a list of supported model.
The resulting image may look similar to the one shown below:
Great, it looks like we are getting closer to our desired result!
5. Add negative prompts
Please continue to copy the following command and paste it in your terminal to run:
curl -L -X POST 'https://api.opendan.ai/api/sd/txt2img' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer sk-RIO8FZ8XHXSWWTHRPRCLW3HESYQCQDVBQQVFGCB3MU4VMXJ4' \
--data-raw '{
"prompt": "masterpiece, best quality, prefect face, shiny skin, cyberpunk",
"sampler_name": "DPM++ SDE Karras",
"width": 600,
"height": 800,
"model": "3a17d0deffa4592fd91c711a798031a258ab44041809ade8b4591c0225ea9401",
"cfg_scale": 7,
"steps": 20,
"loras": [
["62efe75048d55a096a238c6e8c4e12d61b36bf59e388a90589335f750923954c", 0.6],
["3e5d8fe726b4c0f1e7f0905f32ea3d1c9ce89a54028209e8179d64d323048dac", 0.7]
],
"negative_prompt": "sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, bad anatomy,DeepNegative,(fat:1.2),facing away, looking away,tilted head, {Multiple people}, lowres,bad anatomy,bad hands, text, error, missing fingers,extra digit, fewer digits, cropped, worstquality, low quality, normal quality,jpegartifacts,signature, watermark, username,blurry,bad feet,cropped,poorly drawn hands,poorly drawn face,mutation,deformed,worst quality,low quality,normal quality,jpeg artifacts,signature,watermark,extra fingers,fewer digits,extra limbs,extra arms,extra legs,malformed limbs,fused fingers,too many fingers,long neck,cross-eyed,mutated hands,polar lowres,bad body,bad proportions,gross proportions,text,error,missing fingers,missing arms,missing legs,extra digit, extra arms,wrong hand"
}' \
| jq '.data.images[0]' | tr -d '\"' | tr -d '\\' | base64 -d > image.png
This time, we have added the following parameters:
- negative_prompt: describe in words what you do not want the generated image to depict.
The resulting image may look similar to the one shown below:
Congratulations! We have completed this tutorial and generated an image from your prompts.
Visit the API page to explore more.