diff --git a/invokeai/backend/model_management/__init__.py b/invokeai/backend/model_management/__init__.py index 086a8721a1..91180d4474 100644 --- a/invokeai/backend/model_management/__init__.py +++ b/invokeai/backend/model_management/__init__.py @@ -3,6 +3,6 @@ Initialization file for invokeai.backend.model_management """ from .model_manager import ModelManager, ModelInfo, AddModelResult, SchedulerPredictionType from .model_cache import ModelCache -from .models import BaseModelType, ModelType, SubModelType, ModelVariantType, ModelNotFoundException +from .models import BaseModelType, ModelType, SubModelType, ModelVariantType, ModelNotFoundException, DuplicateModelException from .model_merge import ModelMerger, MergeInterpolationMethod diff --git a/invokeai/backend/model_management/model_manager.py b/invokeai/backend/model_management/model_manager.py index 5be1bcb332..a0b3e6d625 100644 --- a/invokeai/backend/model_management/model_manager.py +++ b/invokeai/backend/model_management/model_manager.py @@ -251,7 +251,9 @@ from .model_search import ModelSearch from .models import ( BaseModelType, ModelType, SubModelType, ModelError, SchedulerPredictionType, MODEL_CLASSES, - ModelConfigBase, ModelNotFoundException, InvalidModelException, + ModelConfigBase, + ModelNotFoundException, InvalidModelException, + DuplicateModelException, ) # We are only starting to number the config file with release 3. @@ -891,15 +893,18 @@ class ModelManager(object): model_name = model_path.name if model_path.is_dir() else model_path.stem model_key = self.create_key(model_name, cur_base_model, cur_model_type) - if model_key in self.models: - raise Exception(f"Model with key {model_key} added twice") - - if model_path.is_relative_to(self.app_config.root_path): - model_path = model_path.relative_to(self.app_config.root_path) try: + if model_key in self.models: + raise DuplicateModelException(f"Model with key {model_key} added twice") + + if model_path.is_relative_to(self.app_config.root_path): + model_path = model_path.relative_to(self.app_config.root_path) + model_config: ModelConfigBase = model_class.probe_config(str(model_path)) self.models[model_key] = model_config new_models_found = True + except DuplicateModelException as e: + self.logger.warning(e) except InvalidModelException: self.logger.warning(f"Not a valid model: {model_path}") except NotImplementedError as e: diff --git a/invokeai/backend/model_management/models/__init__.py b/invokeai/backend/model_management/models/__init__.py index 3c4abbcaf1..2eb9028d6e 100644 --- a/invokeai/backend/model_management/models/__init__.py +++ b/invokeai/backend/model_management/models/__init__.py @@ -2,7 +2,11 @@ import inspect from enum import Enum from pydantic import BaseModel from typing import Literal, get_origin -from .base import BaseModelType, ModelType, SubModelType, ModelBase, ModelConfigBase, ModelVariantType, SchedulerPredictionType, ModelError, SilenceWarnings, ModelNotFoundException, InvalidModelException +from .base import ( + BaseModelType, ModelType, SubModelType, ModelBase, ModelConfigBase, + ModelVariantType, SchedulerPredictionType, ModelError, SilenceWarnings, + ModelNotFoundException, InvalidModelException, DuplicateModelException + ) from .stable_diffusion import StableDiffusion1Model, StableDiffusion2Model from .sdxl import StableDiffusionXLModel from .vae import VaeModel diff --git a/invokeai/backend/model_management/models/base.py b/invokeai/backend/model_management/models/base.py index edf2e2a993..912c3270cd 100644 --- a/invokeai/backend/model_management/models/base.py +++ b/invokeai/backend/model_management/models/base.py @@ -15,6 +15,9 @@ from contextlib import suppress from pydantic import BaseModel, Field from typing import List, Dict, Optional, Type, Literal, TypeVar, Generic, Callable, Any, Union +class DuplicateModelException(Exception): + pass + class InvalidModelException(Exception): pass