Skip to content

Instantly share code, notes, and snippets.

@qaixerabbas
Created November 16, 2023 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qaixerabbas/30645c4d5fed9049059ee20a018c4721 to your computer and use it in GitHub Desktop.
Save qaixerabbas/30645c4d5fed9049059ee20a018c4721 to your computer and use it in GitHub Desktop.
Generating Images via Nvidia Stable Diffusion XL API
import requests
import base64
# https://catalog.ngc.nvidia.com/orgs/nvidia/teams/ai-foundation/models/sdxl/api
API_KEY = "GET API KEY FROM NVIDIA PLAYGROUND FROM ABOVE LINK IN COMMENT"
invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/89848fb8-549f-41bb-88cb-95d6597044a4"
fetch_url_format = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
headers = {
"Authorization": API_KEY,
"Accept": "application/json",
}
# give your prompt below
payload = {
"prompt": "A photo of a boy playing voilin in black t shirt and black jeans and a biker jacket",
"negative_prompt": "beach",
"sampler": "DDIM",
"seed": 0,
"unconditional_guidance_scale": 5,
"inference_steps": 50
}
# re-use connections
session = requests.Session()
response = session.post(invoke_url, headers=headers, json=payload)
while response.status_code == 202:
request_id = response.headers.get("NVCF-REQID")
fetch_url = fetch_url_format + request_id
response = session.get(fetch_url, headers=headers)
response.raise_for_status()
response_body = response.json()
# Optionally print the response body
print(response_body)
# The response body contains the dictionary with base64 string of the image
# Code below converts the base64 string to an image that you can save on your local drive
imgdata = base64.b64decode(response_body["b64_json"])
filename = 'output.jpg'
with open(filename, 'wb') as f:
f.write(imgdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment