mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-05-06 16:36:45 -03:00
Compare commits
4 Commits
502b7eab31
...
2ac0eb0f9d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ac0eb0f9d | ||
|
|
f028625ce9 | ||
|
|
06acc7f576 | ||
|
|
d324b57274 |
@@ -1,10 +1,22 @@
|
||||
import folder_paths # type: ignore
|
||||
from ..utils.utils import get_lora_info
|
||||
import os
|
||||
from ..utils.utils import get_lora_info_absolute
|
||||
from ..config import config
|
||||
from .utils import FlexibleOptionalInputType, any_type, get_loras_list
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _relpath_within_loras(abs_path):
|
||||
"""Return abs_path relative to the first matching lora root, or basename as fallback."""
|
||||
all_roots = list(config.loras_roots or []) + list(config.extra_loras_roots or [])
|
||||
for root in all_roots:
|
||||
try:
|
||||
return os.path.relpath(abs_path, root)
|
||||
except ValueError:
|
||||
continue
|
||||
return os.path.basename(abs_path)
|
||||
|
||||
class WanVideoLoraSelectLM:
|
||||
NAME = "WanVideo Lora Select (LoraManager)"
|
||||
CATEGORY = "Lora Manager/stackers"
|
||||
@@ -56,13 +68,13 @@ class WanVideoLoraSelectLM:
|
||||
clip_strength = float(lora.get('clipStrength', model_strength))
|
||||
|
||||
# Get lora path and trigger words
|
||||
lora_path, trigger_words = get_lora_info(lora_name)
|
||||
lora_path, trigger_words = get_lora_info_absolute(lora_name)
|
||||
|
||||
# Create lora item for WanVideo format
|
||||
lora_item = {
|
||||
"path": folder_paths.get_full_path("loras", lora_path),
|
||||
"path": lora_path,
|
||||
"strength": model_strength,
|
||||
"name": lora_path.split(".")[0],
|
||||
"name": os.path.splitext(_relpath_within_loras(lora_path))[0],
|
||||
"blocks": selected_blocks,
|
||||
"layer_filter": layer_filter,
|
||||
"low_mem_load": low_mem_load,
|
||||
|
||||
@@ -1,11 +1,23 @@
|
||||
import folder_paths # type: ignore
|
||||
from ..utils.utils import get_lora_info
|
||||
import os
|
||||
from ..utils.utils import get_lora_info_absolute
|
||||
from ..config import config
|
||||
from .utils import any_type
|
||||
import logging
|
||||
|
||||
# 初始化日志记录器
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _relpath_within_loras(abs_path):
|
||||
"""Return abs_path relative to the first matching lora root, or basename as fallback."""
|
||||
all_roots = list(config.loras_roots or []) + list(config.extra_loras_roots or [])
|
||||
for root in all_roots:
|
||||
try:
|
||||
return os.path.relpath(abs_path, root)
|
||||
except ValueError:
|
||||
continue
|
||||
return os.path.basename(abs_path)
|
||||
|
||||
# 定义新节点的类
|
||||
class WanVideoLoraTextSelectLM:
|
||||
# 节点在UI中显示的名称
|
||||
@@ -87,12 +99,12 @@ class WanVideoLoraTextSelectLM:
|
||||
else:
|
||||
continue
|
||||
|
||||
lora_path, trigger_words = get_lora_info(lora_name_raw)
|
||||
lora_path, trigger_words = get_lora_info_absolute(lora_name_raw)
|
||||
|
||||
lora_item = {
|
||||
"path": folder_paths.get_full_path("loras", lora_path),
|
||||
"path": lora_path,
|
||||
"strength": model_strength,
|
||||
"name": lora_path.split(".")[0],
|
||||
"name": os.path.splitext(_relpath_within_loras(lora_path))[0],
|
||||
"blocks": selected_blocks,
|
||||
"layer_filter": layer_filter,
|
||||
"low_mem_load": low_mem_load,
|
||||
|
||||
@@ -1791,15 +1791,19 @@ class ModelLibraryHandler:
|
||||
exists = True
|
||||
model_type = "embedding"
|
||||
|
||||
if exists:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"exists": True,
|
||||
"modelType": model_type,
|
||||
"hasBeenDownloaded": False,
|
||||
}
|
||||
)
|
||||
|
||||
history_service = await self._get_download_history_service()
|
||||
has_been_downloaded = False
|
||||
history_type = model_type
|
||||
if history_type:
|
||||
has_been_downloaded = await history_service.has_been_downloaded(
|
||||
history_type,
|
||||
model_version_id,
|
||||
)
|
||||
else:
|
||||
history_type = None
|
||||
for candidate_type in ("lora", "checkpoint", "embedding"):
|
||||
if await history_service.has_been_downloaded(
|
||||
candidate_type,
|
||||
@@ -1812,8 +1816,8 @@ class ModelLibraryHandler:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"exists": exists,
|
||||
"modelType": model_type if exists else history_type,
|
||||
"exists": False,
|
||||
"modelType": history_type,
|
||||
"hasBeenDownloaded": has_been_downloaded,
|
||||
}
|
||||
)
|
||||
@@ -1833,29 +1837,35 @@ class ModelLibraryHandler:
|
||||
model_type = None
|
||||
versions = []
|
||||
downloaded_version_ids = []
|
||||
history_service = await self._get_download_history_service()
|
||||
if lora_versions:
|
||||
model_type = "lora"
|
||||
versions = self._with_downloaded_flag(lora_versions)
|
||||
downloaded_version_ids = await history_service.get_downloaded_version_ids(
|
||||
model_type,
|
||||
model_id,
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"modelType": "lora",
|
||||
"versions": self._with_downloaded_flag(lora_versions),
|
||||
"downloadedVersionIds": [],
|
||||
}
|
||||
)
|
||||
elif checkpoint_versions:
|
||||
model_type = "checkpoint"
|
||||
versions = self._with_downloaded_flag(checkpoint_versions)
|
||||
downloaded_version_ids = await history_service.get_downloaded_version_ids(
|
||||
model_type,
|
||||
model_id,
|
||||
if checkpoint_versions:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"modelType": "checkpoint",
|
||||
"versions": self._with_downloaded_flag(checkpoint_versions),
|
||||
"downloadedVersionIds": [],
|
||||
}
|
||||
)
|
||||
elif embedding_versions:
|
||||
model_type = "embedding"
|
||||
versions = self._with_downloaded_flag(embedding_versions)
|
||||
downloaded_version_ids = await history_service.get_downloaded_version_ids(
|
||||
model_type,
|
||||
model_id,
|
||||
if embedding_versions:
|
||||
return web.json_response(
|
||||
{
|
||||
"success": True,
|
||||
"modelType": "embedding",
|
||||
"versions": self._with_downloaded_flag(embedding_versions),
|
||||
"downloadedVersionIds": [],
|
||||
}
|
||||
)
|
||||
else:
|
||||
|
||||
history_service = await self._get_download_history_service()
|
||||
for candidate_type in ("lora", "checkpoint", "embedding"):
|
||||
candidate_downloaded_version_ids = (
|
||||
await history_service.get_downloaded_version_ids(
|
||||
@@ -1880,6 +1890,86 @@ class ModelLibraryHandler:
|
||||
logger.error("Failed to check model existence: %s", exc, exc_info=True)
|
||||
return web.json_response({"success": False, "error": str(exc)}, status=500)
|
||||
|
||||
async def check_models_exist(self, request: web.Request) -> web.Response:
|
||||
try:
|
||||
model_ids_raw = request.query.get("modelIds", "")
|
||||
if not model_ids_raw:
|
||||
return web.json_response(
|
||||
{"success": True, "results": []}
|
||||
)
|
||||
|
||||
raw_ids = model_ids_raw.split(",")
|
||||
seen: set[int] = set()
|
||||
model_ids: list[int] = []
|
||||
for raw in raw_ids:
|
||||
stripped = raw.strip()
|
||||
if not stripped:
|
||||
continue
|
||||
try:
|
||||
mid = int(stripped)
|
||||
except ValueError:
|
||||
continue
|
||||
if mid not in seen:
|
||||
seen.add(mid)
|
||||
model_ids.append(mid)
|
||||
|
||||
if not model_ids:
|
||||
return web.json_response(
|
||||
{"success": True, "results": []}
|
||||
)
|
||||
|
||||
lora_scanner = await self._service_registry.get_lora_scanner()
|
||||
checkpoint_scanner = await self._service_registry.get_checkpoint_scanner()
|
||||
embedding_scanner = await self._service_registry.get_embedding_scanner()
|
||||
|
||||
results: list[dict] = []
|
||||
for model_id in model_ids:
|
||||
lora_versions = await lora_scanner.get_model_versions_by_id(model_id)
|
||||
if lora_versions:
|
||||
results.append({
|
||||
"modelId": model_id,
|
||||
"modelType": "lora",
|
||||
"versions": self._with_downloaded_flag(lora_versions),
|
||||
"downloadedVersionIds": [],
|
||||
})
|
||||
continue
|
||||
|
||||
if checkpoint_scanner:
|
||||
checkpoint_versions = await checkpoint_scanner.get_model_versions_by_id(model_id)
|
||||
if checkpoint_versions:
|
||||
results.append({
|
||||
"modelId": model_id,
|
||||
"modelType": "checkpoint",
|
||||
"versions": self._with_downloaded_flag(checkpoint_versions),
|
||||
"downloadedVersionIds": [],
|
||||
})
|
||||
continue
|
||||
|
||||
if embedding_scanner:
|
||||
embedding_versions = await embedding_scanner.get_model_versions_by_id(model_id)
|
||||
if embedding_versions:
|
||||
results.append({
|
||||
"modelId": model_id,
|
||||
"modelType": "embedding",
|
||||
"versions": self._with_downloaded_flag(embedding_versions),
|
||||
"downloadedVersionIds": [],
|
||||
})
|
||||
continue
|
||||
|
||||
results.append({
|
||||
"modelId": model_id,
|
||||
"modelType": None,
|
||||
"versions": [],
|
||||
"downloadedVersionIds": [],
|
||||
})
|
||||
|
||||
return web.json_response(
|
||||
{"success": True, "results": results}
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.error("Failed to check models existence: %s", exc, exc_info=True)
|
||||
return web.json_response({"success": False, "error": str(exc)}, status=500)
|
||||
|
||||
async def get_model_version_download_status(
|
||||
self, request: web.Request
|
||||
) -> web.Response:
|
||||
@@ -3025,6 +3115,7 @@ class MiscHandlerSet:
|
||||
"update_node_widget": self.node_registry.update_node_widget,
|
||||
"get_registry": self.node_registry.get_registry,
|
||||
"check_model_exists": self.model_library.check_model_exists,
|
||||
"check_models_exist": self.model_library.check_models_exist,
|
||||
"get_model_version_download_status": self.model_library.get_model_version_download_status,
|
||||
"set_model_version_download_status": self.model_library.set_model_version_download_status,
|
||||
"get_civitai_user_models": self.model_library.get_civitai_user_models,
|
||||
|
||||
@@ -43,6 +43,7 @@ MISC_ROUTE_DEFINITIONS: tuple[RouteDefinition, ...] = (
|
||||
RouteDefinition("POST", "/api/lm/update-node-widget", "update_node_widget"),
|
||||
RouteDefinition("GET", "/api/lm/get-registry", "get_registry"),
|
||||
RouteDefinition("GET", "/api/lm/check-model-exists", "check_model_exists"),
|
||||
RouteDefinition("GET", "/api/lm/check-models-exist", "check_models_exist"),
|
||||
RouteDefinition(
|
||||
"GET",
|
||||
"/api/lm/model-version-download-status",
|
||||
|
||||
@@ -64,6 +64,7 @@ class DownloadedVersionHistoryService:
|
||||
self._db_path = db_path or _resolve_database_path()
|
||||
self._settings = settings_manager or get_settings_manager()
|
||||
self._lock = asyncio.Lock()
|
||||
self._conn: sqlite3.Connection | None = None
|
||||
self._schema_initialized = False
|
||||
self._ensure_directory()
|
||||
self._initialize_schema()
|
||||
@@ -78,6 +79,12 @@ class DownloadedVersionHistoryService:
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
def _get_conn(self) -> sqlite3.Connection:
|
||||
if self._conn is None:
|
||||
self._conn = sqlite3.connect(self._db_path, check_same_thread=False)
|
||||
self._conn.row_factory = sqlite3.Row
|
||||
return self._conn
|
||||
|
||||
def _initialize_schema(self) -> None:
|
||||
if self._schema_initialized:
|
||||
return
|
||||
@@ -116,7 +123,7 @@ class DownloadedVersionHistoryService:
|
||||
timestamp = time.time()
|
||||
|
||||
async with self._lock:
|
||||
with self._connect() as conn:
|
||||
conn = self._get_conn()
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO downloaded_model_versions (
|
||||
@@ -180,7 +187,7 @@ class DownloadedVersionHistoryService:
|
||||
return
|
||||
|
||||
async with self._lock:
|
||||
with self._connect() as conn:
|
||||
conn = self._get_conn()
|
||||
conn.executemany(
|
||||
"""
|
||||
INSERT INTO downloaded_model_versions (
|
||||
@@ -208,7 +215,7 @@ class DownloadedVersionHistoryService:
|
||||
timestamp = time.time()
|
||||
|
||||
async with self._lock:
|
||||
with self._connect() as conn:
|
||||
conn = self._get_conn()
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO downloaded_model_versions (
|
||||
@@ -238,7 +245,7 @@ class DownloadedVersionHistoryService:
|
||||
return False
|
||||
|
||||
async with self._lock:
|
||||
with self._connect() as conn:
|
||||
conn = self._get_conn()
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT is_deleted_override
|
||||
@@ -258,7 +265,7 @@ class DownloadedVersionHistoryService:
|
||||
return []
|
||||
|
||||
async with self._lock:
|
||||
with self._connect() as conn:
|
||||
conn = self._get_conn()
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT version_id
|
||||
@@ -291,7 +298,7 @@ class DownloadedVersionHistoryService:
|
||||
params: list[object] = [normalized_type, *normalized_model_ids]
|
||||
|
||||
async with self._lock:
|
||||
with self._connect() as conn:
|
||||
conn = self._get_conn()
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT model_id, version_id
|
||||
|
||||
@@ -413,7 +413,7 @@ app.registerExtension({
|
||||
const savedItem = consumeQueuedState(itemState, itemText);
|
||||
return {
|
||||
text: itemText,
|
||||
active: savedItem ? savedItem.active : defaultActive,
|
||||
active: savedItem ? savedItem.active : true,
|
||||
highlighted: false,
|
||||
strength: null,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user