File size: 17,076 Bytes
ef7f3ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# Developed by Omar - https://github.com/omar92
# https://civitai.com/user/omar92
# discord: Omar92#3374
###
#
# All nodes in this file are deprecated and will be removed in the future , i left them here for backward compatibility
#
###
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
# -------------------------------------------------------------------------------------------
# region deprecated
# region openAITools
class O_ChatGPT_deprecated:
"""
this node is based on the openAI GPT-3 API to generate propmpts using the AI
"""
# Define the input types for the node
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
# Multiline string input for the prompt
"prompt": ("STRING", {"multiline": True}),
# File input for the API key
"api_key_file": ("STRING", {"file": True, "default": "api_key.txt"})
}
}
RETURN_TYPES = ("STR",) # Define the return type of the node
FUNCTION = "fun" # Define the function name for the node
CATEGORY = "O/deprecated/OpenAI" # Define the category for the node
def fun(self, api_key_file, prompt):
self.install_openai() # Install the OpenAI module if not already installed
import openai # Import the OpenAI module
# Get the API key from the file
api_key = self.get_api_key(api_key_file)
openai.api_key = api_key # Set the API key for the OpenAI module
# Create a chat completion using the OpenAI module
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}
]
)
# Get the answer from the chat completion
answer = completion["choices"][0]["message"]["content"]
return (
{
"string": answer, # Return the answer as a string
},
)
# Helper function to get the API key from the file
def get_api_key(self, api_key_file):
custom_nodes_dir = './custom_nodes/' # Define the directory for the file
with open(custom_nodes_dir+api_key_file, 'r') as f: # Open the file and read the API key
api_key = f.read().strip()
return api_key # Return the API key
# Helper function to install the OpenAI module if not already installed
def install_openai(self):
try:
importlib.import_module('openai')
except ImportError:
import pip
pip.main(['install', 'openai'])
# region advanced
"""
this node will load openAI model
"""
# Define the input types for the node
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
# File input for the API key
"api_key_file": ("STRING", {"file": True, "default": "api_key.txt"})
}
}
RETURN_TYPES = ("OPENAI",) # Define the return type of the node
FUNCTION = "fun" # Define the function name for the node
CATEGORY = "O/OpenAI/Advanced" # Define the category for the node
def fun(self, api_key_file):
self.install_openai() # Install the OpenAI module if not already installed
import openai # Import the OpenAI module
# Get the API key from the file
api_key = self.get_api_key(api_key_file)
openai.api_key = api_key # Set the API key for the OpenAI module
return (
{
"openai": openai, # Return openAI model
},
)
# Helper function to install the OpenAI module if not already installed
def install_openai(self):
try:
importlib.import_module('openai')
except ImportError:
import pip
pip.main(['install', 'openai'])
# Helper function to get the API key from the file
def get_api_key(self, api_key_file):
custom_nodes_dir = './custom_nodes/' # Define the directory for the file
with open(custom_nodes_dir+api_key_file, 'r') as f: # Open the file and read the API key
api_key = f.read().strip()
return api_key # Return the API key
# region ChatGPT
class openAi_chat_message_STR_deprecated:
"""
create chat message for openAI chatGPT
"""
# Define the input types for the node
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"role": (["user", "assistant", "system"], {"default": "user"}),
"content": ("STR",),
}
}
# Define the return type of the node
RETURN_TYPES = ("OPENAI_CHAT_MESSAGES",)
FUNCTION = "fun" # Define the function name for the node
# Define the category for the node
CATEGORY = "O/deprecated/OpenAI/Advanced/ChatGPT"
def fun(self, role, content):
return (
{
"messages": [{"role": role, "content": content["string"], }]
},
)
# endregion ChatGPT
# region Image
class openAi_chat_messages_Combine_deprecated:
"""
compine chat messages into 1 tuple
"""
# Define the input types for the node
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"message1": ("OPENAI_CHAT_MESSAGES", ),
"message2": ("OPENAI_CHAT_MESSAGES", ),
}
}
# Define the return type of the node
RETURN_TYPES = ("OPENAI_CHAT_MESSAGES",)
FUNCTION = "fun" # Define the function name for the node
# Define the category for the node
CATEGORY = "O/deprecated/OpenAI/Advanced/ChatGPT"
def fun(self, message1, message2):
messages = message1["messages"] + \
message2["messages"] # compine messages
return (
{
"messages": messages
},
)
class openAi_Image_create_deprecated:
"""
create image using openai
"""
# Define the input types for the node
@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"}),
}
}
# Define the return type of the node
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "fun" # Define the function name for the node
OUTPUT_NODE = True
# Define the category for the node
CATEGORY = "O/deprecated/OpenAI/Advanced/Image"
def fun(self, openai, prompt, number, size):
# Create a chat completion using the OpenAI module
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_np = np.transpose(image_np, (2, 0, 1))
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
"""
# Define the input types for the node
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"openai": ("OPENAI", ),
"model": ("STRING", {"multiline": False, "default": "gpt-3.5-turbo"}),
"messages": ("OPENAI_CHAT_MESSAGES", ),
}
}
# Define the return type of the node
RETURN_TYPES = ("STR", "OPENAI_CHAT_COMPLETION",)
FUNCTION = "fun" # Define the function name for the node
OUTPUT_NODE = True
# Define the category for the node
CATEGORY = "O/deprecated/OpenAI/Advanced/ChatGPT"
def fun(self, openai, model, messages):
# Create a chat completion using the OpenAI module
openai = openai["openai"]
completion = openai.ChatCompletion.create(
model=model,
messages=messages["messages"]
)
# Get the answer from the chat completion
content = completion["choices"][0]["message"]["content"]
return (
{
"string": content, # Return the answer as a string
},
{
"completion": completion, # Return the chat completion
}
)
# endregion Image
# endregion advanced
# endregion openAI
# region StringTools
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))
# need to use the inner `img.im.paste` due to `getmask` returning a core
image.im.paste(font_color, (0, 0) + mask_image.size, mask_image)
# Convert the PIL Image to a tensor
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"]), {}]], )
# region String/operations
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)},)
# replace a string with another string
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"])},)
# endregion
# endregion
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,)
# endregion deprecated
# Define the node class mappings
NODE_CLASS_MAPPINGS = {
# deprecated
"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,
}
|