Skip to content

Commit c1d819c

Browse files
feat(nodes): add get_absolute_path method to context.models API
Given a model config or path (presumably to a model), returns the absolute path to the model. Check the next few commits for use-case.
1 parent 2a8e91f commit c1d819c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

invokeai/app/services/shared/invocation_context.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from invokeai.app.util.step_callback import diffusion_step_callback
2222
from invokeai.backend.model_manager.config import (
2323
AnyModelConfig,
24+
ModelConfigBase,
2425
)
2526
from invokeai.backend.model_manager.load.load_base import LoadedModel, LoadedModelWithoutConfig
2627
from invokeai.backend.model_manager.taxonomy import AnyModel, BaseModelType, ModelFormat, ModelType, SubModelType
@@ -543,6 +544,30 @@ def load_remote_model(
543544
self._util.signal_progress(f"Loading model {source}")
544545
return self._services.model_manager.load.load_model_from_path(model_path=model_path, loader=loader)
545546

547+
def get_absolute_path(self, config_or_path: AnyModelConfig | Path | str) -> Path:
548+
"""Gets the absolute path for a given model config or path.
549+
550+
For example, if the model's path is `flux/main/FLUX Dev.safetensors`, and the models path is
551+
`/home/username/InvokeAI/models`, this method will return
552+
`/home/username/InvokeAI/models/flux/main/FLUX Dev.safetensors`.
553+
554+
Args:
555+
config_or_path: The model config or path.
556+
557+
Returns:
558+
The absolute path to the model.
559+
"""
560+
561+
model_path = Path(config_or_path.path) if isinstance(config_or_path, ModelConfigBase) else Path(config_or_path)
562+
563+
if model_path.is_absolute():
564+
return model_path.resolve()
565+
566+
base_models_path = self._services.configuration.models_path
567+
joined_path = base_models_path / model_path
568+
resolved_path = joined_path.resolve()
569+
return resolved_path
570+
546571

547572
class ConfigInterface(InvocationContextInterface):
548573
def get(self) -> InvokeAIAppConfig:

0 commit comments

Comments
 (0)