Archived
69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
from server import PromptServer
|
|
from .progress import add_progress_hook
|
|
from .routes import SetupRoutes
|
|
from .workflows import *
|
|
from . import resources
|
|
from io import BytesIO
|
|
import json
|
|
|
|
# Custom binary event types (use high numbers to avoid conflicts)
|
|
IMAGELAB_PROGRESS_EVENT = 100
|
|
IMAGELAB_PREVIEW_EVENT = 101
|
|
|
|
# Your custom progress callback
|
|
def my_progress_callback(value, total, preview_image, prompt_id, node_id):
|
|
# Send progress data as binary JSON
|
|
progress_data = {
|
|
'value': value,
|
|
'total': total,
|
|
'prompt_id': prompt_id,
|
|
'node_id': node_id,
|
|
'progress_percent': round((value / total * 100), 2) if total > 0 else 0
|
|
}
|
|
progress_bytes = json.dumps(progress_data).encode('utf-8')
|
|
PromptServer.instance.send_sync(IMAGELAB_PROGRESS_EVENT, progress_bytes, sid=None)
|
|
|
|
# Send preview image separately if available
|
|
if preview_image is not None:
|
|
try:
|
|
# preview_image is a tuple: (format, PIL.Image, max_size)
|
|
if isinstance(preview_image, tuple) and len(preview_image) >= 2:
|
|
image_format = preview_image[0] # 'JPEG' or 'PNG'
|
|
pil_image = preview_image[1]
|
|
|
|
# Convert PIL image to bytes
|
|
buffer = BytesIO()
|
|
pil_image.save(buffer, format=image_format, quality=95 if image_format == 'JPEG' else None)
|
|
image_bytes = buffer.getvalue()
|
|
|
|
# Send as binary message with integer event type
|
|
PromptServer.instance.send_sync(IMAGELAB_PREVIEW_EVENT, image_bytes, sid=None)
|
|
except Exception as e:
|
|
print(f"[ImageLab] Error sending preview image: {e}")
|
|
|
|
add_progress_hook(my_progress_callback)
|
|
|
|
# Register API routes
|
|
SetupRoutes(PromptServer.instance)
|
|
|
|
# Optional: Index all new models at startup (set to True to enable)
|
|
AUTO_INDEX_ON_STARTUP = True
|
|
|
|
if AUTO_INDEX_ON_STARTUP:
|
|
import threading
|
|
def index_on_startup():
|
|
print("[ImageLab] Starting background indexing of new models...")
|
|
try:
|
|
indexed = resources.index_all_new_models()
|
|
if indexed:
|
|
print(f"[ImageLab] Indexed models: {indexed}")
|
|
else:
|
|
print("[ImageLab] No new models to index")
|
|
except Exception as e:
|
|
print(f"[ImageLab] Error during startup indexing: {e}")
|
|
|
|
# Run in background thread to not block startup
|
|
threading.Thread(target=index_on_startup, daemon=True).start()
|
|
|
|
NODE_CLASS_MAPPINGS = {}
|
|
NODE_DISPLAY_NAME_MAPPINGS = {} |