Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import os
|
|
| 3 |
# ============================================================
|
| 4 |
# FORCE CPU
|
| 5 |
# ============================================================
|
| 6 |
-
# MUST be before torch / onnxruntime imports
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
|
@@ -11,7 +11,7 @@ os.environ["PYTORCH_CUDA_ALLOC_CONF"] = ""
|
|
| 11 |
os.environ["ORT_DISABLE_CUDA"] = "1"
|
| 12 |
|
| 13 |
# ============================================================
|
| 14 |
-
#
|
| 15 |
# ============================================================
|
| 16 |
|
| 17 |
import sys
|
|
@@ -19,8 +19,13 @@ import asyncio
|
|
| 19 |
import gc
|
| 20 |
import imageio
|
| 21 |
import tempfile
|
|
|
|
| 22 |
from collections import OrderedDict
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
import numpy as np
|
| 25 |
import cv2
|
| 26 |
import gradio as gr
|
|
@@ -33,8 +38,8 @@ from huggingface_hub import hf_hub_download
|
|
| 33 |
# CONFIG
|
| 34 |
# ============================================================
|
| 35 |
|
| 36 |
-
BASE_DIR = os.path.
|
| 37 |
-
os.path.
|
| 38 |
)
|
| 39 |
|
| 40 |
MODEL_DIR = os.path.join(
|
|
@@ -62,105 +67,53 @@ OUTPUT_DIR = os.path.join(
|
|
| 62 |
"output"
|
| 63 |
)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
#
|
| 67 |
-
#
|
| 68 |
-
#
|
| 69 |
-
# This Space contains ComfyUI DIRECTLY in the repository root:
|
| 70 |
-
#
|
| 71 |
-
# /app/
|
| 72 |
-
# comfy/
|
| 73 |
-
# comfy_extras/
|
| 74 |
-
# execution.py
|
| 75 |
-
# nodes.py
|
| 76 |
-
# server.py
|
| 77 |
-
# folder_paths.py
|
| 78 |
-
# comfyui_version.py
|
| 79 |
-
#
|
| 80 |
-
# Therefore DO NOT require:
|
| 81 |
-
#
|
| 82 |
-
# /app/ComfyUI
|
| 83 |
-
#
|
| 84 |
-
# ============================================================
|
| 85 |
|
| 86 |
COMFYUI_ROOT = BASE_DIR
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"/app/ComfyUI",
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
os.path.join(
|
| 100 |
-
BASE_DIR,
|
| 101 |
-
"comfy"
|
| 102 |
-
),
|
| 103 |
-
]
|
| 104 |
|
| 105 |
-
|
| 106 |
-
# REACTOR PATH
|
| 107 |
-
# ============================================================
|
| 108 |
|
| 109 |
-
|
| 110 |
-
os.path.join(
|
| 111 |
BASE_DIR,
|
| 112 |
"custom_nodes",
|
| 113 |
"comfyui-reactor-node"
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
"/app/custom_nodes/comfyui-reactor-node",
|
| 117 |
-
|
| 118 |
-
os.path.join(
|
| 119 |
-
os.getcwd(),
|
| 120 |
-
"custom_nodes",
|
| 121 |
-
"comfyui-reactor-node"
|
| 122 |
-
),
|
| 123 |
-
]
|
| 124 |
-
|
| 125 |
-
REACTOR_PATH = None
|
| 126 |
|
| 127 |
|
| 128 |
# ============================================================
|
| 129 |
-
#
|
| 130 |
# ============================================================
|
| 131 |
|
| 132 |
-
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
),
|
| 139 |
-
|
| 140 |
-
"hyperswap_1a_256.onnx": (
|
| 141 |
-
"facefusion/models-3.3.0",
|
| 142 |
-
"hyperswap_1a_256.onnx",
|
| 143 |
-
HYPERSWAP_DIR
|
| 144 |
-
),
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
),
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
),
|
| 157 |
|
| 158 |
-
|
| 159 |
-
"martintomov/comfy",
|
| 160 |
-
"facerestore_models/GPEN-BFR-512.onnx",
|
| 161 |
-
FACE_RESTORE_DIR
|
| 162 |
-
),
|
| 163 |
-
}
|
| 164 |
|
| 165 |
|
| 166 |
# ============================================================
|
|
@@ -194,31 +147,48 @@ os.makedirs(
|
|
| 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 |
# ============================================================
|
|
@@ -268,29 +238,11 @@ def download_model(
|
|
| 268 |
exist_ok=True
|
| 269 |
)
|
| 270 |
|
| 271 |
-
print(
|
| 272 |
-
"============================================================"
|
| 273 |
-
)
|
| 274 |
-
|
| 275 |
print(
|
| 276 |
"[MODEL] Downloading:",
|
| 277 |
filename
|
| 278 |
)
|
| 279 |
|
| 280 |
-
print(
|
| 281 |
-
"[MODEL] Repository:",
|
| 282 |
-
repo
|
| 283 |
-
)
|
| 284 |
-
|
| 285 |
-
print(
|
| 286 |
-
"[MODEL] Destination:",
|
| 287 |
-
local_path
|
| 288 |
-
)
|
| 289 |
-
|
| 290 |
-
print(
|
| 291 |
-
"============================================================"
|
| 292 |
-
)
|
| 293 |
-
|
| 294 |
downloaded_path = hf_hub_download(
|
| 295 |
repo_id=repo,
|
| 296 |
filename=filename,
|
|
@@ -306,59 +258,32 @@ def download_model(
|
|
| 306 |
f"{downloaded_path}"
|
| 307 |
)
|
| 308 |
|
| 309 |
-
# Some HF layouts can return a path
|
| 310 |
-
# different from the expected location.
|
| 311 |
-
#
|
| 312 |
-
# Make sure our expected path exists.
|
| 313 |
-
|
| 314 |
-
if not os.path.isfile(
|
| 315 |
-
local_path
|
| 316 |
-
):
|
| 317 |
-
|
| 318 |
-
try:
|
| 319 |
-
|
| 320 |
-
if os.path.isfile(
|
| 321 |
-
downloaded_path
|
| 322 |
-
):
|
| 323 |
-
|
| 324 |
-
import shutil
|
| 325 |
-
|
| 326 |
-
shutil.copy2(
|
| 327 |
-
downloaded_path,
|
| 328 |
-
local_path
|
| 329 |
-
)
|
| 330 |
-
|
| 331 |
-
except Exception:
|
| 332 |
-
pass
|
| 333 |
-
|
| 334 |
-
if not os.path.isfile(
|
| 335 |
-
local_path
|
| 336 |
-
):
|
| 337 |
-
|
| 338 |
-
raise RuntimeError(
|
| 339 |
-
"Model downloaded nhưng không tìm thấy "
|
| 340 |
-
f"local file:\n{local_path}"
|
| 341 |
-
)
|
| 342 |
-
|
| 343 |
print(
|
| 344 |
"[MODEL] Ready:",
|
| 345 |
-
|
| 346 |
)
|
| 347 |
|
| 348 |
-
return
|
| 349 |
|
| 350 |
|
| 351 |
# ============================================================
|
| 352 |
-
#
|
| 353 |
# ============================================================
|
| 354 |
|
| 355 |
-
loaded_models = {}
|
| 356 |
-
|
| 357 |
-
|
| 358 |
def ensure_model(
|
| 359 |
model_name
|
| 360 |
):
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
if model_name in loaded_models:
|
| 363 |
|
| 364 |
cached_path = (
|
|
@@ -373,6 +298,10 @@ def ensure_model(
|
|
| 373 |
|
| 374 |
return cached_path
|
| 375 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
path = download_model(
|
| 377 |
model_name
|
| 378 |
)
|
|
@@ -393,244 +322,298 @@ def ensure_model(
|
|
| 393 |
|
| 394 |
|
| 395 |
# ============================================================
|
| 396 |
-
#
|
| 397 |
# ============================================================
|
| 398 |
|
| 399 |
-
def
|
| 400 |
-
path
|
| 401 |
-
):
|
| 402 |
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
| 405 |
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
|
|
|
|
|
|
| 410 |
|
| 411 |
-
|
| 412 |
-
os.path.join(
|
| 413 |
-
path,
|
| 414 |
-
"nodes.py"
|
| 415 |
-
),
|
| 416 |
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
),
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
),
|
| 431 |
-
]
|
| 432 |
|
| 433 |
-
|
| 434 |
-
os.path.join(
|
| 435 |
-
path,
|
| 436 |
-
"comfy"
|
| 437 |
-
)
|
| 438 |
-
)
|
| 439 |
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
item
|
| 443 |
-
)
|
| 444 |
-
for item in required_any
|
| 445 |
-
)
|
| 446 |
|
| 447 |
-
|
| 448 |
-
comfy_folder
|
| 449 |
-
and required_count >= 2
|
| 450 |
-
)
|
| 451 |
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
-
|
| 454 |
|
| 455 |
-
|
| 456 |
-
# 1. Direct embedded ComfyUI
|
| 457 |
-
# --------------------------------------------------------
|
| 458 |
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
|
|
|
| 468 |
|
| 469 |
-
for candidate in COMFYUI_CANDIDATES:
|
| 470 |
|
| 471 |
-
|
| 472 |
-
candidate
|
| 473 |
-
):
|
| 474 |
|
| 475 |
-
return candidate
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
|
| 481 |
-
|
| 482 |
|
| 483 |
-
|
| 484 |
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
|
|
|
|
|
|
| 488 |
|
| 489 |
-
|
| 490 |
|
| 491 |
-
|
| 492 |
-
|
|
|
|
|
|
|
| 493 |
)
|
| 494 |
|
| 495 |
-
|
| 496 |
-
|
|
|
|
| 497 |
|
| 498 |
-
current = parent
|
| 499 |
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
|
| 503 |
# ============================================================
|
| 504 |
-
#
|
| 505 |
# ============================================================
|
| 506 |
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
if COMFYUI_ROOT is None:
|
| 510 |
|
| 511 |
-
|
| 512 |
-
"
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
)
|
| 524 |
|
| 525 |
-
|
| 526 |
-
|
| 527 |
|
| 528 |
-
if COMFYUI_ROOT not in sys.path:
|
| 529 |
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
)
|
| 534 |
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
)
|
| 539 |
|
| 540 |
-
print(
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
for x in [
|
| 545 |
-
"comfy",
|
| 546 |
-
"nodes.py",
|
| 547 |
-
"execution.py",
|
| 548 |
-
"server.py",
|
| 549 |
-
"folder_paths.py",
|
| 550 |
-
"comfyui_version.py"
|
| 551 |
-
]
|
| 552 |
-
if os.path.exists(
|
| 553 |
-
os.path.join(
|
| 554 |
-
COMFYUI_ROOT,
|
| 555 |
-
x
|
| 556 |
-
)
|
| 557 |
)
|
| 558 |
-
]
|
| 559 |
-
)
|
| 560 |
|
|
|
|
| 561 |
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
|
|
|
| 565 |
|
| 566 |
-
|
|
|
|
|
|
|
| 567 |
|
| 568 |
-
|
|
|
|
|
|
|
| 569 |
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
getattr(
|
| 573 |
-
|
| 574 |
-
"
|
| 575 |
-
|
| 576 |
)
|
| 577 |
-
)
|
| 578 |
|
| 579 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
)
|
| 585 |
|
|
|
|
|
|
|
|
|
|
| 586 |
|
| 587 |
-
|
| 588 |
-
# EXTRA MODEL PATHS
|
| 589 |
-
# ============================================================
|
| 590 |
-
|
| 591 |
-
def find_extra_model_paths():
|
| 592 |
|
| 593 |
-
|
| 594 |
|
| 595 |
-
|
| 596 |
-
COMFYUI_ROOT,
|
| 597 |
-
"extra_model_paths.yaml"
|
| 598 |
-
),
|
| 599 |
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
|
| 610 |
-
|
| 611 |
-
BASE_DIR,
|
| 612 |
-
"extra_model_paths.yml"
|
| 613 |
-
),
|
| 614 |
-
]
|
| 615 |
|
| 616 |
-
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
-
if os.path.isfile(
|
| 619 |
-
path
|
| 620 |
-
):
|
| 621 |
|
| 622 |
-
|
| 623 |
|
| 624 |
-
return None
|
| 625 |
|
|
|
|
|
|
|
|
|
|
| 626 |
|
| 627 |
def add_extra_model_paths():
|
| 628 |
|
| 629 |
-
extra_model_paths = (
|
| 630 |
-
|
|
|
|
| 631 |
)
|
| 632 |
|
| 633 |
-
if not
|
|
|
|
|
|
|
| 634 |
|
| 635 |
print(
|
| 636 |
"[COMFYUI] No extra_model_paths.yaml"
|
|
@@ -638,21 +621,12 @@ def add_extra_model_paths():
|
|
| 638 |
|
| 639 |
return
|
| 640 |
|
| 641 |
-
print(
|
| 642 |
-
"[COMFYUI] Extra model paths:",
|
| 643 |
-
extra_model_paths
|
| 644 |
-
)
|
| 645 |
-
|
| 646 |
try:
|
| 647 |
|
| 648 |
from main import (
|
| 649 |
load_extra_path_config
|
| 650 |
)
|
| 651 |
|
| 652 |
-
load_extra_path_config(
|
| 653 |
-
extra_model_paths
|
| 654 |
-
)
|
| 655 |
-
|
| 656 |
except ImportError:
|
| 657 |
|
| 658 |
try:
|
|
@@ -661,23 +635,33 @@ def add_extra_model_paths():
|
|
| 661 |
load_extra_path_config
|
| 662 |
)
|
| 663 |
|
| 664 |
-
load_extra_path_config(
|
| 665 |
-
extra_model_paths
|
| 666 |
-
)
|
| 667 |
-
|
| 668 |
except Exception as e:
|
| 669 |
|
| 670 |
print(
|
| 671 |
-
"[COMFYUI] Could not
|
| 672 |
-
"extra
|
| 673 |
-
e
|
| 674 |
)
|
| 675 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
except Exception as e:
|
| 677 |
|
| 678 |
print(
|
| 679 |
-
"[COMFYUI] Extra
|
| 680 |
-
|
|
|
|
| 681 |
)
|
| 682 |
|
| 683 |
|
|
@@ -685,21 +669,38 @@ add_extra_model_paths()
|
|
| 685 |
|
| 686 |
|
| 687 |
# ============================================================
|
| 688 |
-
# TORCH
|
| 689 |
# ============================================================
|
| 690 |
|
| 691 |
import torch
|
| 692 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
import comfy.model_management
|
| 694 |
|
| 695 |
from comfy.model_management import (
|
| 696 |
CPUState
|
| 697 |
)
|
| 698 |
|
| 699 |
-
#
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
|
| 704 |
print(
|
| 705 |
"[DEVICE] Forced CPU"
|
|
@@ -712,21 +713,38 @@ print(
|
|
| 712 |
|
| 713 |
|
| 714 |
# ============================================================
|
| 715 |
-
#
|
| 716 |
# ============================================================
|
| 717 |
|
| 718 |
def import_custom_nodes():
|
| 719 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 720 |
import execution
|
| 721 |
|
| 722 |
from nodes import (
|
| 723 |
init_extra_nodes
|
| 724 |
)
|
| 725 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
import server
|
| 727 |
|
| 728 |
# --------------------------------------------------------
|
| 729 |
-
#
|
| 730 |
# --------------------------------------------------------
|
| 731 |
|
| 732 |
loop = asyncio.new_event_loop()
|
|
@@ -735,68 +753,83 @@ def import_custom_nodes():
|
|
| 735 |
loop
|
| 736 |
)
|
| 737 |
|
| 738 |
-
|
| 739 |
-
# PromptServer
|
| 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 |
-
# Cleanup loop
|
| 766 |
-
# --------------------------------------------------------
|
| 767 |
|
| 768 |
-
|
|
|
|
|
|
|
| 769 |
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
|
| 774 |
-
|
| 775 |
|
| 776 |
-
|
| 777 |
|
| 778 |
-
|
|
|
|
|
|
|
| 779 |
|
| 780 |
-
|
|
|
|
| 781 |
|
| 782 |
-
|
| 783 |
|
| 784 |
-
|
| 785 |
|
|
|
|
|
|
|
| 786 |
|
| 787 |
-
|
| 788 |
-
"[COMFYUI] Initializing nodes..."
|
| 789 |
-
)
|
| 790 |
|
| 791 |
-
|
|
|
|
|
|
|
| 792 |
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 796 |
|
| 797 |
|
| 798 |
# ============================================================
|
| 799 |
-
# NODE
|
| 800 |
# ============================================================
|
| 801 |
|
| 802 |
from nodes import (
|
|
@@ -814,7 +847,7 @@ def get_value_at_index(
|
|
| 814 |
):
|
| 815 |
|
| 816 |
"""
|
| 817 |
-
Safely extract ComfyUI node output.
|
| 818 |
"""
|
| 819 |
|
| 820 |
try:
|
|
@@ -846,34 +879,40 @@ def get_value_at_index(
|
|
| 846 |
|
| 847 |
|
| 848 |
# ============================================================
|
| 849 |
-
#
|
| 850 |
# ============================================================
|
| 851 |
|
| 852 |
-
|
| 853 |
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
|
|
|
|
|
|
| 857 |
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
|
| 862 |
-
break
|
| 863 |
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
-
if
|
|
|
|
|
|
|
| 866 |
|
| 867 |
raise RuntimeError(
|
| 868 |
-
"
|
| 869 |
-
"
|
| 870 |
-
"
|
| 871 |
-
+ "\n".join(
|
| 872 |
-
REACTOR_CANDIDATES
|
| 873 |
-
)
|
| 874 |
)
|
| 875 |
|
| 876 |
-
|
| 877 |
if REACTOR_PATH not in sys.path:
|
| 878 |
|
| 879 |
sys.path.insert(
|
|
@@ -881,34 +920,12 @@ if REACTOR_PATH not in sys.path:
|
|
| 881 |
REACTOR_PATH
|
| 882 |
)
|
| 883 |
|
| 884 |
-
|
| 885 |
print(
|
| 886 |
"[REACTOR] Path:",
|
| 887 |
REACTOR_PATH
|
| 888 |
)
|
| 889 |
|
| 890 |
|
| 891 |
-
# ============================================================
|
| 892 |
-
# LOAD LOADIMAGE
|
| 893 |
-
# ============================================================
|
| 894 |
-
|
| 895 |
-
try:
|
| 896 |
-
|
| 897 |
-
loadimage = (
|
| 898 |
-
NODE_CLASS_MAPPINGS[
|
| 899 |
-
"LoadImage"
|
| 900 |
-
]()
|
| 901 |
-
)
|
| 902 |
-
|
| 903 |
-
except Exception as e:
|
| 904 |
-
|
| 905 |
-
raise RuntimeError(
|
| 906 |
-
"Không load được "
|
| 907 |
-
"ComfyUI LoadImage:\n"
|
| 908 |
-
f"{e}"
|
| 909 |
-
)
|
| 910 |
-
|
| 911 |
-
|
| 912 |
# ============================================================
|
| 913 |
# LOAD REACTOR
|
| 914 |
# ============================================================
|
|
@@ -930,11 +947,6 @@ except Exception as e:
|
|
| 930 |
)
|
| 931 |
|
| 932 |
|
| 933 |
-
print(
|
| 934 |
-
"[REACTOR] ReActorFaceSwap loaded."
|
| 935 |
-
)
|
| 936 |
-
|
| 937 |
-
|
| 938 |
# ============================================================
|
| 939 |
# REACTOR FACE FUNCTIONS
|
| 940 |
# ============================================================
|
|
@@ -976,13 +988,11 @@ def detect_reactor_faces(
|
|
| 976 |
):
|
| 977 |
|
| 978 |
if not image_path:
|
| 979 |
-
|
| 980 |
return []
|
| 981 |
|
| 982 |
if not os.path.isfile(
|
| 983 |
image_path
|
| 984 |
):
|
| 985 |
-
|
| 986 |
return []
|
| 987 |
|
| 988 |
try:
|
|
@@ -1029,7 +1039,7 @@ def detect_reactor_faces(
|
|
| 1029 |
return []
|
| 1030 |
|
| 1031 |
# --------------------------------------------------------
|
| 1032 |
-
#
|
| 1033 |
# --------------------------------------------------------
|
| 1034 |
|
| 1035 |
faces = analyze_faces(
|
|
@@ -1045,7 +1055,7 @@ def detect_reactor_faces(
|
|
| 1045 |
faces = []
|
| 1046 |
|
| 1047 |
# --------------------------------------------------------
|
| 1048 |
-
#
|
| 1049 |
# --------------------------------------------------------
|
| 1050 |
|
| 1051 |
faces = sort_by_order(
|
|
@@ -1082,7 +1092,6 @@ def get_file_path(
|
|
| 1082 |
):
|
| 1083 |
|
| 1084 |
if file is None:
|
| 1085 |
-
|
| 1086 |
return None
|
| 1087 |
|
| 1088 |
if isinstance(
|
|
@@ -1116,12 +1125,14 @@ def get_file_paths(
|
|
| 1116 |
):
|
| 1117 |
|
| 1118 |
if not files:
|
| 1119 |
-
|
| 1120 |
return []
|
| 1121 |
|
| 1122 |
if isinstance(
|
| 1123 |
files,
|
| 1124 |
-
(
|
|
|
|
|
|
|
|
|
|
| 1125 |
):
|
| 1126 |
|
| 1127 |
files = [
|
|
@@ -1160,7 +1171,10 @@ def preview_faces(
|
|
| 1160 |
|
| 1161 |
if not image_path:
|
| 1162 |
|
| 1163 |
-
return
|
|
|
|
|
|
|
|
|
|
| 1164 |
|
| 1165 |
img = cv2.imread(
|
| 1166 |
image_path
|
|
@@ -1168,7 +1182,10 @@ def preview_faces(
|
|
| 1168 |
|
| 1169 |
if img is None:
|
| 1170 |
|
| 1171 |
-
return
|
|
|
|
|
|
|
|
|
|
| 1172 |
|
| 1173 |
height, width = (
|
| 1174 |
img.shape[:2]
|
|
@@ -1180,7 +1197,10 @@ def preview_faces(
|
|
| 1180 |
|
| 1181 |
if not faces:
|
| 1182 |
|
| 1183 |
-
return
|
|
|
|
|
|
|
|
|
|
| 1184 |
|
| 1185 |
previews = []
|
| 1186 |
|
|
@@ -1323,7 +1343,7 @@ def preview_faces(
|
|
| 1323 |
)
|
| 1324 |
|
| 1325 |
# ----------------------------------------------------
|
| 1326 |
-
#
|
| 1327 |
# ----------------------------------------------------
|
| 1328 |
|
| 1329 |
cv2.rectangle(
|
|
@@ -1373,7 +1393,10 @@ def preview_faces(
|
|
| 1373 |
|
| 1374 |
if not previews:
|
| 1375 |
|
| 1376 |
-
return
|
|
|
|
|
|
|
|
|
|
| 1377 |
|
| 1378 |
preview = np.hstack(
|
| 1379 |
previews
|
|
@@ -1386,7 +1409,7 @@ def preview_faces(
|
|
| 1386 |
|
| 1387 |
|
| 1388 |
# ============================================================
|
| 1389 |
-
# GIF
|
| 1390 |
# ============================================================
|
| 1391 |
|
| 1392 |
def get_preview_image(
|
|
@@ -1395,7 +1418,10 @@ def get_preview_image(
|
|
| 1395 |
|
| 1396 |
if not target_path:
|
| 1397 |
|
| 1398 |
-
return
|
|
|
|
|
|
|
|
|
|
| 1399 |
|
| 1400 |
# --------------------------------------------------------
|
| 1401 |
# GIF
|
|
@@ -1524,9 +1550,6 @@ def convert_result(
|
|
| 1524 |
image
|
| 1525 |
):
|
| 1526 |
|
| 1527 |
-
# Import torch lazily here
|
| 1528 |
-
# because torch is already initialized above.
|
| 1529 |
-
|
| 1530 |
if isinstance(
|
| 1531 |
image,
|
| 1532 |
torch.Tensor
|
|
@@ -1570,16 +1593,22 @@ def convert_result(
|
|
| 1570 |
# ============================================================
|
| 1571 |
|
| 1572 |
def swap_single_image(
|
|
|
|
| 1573 |
source_tensor,
|
|
|
|
| 1574 |
target_path,
|
|
|
|
| 1575 |
selected_face_index,
|
|
|
|
| 1576 |
swap_model,
|
|
|
|
| 1577 |
face_restore_model,
|
|
|
|
| 1578 |
restore_strength
|
| 1579 |
):
|
| 1580 |
|
| 1581 |
print(
|
| 1582 |
-
"======================================
|
| 1583 |
)
|
| 1584 |
|
| 1585 |
print(
|
|
@@ -1612,11 +1641,11 @@ def swap_single_image(
|
|
| 1612 |
)
|
| 1613 |
|
| 1614 |
print(
|
| 1615 |
-
"======================================
|
| 1616 |
)
|
| 1617 |
|
| 1618 |
# --------------------------------------------------------
|
| 1619 |
-
#
|
| 1620 |
# --------------------------------------------------------
|
| 1621 |
|
| 1622 |
ensure_model(
|
|
@@ -1633,7 +1662,7 @@ def swap_single_image(
|
|
| 1633 |
)
|
| 1634 |
|
| 1635 |
# --------------------------------------------------------
|
| 1636 |
-
#
|
| 1637 |
# --------------------------------------------------------
|
| 1638 |
|
| 1639 |
target_data = (
|
|
@@ -1650,7 +1679,7 @@ def swap_single_image(
|
|
| 1650 |
)
|
| 1651 |
|
| 1652 |
# --------------------------------------------------------
|
| 1653 |
-
# Restore
|
| 1654 |
# --------------------------------------------------------
|
| 1655 |
|
| 1656 |
restore_model = (
|
|
@@ -1733,11 +1762,17 @@ def swap_single_image(
|
|
| 1733 |
# ============================================================
|
| 1734 |
|
| 1735 |
def process_gif(
|
|
|
|
| 1736 |
source_tensor,
|
|
|
|
| 1737 |
target_path,
|
|
|
|
| 1738 |
selected_face_index,
|
|
|
|
| 1739 |
swap_model,
|
|
|
|
| 1740 |
face_restore_model,
|
|
|
|
| 1741 |
restore_strength
|
| 1742 |
):
|
| 1743 |
|
|
@@ -1777,6 +1812,10 @@ def process_gif(
|
|
| 1777 |
frame_number + 1
|
| 1778 |
)
|
| 1779 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1780 |
frame_image = (
|
| 1781 |
Image.fromarray(
|
| 1782 |
frame
|
|
@@ -1840,10 +1879,18 @@ def process_gif(
|
|
| 1840 |
pass
|
| 1841 |
|
| 1842 |
del frame
|
| 1843 |
-
|
| 1844 |
del frame_image
|
| 1845 |
|
| 1846 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1847 |
|
| 1848 |
finally:
|
| 1849 |
|
|
@@ -1860,7 +1907,9 @@ def process_gif(
|
|
| 1860 |
# ============================================================
|
| 1861 |
|
| 1862 |
def make_output_path(
|
|
|
|
| 1863 |
source_path,
|
|
|
|
| 1864 |
target_path
|
| 1865 |
):
|
| 1866 |
|
|
@@ -1898,11 +1947,17 @@ def make_output_path(
|
|
| 1898 |
# ============================================================
|
| 1899 |
|
| 1900 |
def generate_image(
|
|
|
|
| 1901 |
source_files,
|
|
|
|
| 1902 |
target_files,
|
|
|
|
| 1903 |
selected_face_index,
|
|
|
|
| 1904 |
swap_model,
|
|
|
|
| 1905 |
face_restore_model,
|
|
|
|
| 1906 |
restore_strength
|
| 1907 |
):
|
| 1908 |
|
|
@@ -1915,7 +1970,7 @@ def generate_image(
|
|
| 1915 |
)
|
| 1916 |
|
| 1917 |
# --------------------------------------------------------
|
| 1918 |
-
#
|
| 1919 |
# --------------------------------------------------------
|
| 1920 |
|
| 1921 |
if not source_paths:
|
|
@@ -1993,7 +2048,7 @@ def generate_image(
|
|
| 1993 |
# --------------------------------------------------------
|
| 1994 |
|
| 1995 |
print(
|
| 1996 |
-
"======================================
|
| 1997 |
)
|
| 1998 |
|
| 1999 |
print(
|
|
@@ -2031,7 +2086,7 @@ def generate_image(
|
|
| 2031 |
)
|
| 2032 |
|
| 2033 |
print(
|
| 2034 |
-
"======================================
|
| 2035 |
)
|
| 2036 |
|
| 2037 |
os.makedirs(
|
|
@@ -2042,7 +2097,7 @@ def generate_image(
|
|
| 2042 |
output_paths = []
|
| 2043 |
|
| 2044 |
# --------------------------------------------------------
|
| 2045 |
-
# Download
|
| 2046 |
# --------------------------------------------------------
|
| 2047 |
|
| 2048 |
ensure_model(
|
|
@@ -2106,9 +2161,9 @@ def generate_image(
|
|
| 2106 |
)
|
| 2107 |
)
|
| 2108 |
|
| 2109 |
-
# ============================================
|
| 2110 |
# GIF
|
| 2111 |
-
# ============================================
|
| 2112 |
|
| 2113 |
if target_path.lower().endswith(
|
| 2114 |
".gif"
|
|
@@ -2160,12 +2215,11 @@ def generate_image(
|
|
| 2160 |
)
|
| 2161 |
|
| 2162 |
del output_frames
|
| 2163 |
-
|
| 2164 |
del durations
|
| 2165 |
|
| 2166 |
-
# ============================================
|
| 2167 |
# NORMAL IMAGE
|
| 2168 |
-
# ============================================
|
| 2169 |
|
| 2170 |
else:
|
| 2171 |
|
|
@@ -2204,7 +2258,6 @@ def generate_image(
|
|
| 2204 |
)
|
| 2205 |
|
| 2206 |
del source_tensor
|
| 2207 |
-
|
| 2208 |
del source_data
|
| 2209 |
|
| 2210 |
gc.collect()
|
|
@@ -2254,9 +2307,7 @@ def use_output_and_preview(
|
|
| 2254 |
valid_paths = [
|
| 2255 |
p
|
| 2256 |
for p in paths
|
| 2257 |
-
if os.path.isfile(
|
| 2258 |
-
p
|
| 2259 |
-
)
|
| 2260 |
]
|
| 2261 |
|
| 2262 |
if not valid_paths:
|
|
@@ -2316,9 +2367,7 @@ def clear_target():
|
|
| 2316 |
[],
|
| 2317 |
|
| 2318 |
gr.update(
|
| 2319 |
-
|
| 2320 |
choices=[],
|
| 2321 |
-
|
| 2322 |
value=None
|
| 2323 |
)
|
| 2324 |
)
|
|
@@ -2337,9 +2386,7 @@ def reset_face_selection():
|
|
| 2337 |
[],
|
| 2338 |
|
| 2339 |
gr.update(
|
| 2340 |
-
|
| 2341 |
choices=[],
|
| 2342 |
-
|
| 2343 |
value=None
|
| 2344 |
)
|
| 2345 |
)
|
|
@@ -2365,11 +2412,23 @@ with gr.Blocks(
|
|
| 2365 |
|
| 2366 |
**Source → Target → Preview Face → Generate → Dùng kết quả làm Target → Swap tiếp**
|
| 2367 |
|
| 2368 |
-
###
|
| 2369 |
|
| 2370 |
-
- CPU-only
|
| 2371 |
- ComfyUI embedded
|
| 2372 |
-
- ComfyUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2373 |
- Không tải toàn bộ model khi startup
|
| 2374 |
- Chỉ tải model đang chọn
|
| 2375 |
- Face detection cache giới hạn
|
|
@@ -2578,6 +2637,7 @@ Không cần tải ảnh xuống rồi upload lại.
|
|
| 2578 |
inputs=[
|
| 2579 |
|
| 2580 |
target_files
|
|
|
|
| 2581 |
],
|
| 2582 |
|
| 2583 |
outputs=[
|
|
@@ -2611,7 +2671,7 @@ Không cần tải ảnh xuống rồi upload lại.
|
|
| 2611 |
)
|
| 2612 |
|
| 2613 |
# ========================================================
|
| 2614 |
-
# GENERATE
|
| 2615 |
# ========================================================
|
| 2616 |
|
| 2617 |
generate_btn.click(
|
|
@@ -2700,28 +2760,27 @@ app.queue(
|
|
| 2700 |
# LAUNCH
|
| 2701 |
# ============================================================
|
| 2702 |
|
| 2703 |
-
print(
|
| 2704 |
-
"============================================================"
|
| 2705 |
-
)
|
| 2706 |
|
| 2707 |
print(
|
| 2708 |
"[APP] Starting Gradio..."
|
| 2709 |
)
|
| 2710 |
|
| 2711 |
print(
|
| 2712 |
-
"[APP]
|
| 2713 |
-
COMFYUI_ROOT
|
| 2714 |
)
|
| 2715 |
|
| 2716 |
print(
|
| 2717 |
-
"[APP]
|
| 2718 |
-
REACTOR_PATH
|
| 2719 |
)
|
| 2720 |
|
| 2721 |
print(
|
| 2722 |
-
"
|
| 2723 |
)
|
| 2724 |
|
|
|
|
|
|
|
|
|
|
| 2725 |
app.launch(
|
| 2726 |
share=True
|
| 2727 |
)
|
|
|
|
| 3 |
# ============================================================
|
| 4 |
# FORCE CPU
|
| 5 |
# ============================================================
|
| 6 |
+
# MUST be before torch / onnxruntime / ComfyUI imports.
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
|
|
|
| 11 |
os.environ["ORT_DISABLE_CUDA"] = "1"
|
| 12 |
|
| 13 |
# ============================================================
|
| 14 |
+
# STANDARD LIBRARY
|
| 15 |
# ============================================================
|
| 16 |
|
| 17 |
import sys
|
|
|
|
| 19 |
import gc
|
| 20 |
import imageio
|
| 21 |
import tempfile
|
| 22 |
+
import importlib
|
| 23 |
from collections import OrderedDict
|
| 24 |
|
| 25 |
+
# ============================================================
|
| 26 |
+
# THIRD PARTY
|
| 27 |
+
# ============================================================
|
| 28 |
+
|
| 29 |
import numpy as np
|
| 30 |
import cv2
|
| 31 |
import gradio as gr
|
|
|
|
| 38 |
# CONFIG
|
| 39 |
# ============================================================
|
| 40 |
|
| 41 |
+
BASE_DIR = os.path.abspath(
|
| 42 |
+
os.path.dirname(__file__)
|
| 43 |
)
|
| 44 |
|
| 45 |
MODEL_DIR = os.path.join(
|
|
|
|
| 67 |
"output"
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# ------------------------------------------------------------
|
| 71 |
+
# ComfyUI is embedded directly in /app
|
| 72 |
+
# ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
COMFYUI_ROOT = BASE_DIR
|
| 75 |
|
| 76 |
+
# ------------------------------------------------------------
|
| 77 |
+
# ReActor
|
| 78 |
+
# ------------------------------------------------------------
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
REACTOR_PATH = os.path.join(
|
| 81 |
+
COMFYUI_ROOT,
|
| 82 |
+
"custom_nodes",
|
| 83 |
+
"comfyui-reactor-node"
|
| 84 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
if not os.path.isdir(REACTOR_PATH):
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
REACTOR_PATH = os.path.join(
|
|
|
|
| 89 |
BASE_DIR,
|
| 90 |
"custom_nodes",
|
| 91 |
"comfyui-reactor-node"
|
| 92 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
# ============================================================
|
| 96 |
+
# PRINT APP INFO
|
| 97 |
# ============================================================
|
| 98 |
|
| 99 |
+
print("=" * 60)
|
| 100 |
|
| 101 |
+
print(
|
| 102 |
+
"[APP] BASE DIR:",
|
| 103 |
+
BASE_DIR
|
| 104 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
print(
|
| 107 |
+
"[APP] MODEL DIR:",
|
| 108 |
+
MODEL_DIR
|
| 109 |
+
)
|
|
|
|
| 110 |
|
| 111 |
+
print(
|
| 112 |
+
"[APP] OUTPUT DIR:",
|
| 113 |
+
OUTPUT_DIR
|
| 114 |
+
)
|
|
|
|
| 115 |
|
| 116 |
+
print("=" * 60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
# ============================================================
|
|
|
|
| 147 |
|
| 148 |
|
| 149 |
# ============================================================
|
| 150 |
+
# MODEL DEFINITIONS
|
| 151 |
# ============================================================
|
| 152 |
|
| 153 |
+
MODEL_REPOS = {
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
"inswapper_128.onnx": (
|
| 156 |
+
"ezioruan/inswapper_128.onnx",
|
| 157 |
+
"inswapper_128.onnx",
|
| 158 |
+
INSIGHTFACE_DIR
|
| 159 |
+
),
|
| 160 |
|
| 161 |
+
"hyperswap_1a_256.onnx": (
|
| 162 |
+
"facefusion/models-3.3.0",
|
| 163 |
+
"hyperswap_1a_256.onnx",
|
| 164 |
+
HYPERSWAP_DIR
|
| 165 |
+
),
|
| 166 |
|
| 167 |
+
"hyperswap_1b_256.onnx": (
|
| 168 |
+
"facefusion/models-3.3.0",
|
| 169 |
+
"hyperswap_1b_256.onnx",
|
| 170 |
+
HYPERSWAP_DIR
|
| 171 |
+
),
|
| 172 |
|
| 173 |
+
"hyperswap_1c_256.onnx": (
|
| 174 |
+
"facefusion/models-3.3.0",
|
| 175 |
+
"hyperswap_1c_256.onnx",
|
| 176 |
+
HYPERSWAP_DIR
|
| 177 |
+
),
|
| 178 |
+
|
| 179 |
+
"GPEN-BFR-512.onnx": (
|
| 180 |
+
"martintomov/comfy",
|
| 181 |
+
"facerestore_models/GPEN-BFR-512.onnx",
|
| 182 |
+
MODEL_DIR
|
| 183 |
+
),
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
# ============================================================
|
| 188 |
+
# MODEL CACHE
|
| 189 |
+
# ============================================================
|
| 190 |
+
|
| 191 |
+
loaded_models = {}
|
| 192 |
|
| 193 |
|
| 194 |
# ============================================================
|
|
|
|
| 238 |
exist_ok=True
|
| 239 |
)
|
| 240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
print(
|
| 242 |
"[MODEL] Downloading:",
|
| 243 |
filename
|
| 244 |
)
|
| 245 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
downloaded_path = hf_hub_download(
|
| 247 |
repo_id=repo,
|
| 248 |
filename=filename,
|
|
|
|
| 258 |
f"{downloaded_path}"
|
| 259 |
)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
print(
|
| 262 |
"[MODEL] Ready:",
|
| 263 |
+
downloaded_path
|
| 264 |
)
|
| 265 |
|
| 266 |
+
return downloaded_path
|
| 267 |
|
| 268 |
|
| 269 |
# ============================================================
|
| 270 |
+
# ENSURE MODEL
|
| 271 |
# ============================================================
|
| 272 |
|
|
|
|
|
|
|
|
|
|
| 273 |
def ensure_model(
|
| 274 |
model_name
|
| 275 |
):
|
| 276 |
|
| 277 |
+
if not model_name:
|
| 278 |
+
return None
|
| 279 |
+
|
| 280 |
+
if model_name == "none":
|
| 281 |
+
return None
|
| 282 |
+
|
| 283 |
+
# --------------------------------------------------------
|
| 284 |
+
# Cache
|
| 285 |
+
# --------------------------------------------------------
|
| 286 |
+
|
| 287 |
if model_name in loaded_models:
|
| 288 |
|
| 289 |
cached_path = (
|
|
|
|
| 298 |
|
| 299 |
return cached_path
|
| 300 |
|
| 301 |
+
# --------------------------------------------------------
|
| 302 |
+
# Download
|
| 303 |
+
# --------------------------------------------------------
|
| 304 |
+
|
| 305 |
path = download_model(
|
| 306 |
model_name
|
| 307 |
)
|
|
|
|
| 322 |
|
| 323 |
|
| 324 |
# ============================================================
|
| 325 |
+
# COMFYUI PATH SETUP
|
| 326 |
# ============================================================
|
| 327 |
|
| 328 |
+
def setup_comfyui_path():
|
|
|
|
|
|
|
| 329 |
|
| 330 |
+
print(
|
| 331 |
+
"[COMFYUI] Root:",
|
| 332 |
+
COMFYUI_ROOT
|
| 333 |
+
)
|
| 334 |
|
| 335 |
+
# --------------------------------------------------------
|
| 336 |
+
# IMPORTANT
|
| 337 |
+
#
|
| 338 |
+
# ComfyUI itself is /app.
|
| 339 |
+
# Put it FIRST.
|
| 340 |
+
# --------------------------------------------------------
|
| 341 |
|
| 342 |
+
if COMFYUI_ROOT in sys.path:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
+
sys.path.remove(
|
| 345 |
+
COMFYUI_ROOT
|
| 346 |
+
)
|
|
|
|
| 347 |
|
| 348 |
+
sys.path.insert(
|
| 349 |
+
0,
|
| 350 |
+
COMFYUI_ROOT
|
| 351 |
+
)
|
| 352 |
|
| 353 |
+
# --------------------------------------------------------
|
| 354 |
+
# Remove possible wrong ComfyUI paths.
|
| 355 |
+
# --------------------------------------------------------
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
for p in list(sys.path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
+
if not p:
|
| 360 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
try:
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
+
absolute = os.path.abspath(
|
| 365 |
+
p
|
| 366 |
+
)
|
| 367 |
|
| 368 |
+
except Exception:
|
| 369 |
|
| 370 |
+
continue
|
|
|
|
|
|
|
| 371 |
|
| 372 |
+
if (
|
| 373 |
+
absolute != COMFYUI_ROOT
|
| 374 |
+
and
|
| 375 |
+
os.path.basename(
|
| 376 |
+
absolute
|
| 377 |
+
).lower() == "comfyui"
|
| 378 |
+
):
|
| 379 |
|
| 380 |
+
try:
|
| 381 |
+
sys.path.remove(
|
| 382 |
+
p
|
| 383 |
+
)
|
| 384 |
+
except ValueError:
|
| 385 |
+
pass
|
| 386 |
|
| 387 |
+
print(
|
| 388 |
+
"[COMFYUI] sys.path[0]:",
|
| 389 |
+
sys.path[0]
|
| 390 |
+
)
|
| 391 |
|
|
|
|
| 392 |
|
| 393 |
+
setup_comfyui_path()
|
|
|
|
|
|
|
| 394 |
|
|
|
|
| 395 |
|
| 396 |
+
# ============================================================
|
| 397 |
+
# COMFYUI VERSION
|
| 398 |
+
# ============================================================
|
| 399 |
|
| 400 |
+
try:
|
| 401 |
|
| 402 |
+
import comfyui_version
|
| 403 |
|
| 404 |
+
comfy_version = getattr(
|
| 405 |
+
comfyui_version,
|
| 406 |
+
"__version__",
|
| 407 |
+
None
|
| 408 |
+
)
|
| 409 |
|
| 410 |
+
if comfy_version is None:
|
| 411 |
|
| 412 |
+
comfy_version = getattr(
|
| 413 |
+
comfyui_version,
|
| 414 |
+
"VERSION",
|
| 415 |
+
"unknown"
|
| 416 |
)
|
| 417 |
|
| 418 |
+
except Exception:
|
| 419 |
+
|
| 420 |
+
comfy_version = "unknown"
|
| 421 |
|
|
|
|
| 422 |
|
| 423 |
+
print(
|
| 424 |
+
"[COMFYUI] Version:",
|
| 425 |
+
comfy_version
|
| 426 |
+
)
|
| 427 |
|
| 428 |
|
| 429 |
# ============================================================
|
| 430 |
+
# SHOW COMFYUI ROOT
|
| 431 |
# ============================================================
|
| 432 |
|
| 433 |
+
try:
|
|
|
|
|
|
|
| 434 |
|
| 435 |
+
print(
|
| 436 |
+
"[COMFYUI] Root files:",
|
| 437 |
+
[
|
| 438 |
+
x
|
| 439 |
+
for x in os.listdir(
|
| 440 |
+
COMFYUI_ROOT
|
| 441 |
+
)
|
| 442 |
+
if x in (
|
| 443 |
+
"comfy",
|
| 444 |
+
"nodes.py",
|
| 445 |
+
"execution.py",
|
| 446 |
+
"server.py",
|
| 447 |
+
"folder_paths.py",
|
| 448 |
+
"comfyui_version.py",
|
| 449 |
+
"utils"
|
| 450 |
+
)
|
| 451 |
+
]
|
| 452 |
)
|
| 453 |
|
| 454 |
+
except Exception:
|
| 455 |
+
pass
|
| 456 |
|
|
|
|
| 457 |
|
| 458 |
+
# ============================================================
|
| 459 |
+
# FIX PYTHON "utils" MODULE COLLISION
|
| 460 |
+
# ============================================================
|
| 461 |
+
#
|
| 462 |
+
# This is the critical fix for:
|
| 463 |
+
#
|
| 464 |
+
# ModuleNotFoundError:
|
| 465 |
+
# No module named 'utils.install_util';
|
| 466 |
+
# 'utils' is not a package
|
| 467 |
+
#
|
| 468 |
+
# ComfyUI expects:
|
| 469 |
+
#
|
| 470 |
+
# /app/utils/__init__.py
|
| 471 |
+
# /app/utils/install_util.py
|
| 472 |
+
#
|
| 473 |
+
# but another module named "utils" may already be loaded.
|
| 474 |
+
# ============================================================
|
| 475 |
+
|
| 476 |
+
def fix_comfy_utils_namespace():
|
| 477 |
+
|
| 478 |
+
utils_dir = os.path.join(
|
| 479 |
+
COMFYUI_ROOT,
|
| 480 |
+
"utils"
|
| 481 |
)
|
| 482 |
|
| 483 |
+
if not os.path.isdir(
|
| 484 |
+
utils_dir
|
| 485 |
+
):
|
|
|
|
| 486 |
|
| 487 |
+
print(
|
| 488 |
+
"[COMFYUI] WARNING: "
|
| 489 |
+
"utils directory not found:",
|
| 490 |
+
utils_dir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
)
|
|
|
|
|
|
|
| 492 |
|
| 493 |
+
return
|
| 494 |
|
| 495 |
+
print(
|
| 496 |
+
"[COMFYUI] utils directory:",
|
| 497 |
+
utils_dir
|
| 498 |
+
)
|
| 499 |
|
| 500 |
+
# --------------------------------------------------------
|
| 501 |
+
# Remove bad utils module.
|
| 502 |
+
# --------------------------------------------------------
|
| 503 |
|
| 504 |
+
existing_utils = sys.modules.get(
|
| 505 |
+
"utils"
|
| 506 |
+
)
|
| 507 |
|
| 508 |
+
if existing_utils is not None:
|
| 509 |
+
|
| 510 |
+
existing_file = getattr(
|
| 511 |
+
existing_utils,
|
| 512 |
+
"__file__",
|
| 513 |
+
None
|
| 514 |
)
|
|
|
|
| 515 |
|
| 516 |
+
existing_path = getattr(
|
| 517 |
+
existing_utils,
|
| 518 |
+
"__path__",
|
| 519 |
+
None
|
| 520 |
+
)
|
| 521 |
+
|
| 522 |
+
print(
|
| 523 |
+
"[COMFYUI] Existing utils:",
|
| 524 |
+
existing_file
|
| 525 |
+
)
|
| 526 |
+
|
| 527 |
+
# If it is NOT a package, remove it.
|
| 528 |
+
if existing_path is None:
|
| 529 |
+
|
| 530 |
+
print(
|
| 531 |
+
"[COMFYUI] Removing conflicting "
|
| 532 |
+
"'utils' module"
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
try:
|
| 536 |
+
|
| 537 |
+
del sys.modules[
|
| 538 |
+
"utils"
|
| 539 |
+
]
|
| 540 |
+
|
| 541 |
+
except KeyError:
|
| 542 |
+
pass
|
| 543 |
+
|
| 544 |
+
# --------------------------------------------------------
|
| 545 |
+
# Make sure /app is first.
|
| 546 |
+
# --------------------------------------------------------
|
| 547 |
+
|
| 548 |
+
if COMFYUI_ROOT in sys.path:
|
| 549 |
+
|
| 550 |
+
try:
|
| 551 |
+
|
| 552 |
+
sys.path.remove(
|
| 553 |
+
COMFYUI_ROOT
|
| 554 |
+
)
|
| 555 |
+
|
| 556 |
+
except ValueError:
|
| 557 |
+
pass
|
| 558 |
|
| 559 |
+
sys.path.insert(
|
| 560 |
+
0,
|
| 561 |
+
COMFYUI_ROOT
|
| 562 |
)
|
| 563 |
|
| 564 |
+
# --------------------------------------------------------
|
| 565 |
+
# Import correct package if possible.
|
| 566 |
+
# --------------------------------------------------------
|
| 567 |
|
| 568 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
|
| 570 |
+
importlib.invalidate_caches()
|
| 571 |
|
| 572 |
+
import utils
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
+
print(
|
| 575 |
+
"[COMFYUI] utils loaded from:",
|
| 576 |
+
getattr(
|
| 577 |
+
utils,
|
| 578 |
+
"__file__",
|
| 579 |
+
None
|
| 580 |
+
)
|
| 581 |
+
)
|
| 582 |
|
| 583 |
+
print(
|
| 584 |
+
"[COMFYUI] utils package path:",
|
| 585 |
+
getattr(
|
| 586 |
+
utils,
|
| 587 |
+
"__path__",
|
| 588 |
+
None
|
| 589 |
+
)
|
| 590 |
+
)
|
| 591 |
|
| 592 |
+
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
|
| 594 |
+
print(
|
| 595 |
+
"[COMFYUI] utils import warning:",
|
| 596 |
+
repr(e)
|
| 597 |
+
)
|
| 598 |
|
|
|
|
|
|
|
|
|
|
| 599 |
|
| 600 |
+
fix_comfy_utils_namespace()
|
| 601 |
|
|
|
|
| 602 |
|
| 603 |
+
# ============================================================
|
| 604 |
+
# EXTRA MODEL PATHS
|
| 605 |
+
# ============================================================
|
| 606 |
|
| 607 |
def add_extra_model_paths():
|
| 608 |
|
| 609 |
+
extra_model_paths = os.path.join(
|
| 610 |
+
COMFYUI_ROOT,
|
| 611 |
+
"extra_model_paths.yaml"
|
| 612 |
)
|
| 613 |
|
| 614 |
+
if not os.path.isfile(
|
| 615 |
+
extra_model_paths
|
| 616 |
+
):
|
| 617 |
|
| 618 |
print(
|
| 619 |
"[COMFYUI] No extra_model_paths.yaml"
|
|
|
|
| 621 |
|
| 622 |
return
|
| 623 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
try:
|
| 625 |
|
| 626 |
from main import (
|
| 627 |
load_extra_path_config
|
| 628 |
)
|
| 629 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
except ImportError:
|
| 631 |
|
| 632 |
try:
|
|
|
|
| 635 |
load_extra_path_config
|
| 636 |
)
|
| 637 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
except Exception as e:
|
| 639 |
|
| 640 |
print(
|
| 641 |
+
"[COMFYUI] Could not import "
|
| 642 |
+
"extra path config:",
|
| 643 |
+
repr(e)
|
| 644 |
)
|
| 645 |
|
| 646 |
+
return
|
| 647 |
+
|
| 648 |
+
print(
|
| 649 |
+
"[COMFYUI] Extra model paths:",
|
| 650 |
+
extra_model_paths
|
| 651 |
+
)
|
| 652 |
+
|
| 653 |
+
try:
|
| 654 |
+
|
| 655 |
+
load_extra_path_config(
|
| 656 |
+
extra_model_paths
|
| 657 |
+
)
|
| 658 |
+
|
| 659 |
except Exception as e:
|
| 660 |
|
| 661 |
print(
|
| 662 |
+
"[COMFYUI] Extra model paths "
|
| 663 |
+
"warning:",
|
| 664 |
+
repr(e)
|
| 665 |
)
|
| 666 |
|
| 667 |
|
|
|
|
| 669 |
|
| 670 |
|
| 671 |
# ============================================================
|
| 672 |
+
# TORCH
|
| 673 |
# ============================================================
|
| 674 |
|
| 675 |
import torch
|
| 676 |
|
| 677 |
+
|
| 678 |
+
# ============================================================
|
| 679 |
+
# COMFY CPU
|
| 680 |
+
# ============================================================
|
| 681 |
+
|
| 682 |
import comfy.model_management
|
| 683 |
|
| 684 |
from comfy.model_management import (
|
| 685 |
CPUState
|
| 686 |
)
|
| 687 |
|
| 688 |
+
# ------------------------------------------------------------
|
| 689 |
+
# Force CPU
|
| 690 |
+
# ------------------------------------------------------------
|
| 691 |
+
|
| 692 |
+
try:
|
| 693 |
+
|
| 694 |
+
comfy.model_management.cpu_state = (
|
| 695 |
+
CPUState.CPU
|
| 696 |
+
)
|
| 697 |
+
|
| 698 |
+
except Exception as e:
|
| 699 |
+
|
| 700 |
+
print(
|
| 701 |
+
"[DEVICE] CPU state warning:",
|
| 702 |
+
repr(e)
|
| 703 |
+
)
|
| 704 |
|
| 705 |
print(
|
| 706 |
"[DEVICE] Forced CPU"
|
|
|
|
| 713 |
|
| 714 |
|
| 715 |
# ============================================================
|
| 716 |
+
# LOAD COMFYUI NODES
|
| 717 |
# ============================================================
|
| 718 |
|
| 719 |
def import_custom_nodes():
|
| 720 |
|
| 721 |
+
print(
|
| 722 |
+
"[COMFYUI] Initializing nodes..."
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
# --------------------------------------------------------
|
| 726 |
+
# IMPORTANT:
|
| 727 |
+
#
|
| 728 |
+
# Fix utils AGAIN immediately before importing
|
| 729 |
+
# ComfyUI modules that depend on utils.install_util.
|
| 730 |
+
# --------------------------------------------------------
|
| 731 |
+
|
| 732 |
+
fix_comfy_utils_namespace()
|
| 733 |
+
|
| 734 |
import execution
|
| 735 |
|
| 736 |
from nodes import (
|
| 737 |
init_extra_nodes
|
| 738 |
)
|
| 739 |
|
| 740 |
+
# --------------------------------------------------------
|
| 741 |
+
# DO NOT import server before the namespace fix.
|
| 742 |
+
# --------------------------------------------------------
|
| 743 |
+
|
| 744 |
import server
|
| 745 |
|
| 746 |
# --------------------------------------------------------
|
| 747 |
+
# ComfyUI uses one asyncio loop.
|
| 748 |
# --------------------------------------------------------
|
| 749 |
|
| 750 |
loop = asyncio.new_event_loop()
|
|
|
|
| 753 |
loop
|
| 754 |
)
|
| 755 |
|
| 756 |
+
try:
|
|
|
|
|
|
|
| 757 |
|
| 758 |
+
server_instance = (
|
| 759 |
+
server.PromptServer(
|
| 760 |
+
loop
|
| 761 |
+
)
|
| 762 |
)
|
|
|
|
| 763 |
|
| 764 |
+
# ----------------------------------------------------
|
| 765 |
+
# PromptServer already creates a PromptQueue in
|
| 766 |
+
# current ComfyUI versions.
|
| 767 |
+
#
|
| 768 |
+
# Older versions may need explicit creation.
|
| 769 |
+
# ----------------------------------------------------
|
| 770 |
|
| 771 |
+
if not hasattr(
|
| 772 |
+
server_instance,
|
| 773 |
+
"prompt_queue"
|
| 774 |
+
):
|
| 775 |
|
| 776 |
+
execution.PromptQueue(
|
| 777 |
+
server_instance
|
| 778 |
+
)
|
| 779 |
|
| 780 |
+
# ----------------------------------------------------
|
| 781 |
+
# Initialize nodes.
|
| 782 |
+
#
|
| 783 |
+
# ComfyUI 0.3.x uses async init_extra_nodes().
|
| 784 |
+
# ----------------------------------------------------
|
| 785 |
|
| 786 |
+
result = init_extra_nodes()
|
|
|
|
|
|
|
| 787 |
|
| 788 |
+
if asyncio.iscoroutine(
|
| 789 |
+
result
|
| 790 |
+
):
|
| 791 |
|
| 792 |
+
loop.run_until_complete(
|
| 793 |
+
result
|
| 794 |
+
)
|
| 795 |
|
| 796 |
+
finally:
|
| 797 |
|
| 798 |
+
try:
|
| 799 |
|
| 800 |
+
loop.run_until_complete(
|
| 801 |
+
asyncio.sleep(0)
|
| 802 |
+
)
|
| 803 |
|
| 804 |
+
except Exception:
|
| 805 |
+
pass
|
| 806 |
|
| 807 |
+
try:
|
| 808 |
|
| 809 |
+
loop.close()
|
| 810 |
|
| 811 |
+
except Exception:
|
| 812 |
+
pass
|
| 813 |
|
| 814 |
+
try:
|
|
|
|
|
|
|
| 815 |
|
| 816 |
+
asyncio.set_event_loop(
|
| 817 |
+
None
|
| 818 |
+
)
|
| 819 |
|
| 820 |
+
except Exception:
|
| 821 |
+
pass
|
| 822 |
+
|
| 823 |
+
print(
|
| 824 |
+
"[COMFYUI] Nodes initialized."
|
| 825 |
+
)
|
| 826 |
+
|
| 827 |
+
|
| 828 |
+
import_custom_nodes()
|
| 829 |
|
| 830 |
|
| 831 |
# ============================================================
|
| 832 |
+
# NODE MAPPINGS
|
| 833 |
# ============================================================
|
| 834 |
|
| 835 |
from nodes import (
|
|
|
|
| 847 |
):
|
| 848 |
|
| 849 |
"""
|
| 850 |
+
Safely extract a ComfyUI node output.
|
| 851 |
"""
|
| 852 |
|
| 853 |
try:
|
|
|
|
| 879 |
|
| 880 |
|
| 881 |
# ============================================================
|
| 882 |
+
# LOAD LOADIMAGE
|
| 883 |
# ============================================================
|
| 884 |
|
| 885 |
+
try:
|
| 886 |
|
| 887 |
+
loadimage = (
|
| 888 |
+
NODE_CLASS_MAPPINGS[
|
| 889 |
+
"LoadImage"
|
| 890 |
+
]()
|
| 891 |
+
)
|
| 892 |
|
| 893 |
+
except Exception as e:
|
| 894 |
+
|
| 895 |
+
raise RuntimeError(
|
| 896 |
+
"Không load được "
|
| 897 |
+
"ComfyUI LoadImage:\n"
|
| 898 |
+
f"{e}"
|
| 899 |
+
)
|
| 900 |
|
|
|
|
| 901 |
|
| 902 |
+
# ============================================================
|
| 903 |
+
# VERIFY REACTOR
|
| 904 |
+
# ============================================================
|
| 905 |
|
| 906 |
+
if not os.path.isdir(
|
| 907 |
+
REACTOR_PATH
|
| 908 |
+
):
|
| 909 |
|
| 910 |
raise RuntimeError(
|
| 911 |
+
"Không tìm thấy "
|
| 912 |
+
"ComfyUI-ReActor tại:\n"
|
| 913 |
+
f"{REACTOR_PATH}"
|
|
|
|
|
|
|
|
|
|
| 914 |
)
|
| 915 |
|
|
|
|
| 916 |
if REACTOR_PATH not in sys.path:
|
| 917 |
|
| 918 |
sys.path.insert(
|
|
|
|
| 920 |
REACTOR_PATH
|
| 921 |
)
|
| 922 |
|
|
|
|
| 923 |
print(
|
| 924 |
"[REACTOR] Path:",
|
| 925 |
REACTOR_PATH
|
| 926 |
)
|
| 927 |
|
| 928 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 929 |
# ============================================================
|
| 930 |
# LOAD REACTOR
|
| 931 |
# ============================================================
|
|
|
|
| 947 |
)
|
| 948 |
|
| 949 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 950 |
# ============================================================
|
| 951 |
# REACTOR FACE FUNCTIONS
|
| 952 |
# ============================================================
|
|
|
|
| 988 |
):
|
| 989 |
|
| 990 |
if not image_path:
|
|
|
|
| 991 |
return []
|
| 992 |
|
| 993 |
if not os.path.isfile(
|
| 994 |
image_path
|
| 995 |
):
|
|
|
|
| 996 |
return []
|
| 997 |
|
| 998 |
try:
|
|
|
|
| 1039 |
return []
|
| 1040 |
|
| 1041 |
# --------------------------------------------------------
|
| 1042 |
+
# Same detection as ReActor
|
| 1043 |
# --------------------------------------------------------
|
| 1044 |
|
| 1045 |
faces = analyze_faces(
|
|
|
|
| 1055 |
faces = []
|
| 1056 |
|
| 1057 |
# --------------------------------------------------------
|
| 1058 |
+
# Same order as ReActor
|
| 1059 |
# --------------------------------------------------------
|
| 1060 |
|
| 1061 |
faces = sort_by_order(
|
|
|
|
| 1092 |
):
|
| 1093 |
|
| 1094 |
if file is None:
|
|
|
|
| 1095 |
return None
|
| 1096 |
|
| 1097 |
if isinstance(
|
|
|
|
| 1125 |
):
|
| 1126 |
|
| 1127 |
if not files:
|
|
|
|
| 1128 |
return []
|
| 1129 |
|
| 1130 |
if isinstance(
|
| 1131 |
files,
|
| 1132 |
+
(
|
| 1133 |
+
str,
|
| 1134 |
+
bytes
|
| 1135 |
+
)
|
| 1136 |
):
|
| 1137 |
|
| 1138 |
files = [
|
|
|
|
| 1171 |
|
| 1172 |
if not image_path:
|
| 1173 |
|
| 1174 |
+
return (
|
| 1175 |
+
None,
|
| 1176 |
+
[]
|
| 1177 |
+
)
|
| 1178 |
|
| 1179 |
img = cv2.imread(
|
| 1180 |
image_path
|
|
|
|
| 1182 |
|
| 1183 |
if img is None:
|
| 1184 |
|
| 1185 |
+
return (
|
| 1186 |
+
None,
|
| 1187 |
+
[]
|
| 1188 |
+
)
|
| 1189 |
|
| 1190 |
height, width = (
|
| 1191 |
img.shape[:2]
|
|
|
|
| 1197 |
|
| 1198 |
if not faces:
|
| 1199 |
|
| 1200 |
+
return (
|
| 1201 |
+
None,
|
| 1202 |
+
[]
|
| 1203 |
+
)
|
| 1204 |
|
| 1205 |
previews = []
|
| 1206 |
|
|
|
|
| 1343 |
)
|
| 1344 |
|
| 1345 |
# ----------------------------------------------------
|
| 1346 |
+
# Label
|
| 1347 |
# ----------------------------------------------------
|
| 1348 |
|
| 1349 |
cv2.rectangle(
|
|
|
|
| 1393 |
|
| 1394 |
if not previews:
|
| 1395 |
|
| 1396 |
+
return (
|
| 1397 |
+
None,
|
| 1398 |
+
[]
|
| 1399 |
+
)
|
| 1400 |
|
| 1401 |
preview = np.hstack(
|
| 1402 |
previews
|
|
|
|
| 1409 |
|
| 1410 |
|
| 1411 |
# ============================================================
|
| 1412 |
+
# GIF PREVIEW
|
| 1413 |
# ============================================================
|
| 1414 |
|
| 1415 |
def get_preview_image(
|
|
|
|
| 1418 |
|
| 1419 |
if not target_path:
|
| 1420 |
|
| 1421 |
+
return (
|
| 1422 |
+
None,
|
| 1423 |
+
[]
|
| 1424 |
+
)
|
| 1425 |
|
| 1426 |
# --------------------------------------------------------
|
| 1427 |
# GIF
|
|
|
|
| 1550 |
image
|
| 1551 |
):
|
| 1552 |
|
|
|
|
|
|
|
|
|
|
| 1553 |
if isinstance(
|
| 1554 |
image,
|
| 1555 |
torch.Tensor
|
|
|
|
| 1593 |
# ============================================================
|
| 1594 |
|
| 1595 |
def swap_single_image(
|
| 1596 |
+
|
| 1597 |
source_tensor,
|
| 1598 |
+
|
| 1599 |
target_path,
|
| 1600 |
+
|
| 1601 |
selected_face_index,
|
| 1602 |
+
|
| 1603 |
swap_model,
|
| 1604 |
+
|
| 1605 |
face_restore_model,
|
| 1606 |
+
|
| 1607 |
restore_strength
|
| 1608 |
):
|
| 1609 |
|
| 1610 |
print(
|
| 1611 |
+
"======================================"
|
| 1612 |
)
|
| 1613 |
|
| 1614 |
print(
|
|
|
|
| 1641 |
)
|
| 1642 |
|
| 1643 |
print(
|
| 1644 |
+
"======================================"
|
| 1645 |
)
|
| 1646 |
|
| 1647 |
# --------------------------------------------------------
|
| 1648 |
+
# Make sure models exist
|
| 1649 |
# --------------------------------------------------------
|
| 1650 |
|
| 1651 |
ensure_model(
|
|
|
|
| 1662 |
)
|
| 1663 |
|
| 1664 |
# --------------------------------------------------------
|
| 1665 |
+
# Target
|
| 1666 |
# --------------------------------------------------------
|
| 1667 |
|
| 1668 |
target_data = (
|
|
|
|
| 1679 |
)
|
| 1680 |
|
| 1681 |
# --------------------------------------------------------
|
| 1682 |
+
# Restore
|
| 1683 |
# --------------------------------------------------------
|
| 1684 |
|
| 1685 |
restore_model = (
|
|
|
|
| 1762 |
# ============================================================
|
| 1763 |
|
| 1764 |
def process_gif(
|
| 1765 |
+
|
| 1766 |
source_tensor,
|
| 1767 |
+
|
| 1768 |
target_path,
|
| 1769 |
+
|
| 1770 |
selected_face_index,
|
| 1771 |
+
|
| 1772 |
swap_model,
|
| 1773 |
+
|
| 1774 |
face_restore_model,
|
| 1775 |
+
|
| 1776 |
restore_strength
|
| 1777 |
):
|
| 1778 |
|
|
|
|
| 1812 |
frame_number + 1
|
| 1813 |
)
|
| 1814 |
|
| 1815 |
+
# ------------------------------------------------
|
| 1816 |
+
# Convert frame
|
| 1817 |
+
# ------------------------------------------------
|
| 1818 |
+
|
| 1819 |
frame_image = (
|
| 1820 |
Image.fromarray(
|
| 1821 |
frame
|
|
|
|
| 1879 |
pass
|
| 1880 |
|
| 1881 |
del frame
|
|
|
|
| 1882 |
del frame_image
|
| 1883 |
|
| 1884 |
+
# ------------------------------------------------
|
| 1885 |
+
# Periodic cleanup
|
| 1886 |
+
# ------------------------------------------------
|
| 1887 |
+
|
| 1888 |
+
if (
|
| 1889 |
+
frame_number % 5
|
| 1890 |
+
== 0
|
| 1891 |
+
):
|
| 1892 |
+
|
| 1893 |
+
gc.collect()
|
| 1894 |
|
| 1895 |
finally:
|
| 1896 |
|
|
|
|
| 1907 |
# ============================================================
|
| 1908 |
|
| 1909 |
def make_output_path(
|
| 1910 |
+
|
| 1911 |
source_path,
|
| 1912 |
+
|
| 1913 |
target_path
|
| 1914 |
):
|
| 1915 |
|
|
|
|
| 1947 |
# ============================================================
|
| 1948 |
|
| 1949 |
def generate_image(
|
| 1950 |
+
|
| 1951 |
source_files,
|
| 1952 |
+
|
| 1953 |
target_files,
|
| 1954 |
+
|
| 1955 |
selected_face_index,
|
| 1956 |
+
|
| 1957 |
swap_model,
|
| 1958 |
+
|
| 1959 |
face_restore_model,
|
| 1960 |
+
|
| 1961 |
restore_strength
|
| 1962 |
):
|
| 1963 |
|
|
|
|
| 1970 |
)
|
| 1971 |
|
| 1972 |
# --------------------------------------------------------
|
| 1973 |
+
# Validate
|
| 1974 |
# --------------------------------------------------------
|
| 1975 |
|
| 1976 |
if not source_paths:
|
|
|
|
| 2048 |
# --------------------------------------------------------
|
| 2049 |
|
| 2050 |
print(
|
| 2051 |
+
"======================================"
|
| 2052 |
)
|
| 2053 |
|
| 2054 |
print(
|
|
|
|
| 2086 |
)
|
| 2087 |
|
| 2088 |
print(
|
| 2089 |
+
"======================================"
|
| 2090 |
)
|
| 2091 |
|
| 2092 |
os.makedirs(
|
|
|
|
| 2097 |
output_paths = []
|
| 2098 |
|
| 2099 |
# --------------------------------------------------------
|
| 2100 |
+
# Download selected models only
|
| 2101 |
# --------------------------------------------------------
|
| 2102 |
|
| 2103 |
ensure_model(
|
|
|
|
| 2161 |
)
|
| 2162 |
)
|
| 2163 |
|
| 2164 |
+
# ============================================
|
| 2165 |
# GIF
|
| 2166 |
+
# ============================================
|
| 2167 |
|
| 2168 |
if target_path.lower().endswith(
|
| 2169 |
".gif"
|
|
|
|
| 2215 |
)
|
| 2216 |
|
| 2217 |
del output_frames
|
|
|
|
| 2218 |
del durations
|
| 2219 |
|
| 2220 |
+
# ============================================
|
| 2221 |
# NORMAL IMAGE
|
| 2222 |
+
# ============================================
|
| 2223 |
|
| 2224 |
else:
|
| 2225 |
|
|
|
|
| 2258 |
)
|
| 2259 |
|
| 2260 |
del source_tensor
|
|
|
|
| 2261 |
del source_data
|
| 2262 |
|
| 2263 |
gc.collect()
|
|
|
|
| 2307 |
valid_paths = [
|
| 2308 |
p
|
| 2309 |
for p in paths
|
| 2310 |
+
if os.path.isfile(p)
|
|
|
|
|
|
|
| 2311 |
]
|
| 2312 |
|
| 2313 |
if not valid_paths:
|
|
|
|
| 2367 |
[],
|
| 2368 |
|
| 2369 |
gr.update(
|
|
|
|
| 2370 |
choices=[],
|
|
|
|
| 2371 |
value=None
|
| 2372 |
)
|
| 2373 |
)
|
|
|
|
| 2386 |
[],
|
| 2387 |
|
| 2388 |
gr.update(
|
|
|
|
| 2389 |
choices=[],
|
|
|
|
| 2390 |
value=None
|
| 2391 |
)
|
| 2392 |
)
|
|
|
|
| 2412 |
|
| 2413 |
**Source → Target → Preview Face → Generate → Dùng kết quả làm Target → Swap tiếp**
|
| 2414 |
|
| 2415 |
+
### ComfyUI
|
| 2416 |
|
|
|
|
| 2417 |
- ComfyUI embedded
|
| 2418 |
+
- ComfyUI `comfyui_version.py`
|
| 2419 |
+
- ReActor custom node
|
| 2420 |
+
- CPU-only
|
| 2421 |
+
- Không chạy ComfyUI web server riêng
|
| 2422 |
+
|
| 2423 |
+
### Mặc định
|
| 2424 |
+
|
| 2425 |
+
- Swap: **inswapper_128.onnx**
|
| 2426 |
+
- Restore: **GPEN-BFR-512.onnx**
|
| 2427 |
+
- Restore strength: **0.70**
|
| 2428 |
+
|
| 2429 |
+
### CPU optimized
|
| 2430 |
+
|
| 2431 |
+
- CPU-only
|
| 2432 |
- Không tải toàn bộ model khi startup
|
| 2433 |
- Chỉ tải model đang chọn
|
| 2434 |
- Face detection cache giới hạn
|
|
|
|
| 2637 |
inputs=[
|
| 2638 |
|
| 2639 |
target_files
|
| 2640 |
+
|
| 2641 |
],
|
| 2642 |
|
| 2643 |
outputs=[
|
|
|
|
| 2671 |
)
|
| 2672 |
|
| 2673 |
# ========================================================
|
| 2674 |
+
# GENERATE EVENT
|
| 2675 |
# ========================================================
|
| 2676 |
|
| 2677 |
generate_btn.click(
|
|
|
|
| 2760 |
# LAUNCH
|
| 2761 |
# ============================================================
|
| 2762 |
|
| 2763 |
+
print("=" * 60)
|
|
|
|
|
|
|
| 2764 |
|
| 2765 |
print(
|
| 2766 |
"[APP] Starting Gradio..."
|
| 2767 |
)
|
| 2768 |
|
| 2769 |
print(
|
| 2770 |
+
"[APP] CPU mode: ENABLED"
|
|
|
|
| 2771 |
)
|
| 2772 |
|
| 2773 |
print(
|
| 2774 |
+
"[APP] ComfyUI: ENABLED"
|
|
|
|
| 2775 |
)
|
| 2776 |
|
| 2777 |
print(
|
| 2778 |
+
"[APP] ReActor: ENABLED"
|
| 2779 |
)
|
| 2780 |
|
| 2781 |
+
print("=" * 60)
|
| 2782 |
+
|
| 2783 |
+
|
| 2784 |
app.launch(
|
| 2785 |
share=True
|
| 2786 |
)
|