From 56c2eb04d6cf797aab38723cc4bca3df63d73d63 Mon Sep 17 00:00:00 2001 From: matan Date: Wed, 7 Aug 2024 00:58:35 +0300 Subject: [PATCH] Allow default image in image node --- .gitignore | 6 +++++- README.md | 7 +++++-- examples/package-lock.json | 37 +++++++++++++++++++++++++++++++++++++ examples/package.json | 15 +++++++++++++++ examples/websocket.js | 7 +++---- nodes.py | 16 +++++++++++----- requirements.txt | 3 ++- 7 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 examples/package-lock.json create mode 100644 examples/package.json diff --git a/.gitignore b/.gitignore index 763624e..2ec88bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -__pycache__/* \ No newline at end of file +__pycache__/* +/examples/node_modules/* + +.idea +.vscode \ No newline at end of file diff --git a/README.md b/README.md index 7c3b499..338e41f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -## Updated to include "Serving Input Image" node, you can now send image attachments via Discord for img2img generation. - # ComfyUI Serving Toolkit Welcome to the ComfyUI Serving Toolkit, a powerful tool for serving image generation workflows in Discord and other platforms (soon). This toolkit is designed to simplify the process of serving your ComfyUI workflow, making image generation bots easier than ever before. @@ -55,6 +53,11 @@ Outputs: This will connect to a websocket and wait for JSON of {_requestId, prompt, arguments} and will return a json of {_requestId, base64_img} You can see an example ws server over at examples/websocket.js +to run it you would need to navigate to the examples folder and run these commands: +``` +npm install +node websocket.js +``` Inputs: * websocket_url - the url of the websocket you connect to, if you use the example it will be ws://localhost:8080 diff --git a/examples/package-lock.json b/examples/package-lock.json new file mode 100644 index 0000000..3f7b918 --- /dev/null +++ b/examples/package-lock.json @@ -0,0 +1,37 @@ +{ + "name": "websocket-example", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "websocket-example", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "ws": "^8.18.0" + } + }, + "node_modules/ws": { + "version": "8.18.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + } + } +} diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 0000000..0bc02c6 --- /dev/null +++ b/examples/package.json @@ -0,0 +1,15 @@ +{ + "name": "websocket-example", + "version": "1.0.0", + "main": "websocket.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "ws": "^8.18.0" + } +} diff --git a/examples/websocket.js b/examples/websocket.js index 92a8105..4984cfb 100644 --- a/examples/websocket.js +++ b/examples/websocket.js @@ -35,12 +35,11 @@ function saveBase64Image(base64String, filePath) { } }); } -console.log("Listening on 8080") +console.log("Listening on 8080. Use ws://localhost:8080 as the websocket URL in the websocket serving node"); function sendMessage(message) { wss.clients.forEach((client) => { - console.log("Messaging Everyone a hi") - + console.log("Sending the prompt - ", message.prompt) if (client.readyState === WebSocket.OPEN) { client.send(message); } @@ -51,5 +50,5 @@ function sendMessage(message) { let i = 0 setInterval(() => sendMessage(JSON.stringify({ _requestId: ++i, - prompt: "Cow" + prompt: "Robot saying 'it works!'", })), 5000) \ No newline at end of file diff --git a/nodes.py b/nodes.py index 334340e..881a588 100644 --- a/nodes.py +++ b/nodes.py @@ -389,9 +389,7 @@ class WebSocketServing(): "_requestId": data["_requestId"] } self.ws.send(json.dumps(response)) - data["serve_image_function"] = serve_image_function - data["serve_multi_image_function"] = serve_multi_image_function - data["serve_text_function"] = lambda text: self.ws.send(json.dumps({"text": text, "_requestId": data["_requestId"]})) + def serve_image_function(image, frame_duration): image_file = tensorToImageConversion(image, frame_duration) base64_img = base64.b64encode(image_file.read()).decode('utf-8') @@ -400,7 +398,11 @@ class WebSocketServing(): "_requestId":data["_requestId"] # It's assumed that it will exist. } self.ws.send(json.dumps(response)) + data["serve_image_function"] = serve_image_function + data["serve_multi_image_function"] = serve_multi_image_function + data["serve_text_function"] = lambda text: self.ws.send( + json.dumps({"text": text, "_requestId": data["_requestId"]})) return (data,) @@ -413,7 +415,9 @@ class ServingInputImage: return { "required": { "serving_config": ("SERVING_CONFIG",), - } + + }, + "default_image": ("IMAGE",) } RETURN_TYPES = ("IMAGE",) @@ -433,9 +437,11 @@ class ServingInputImage: image = torch.from_numpy(result)[None,] return image - def out(self, serving_config): + def out(self, serving_config, default_image = None): attachment_url_key = "attachment_url_0" if attachment_url_key not in serving_config: + if default_image is not None: + return (default_image,) raise ValueError("No attachment found in serving_config") attachment_url = serving_config[attachment_url_key] diff --git a/requirements.txt b/requirements.txt index cdd0bc9..661a6a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ discord.py websocket-client -rel \ No newline at end of file +rel +opencv-python \ No newline at end of file