diff --git a/README.md b/README.md index 698e992..976d8b3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 🔗 Comfyui : Bjornulf_custom_nodes v0.19 🔗 +# 🔗 Comfyui : Bjornulf_custom_nodes v0.21 🔗 # Dependencies @@ -29,16 +29,19 @@ - **v0.17**: New loop node, combine by lines. - **v0.18**: New loop node, Free VRAM hack - **v0.19**: Changes for save to folder node : ignore missing images filenames, will use the highest number found + 1. +- **v0.20**: Changes for lobechat save image : include the code of free VRAM hack + ignore missing images filenames +- **v0.21**: Add a write text node that also display the text in the comfyui console (good for debugging) # 📝 Nodes descriptions ## 1/2 - 👁 + ✒ Show/Write Text ![Show Text](screenshots/write+show_text.png) +![Show Text](screenshots/write_in_console.png) **Description:** -Two simple nodes to write and show text. +Three simple nodes to write and show text. Write node is a textarea where you can write your text. -The show text node will only display the text. (That's why I made it a different color : green, uneditable, display only.) +The show text node will only display the text. (That's why I made it a different color : green, uneditable, display only.) ## 3 - 🔗 Combine Texts ![Combine Texts](screenshots/combine_texts.png) @@ -286,4 +289,5 @@ For me, on launch ComfyUI is using 180MB of VRAM, after my clean up VRAM node it I don't think there is a clean way to do that, so I'm using a hacky way. So, not perfect but better than being stuck at 6GB of VRAM used if I know I won't be using it again... Just connect this node with your workflow, it takes an image as input and return the same image without any changes. -❗ Comfyui is using cache to run faster (like not reloading checkpoints), so only use this free VRAM node when you need it. \ No newline at end of file +❗ Comfyui is using cache to run faster (like not reloading checkpoints), so only use this free VRAM node when you need it. +❗ For this node to work properly, you need to enable the dev/api mode in ComfyUI. (You can do that in the settings) \ No newline at end of file diff --git a/__init__.py b/__init__.py index 1ef840a..a67643e 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,6 @@ from .images_to_video import imagesToVideo from .write_text import WriteText +from .write_text_console import WriteTextInConsole from .write_image_environment import WriteImageEnvironment from .write_image_characters import WriteImageCharacters from .write_image_character import WriteImageCharacter @@ -61,6 +62,7 @@ NODE_CLASS_MAPPINGS = { # "Bjornulf_ClearVRAM": ClearVRAM, "Bjornulf_SaveBjornulfLobeChat": SaveBjornulfLobeChat, "Bjornulf_WriteText": WriteText, + "Bjornulf_WriteTextInConsole": WriteTextInConsole, "Bjornulf_RemoveTransparency": RemoveTransparency, "Bjornulf_GrayscaleTransform": GrayscaleTransform, "Bjornulf_CombineBackgroundOverlay": CombineBackgroundOverlay, @@ -121,6 +123,7 @@ NODE_DISPLAY_NAME_MAPPINGS = { "Bjornulf_SaveText": "💾 Save Text", #Make SaveCharacter, SaveLocation, SaveCamera, SaveAction, SaveClothes, SaveEmotion... "Bjornulf_LoadText": "📥 Load Text", #Make LoadCharacter, LoadLocation, LoadCamera, LoadAction, LoadClothes, LoadEmotion... "Bjornulf_WriteText": "✒ Write Text", + "Bjornulf_WriteTextInConsole": "✒🗔 Write Text (Console too) ", # "Bjornulf_WriteImageEnvironment": "✒ Write Image Environment", # "Bjornulf_WriteImageCharacters": "✒ Write Image Characters", # "Bjornulf_WriteImageCharacter": "✒ Write Image Character", diff --git a/pyproject.toml b/pyproject.toml index 386d910..ed40524 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "bjornulf_custom_nodes" description = "Nodes: Ollama, Text to Speech, Save image for Bjornulf LobeChat, Text with random Seed, Random line from input, Combine images (Background+Overlay alpha), Image to grayscale (black & white), Remove image Transparency (alpha), Resize Image, ..." -version = "0.19" +version = "0.21" license = {file = "LICENSE"} [project.urls] diff --git a/save_bjornulf_lobechat.py b/save_bjornulf_lobechat.py index 496fe62..4ecf082 100644 --- a/save_bjornulf_lobechat.py +++ b/save_bjornulf_lobechat.py @@ -3,6 +3,9 @@ import numpy as np from PIL import Image import json from PIL.PngImagePlugin import PngInfo +import torch +import gc +import requests class SaveBjornulfLobeChat: @classmethod @@ -20,6 +23,9 @@ class SaveBjornulfLobeChat: CATEGORY = "Bjornulf" def save_bjornulf_lobe_chat(self, image, prompt=None, extra_pnginfo=None): + # First, attempt to free VRAM + self.free_vram() + # Ensure the output directory exists output_dir = "./output/BJORNULF_LOBECHAT/" os.makedirs(output_dir, exist_ok=True) @@ -33,13 +39,16 @@ class SaveBjornulfLobeChat: img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)) - # Determine the next available filename - counter = 1 - while True: - filename = f"{output_dir}api_{counter:05d}.png" - if not os.path.exists(filename): - break - counter += 1 + # Find the highest existing file number + existing_files = [f for f in os.listdir(output_dir) if f.startswith('api_') and f.endswith('.png') and f[4:-4].isdigit()] + if existing_files: + highest_num = max(int(f[4:-4]) for f in existing_files) + counter = highest_num + 1 + else: + counter = 1 + + # Determine the filename + filename = f"{output_dir}api_{counter:05d}.png" # Prepare metadata metadata = PngInfo() @@ -60,4 +69,44 @@ class SaveBjornulfLobeChat: print(f"Image saved as: {filename}") - return {"ui": {"images": [{"filename": filename, "type": "output"}]}} \ No newline at end of file + return {"ui": {"images": [{"filename": filename, "type": "output"}]}} + + def free_vram(self): + print("Attempting to free VRAM...") + + # Clear CUDA cache + if torch.cuda.is_available(): + torch.cuda.empty_cache() + print("CUDA cache cleared.") + + # Run garbage collection + collected = gc.collect() + print(f"Garbage collector: collected {collected} objects.") + + # Trigger the HTTP request + self.trigger_http_request() + + def trigger_http_request(self): + url = "http://localhost:8188/prompt" + headers = {"Content-Type": "application/json"} + payload = { + "prompt": { + "3": { + "inputs": {"text": "free VRAM hack"}, + "class_type": "Bjornulf_WriteText", + "_meta": {"title": "✒ Write Text"} + }, + "4": { + "inputs": {"text_value": ["3", 0], "text": "free VRAM hack"}, + "class_type": "Bjornulf_ShowText", + "_meta": {"title": "👁 Show (Text)"} + } + } + } + + try: + response = requests.post(url, headers=headers, data=json.dumps(payload)) + response.raise_for_status() + print("HTTP request triggered successfully") + except requests.exceptions.RequestException as e: + print(f"Failed to trigger HTTP request: {e}") \ No newline at end of file diff --git a/screenshots/write_in_console.png b/screenshots/write_in_console.png new file mode 100644 index 0000000..a04ac09 Binary files /dev/null and b/screenshots/write_in_console.png differ diff --git a/write_text_console.py b/write_text_console.py new file mode 100644 index 0000000..e17f77b --- /dev/null +++ b/write_text_console.py @@ -0,0 +1,21 @@ +import logging +class WriteTextInConsole: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "text": ("STRING", {"multiline": True}), + } + } + + # INPUT_IS_LIST = True + RETURN_TYPES = ("STRING",) + RETURN_NAMES = ("text",) + FUNCTION = "write_text_in_console" + OUTPUT_NODE = True + OUTPUT_IS_LIST = (False,) + CATEGORY = "Bjornulf" + + def write_text_in_console(self, text): + logging.info(f"Text: {text}") + return {"ui": {"text": text}, "result": (text,)}