From f7cb1a19dc8f55679185b44f35831a978b34f7a1 Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:00:49 +0300 Subject: [PATCH] API key from file --- config.json | 3 +++ nodes.py | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..e44b556 --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "sai_api_key": "your_api_key_here" +} \ No newline at end of file diff --git a/nodes.py b/nodes.py index a4ee9b3..27f1a3c 100644 --- a/nodes.py +++ b/nodes.py @@ -4619,7 +4619,6 @@ class StabilityAPI_SD3: def INPUT_TYPES(cls): return { "required": { - "api_key": ("STRING", {"multiline": True}), "prompt": ("STRING", {"multiline": True}), "n_prompt": ("STRING", {"multiline": True}), "seed": ("INT", {"default": 123,"min": 0, "max": 4294967294, "step": 1}), @@ -4656,9 +4655,11 @@ class StabilityAPI_SD3: }), }, "optional": { + "api_key": ("STRING", {"multiline": True}), "image": ("IMAGE",), "img2img_strength": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}), - } + "disable_metadata": ("BOOLEAN", {"default": True}), + }, } RETURN_TYPES = ("IMAGE",) @@ -4672,6 +4673,13 @@ Although you may have multiple keys in your account, you should use the same key for all requests to this API. Get your API key here: https://platform.stability.ai/account/keys +Recommended to set the key in the config.json -file under this +node packs folder. +# WARNING: +Otherwise the API key may get saved in the image metadata even +with "disable_metadata" on if the workflow includes save nodes +separate from this node. + sd3 requires 6.5 credits per generation sd3-turbo requires 4 credits per generation @@ -4679,8 +4687,13 @@ If no image is provided, mode is set to text-to-image """ - def apicall(self, api_key, prompt, n_prompt, model, seed, aspect_ratio, output_format, - img2img_strength=0.5, image=None): + def apicall(self, prompt, n_prompt, model, seed, aspect_ratio, output_format, + img2img_strength=0.5, image=None, disable_metadata=True, api_key=""): + from comfy.cli_args import args + if disable_metadata: + args.disable_metadata = True + else: + args.disable_metadata = False import requests from io import BytesIO @@ -4713,13 +4726,24 @@ If no image is provided, mode is set to text-to-image if model != "sd3-turbo": data["negative_prompt"] = n_prompt - + + + headers={ + "accept": "image/*" + } + + if api_key != "": + headers["authorization"] = api_key + else: + config_file_path = os.path.join(script_directory,"config.json") + with open(config_file_path, 'r') as file: + config = json.load(file) + api_key_from_config = config.get("sai_api_key") + headers["authorization"] = api_key_from_config + response = requests.post( f"https://api.stability.ai/v2beta/stable-image/generate/sd3", - headers={ - "authorization": api_key, - "accept": "image/*" - }, + headers=headers, files = files, data = data, )