This commit is contained in:
justumen
2024-09-10 14:55:35 +02:00
parent 7defc44cde
commit 98b322956d
6 changed files with 90 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
# 🔗 Comfyui : Bjornulf_custom_nodes v0.19 🔗
# 🔗 Comfyui : Bjornulf_custom_nodes v0.21 🔗
# Dependencies
@@ -29,14 +29,17 @@
- **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.)
@@ -287,3 +290,4 @@ 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.
❗ For this node to work properly, you need to enable the dev/api mode in ComfyUI. (You can do that in the settings)

View File

@@ -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",

View File

@@ -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]

View File

@@ -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
# 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
while True:
# Determine the filename
filename = f"{output_dir}api_{counter:05d}.png"
if not os.path.exists(filename):
break
counter += 1
# Prepare metadata
metadata = PngInfo()
@@ -61,3 +70,43 @@ class SaveBjornulfLobeChat:
print(f"Image saved as: {filename}")
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}")

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

21
write_text_console.py Normal file
View File

@@ -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,)}