From 4aa28806d52f66a2639e789b0ff099c4e5fab330 Mon Sep 17 00:00:00 2001 From: Rod Brandon Date: Wed, 6 Dec 2023 15:26:22 -0700 Subject: [PATCH] Updates to __init__, style_prompt, config.json --- __init__ .py | 53 ++++++++++++++++++++++++++++++ __init__.py | 43 ------------------------ config.json | 7 ++-- style_prompt.py | 87 ++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 129 insertions(+), 61 deletions(-) create mode 100644 __init__ .py delete mode 100644 __init__.py diff --git a/__init__ .py b/__init__ .py new file mode 100644 index 0000000..4b31df8 --- /dev/null +++ b/__init__ .py @@ -0,0 +1,53 @@ + +import os +import json +import sys +#new +import shutil +import json + + +config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json") +backup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "bkup") +backup_config_path = os.path.join(backup_dir, "config.json") +#debug +#print(sys.path) +## Check if config file exists in ComfyUI_plush directory +if not os.path.isfile(config_file_path): + # Check if there's a backup in the bkup directory + if os.path.isfile(backup_config_path): + # Copy the backup config.json to ComfyUI_plush directory + shutil.copy(backup_config_path, config_file_path) + else: + pass + + #Try and open the JSON file and deal with a decode error +config_bad = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.bad") +try: + with open(config_file_path, 'r') as config_file: + config_data = json.load(config_file) + #if the config.json is corrupt, rename it config.bad and copy the backup in its place +except json.JSONDecodeError as e: + print(f"Error decoding JSON in, attempting to replace corrupt file {config_file_path}: {e}") + os.rename(config_file_path, config_bad) + shutil.copy(backup_config_path, config_file_path) + + + + """ config = { + "autoUpdate": True, + "branch": "dev", + "openAI_API_Key": "sk-#########################################" + } + with open(config_file_path, "w") as f: + json.dump(config, f, indent=4) """ +#******************************************************************************** +from . import style_prompt + +NODE_CLASS_MAPPINGS = style_prompt.NODE_CLASS_MAPPINGS +NODE_DISPLAY_NAME_MAPPINGS = style_prompt.NODE_DISPLAY_NAME_MAPPINGS +#from .style_prompt import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS +#from sty import NODE_CLASS_MAPPINGS + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] + diff --git a/__init__.py b/__init__.py deleted file mode 100644 index b009b23..0000000 --- a/__init__.py +++ /dev/null @@ -1,43 +0,0 @@ -#In ComfyUI_plush Lets puch this add to repostitory -from . import style_prompt - -NODE_CLASS_MAPPINGS = style_prompt.NODE_CLASS_MAPPINGS -NODE_DISPLAY_NAME_MAPPINGS = style_prompt.NODE_DISPLAY_NAME_MAPPINGS -#from .style_prompt import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS -#from sty import NODE_CLASS_MAPPINGS - -__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] - - - -""" import os -import json -import sys -#new -import shutil """ - - -""" config_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.json") -backup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "bkup") -#debug -#print(sys.path) -## Check if config file exists in ComfyUI_plush directory -if not os.path.isfile(config_file_path): - # Check if there's a backup in the bkup directory - backup_config_path = os.path.join(backup_dir, "config.json") - - if os.path.isfile(backup_config_path): - # Copy the backup config.json to ComfyUI_plush directory - shutil.copy(backup_config_path, config_file_path) - else: - # Create config file in ComfyUI_plush directory if no backup exists - pass """ - -""" config = { - "autoUpdate": True, - "branch": "dev", - "openAI_API_Key": "sk-#########################################" - } - with open(config_file_path, "w") as f: - json.dump(config, f, indent=4) """ -#******************************************************************************** diff --git a/config.json b/config.json index 6a18d0c..2e9aa9f 100644 --- a/config.json +++ b/config.json @@ -1,12 +1,13 @@ { - "key":"sk-#########", + "key":"sk-##########", "instruction":"Act as a creative agent who generates a terse but highly creative image prompt derived from the prompt I send you. Include descriptive visual elements of the subject, lighting and surroundings. Specify {} as the artistic style at beginning of the sentence follwed by 'of', use descriptive elements that pertain to this artistic style. Include no more than {} elements presented as discrete descriptors in one long sentence without story. Put the most important descriptive elements at the beginning of the sentence. ", "example":"Female warrior dressed in animal skins, stormy skies, blood-smeared face, wild flowing hair, gripping a sharp spear", "style":[ - "Abstract Painting", + "Abstract Expressionism", "Acrylic Painting", "Anime", "Art Deco", + "Biomorphic Abstraction", "Black and White Photograph", "Cartoon", "Charcoal Sketch", @@ -20,6 +21,7 @@ "Digital Art", "Fantasy Art", "Fashion Art", + "Fashion Sketch", "Fish-Eye lens Photograph", "Goth Art", "Graffiti", @@ -29,6 +31,7 @@ "Impressionist Painting", "Josei Anime", "Long Exposure Photograph", + "Low Key Photograph", "Macro Photograph", "Manga", "Metal Sculpture", diff --git a/style_prompt.py b/style_prompt.py index 40f21cb..db5c753 100644 --- a/style_prompt.py +++ b/style_prompt.py @@ -100,13 +100,52 @@ class Enhancer: #build the prompt from user input if self.eFig.instruction.count("{}") >= 2: instruc = self.eFig.instruction.format(style, elements) + elif self.eFig.instruction.count("{}") == 1: + instruc - self.eFig.instruction.format(style) else: instruc = self.eFig.instruction if artist: instruc = instruc + " Include an artist who works in the specifed artistic style by placing the artist's name at the end of the sentence prefaced by 'style of'. " - return(instruc) + + + def cgptRequest(self, GPTmodel, client, creative_latitude, tokens, instruction, example, prompt): + #Requet a prompt or backgrounder from ChatGPT + print(f"Talking to model: {GPTmodel}") + try: + #call the ChatGPT API with the user selections, instruction, example and prompt + chat_completion = client.chat.completions.create( + model = GPTmodel, + temperature = creative_latitude, + max_tokens = tokens, + stream = False, + messages = [ + {"role": "system", "content": instruction}, + {"role": "assistant", "content": example}, + {"role": "user", "content": prompt,}, + ], + ) + except openai.APIConnectionError as e: + print("Server connection error: {e.__cause__}") # from httpx. + raise + except openai.RateLimitError as e: + print(f"OpenAI RATE LIMIT error {e.status_code}: (e.response)") + raise + except openai.APIStatusError as e: + print(f"OpenAI STATUS error {e.status_code}: (e.response)") + raise + except openai.BadRequestError as e: + print(f"OpenAI BAD REQUEST error {e.status_code}: (e.response)") + raise + except Exception as e: + print(f"An unexpected error occurred: {e}") + raise + + #First of choices [0] content is the generated prompt + CGPT_prompt = chat_completion.choices[0].message.content + + return(CGPT_prompt) @classmethod @@ -115,19 +154,20 @@ class Enhancer: return { "required": { - "GPTmodel": (["gpt-3.5-turbo","gpt-4"], ), + "GPTmodel": (["gpt-3.5-turbo","gpt-4"],{"default": "gpt-4"} ), "creative_latitude" : ("FLOAT", {"max": 1.2, "min": 0.1, "step": 0.1, "display": "number", "default": 0.7}), - "tokens" : ("INT", {"max": 8000, "min": 100, "step": 10, "default": 2000, "display": "number"}), + "tokens" : ("INT", {"max": 8000, "min": 50, "step": 10, "default": 2000, "display": "number"}), "prompt": ("STRING",{"multiline": True, "forceInput": True}), "example" : ("STRING", {"forceInput": True, "multiline": True}), "style": (iFig.style,{"default": "Photograph"}), - "artist" : ("BOOLEAN", {"default": False}), - "max_elements" : ("INT", {"max": 20, "min": 3, "step": 1, "default": 10, "display": "number"}), + "artist" : ("BOOLEAN", {"default": True}), + "max_elements" : ("INT", {"max": 20, "min": 3, "step": 1, "default": 10, "display": "number"}), + "style_info" : ("BOOLEAN", {"default": False}) }, } - RETURN_TYPES = ("STRING", "STRING") - RETURN_NAMES = ("CGPTprompt", "CGPTinstruction") + RETURN_TYPES = ("STRING", "STRING", "STRING") + RETURN_NAMES = ("CGPTprompt", "CGPTinstruction","Style Info") FUNCTION = "gogo" @@ -136,7 +176,7 @@ class Enhancer: CATEGORY = "Plush" - def gogo(self, GPTmodel, creative_latitude, tokens, example, prompt, style, artist, max_elements): + def gogo(self, GPTmodel, creative_latitude, tokens, example, prompt, style, artist, max_elements, style_info): #If no example text was provided by the user, use my default if not example: @@ -148,9 +188,22 @@ class Enhancer: enH = Enhancer() #build instruction based on user input - instruction = enH.build_instruction(style, max_elements, artist) + instruction = enH.build_instruction(style, max_elements, artist) - print(f"Talking to model: {GPTmodel}") + CGPT_styleInfo = "" + + if style_info: + #User has request information about the art style. GPT will provide it + sty_prompt = "Give an 150 word backgrounder on the art style: {}. Starting with describing what it is, include information about its history and which artists represent the style." + sty_prompt = sty_prompt.format(style) + sty_instruction = "" + sty_example = "" + + CGPT_styleInfo = self.cgptRequest(GPTmodel, client, creative_latitude, tokens, sty_instruction, sty_example, sty_prompt) + + CGPT_prompt = self.cgptRequest(GPTmodel, client, creative_latitude, tokens, instruction, example, prompt) + + """ print(f"Talking to model: {GPTmodel}") try: #call the ChatGPT API with the user selections, instruction, example and prompt chat_completion = client.chat.completions.create( @@ -181,21 +234,23 @@ class Enhancer: raise #get the prompt as item[0] from the completion object - CGPT_prompt = chat_completion.choices[0].message.content - print("Tokens: " + str(tokens)) - print("Creativity: " + str(creative_latitude)) + CGPT_prompt = chat_completion.choices[0].message.content """ + # ***************************************** + # print("Tokens: " + str(tokens)) + # print("Creativity: " + str(creative_latitude)) #print("Prompt: " + CGPT_prompt) #print("Instruction: " + instruction) - return (CGPT_prompt, instruction) + return (CGPT_prompt, instruction, CGPT_styleInfo) #debug testing """ Enh = Enhancer() Enh.INPUT_TYPES() -Enh.gogo("gpt-4", 0.7, 2000, "", "A beautiful eagle soaring above a verdant forest", "Photograph", True, 10) - """ +test_resp = Enh.gogo("gpt-4", 0.7, 2000, "", "A beautiful eagle soaring above a verdant forest", "Biomorphic Abstraction", True, 10,True) +print (test_resp[2]) """ + class DalleImage: #Accept a user prompt and parameters to produce a Dall_e generated image