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

@@ -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"}]}}
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}")