add ABC for config storage

This commit is contained in:
Lincoln Stein
2023-08-12 17:50:55 -04:00
parent b5d97b18f1
commit e8edb0d434
2 changed files with 93 additions and 5 deletions

View File

@@ -1,5 +1,23 @@
# Copyright (c) 2023 Lincoln D. Stein and the InvokeAI Development Team
"""Configuration definitions for image generation models."""
"""
Configuration definitions for image generation models.
Typical usage:
from invokeai.backend.model_management2.model_config import ModelConfig
raw = dict(path='models/sd-1/main/foo.ckpt',
name='foo',
base_model='sd-1',
model_type='main',
config='configs/stable-diffusion/v1-inference.yaml',
model_variant='normal',
model_format='checkpoint'
)
config = ModelConfig.parse_obj(raw)
Validation errors will raise an InvalidModelConfigException error.
"""
from enum import Enum
from pathlib import Path
from pydantic import BaseModel, Field, Extra
@@ -8,10 +26,8 @@ from pydantic.error_wrappers import ValidationError
class InvalidModelConfigException(Exception):
"""
Exception raised when the config parser doesn't recognize the passed
combo of model type and format.
"""
"""Exception raised when the config parser doesn't recognize the passed
combination of model type and format."""
pass

View File

@@ -0,0 +1,72 @@
# Copyright (c) 2023 Lincoln D. Stein and the InvokeAI Development Team
"""
Abstract base class for storing and retrieving model configuration records.
"""
from abc import ABC, abstractmethod
from typing import Union
from ..model_config import ModelConfigBase
class DuplicateModelException(Exception):
"""Raised on an attempt to add a model with the same key twice."""
pass
class UnknownModelException(Exception):
"""Raised on an attempt to delete a model with a nonexistent key."""
pass
class ModelConfigStore(ABC):
"""Abstract base class for storage and retrieval of model configs."""
@abstractmethod
def add_model(self, key: str, config: Union[dict, ModelConfigBase]) -> None:
"""
Add a model to the database.
:param key: Unique key for the model
:param config: Model configuration record, either a dict with the
required fields or a ModelConfigBase instance.
Can raise a DuplicateModelException error.
"""
pass
@abstractmethod
def del_model(self, key: str) -> None:
"""
Delete a model.
:param key: Unique key for the model to be deleted
Can raise an UnknownModelException
"""
pass
@abstractmethod
def update_model(self, key: str, config: Union[dict, ModelConfigBase]) -> ModelConfigBase:
"""
Update the model, returning the updated version.
:param key: Unique key for the model to be updated
:param config: Model configuration record. Either a dict with the
required fields, or a ModelConfigBase instance.
"""
pass
@abstractmethod
def get_model(self, key: str) -> ModelConfigBase:
"""
Retrieve the ModelConfigBase instance for the indicated model.
:param key: Key of model config to be fetched.
Exceptions: UnknownModelException
"""
pass