How to Integrate Stable Diffusion via Inference API
This tutorial guides you on how to integrate the Stable Diffusion via inference API into your products.
Last updated
payload = {
"prompt": "apple",
"negative_prompt": "red",
"sampler_name": "DPM++ 2M Karras",
"seed": 985454925,
"cfg_scale": 7,
"steps": 20,
"width": 512,
"height": 512,
}import json
import requests
import io
import base64
from PIL import Image
url = "https://<API_ENDPOINT>"
username = 'admin'
password = 'admin1234'
# Encode the username and password in Base64
credentials = f'{username}:{password}'
credentials = base64.b64encode(credentials.encode()).decode()
headers = {
'Authorization': f'Basic {credentials}',
'Content-Type': 'application/json'
}
response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload, headers=headers)r = response.json()
# 'images' is a list of base64-encoded generated images
image = Image.open(io.BytesIO(base64.b64decode(r['images'][0]))
image.save('output.png')import json
import requests
import io
import base64
from PIL import Image
url = "https://fk5ge5uhhu.mars.nebulablock.com"
username = 'admin'
password = 'admin1234'
# Encode the username and password in Base64
credentials = f'{username}:{password}'
credentials = base64.b64encode(credentials.encode()).decode()
headers = {
'Authorization': f'Basic {credentials}',
'Content-Type': 'application/json' # Adjust content type if necessary
}
payload = {
"prompt": "apple",
"negative_prompt": "red",
"sampler_name": "DPM++ 2M Karras",
"seed": 985454925,
"cfg_scale": 7,
"steps": 20,
"width": 512,
"height": 512,
}
response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload, headers=headers)
r = response.json()
image = Image.open(io.BytesIO(base64.b64decode(r['images'][0])))
image.save('output.png')import json
import requests
import base64
url = "https://fk5ge5uhhu.mars.nebulablock.com"
username = 'admin'
password = 'admin1234'
# Encode the username and password in Base64
credentials = f'{username}:{password}'
credentials = base64.b64encode(credentials.encode()).decode()
headers = {
'Authorization': f'Basic {credentials}',
'Content-Type': 'application/json' # Adjust content type if necessary
}
response = requests.get(url=f'{url}/sdapi/v1/sd-models', headers=headers)
print(json.dumps(response.json()))[ { "title":"chilloutmix-Ni.safetensors [7234b76e42]", "model_name":"chilloutmix-Ni", "hash":"7234b76e42", "sha256":"7234b76e423f010b409268386062a4111c0da6adebdf3a9b1a825937bdf17683", "filename":"/stable-diffusion-webui/models/Stable-diffusion/chilloutmix-Ni.safetensors", "config":null }, { "title":"v1-5-pruned-emaonly.safetensors [6ce0161689]", "model_name":"v1-5-pruned-emaonly", "hash":"6ce0161689", "sha256":"6ce0161689b3853acaa03779ec93eafe75a02f4ced659bee03f50797806fa2fa", "filename":"/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors", "config":null } ]import json
import requests
import base64
url = "https://fk5ge5uhhu.mars.nebulablock.com"
username = 'admin'
password = 'admin1234'
# Encode the username and password in Base64
credentials = f'{username}:{password}'
credentials = base64.b64encode(credentials.encode()).decode()
headers = {
'Authorization': f'Basic {credentials}',
'Content-Type': 'application/json' # Adjust content type if necessary
}
payload = {
"sd_model_checkpoint": "v1-5-pruned-emaonly.safetensors [6ce0161689]",
}
response = requests.post(url=f'{url}/sdapi/v1/options', json=payload, headers=headers)
print(json.dumps(response.json()))