From 87fc538fd1cf01857fb4cb602ca43d6d21e6e0c5 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Sun, 7 Jul 2024 09:55:12 +0900 Subject: [PATCH] feat: free model/cache feature on new style menu. --- glob/manager_core.py | 2 +- js/comfyui-manager.js | 67 ++++++++++++++++++++++++++++++++++--------- js/common.js | 48 ++++++++++++++++++++++++------- pyproject.toml | 2 +- 4 files changed, 93 insertions(+), 26 deletions(-) diff --git a/glob/manager_core.py b/glob/manager_core.py index fc08795a..db0d9d50 100644 --- a/glob/manager_core.py +++ b/glob/manager_core.py @@ -23,7 +23,7 @@ sys.path.append(glob_path) import cm_global from manager_util import * -version = [2, 44, 3] +version = [2, 45] version_str = f"V{version[0]}.{version[1]}" + (f'.{version[2]}' if len(version) > 2 else '') diff --git a/js/comfyui-manager.js b/js/comfyui-manager.js index 1390513a..df07d435 100644 --- a/js/comfyui-manager.js +++ b/js/comfyui-manager.js @@ -20,6 +20,23 @@ import { SnapshotManager } from "./snapshot.js"; var docStyle = document.createElement('style'); docStyle.innerHTML = ` +.comfy-toast { + position: fixed; + bottom: 20px; + left: 50%; + transform: translateX(-50%); + background-color: rgba(0, 0, 0, 0.7); + color: white; + padding: 10px 20px; + border-radius: 5px; + z-index: 1000; + transition: opacity 0.5s; +} + +.comfy-toast-fadeout { + opacity: 0; +} + #cm-manager-dialog { width: 1000px; height: 520px; @@ -1288,18 +1305,43 @@ app.registerExtension({ separator.style.width = "100%"; menu.append(separator); - // new style Manager button - app.menu?.saveButton.element.after(new(await import("../../scripts/ui/components/button.js")).ComfyButton({ - icon: "puzzle", - action: () => { - if(!manager_instance) - setManagerInstance(new ManagerMenuDialog()); - manager_instance.show(); - }, - tooltip: "ComfyUI Manager", - content: "ComfyUI Manager", - classList: "comfyui-button comfyui-menu-mobile-collapse primary" - }).element); + try { + // new style Manager button + app.menu?.saveButton.element.after(new(await import("../../scripts/ui/components/button.js")).ComfyButton({ + icon: "puzzle", + action: () => { + if(!manager_instance) + setManagerInstance(new ManagerMenuDialog()); + manager_instance.show(); + }, + tooltip: "ComfyUI Manager", + content: "ComfyUI Manager", + classList: "comfyui-button comfyui-menu-mobile-collapse primary" + }).element); + + // unload models button into new style Manager button + let cmGroup = new (await import("../../scripts/ui/components/buttonGroup.js")).ComfyButtonGroup( + new(await import("../../scripts/ui/components/button.js")).ComfyButton({ + icon: "vacuum-outline", + action: () => { + free_models(); + }, + tooltip: "Unload Models" + }).element, + new(await import("../../scripts/ui/components/button.js")).ComfyButton({ + icon: "vacuum", + action: () => { + free_models(true); + }, + tooltip: "Unload Whole Cache" + }).element + ); + + app.menu?.settingsGroup.element.before(cmGroup.element); + } + catch(exception) { + log.console('ComfyUI is outdated. New style menu based features are disabled.'); + } // old style Manager button const managerButton = document.createElement("button"); @@ -1311,7 +1353,6 @@ app.registerExtension({ } menu.append(managerButton); - const shareButton = document.createElement("button"); shareButton.id = "shareButton"; shareButton.textContent = "Share"; diff --git a/js/common.js b/js/common.js index 419862f0..6d5104dd 100644 --- a/js/common.js +++ b/js/common.js @@ -1,5 +1,6 @@ import { app } from "../../scripts/app.js"; import { api } from "../../scripts/api.js"; +import { $el, ComfyDialog } from "../../scripts/ui.js"; export function show_message(msg) { app.ui.dialog.show(msg); @@ -30,6 +31,15 @@ export function setManagerInstance(obj) { manager_instance = obj; } +export function showToast(message, duration = 3000) { + const toast = $el("div.comfy-toast", {textContent: message}); + document.body.appendChild(toast); + setTimeout(() => { + toast.classList.add("comfy-toast-fadeout"); + setTimeout(() => toast.remove(), 500); + }, duration); +} + function isValidURL(url) { if(url.includes('&')) return false; @@ -106,18 +116,34 @@ export async function install_via_git_url(url, manager_dialog) { } } -export async function free_models() { - let res = await api.fetchApi(`/free`, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: '{"unload_models": true}' - }); +export async function free_models(free_execution_cache) { + try { + let mode = ""; + if(free_execution_cache) { + mode = '{"unload_models": true, "free_memory": true}'; + } + else { + mode = '{"unload_models": true}'; + } - if(res.status == 200) { - show_message('Models have been unloaded.') - } - else { - show_message('Unloading of models failed.

Installed ComfyUI may be an outdated version.') + let res = await api.fetchApi(`/free`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: mode + }); + + if (res.status == 200) { + if(free_execution_cache) { + showToast("'Models' and 'Execution Cache' have been unloaded.", 3000); + } + else { + showToast("Models' have been unloaded.", 3000); + } + } else { + showToast('Unloading of models failed. Installed ComfyUI may be an outdated version.', 5000); + } + } catch (error) { + showToast('An error occurred while trying to unload models.', 5000); } } diff --git a/pyproject.toml b/pyproject.toml index a80af338..9d4ef703 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-manager" description = "ComfyUI-Manager provides features to install and manage custom nodes for ComfyUI, as well as various functionalities to assist with ComfyUI." -version = "2.44.3" +version = "2.45" license = "LICENSE" dependencies = ["GitPython", "PyGithub", "matrix-client==0.4.0", "transformers", "huggingface-hub>0.20", "typer", "rich", "typing-extensions"]