Archived
implemented full binary progress system using websockets
This commit is contained in:
1 parent
43ebd851ad
commit
6711e006c1
3 files changed
+34
-26
No files matched your search
@@ -1,3 +1,6 @@
|
||||
# User data
|
||||
/resources
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[codz]
|
||||
|
||||
+27
-22
@@ -2,38 +2,43 @@ from server import PromptServer
|
||||
from .progress import add_progress_hook
|
||||
from .routes import SetupRoutes
|
||||
from .workflows import *
|
||||
import base64
|
||||
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):
|
||||
# print(f"Progress: {value}/{total}, Prompt: {prompt_id}, Node: {node_id}, Preview: {preview_image}")
|
||||
# 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)
|
||||
|
||||
# Convert preview image to base64 if available
|
||||
preview_base64 = 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:
|
||||
pil_image = preview_image[1] # Extract the PIL Image
|
||||
else:
|
||||
pil_image = preview_image
|
||||
|
||||
buffer = BytesIO()
|
||||
pil_image.save(buffer, format='JPEG', quality=80, optimize=True)
|
||||
preview_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
||||
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 encoding preview image: {e}")
|
||||
|
||||
# Broadcast to ALL WebSocket clients using ComfyUI's system (sid=None means broadcast to all)
|
||||
PromptServer.instance.send_sync("imagelab_progress", {
|
||||
'value': value,
|
||||
'total': total,
|
||||
'preview_image': preview_base64, # base64 encoded JPEG or None
|
||||
'prompt_id': prompt_id,
|
||||
'node_id': node_id,
|
||||
'progress_percent': round((value / total * 100), 2) if total > 0 else 0
|
||||
}, sid=None)
|
||||
print(f"[ImageLab] Error sending preview image: {e}")
|
||||
|
||||
add_progress_hook(my_progress_callback)
|
||||
|
||||
|
||||
@@ -9,14 +9,14 @@ from .workflows import get_workflow, get_workflow_metadata, list_workflows
|
||||
def SetupRoutes(server: PromptServer):
|
||||
"""Register API routes with the ComfyUI server."""
|
||||
|
||||
@server.instance.routes.get("/external-api/workflows")
|
||||
@server.instance.routes.get("/imagelab/workflows")
|
||||
async def list_available_workflows(request):
|
||||
"""List all available workflows with metadata."""
|
||||
return web.json_response({
|
||||
'workflows': list_workflows()
|
||||
})
|
||||
|
||||
@server.instance.routes.get("/external-api/workflow/{workflow_name}/metadata")
|
||||
@server.instance.routes.get("/imagelab/workflow/{workflow_name}/metadata")
|
||||
async def get_workflow_info(request):
|
||||
"""Get metadata for a specific workflow."""
|
||||
workflow_name = request.match_info['workflow_name']
|
||||
@@ -33,7 +33,7 @@ def SetupRoutes(server: PromptServer):
|
||||
'error': str(e)
|
||||
}, status=404)
|
||||
|
||||
@server.instance.routes.post("/external-api/workflow/{workflow_name}")
|
||||
@server.instance.routes.post("/imagelab/workflow/{workflow_name}")
|
||||
async def execute_workflow(request):
|
||||
"""Execute a specific workflow by name."""
|
||||
workflow_name = request.match_info['workflow_name']
|
||||
@@ -71,7 +71,7 @@ def SetupRoutes(server: PromptServer):
|
||||
'error': f"Workflow execution failed: {str(e)}"
|
||||
}, status=500)
|
||||
|
||||
@server.instance.routes.get("/external-api/hello")
|
||||
@server.instance.routes.get("/imagelab/hello")
|
||||
async def get_hello(request):
|
||||
"""Test endpoint."""
|
||||
return web.json_response({
|
||||
|
||||
Reference in new issue
Block a user