DALL·E node

This commit is contained in:
🐦
2023-10-12 17:57:57 +11:00
parent 676462bae3
commit 7661cf337a
5 changed files with 94 additions and 5 deletions
+3
View File
@@ -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",
}
+64
View File
@@ -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
+4 -5
View File
@@ -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)
---
![img.png](img.png)
![img_1.png](img_1.png)
---
Binary file not shown.

After

Width:  |  Height:  |  Size: 456 KiB

+23
View File
@@ -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)