|
28 | 28 | UnknownModelException, |
29 | 29 | ) |
30 | 30 | from invokeai.app.util.suppress_output import SuppressOutput |
31 | | -from invokeai.backend.model_manager.configs.factory import AnyModelConfig |
| 31 | +from invokeai.backend.model_manager.configs.factory import AnyModelConfig, ModelConfigFactory |
32 | 32 | from invokeai.backend.model_manager.configs.main import ( |
33 | 33 | Main_Checkpoint_SD1_Config, |
34 | 34 | Main_Checkpoint_SD2_Config, |
|
38 | 38 | from invokeai.backend.model_manager.load.model_cache.cache_stats import CacheStats |
39 | 39 | from invokeai.backend.model_manager.metadata.fetch.huggingface import HuggingFaceMetadataFetch |
40 | 40 | from invokeai.backend.model_manager.metadata.metadata_base import ModelMetadataWithFiles, UnknownMetadataException |
| 41 | +from invokeai.backend.model_manager.model_on_disk import ModelOnDisk |
41 | 42 | from invokeai.backend.model_manager.search import ModelSearch |
42 | 43 | from invokeai.backend.model_manager.starter_models import ( |
43 | 44 | STARTER_BUNDLES, |
@@ -191,6 +192,40 @@ async def get_model_record( |
191 | 192 | raise HTTPException(status_code=404, detail=str(e)) |
192 | 193 |
|
193 | 194 |
|
| 195 | +@model_manager_router.post( |
| 196 | + "/i/{key}/reidentify", |
| 197 | + operation_id="reidentify_model", |
| 198 | + responses={ |
| 199 | + 200: { |
| 200 | + "description": "The model configuration was retrieved successfully", |
| 201 | + "content": {"application/json": {"example": example_model_config}}, |
| 202 | + }, |
| 203 | + 400: {"description": "Bad request"}, |
| 204 | + 404: {"description": "The model could not be found"}, |
| 205 | + }, |
| 206 | +) |
| 207 | +async def reidentify_model( |
| 208 | + key: Annotated[str, Path(description="Key of the model to reidentify.")], |
| 209 | +) -> AnyModelConfig: |
| 210 | + """Attempt to reidentify a model by re-probing its weights file.""" |
| 211 | + try: |
| 212 | + config = ApiDependencies.invoker.services.model_manager.store.get_model(key) |
| 213 | + models_path = ApiDependencies.invoker.services.configuration.models_path |
| 214 | + if pathlib.Path(config.path).is_relative_to(models_path): |
| 215 | + model_path = pathlib.Path(config.path) |
| 216 | + else: |
| 217 | + model_path = models_path / config.path |
| 218 | + mod = ModelOnDisk(model_path) |
| 219 | + result = ModelConfigFactory.from_model_on_disk(mod) |
| 220 | + if result.config is None: |
| 221 | + raise InvalidModelException("Unable to identify model format") |
| 222 | + result.config.key = config.key # retain the same key |
| 223 | + new_config = ApiDependencies.invoker.services.model_manager.store.replace_model(config.key, result.config) |
| 224 | + return new_config |
| 225 | + except UnknownModelException as e: |
| 226 | + raise HTTPException(status_code=404, detail=str(e)) |
| 227 | + |
| 228 | + |
194 | 229 | class FoundModel(BaseModel): |
195 | 230 | path: str = Field(description="Path to the model") |
196 | 231 | is_installed: bool = Field(description="Whether or not the model is already installed") |
|
0 commit comments