Forked from qaixerabbas/gen_image_stable_diff_nvidia.py
Created
February 2, 2024 19:48
-
-
Save spirit-q2/582aea5a52bb73209014ad5768507eb3 to your computer and use it in GitHub Desktop.
Generating Images via Nvidia Stable Diffusion XL API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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