-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiapi.py
More file actions
executable file
·1506 lines (1262 loc) · 52.9 KB
/
Copy pathuiapi.py
File metadata and controls
executable file
·1506 lines (1262 loc) · 52.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
from calendar import c
import logging
import time
import json
import traceback
from typing import Dict, Any, Optional, Callable, Awaitable, Tuple, List
import base64
from pathlib import Path
import os
import uuid
from dataclasses import dataclass
from rich.logging import RichHandler
from rich.console import Console
from rich.traceback import install
# ANSI color codes
# Regular colors
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
# Bright colors
BRIGHT_BLACK = "\033[90m"
BRIGHT_RED = "\033[91m"
BRIGHT_GREEN = "\033[92m"
BRIGHT_YELLOW = "\033[93m"
BRIGHT_BLUE = "\033[94m"
BRIGHT_MAGENTA = "\033[95m"
BRIGHT_CYAN = "\033[96m"
BRIGHT_WHITE = "\033[97m"
# Background colors
BG_BLACK = "\033[40m"
BG_RED = "\033[41m"
BG_GREEN = "\033[42m"
BG_YELLOW = "\033[43m"
BG_BLUE = "\033[44m"
BG_MAGENTA = "\033[45m"
BG_CYAN = "\033[46m"
BG_WHITE = "\033[47m"
# Bright background colors
BG_BRIGHT_BLACK = "\033[100m"
BG_BRIGHT_RED = "\033[101m"
BG_BRIGHT_GREEN = "\033[102m"
BG_BRIGHT_YELLOW = "\033[103m"
BG_BRIGHT_BLUE = "\033[104m"
BG_BRIGHT_MAGENTA = "\033[105m"
BG_BRIGHT_CYAN = "\033[106m"
BG_BRIGHT_WHITE = "\033[107m"
# Text style codes
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
ITALIC = "\033[3m"
STRIKE = "\033[9m"
RESET = "\033[0m"
LOG_WEBUI_RESPOND = f"{BLUE}(webui:respond){RESET}"
LOG_WEBUI_SEND = f"{BLUE}(webui:send){RESET}"
LOG_WEBUI_CONNECT = f"{BLUE}(webui){RESET}"
LOG_WEBUI_DISCONNECT = f"{BLUE}(webui){RESET}"
LOG_TASK_START = f"{MAGENTA}(task:start){RESET}"
LOG_TASK_END = f"{MAGENTA}(task:end){RESET}"
LOG_TASK = f"{MAGENTA}(task){RESET}"
LOG_MODEL = f"{GREEN}(model){RESET}"
from aiohttp import web
from aiohttp import web_response
from aiohttp.web import RouteTableDef
import server
from .model_defs import ModelDef
from . import model_defs
from server import PromptServer
# Path to store model URLs
MODEL_URLS_PATH = Path(__file__).parent / "model_urls.json"
INF_TIMEOUT = 99999999999
def load_stored_urlmap() -> Dict[str, str]:
"""Load saved model URLs from JSON file"""
if MODEL_URLS_PATH.exists():
try:
with open(MODEL_URLS_PATH, "r") as f:
return json.load(f)
except Exception as e:
log.error(f"Error loading model URLs: {e}")
return {}
def save_model_urls(urls: Dict[str, str]) -> None:
"""Save model URLs to JSON file, merging with existing URLs"""
try:
existing_urls = load_stored_urlmap()
merged_urls = {**existing_urls, **urls}
os.makedirs(MODEL_URLS_PATH.parent, exist_ok=True)
with open(MODEL_URLS_PATH, "w") as f:
json.dump(merged_urls, f, indent=2)
log.info(f"Saved {len(merged_urls)} model URLs ({len(urls)} new/updated)")
except Exception as e:
log.error(f"Error saving model URLs: {e}")
# Mock routes if server not initialized
routes = PromptServer.instance.routes if hasattr(PromptServer, 'instance') and PromptServer.instance else RouteTableDef()
server_start_time = time.time()
# Install rich traceback handler
install(show_locals=True)
# Set up rich console
console = Console()
# Configure logging with rich
logging.basicConfig(
level=logging.DEBUG,
format="%(message)s",
datefmt="[%X]",
handlers=[
RichHandler(rich_tracebacks=True, markup=True, show_time=True, show_path=True)
],
)
log = logging.getLogger("uiapi")
# Remove any existing handlers to avoid duplicate logging
for handler in log.handlers[:]:
log.removeHandler(handler)
# Add rich handler with markup enabled
# rich_handler = RichHandler(
# console=console,
# rich_tracebacks=True,
# markup=True,
# show_time=True,
# show_path=True,
# enable_link_path=False,
# )
# log.addHandler(rich_handler)
# log.propagate = False
# Set up handlers to pass over to the client and then back.
# This python server extension is simply a middleman from the
# other client which connects to the server and our own webui client
# ------------------------------------------
INPUT_DIR = Path(__file__).parent.parent.parent / "input"
NEXT_CLIENT_ID = 0
@dataclass
class PendingRequest:
"""Represents a request that's waiting for client response"""
request_id: str
endpoint: str
data: dict
event: asyncio.Event
response: Any = None
post_process: Optional[Callable[[Any], Awaitable[Any]]] = None
class WebuiManager:
# Class-level dictionary to store all client managers
_client_managers: Dict[int, "WebuiManager"] = {}
_main_webui: Optional["WebuiManager"] = None
def __init__(self):
self._pending_requests: Dict[str, PendingRequest] = {}
self._client_id: Optional[int] = None
self._webui_ready = asyncio.Event()
self._buffered_requests: list[PendingRequest] = []
self._lock = asyncio.Lock()
log.info(f"{LOG_WEBUI_CONNECT} - *")
@property
def client_id(self) -> Optional[int]:
"""Get the client ID for this manager"""
return self._client_id
@classmethod
def get_or_create_manager(cls, client_id: int) -> "WebuiManager":
"""Get an existing manager or create a new one for a client ID"""
id = int(client_id)
if id == -1:
global NEXT_CLIENT_ID
NEXT_CLIENT_ID += 1
# Create new manager and generate UUID
manager = cls()
manager._client_id = NEXT_CLIENT_ID
cls._client_managers[NEXT_CLIENT_ID] = manager
return manager
if id in cls._client_managers:
return cls._client_managers[id]
# Create new manager with provided ID
manager = cls()
manager._client_id = id
cls._client_managers[id] = manager
return manager
@classmethod
def get_main_webui(cls) -> Optional["WebuiManager"]:
"""Get the main WebUI manager instance"""
return cls._main_webui
def set_connected(self):
"""Called when ComfyUI web interface connects"""
self._webui_ready.set()
# Set this as the main WebUI if none exists or current main is disconnected
if (
not self.__class__._main_webui
or not self.__class__._main_webui._webui_ready.is_set()
):
log.info(f"{LOG_WEBUI_CONNECT} [{self._client_id}] - Setting as main WebUI")
self.__class__._main_webui = self
# Process buffered requests
if len(self._buffered_requests) > 0:
log.info(
f"{LOG_WEBUI_CONNECT} [{self._client_id}] - {len(self._buffered_requests)} requests in buffer"
)
for req in self._buffered_requests:
log.info(f"Processing buffered request: {req.endpoint}")
else:
log.info(f"{LOG_WEBUI_CONNECT} [{self._client_id}]")
self._buffered_requests.clear()
def set_disconnected(self):
"""Called when ComfyUI web interface disconnects"""
log.warning(f"{LOG_WEBUI_DISCONNECT} [{self._client_id}]")
self._webui_ready.clear()
# If this was the main WebUI, try to find another connected one
if self.__class__._main_webui == self:
self.__class__._main_webui = None
for manager in self.__class__._client_managers.values():
if manager != self and manager._webui_ready.is_set():
log.info(
f"{LOG_WEBUI_CONNECT} [{manager._client_id}] - Setting as new main WebUI"
)
self.__class__._main_webui = manager
break
if self._client_id in self._client_managers:
del self._client_managers[self._client_id]
async def _send(self, request: PendingRequest):
"""Send a request to the client"""
data_str = json.dumps(request.data, indent=2).replace("\n", " ")
log.info(
f"{LOG_WEBUI_SEND} [{self._client_id}] {request.request_id} {request.endpoint} {data_str[:100]}{'...' if len(data_str) > 100 else ''}"
)
# Add client_id to request data
request.data["client_id"] = self._client_id
await server.PromptServer.instance.send_json(request.endpoint, request.data)
async def send(
self,
endpoint: str,
data: dict | None = None,
buffered: bool = False,
post_process: Optional[Callable[[Any], Awaitable[Any]]] = None,
) -> PendingRequest:
"""Create a new request and optionally buffer it"""
request_id = str(uuid.uuid4())[:8]
endpoint = f"/uiapi/{endpoint}"
if data is None:
data = {}
data["request_id"] = request_id
data["client_id"] = self._client_id
request = PendingRequest(
request_id=request_id,
endpoint=endpoint,
data=data,
event=asyncio.Event(),
post_process=post_process,
)
async with self._lock:
self._pending_requests[request_id] = request
if buffered and not self._webui_ready.is_set():
log.info(
f"{LOG_WEBUI_SEND} [{self._client_id}] {request_id} - WebUI not ready, buffering ..."
)
self._buffered_requests.append(request)
return request
await self._send(request)
return request
async def wsend(
self,
endpoint: str,
data: dict | None = None,
buffered: bool = False,
post_process: Optional[Callable[[Any], Awaitable[Any]]] = None,
) -> Any:
"""Send a request to the client and wait for a response"""
request = await self.send(endpoint, data, buffered, post_process)
return await self.wait(request)
async def respond(self, request_id: str, response_data: Any):
"""Set response for a request and process it"""
response_str = json.dumps(response_data, indent=2)
log.info(
f"{LOG_WEBUI_RESPOND} [{self._client_id}] {request_id} {response_str[:100]}{'...' if len(response_str) > 100 else ''}"
)
async with self._lock:
if (
request_id not in self._pending_requests
or self._pending_requests[request_id].response
):
log.warning(
f"{LOG_WEBUI_RESPOND} [{self._client_id}] response received for unknown request: {request_id}"
)
return
request = self._pending_requests[request_id]
request.response = response_data
if request.post_process:
log.info(f"Running post-processing for request {request_id}")
try:
request.response = await request.post_process(response_data)
log.info(
f"Post-processed response: {json.dumps(request.response, indent=2)}"
)
except Exception as e:
log.error(f"Error in post-processing for {request_id}: {e}")
log.error(traceback.format_exc())
request.event.set()
log.debug(f"{LOG_WEBUI_RESPOND} [{self._client_id}] {request_id} complete")
async def wait(self, request: PendingRequest, timeout: float = 30.0) -> Any:
"""Wait for and return response for a request"""
log.debug(
f"{LOG_WEBUI_SEND} [{self._client_id}] {request.request_id} (timeout={timeout}s)"
)
try:
await asyncio.wait_for(request.event.wait(), timeout)
return request.response
except asyncio.TimeoutError:
log.error(
f"{LOG_WEBUI_SEND} [{self._client_id}] {request.request_id} timed out after {timeout}s"
)
finally:
async with self._lock:
self._pending_requests.pop(request.request_id, None)
log.debug(
f"{LOG_WEBUI_SEND} [{self._client_id}] {request.request_id} popped"
)
# Initialize the request manager
webui_manager = WebuiManager()
async def handle_uiapi_request(
endpoint: str,
request_data: dict | None = None,
wait_for_client: bool = False,
post_process: Optional[Callable[[Any], Awaitable[Any]]] = None,
) -> web.Response:
"""Handle a UI API request with optional post-processing"""
log.info(f"-> /uiapi/{endpoint}")
log.info(
f"-> /uiapi/{endpoint} {json.dumps(request_data, indent=2) if request_data else 'None'}"
)
try:
# Get the main WebUI manager
main_webui = WebuiManager.get_main_webui()
if not main_webui:
return web.json_response(
{"status": "error", "error": "No main WebUI connected"}, status=503
)
request = await main_webui.send(
endpoint, request_data, wait_for_client, post_process
)
if request in main_webui._buffered_requests:
log.info(f"{LOG_WEBUI_SEND} {request.request_id} - buffered")
return web.json_response(
{
"status": "pending",
"message": "Request buffered until client connects",
"request_id": request.request_id,
}
)
response = await main_webui.wait(request, timeout=30.0)
log.info(f"Request completed successfully {request.request_id}")
return web.json_response({"status": "ok", "response": response})
except asyncio.TimeoutError:
log.error(f"{LOG_WEBUI_SEND} {request.request_id} - timeout")
return web.json_response(
{"status": "error", "error": "Request timed out"}, status=408
)
except Exception as e:
log.error(f"{LOG_WEBUI_SEND} {request.request_id} - {e}")
log.error(traceback.format_exc())
return web.json_response({"status": "error", "error": str(e)}, status=500)
# Track ongoing downloads
download_tasks: Dict[str, Dict[str, Any]] = {}
def analyze_workflow_models(workflow: dict) -> Tuple[List[str], List[str], List[str]]:
"""Analyze a workflow and return lists of missing, existing, and all checkpoints.
Args:
workflow: The workflow to analyze
Returns:
Tuple of (missing_models, existing_models, all_checkpoints)
"""
checkpoints = []
missing_models = []
existing_models = []
# Extract nodes from workflow
nodes = workflow.get("nodes", {})
for node in nodes:
if isinstance(node, dict) and "widgets_values" in node:
for value in node["widgets_values"]:
if isinstance(value, str) and any(
value.endswith(ext) for ext in [".safetensors", ".ckpt", ".bin"]
):
checkpoints.append(value)
name = value.split("/")[-1]
if model_defs.has_model(name):
existing_models.append(name)
else:
missing_models.append(value)
return missing_models, existing_models, checkpoints
@routes.post("/uiapi/webui_ready")
async def uiapi_webui_ready(request):
"""Called when ComfyUI web interface connects"""
print()
data = await request.json()
client_id = data.get("client_id", "-1")
browser_info = data.get("browserInfo", {})
browser_name = browser_info.get("browser", "Unknown Browser")
platform = browser_info.get("platform", "Unknown Platform")
# Get or create manager for this client
print("")
log.info("-> /uiapi/webui_ready")
manager = WebuiManager.get_or_create_manager(client_id)
log.info(
f"{LOG_WEBUI_CONNECT} [{manager.client_id}] - Hello from {browser_name} on {platform}!"
)
manager.set_connected()
try:
# Fetch current workflow using this manager since it just connected
workflow = await manager.wsend("get_workflow", buffered=True)
if workflow:
workflow = workflow["workflow"]["workflow"]
missing_models, existing_models, checkpoints = analyze_workflow_models(
workflow
)
console.print()
console.print(
f"{BOLD}{BLUE}========== Workflow Analysis Results =========={RESET}"
)
console.print(f"{LOG_MODEL} Total checkpoints found: {len(checkpoints)}")
console.print(f"{LOG_MODEL} Missing models: {len(missing_models)}")
if missing_models:
for model in missing_models:
console.print(f"{LOG_MODEL} • {model}")
console.print(f"{LOG_MODEL} Existing models: {len(existing_models)}")
if existing_models:
for model in existing_models:
console.print(f"{LOG_MODEL} • {model}")
else:
console.print(f"{YELLOW}No workflow available to analyze{RESET}")
except Exception as e:
log.error(f"Error analyzing workflow: {e}")
log.error(traceback.format_exc())
return web.json_response({"status": "ok", "client_id": manager.client_id})
@routes.post("/uiapi/webui_disconnect")
async def uiapi_webui_disconnect(request):
"""Called when ComfyUI web interface disconnects"""
data = await request.json()
client_id = data.get("client_id")
if client_id:
manager = WebuiManager.get_or_create_manager(client_id)
manager.set_disconnected()
return web.json_response({"status": "ok"})
@routes.post("/uiapi/webui_response")
async def uiapi_response(request):
data = await request.json()
request_id = data.get("request_id")
client_id = data.get("client_id")
if not request_id:
return web.json_response(
{"status": "error", "error": "No request_id provided"}, status=400
)
if not client_id:
return web.json_response(
{"status": "error", "error": "No client_id provided"}, status=400
)
manager = WebuiManager.get_or_create_manager(client_id)
await manager.respond(request_id, data.get("response"))
return web.json_response({"status": "ok"})
# Example of download_models with post-processing
async def process_workflow_response(response: Any) -> Any:
"""Process workflow response for download_models"""
if isinstance(response, dict):
# Handle pending status
if response.get("status") == "pending":
return response
# Handle normal response
return response.get("response", {})
elif isinstance(response, web.Response):
# Get response text directly from the response object
try:
if hasattr(response, "text"):
if callable(response.text):
response_text = await response.text()
else:
response_text = response.text
if isinstance(response_text, str):
response_data = json.loads(response_text)
if "workflow" in response_data:
return response_data["workflow"]
except Exception as e:
log.error(f"Error processing workflow response: {e}")
log.error(traceback.format_exc())
return response
# Add this near other global variables
pending_downloads: Dict[str, Dict] = {}
@routes.post("/uiapi/download_models")
async def uiapi_download_models(request):
try:
print()
log.info("-> /uiapi/download_models")
# Get the request data containing the download table
request_data = await request.json()
download_table = request_data.get("download_table", {})
workflow = request_data.get("workflow", None)
# Get workflow from main WebUI if not provided
# ----------------------------------------
if not workflow:
log.info("/uiapi/download_models - No workflow provided, checking main WebUI")
main_webui = WebuiManager.get_main_webui()
if main_webui:
workflow = await main_webui.wsend("get_workflow", buffered=True)
if workflow:
workflow = workflow["workflow"]["workflow"]
log.info("/uiapi/download_models - Got workflow from main WebUI")
else:
log.info("/uiapi/download_models - Failed to get workflow from main WebUI")
return web.json_response(
{"status": "error", "error": "No workflow available"}, status=400
)
else:
log.info("/uiapi/download_models - No main WebUI available")
return web.json_response(
{"status": "error", "error": "No workflow provided and no main WebUI available"}, status=400
)
# Create task
# ----------------------------------------
task_id = str(uuid.uuid4())[:8]
download_tasks[task_id] = {
"status": "initializing",
"start_time": time.time(),
"progress": {},
"completed": False,
}
download_task = download_models_task(task_id, download_table, workflow)
asyncio.create_task(download_task)
log.info(f"{LOG_TASK_START} {task_id} - *")
return web.json_response(
{"status": "ok", "message": "Download task started", "download_id": task_id}
)
except Exception as e:
log.error(f"{LOG_TASK_END} Download task error: {e}")
log.error(traceback.format_exc())
return web.json_response({"status": "error", "error": str(e)}, status=500)
@routes.get("/uiapi/download_status/{request_id}")
async def uiapi_download_status(request):
"""Handle status requests for downloads in progress"""
request_id = request.match_info["request_id"]
log.info(f"-> /uiapi/download_status/{request_id}")
# First check pending downloads
if request_id in pending_downloads:
# Clean up old pending downloads (older than 5 minutes)
current_time = time.time()
for rid, info in list(pending_downloads.items()):
if current_time - info["timestamp"] > 300: # 5 minutes
pending_downloads.pop(rid)
# Return status if still pending
if request_id in pending_downloads:
info = pending_downloads[request_id]
# If task_id is set, redirect to that status
if info["task_id"]:
return web.json_response(download_tasks[info["task_id"]])
return web.json_response(info)
# Then check active download tasks
if request_id in download_tasks:
return web.json_response(download_tasks[request_id])
return web.json_response({"status": "error", "error": "Task not found"}, status=404)
@routes.post("/uiapi/get_workflow")
async def uiapi_get_workflow(request):
print()
log.info("-> /uiapi/get_workflow")
return await handle_uiapi_request("get_workflow")
@routes.post("/uiapi/get_workflow_api")
async def uiapi_get_workflow_api(request):
print()
log.info("-> /uiapi/get_workflow_api")
return await handle_uiapi_request("get_workflow_api")
@routes.post("/uiapi/get_fields")
async def uiapi_get_field(request):
print()
log.info("-> /uiapi/get_fields")
return await handle_uiapi_request("get_fields", await request.json())
def save_base64_image(base64_data: str, filepath: Path) -> None:
"""Save base64 image data to a file"""
try:
image_data = base64.b64decode(base64_data)
os.makedirs(filepath.parent, exist_ok=True)
with open(filepath, "wb") as f:
f.write(image_data)
except Exception as e:
log.error(f"Failed to save base64 image: {e}")
import traceback
log.error(traceback.format_exc())
raise
@routes.post("/uiapi/set_fields")
async def uiapi_set_field(request):
request_data = await request.json()
print()
log.info("-> /uiapi/set_fields")
# Process any image fields before passing to the main handler
if "fields" in request_data:
for field in request_data["fields"]:
if isinstance(field[1], dict) and field[1].get("type") == "image_base64":
path = field[0]
# Generate input name from the node path
input_name = f'INPUT_{path.split(".")[0]}.png'
# TODO: Verify this is the correct path for ComfyUI's input folder
filepath = INPUT_DIR / input_name
# Save the base64 image
save_base64_image(field[1]["data"], filepath)
# Replace the base64 data with just the filename
field[1] = input_name
return await handle_uiapi_request("set_fields", request_data)
@routes.post("/uiapi/set_connection")
async def uiapi_set_connection(request):
print()
log.info("-> /uiapi/set_connection")
return await handle_uiapi_request("set_connection", await request.json())
@routes.post("/uiapi/execute")
async def uiapi_execute(request):
print()
log.info("-> /uiapi/execute")
return await handle_uiapi_request("execute", await request.json())
@routes.post("/uiapi/query_fields")
async def uiapi_query_fields(request):
print()
log.info("-> /uiapi/query_fields")
return await handle_uiapi_request("query_fields", await request.json())
@routes.post("/uiapi/add_model_url")
async def uiapi_add_model_url(request):
"""Add a URL for a model that wasn't in the download table"""
print()
log.info("-> /uiapi/add_model_url")
try:
data = await request.json()
model_name = data.get("model_name")
model_url = data.get("url")
model_type = data.get("model_type", "model") # Default to 'model' type
if not model_name or not model_url:
return web.json_response(
{"status": "error", "error": "Missing model_name or url"}, status=400
)
# Create model definition
model_def = {"url": model_url, "ckpt_type": model_type}
return web.json_response({"status": "ok", "model_def": model_def})
except Exception as e:
log.error(f"Error adding model URL: {e}")
log.error(traceback.format_exc())
return web.json_response({"status": "error", "error": str(e)}, status=500)
async def download_models_task(
task_id: str, urlmap_request: Dict[str, dict], workflow: dict
) -> None:
"""Background task to handle model downloads"""
log.info(f"{LOG_TASK_START} {task_id} - Starting")
task_info = download_tasks[task_id]
start_time = time.time()
try:
# Determine missing models
# ----------------------------------------
checkpoints = []
missing_ckpts = []
existing_models = []
ckpt_types = {}
task_info.update(
{
"status": "checking",
"progress": {},
}
)
# Read workflow nodes
# ----------------------------------------
def process_model_value(value, ntype, task_id):
"""Process a single model value and update tracking data"""
if not (isinstance(value, str) and any(
value.endswith(ext) for ext in [".safetensors", ".ckpt", ".bin"]
)):
return None
ckpt = value
name = ckpt.split("/")[-1]
# Determine checkpoint type
if 'lora' in ntype or 'loraloader' in ntype:
ckpt_types[ckpt] = 'loras'
elif 'control' in ntype:
ckpt_types[ckpt] = 'controlnet'
else:
ckpt_types[ckpt] = 'checkpoints'
checkpoints.append(ckpt)
# Check if model exists
if model_defs.has_model(ckpt, ckpt_types[ckpt]):
path = model_defs.get_model_path(ckpt, ckpt_types[ckpt])
log.info(
f"{LOG_TASK} {task_id} - {GREEN} ✓ {ckpt_types[ckpt]}: {ckpt}{RESET}"
)
existing_models.append(ckpt)
task_info["progress"][value] = {
"status": "success",
"path": str(path),
"model_number": "existing",
}
else:
missing_ckpts.append(ckpt)
log.info(
f"{LOG_TASK} {task_id} - {RED} ✗ {ckpt_types[ckpt]}: {ckpt}{RESET}"
)
# 1) UI json format
nodes = workflow.get("nodes", [])
if nodes:
log.info(
f"{LOG_TASK_START} {task_id} - Extracted {len(nodes)} nodes from workflow"
)
for node in nodes:
if isinstance(node, dict) and "widgets_values" in node:
for value in node["widgets_values"]:
process_model_value(value, node["type"].lower(), task_id)
# 2) API json format
if not nodes and isinstance(workflow, dict):
log.info(f"{LOG_TASK_START} {task_id} - Processing alternate workflow format")
for node_id, node_data in workflow.items():
if isinstance(node_data, dict):
# Check inputs for model paths
inputs = node_data.get('inputs', {})
for key, value in inputs.items():
process_model_value(value, node_data.get('class_type', '').lower(), task_id)
# Tally models
# ----------------------------------------
total_models = len(missing_ckpts)
if total_models == 0:
log.info(
f"{LOG_TASK_END} {task_id} - All models already exist, nothing to download"
)
task_info.update(
{
"status": "completed",
"completed": True,
"elapsed_time": time.time() - start_time,
}
)
return
# Download models
# ----------------------------------------
log.info(
f"{LOG_TASK_START} {task_id} - Found {total_models} models to download"
)
task_info.update(
{
"total_models": total_models,
"current_model": 0,
"status": "downloading",
}
)
# Load saved URLs
urlmap_store = load_stored_urlmap()
log.info(
f"{LOG_TASK_START} {task_id} - Loaded {len(urlmap_store)} saved model URLs"
)
# Get URLs from client not in the download map
urlmap_both = {**urlmap_store, **urlmap_request}
nourl_ckpts = [ckpt for ckpt in missing_ckpts if ckpt not in urlmap_both]
hasurl_ckpts = [ckpt for ckpt in missing_ckpts if ckpt in urlmap_both]
webui = WebuiManager.get_main_webui()
if len(nourl_ckpts) > 0 and webui is not None:
log.info(
f"{LOG_TASK_START} {task_id} - Requesting urlmap from webui for {len(nourl_ckpts)} models ..."
)
request = await webui.wsend(
"get_model_url",
{
"requested_ckpts": nourl_ckpts,
"existing_ckpts": hasurl_ckpts,
},
buffered=True,
)
# Wait for response from webui
urlmap_webui = await webui.wait(request, timeout=INF_TIMEOUT)
if not urlmap_webui:
urlmap_webui = {}
log.warning(
f"{LOG_TASK_END} {task_id} - No urlmap provided, the following models will be missing and cause the workflow not to run:"
)
for model in missing_ckpts:
log.warning(f" {model}")
else:
# Combine saved URLs with new ones
for name in missing_ckpts:
if name in urlmap_webui and urlmap_webui[name]:
urlmap_store[name] = urlmap_webui[name]
# Save updated URLs
save_model_urls(urlmap_store)
log.info(
f"{LOG_TASK_END} {task_id} - Saved {len(urlmap_store)} new model URLs"
)
# Use combined URL map for downloads
urlmap = {**urlmap_store, **urlmap_request, **urlmap_webui}
else:
urlmap = {**urlmap_store, **urlmap_request}
# Process downloads one at a time
for idx, ckpt in enumerate(missing_ckpts, 1):
name = ckpt.split("/")[-1]
elapsed = time.time() - start_time
# log.info(
# f"{LOG_TASK_START} {task_id} - Processing model {idx}/{len(missing_models)}: {ckpt}"
# )
task_info["current_model"] = idx
task_info["elapsed_time"] = elapsed
task_info["progress"][ckpt] = {
"status": "downloading",
"model_number": f"{idx}/{len(missing_ckpts)}",
}
try:
url = urlmap.get(ckpt) or urlmap.get(name)
if isinstance(url, dict):
model_def = ModelDef(**url)
elif isinstance(url, str):
model_def = ModelDef(url=url)
else:
log.warning(
f"{LOG_TASK_END} {task_id} - No URL provided for {ckpt}, skipping..."
)
task_info["progress"][ckpt].update(
{"status": "error", "error": "No download URL provided"}
)
continue
path = model_def.download(ckpt, type=ckpt_types[ckpt])
task_info["progress"][ckpt].update(
{"status": "success", "path": str(path)}
)
except Exception as e:
log.error(f"{LOG_TASK_END} {task_id} - Error downloading {ckpt}: {e}")
log.error(traceback.format_exc())
task_info["progress"][ckpt].update({"status": "error", "error": str(e)})
task_info["status"] = "completed"
task_info["completed"] = True
task_info["elapsed_time"] = time.time() - start_time
log.info(
f"{LOG_TASK_END} {task_id} - Completed in {task_info['elapsed_time']:.1f}s"
)
except Exception as e:
log.error(f"{LOG_TASK_END} {task_id} - Failed: {e}")
log.error(traceback.format_exc())
task_info["status"] = "error"
task_info["error"] = str(e)
@routes.get("/uiapi/connection_status")
async def uiapi_connection_status(request):
"""Check if ComfyUI web interface is connected and get system status"""