|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io |
|
import os |
|
import time |
|
import numpy as np |
|
import requests |
|
import torch |
|
from PIL import Image, ImageFont, ImageDraw |
|
from PIL import Image, ImageDraw |
|
import importlib |
|
import comfy.samplers |
|
import comfy.sd |
|
import comfy.utils |
|
|
|
|
|
|
|
|
|
|
|
|
|
class O_ChatGPT_deprecated: |
|
""" |
|
this node is based on the openAI GPT-3 API to generate propmpts using the AI |
|
""" |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
|
|
"prompt": ("STRING", {"multiline": True}), |
|
|
|
"api_key_file": ("STRING", {"file": True, "default": "api_key.txt"}) |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("STR",) |
|
FUNCTION = "fun" |
|
CATEGORY = "O/deprecated/OpenAI" |
|
|
|
def fun(self, api_key_file, prompt): |
|
self.install_openai() |
|
import openai |
|
|
|
|
|
api_key = self.get_api_key(api_key_file) |
|
|
|
openai.api_key = api_key |
|
|
|
|
|
completion = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=[ |
|
{"role": "user", "content": "act as prompt generator ,i will give you text and you describe an image that match that text in details, answer with one response only"}, |
|
{"role": "user", "content": prompt} |
|
] |
|
) |
|
|
|
answer = completion["choices"][0]["message"]["content"] |
|
|
|
return ( |
|
{ |
|
"string": answer, |
|
}, |
|
) |
|
|
|
|
|
def get_api_key(self, api_key_file): |
|
custom_nodes_dir = './custom_nodes/' |
|
with open(custom_nodes_dir+api_key_file, 'r') as f: |
|
api_key = f.read().strip() |
|
return api_key |
|
|
|
|
|
def install_openai(self): |
|
try: |
|
importlib.import_module('openai') |
|
except ImportError: |
|
import pip |
|
pip.main(['install', 'openai']) |
|
|
|
|
|
""" |
|
this node will load openAI model |
|
""" |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
|
|
"api_key_file": ("STRING", {"file": True, "default": "api_key.txt"}) |
|
} |
|
} |
|
RETURN_TYPES = ("OPENAI",) |
|
FUNCTION = "fun" |
|
CATEGORY = "O/OpenAI/Advanced" |
|
|
|
def fun(self, api_key_file): |
|
self.install_openai() |
|
import openai |
|
|
|
|
|
api_key = self.get_api_key(api_key_file) |
|
openai.api_key = api_key |
|
|
|
return ( |
|
{ |
|
"openai": openai, |
|
}, |
|
) |
|
|
|
|
|
def install_openai(self): |
|
try: |
|
importlib.import_module('openai') |
|
except ImportError: |
|
import pip |
|
pip.main(['install', 'openai']) |
|
|
|
|
|
def get_api_key(self, api_key_file): |
|
custom_nodes_dir = './custom_nodes/' |
|
with open(custom_nodes_dir+api_key_file, 'r') as f: |
|
api_key = f.read().strip() |
|
return api_key |
|
|
|
|
|
|
|
class openAi_chat_message_STR_deprecated: |
|
""" |
|
create chat message for openAI chatGPT |
|
""" |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
"role": (["user", "assistant", "system"], {"default": "user"}), |
|
"content": ("STR",), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("OPENAI_CHAT_MESSAGES",) |
|
FUNCTION = "fun" |
|
|
|
CATEGORY = "O/deprecated/OpenAI/Advanced/ChatGPT" |
|
|
|
def fun(self, role, content): |
|
return ( |
|
{ |
|
"messages": [{"role": role, "content": content["string"], }] |
|
}, |
|
) |
|
|
|
|
|
|
|
class openAi_chat_messages_Combine_deprecated: |
|
""" |
|
compine chat messages into 1 tuple |
|
""" |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
"message1": ("OPENAI_CHAT_MESSAGES", ), |
|
"message2": ("OPENAI_CHAT_MESSAGES", ), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("OPENAI_CHAT_MESSAGES",) |
|
FUNCTION = "fun" |
|
|
|
CATEGORY = "O/deprecated/OpenAI/Advanced/ChatGPT" |
|
|
|
def fun(self, message1, message2): |
|
messages = message1["messages"] + \ |
|
message2["messages"] |
|
|
|
return ( |
|
{ |
|
"messages": messages |
|
}, |
|
) |
|
class openAi_Image_create_deprecated: |
|
""" |
|
create image using openai |
|
""" |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
"openai": ("OPENAI", ), |
|
"prompt": ("STR",), |
|
"number": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1}), |
|
"size": (["256x256", "512x512", "1024x1024"], {"default": "256x256"}), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("IMAGE", "MASK") |
|
FUNCTION = "fun" |
|
OUTPUT_NODE = True |
|
|
|
CATEGORY = "O/deprecated/OpenAI/Advanced/Image" |
|
|
|
def fun(self, openai, prompt, number, size): |
|
|
|
openai = openai["openai"] |
|
prompt = prompt["string"] |
|
number = 1 |
|
imagesURLS = openai.Image.create( |
|
prompt=prompt, |
|
n=number, |
|
size=size |
|
) |
|
imageURL = imagesURLS["data"][0]["url"] |
|
print("imageURL:", imageURL) |
|
image = requests.get(imageURL).content |
|
i = Image.open(io.BytesIO(image)) |
|
image = i.convert("RGBA") |
|
image = np.array(image).astype(np.float32) / 255.0 |
|
|
|
image = torch.from_numpy(image)[None,] |
|
if 'A' in i.getbands(): |
|
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 |
|
mask = 1. - torch.from_numpy(mask) |
|
else: |
|
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu") |
|
print("image_tensor: done") |
|
return (image, mask) |
|
|
|
|
|
class openAi_chat_completion_deprecated: |
|
""" |
|
create chat completion for openAI chatGPT |
|
""" |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
"openai": ("OPENAI", ), |
|
"model": ("STRING", {"multiline": False, "default": "gpt-3.5-turbo"}), |
|
"messages": ("OPENAI_CHAT_MESSAGES", ), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("STR", "OPENAI_CHAT_COMPLETION",) |
|
FUNCTION = "fun" |
|
OUTPUT_NODE = True |
|
|
|
CATEGORY = "O/deprecated/OpenAI/Advanced/ChatGPT" |
|
|
|
def fun(self, openai, model, messages): |
|
|
|
openai = openai["openai"] |
|
completion = openai.ChatCompletion.create( |
|
model=model, |
|
messages=messages["messages"] |
|
) |
|
|
|
content = completion["choices"][0]["message"]["content"] |
|
return ( |
|
{ |
|
"string": content, |
|
}, |
|
{ |
|
"completion": completion, |
|
} |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class O_String_deprecated: |
|
""" |
|
this node is a simple string node that can be used to hold userinput as string |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": {"string": ("STRING", {"multiline": True})}} |
|
|
|
RETURN_TYPES = ("STR",) |
|
FUNCTION = "ostr" |
|
CATEGORY = "O/deprecated/string" |
|
|
|
@staticmethod |
|
def ostr(string): |
|
return ({"string": string},) |
|
|
|
|
|
class DebugString_deprecated: |
|
""" |
|
This node will write a string to the console |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": {"string": ("STR",)}} |
|
|
|
RETURN_TYPES = () |
|
FUNCTION = "debug_string" |
|
OUTPUT_NODE = True |
|
CATEGORY = "O/deprecated/string" |
|
|
|
@staticmethod |
|
def debug_string(string): |
|
print("debugString:", string["string"]) |
|
return () |
|
|
|
|
|
class string2Image_deprecated: |
|
""" |
|
This node will convert a string to an image |
|
""" |
|
|
|
def __init__(self): |
|
self.font_filepath = os.path.join( |
|
os.path.dirname(os.path.realpath(__file__)), "fonts") |
|
|
|
@classmethod |
|
def INPUT_TYPES(s): |
|
return { |
|
"required": { |
|
"string": ("STR",), |
|
"font": ("STRING", {"default": "CALIBRI.TTF", "multiline": False}), |
|
"size": ("INT", {"default": 36, "min": 0, "max": 255, "step": 1}), |
|
"font_R": ("INT", {"default": 0, "min": 0, "max": 255, "step": 1}), |
|
"font_G": ("INT", {"default": 0, "min": 0, "max": 255, "step": 1}), |
|
"font_B": ("INT", {"default": 0, "min": 0, "max": 255, "step": 1}), |
|
"background_R": ("INT", {"default": 255, "min": 0, "max": 255, "step": 1}), |
|
"background_G": ("INT", {"default": 255, "min": 0, "max": 255, "step": 1}), |
|
"background_B": ("INT", {"default": 255, "min": 0, "max": 255, "step": 1}), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("IMAGE",) |
|
FUNCTION = "create_image" |
|
OUTPUT_NODE = False |
|
CATEGORY = "O/deprecated/string" |
|
|
|
def create_image(self, string, font, size, font_R, font_G, font_B, background_R, background_G, background_B): |
|
font_color = (font_R, font_G, font_B) |
|
font = ImageFont.truetype(self.font_filepath+"\\"+font, size) |
|
mask_image = font.getmask(string["string"], "L") |
|
image = Image.new("RGBA", mask_image.size, |
|
(background_R, background_G, background_B)) |
|
|
|
image.im.paste(font_color, (0, 0) + mask_image.size, mask_image) |
|
|
|
|
|
image_np = np.array(image).astype(np.float32) / 255.0 |
|
image_tensor = torch.from_numpy(image_np).unsqueeze(0) |
|
return (image_tensor,) |
|
|
|
|
|
class CLIPStringEncode_deprecated: |
|
""" |
|
This node will encode a string with CLIP |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(s): |
|
return {"required": { |
|
"string": ("STR",), |
|
"clip": ("CLIP", ) |
|
}} |
|
RETURN_TYPES = ("CONDITIONING",) |
|
FUNCTION = "encode" |
|
|
|
CATEGORY = "O/deprecated/string" |
|
|
|
def encode(self, string, clip): |
|
return ([[clip.encode(string["string"]), {}]], ) |
|
|
|
|
|
|
|
class concat_String_deprecated: |
|
""" |
|
This node will concatenate two strings together |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": { |
|
"string1": ("STR",), |
|
"string2": ("STR",) |
|
}} |
|
|
|
RETURN_TYPES = ("STR",) |
|
FUNCTION = "fun" |
|
CATEGORY = "O/deprecated/string/operations" |
|
|
|
@staticmethod |
|
def fun(string1, string2): |
|
return ({"string": string1["string"] + string2["string"]},) |
|
|
|
|
|
class trim_String_deprecated: |
|
""" |
|
This node will trim a string from the left and right |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": { |
|
"string": ("STR",), |
|
}} |
|
|
|
RETURN_TYPES = ("STR",) |
|
FUNCTION = "fun" |
|
CATEGORY = "O/deprecated/string/operations" |
|
|
|
def fun(self, string): |
|
return ( |
|
{ |
|
"string": (string["string"].strip()), |
|
}, |
|
) |
|
|
|
|
|
class replace_String_deprecated: |
|
""" |
|
This node will replace a string with another string |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": { |
|
"string": ("STR",), |
|
"old": ("STRING", {"multiline": False}), |
|
"new": ("STRING", {"multiline": False}) |
|
}} |
|
|
|
RETURN_TYPES = ("STR",) |
|
FUNCTION = "fun" |
|
CATEGORY = "O/deprecated/string/operations" |
|
|
|
@staticmethod |
|
def fun(string, old, new): |
|
return ({"string": string["string"].replace(old, new)},) |
|
|
|
|
|
|
|
|
|
class replace_String_advanced_deprecated: |
|
""" |
|
This node will replace a string with another string |
|
""" |
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return {"required": { |
|
"string": ("STR",), |
|
"old": ("STR",), |
|
"new": ("STR",), |
|
}} |
|
|
|
RETURN_TYPES = ("STR",) |
|
FUNCTION = "fun" |
|
CATEGORY = "O/deprecated/string/operations" |
|
|
|
@staticmethod |
|
def fun(string, old, new): |
|
return ({"string": string["string"].replace(old["string"], new["string"])},) |
|
|
|
|
|
|
|
|
|
class LatentUpscaleMultiply_deprecated: |
|
""" |
|
Upscale the latent code by multiplying the width and height by a factor |
|
""" |
|
upscale_methods = ["nearest-exact", "bilinear", "area"] |
|
crop_methods = ["disabled", "center"] |
|
|
|
@classmethod |
|
def INPUT_TYPES(cls): |
|
return { |
|
"required": { |
|
"samples": ("LATENT",), |
|
"upscale_method": (cls.upscale_methods,), |
|
"WidthMul": ("FLOAT", {"default": 1.25, "min": 0.0, "max": 10.0, "step": 0.1}), |
|
"HeightMul": ("FLOAT", {"default": 1.25, "min": 0.0, "max": 10.0, "step": 0.1}), |
|
"crop": (cls.crop_methods,), |
|
} |
|
} |
|
|
|
RETURN_TYPES = ("LATENT",) |
|
FUNCTION = "upscale" |
|
CATEGORY = "O/deprecated/latent" |
|
|
|
def upscale(self, samples, upscale_method, WidthMul, HeightMul, crop): |
|
s = samples.copy() |
|
x = samples["samples"].shape[3] |
|
y = samples["samples"].shape[2] |
|
|
|
new_x = int(x * WidthMul) |
|
new_y = int(y * HeightMul) |
|
print(f"upscale from ({x*8},{y*8}) to ({new_x*8},{new_y*8})") |
|
|
|
def enforce_mul_of_64(d): |
|
leftover = d % 8 |
|
if leftover != 0: |
|
d += 8 - leftover |
|
return d |
|
|
|
s["samples"] = comfy.utils.common_upscale( |
|
samples["samples"], enforce_mul_of_64( |
|
new_x), enforce_mul_of_64(new_y), upscale_method, crop |
|
) |
|
return (s,) |
|
|
|
|
|
|
|
|
|
|
|
NODE_CLASS_MAPPINGS = { |
|
|
|
"ChatGPT _O": O_ChatGPT_deprecated, |
|
"Chat_Message_fromString _O": openAi_chat_message_STR_deprecated, |
|
"compine_chat_messages _O": openAi_chat_messages_Combine_deprecated, |
|
"Chat_Completion _O": openAi_chat_completion_deprecated, |
|
"create_image _O": openAi_Image_create_deprecated, |
|
"String _O": O_String_deprecated, |
|
"Debug String _O": DebugString_deprecated, |
|
"concat Strings _O": concat_String_deprecated, |
|
"trim String _O": trim_String_deprecated, |
|
"replace String _O": replace_String_deprecated, |
|
"replace String advanced _O": replace_String_advanced_deprecated, |
|
"string2Image _O": string2Image_deprecated, |
|
"CLIPStringEncode _O": CLIPStringEncode_deprecated, |
|
"CLIPStringEncode _O": CLIPStringEncode_deprecated, |
|
"LatentUpscaleMultiply": LatentUpscaleMultiply_deprecated, |
|
} |
|
|