diff --git a/__init__.py b/__init__.py index 1752e0a..7b25c0e 100644 --- a/__init__.py +++ b/__init__.py @@ -2,12 +2,14 @@ from .cyberdolphin_gradio import CyberDolphinGradioApi from .cyberdolphin_openai_advanced import CyberdolphinOpenAIAdvanced from .cyberdolphin_openai_simple import CyberdolphinOpenAISimple from .cyberdolphin_openai_compatible import CyberdolphinOpenAICompatible +from .cyberdolphin_imageneering import CyberDolphinImageneering NODE_CLASS_MAPPINGS = { "🐬 Gradio ChatInterface": CyberDolphinGradioApi, "🐬 OpenAI Simple": CyberdolphinOpenAISimple, "🐬 OpenAI Advanced": CyberdolphinOpenAIAdvanced, "🐬 OpenAI Compatible": CyberdolphinOpenAICompatible, + "🐬 OpenAI DALL·E": CyberDolphinImageneering, } NODE_DISPLAY_NAME_MAPPINGS = { @@ -15,4 +17,5 @@ NODE_DISPLAY_NAME_MAPPINGS = { "CyberDolphin GPT-3.5 (Simple)": "🐬 CyberDolphin GPT-3.5 (Simple)", "CyberDolphin OpenAI (Advanced)": "🐬 CyberDolphin OpenAI (Advanced)", "CyberDolphin OpenAI Compatible": "🐬 CyberDolphin OpenAI Compatible", + "CyberDolphin OpenAI DALL·E": "🐬 CyberDolphin OpenAI DALL·E", } diff --git a/cyberdolphin_imageneering.py b/cyberdolphin_imageneering.py new file mode 100644 index 0000000..20f2b3f --- /dev/null +++ b/cyberdolphin_imageneering.py @@ -0,0 +1,64 @@ +import hashlib +import os + +from PIL import Image, ImageOps +import torch +import numpy as np + +import folder_paths +from .openai_client import convert_bson_to_image, OpenAiClient +from .settings import load_settings +import openai + + +class CyberDolphinImageneering: + @classmethod + def INPUT_TYPES(s): + return {"required": { + "prompt": ('STRING', {'default': 'a white siamese cat'}), + "size": (["256x256", "512x512", "1024x1024"], {'default': "1024x1024"}), + }} + + CATEGORY = "🐬 CyberDolphin" + RETURN_TYPES = ("IMAGE", "MASK") + FUNCTION = "load_image" + + def load_image(self, prompt: str, size: str): + """ + Loads an image from api.openai.com. + https://platform.openai.com/docs/api-reference/images/create + + Args: + prompt: A text description of the desired image. The maximum length is 1000 characters. + size: Must be one of: 256x256, 512x512, 1024x1024 + + Returns: + + """ + i = OpenAiClient.image_create(prompt=prompt, size=size) + i = ImageOps.exif_transpose(i) + + image = i.convert("RGB") + 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") + return (image, mask.unsqueeze(0)) + + @classmethod + def IS_CHANGED(s, image): + image_path = folder_paths.get_annotated_filepath(image) + m = hashlib.sha256() + with open(image_path, 'rb') as f: + m.update(f.read()) + return m.digest().hex() + + # @classmethod + # def VALIDATE_INPUTS(s, image): + # # if not folder_paths.exists_annotated_filepath(image): + # # return "Invalid image file: {}".format(image) + # + # return True diff --git a/examples/README.md b/examples/README.md index bf2d7a3..b26c953 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,11 +1,10 @@ # Examples ## Cyber Dolphin -![img.png](img.png) -[dolphin-openai workflow](./dolphin-openai.workflow.json) --- -![Comfy-PNG](./Gpt-3.5-turbo-gen_00005_.png) -_(comfyui png depends on a stable api) ---- \ No newline at end of file +![img.png](img.png) +![img_1.png](img_1.png) + +--- diff --git a/examples/img_1.png b/examples/img_1.png new file mode 100644 index 0000000..7f5d0d1 Binary files /dev/null and b/examples/img_1.png differ diff --git a/openai_client.py b/openai_client.py index 58acaf4..c824b6b 100644 --- a/openai_client.py +++ b/openai_client.py @@ -1,3 +1,4 @@ +import PIL.Image import openai from custom_nodes.cyberdolphin.settings import api_settings @@ -18,8 +19,30 @@ def validation(temperature: float, top_p: float = None) -> list[str]: return errors_list +def convert_bson_to_image(image_bson: str): + import base64 + import io + from PIL import Image + image_bytes = base64.b64decode(image_bson) + image = Image.open(io.BytesIO(image_bytes)) + return image + + class OpenAiClient: + @staticmethod + def image_create(prompt: str, size: str = "1024x1024", api='openai') -> PIL.Image.Image: + openai.api_base, openai.api_key, openai.organization = api_settings(api) + response = openai.Image.create( + n=1, + size=size, + prompt=prompt, + response_format="b64_json" + ) + image_bson = response['data'][0]['b64_json'] + i = convert_bson_to_image(image_bson) + return i + @staticmethod def complete(key: str, model: str, temperature: float, top_p: float, system_content: str, user_content: str): errors = validation(temperature, top_p)