mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
add controlnet model downloading
This commit is contained in:
@ -43,7 +43,7 @@ from invokeai.app.services.config import get_invokeai_config
|
||||
|
||||
# minimum size for the UI
|
||||
MIN_COLS = 120
|
||||
MIN_LINES = 45
|
||||
MIN_LINES = 50
|
||||
|
||||
config = get_invokeai_config()
|
||||
|
||||
@ -53,16 +53,16 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
|
||||
def __init__(self, parentApp, name, multipage=False, *args, **keywords):
|
||||
self.multipage = multipage
|
||||
|
||||
self.initial_models = OmegaConf.load(Dataset_path)['diffusers']
|
||||
self.control_net_models = OmegaConf.load(Dataset_path)['controlnet']
|
||||
self.installed_cn_models = self._get_installed_cn_models()
|
||||
self._add_additional_cn_models(self.control_net_models,self.installed_cn_models)
|
||||
|
||||
try:
|
||||
self.existing_models = OmegaConf.load(default_config_file())
|
||||
except:
|
||||
self.existing_models = dict()
|
||||
# self.starter_model_list = [
|
||||
# x for x in list(self.initial_models.keys()) if x not in self.existing_models
|
||||
# ]
|
||||
self.starter_model_list = list(self.initial_models.keys())
|
||||
self.installed_models = dict()
|
||||
super().__init__(parentApp=parentApp, name=name, *args, **keywords)
|
||||
@ -95,40 +95,6 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
color="CAUTION",
|
||||
)
|
||||
self.nextrely += 1
|
||||
# if len(self.installed_models) > 0:
|
||||
# self.add_widget_intelligent(
|
||||
# CenteredTitleText,
|
||||
# name="== INSTALLED STARTER MODELS ==",
|
||||
# editable=False,
|
||||
# color="CONTROL",
|
||||
# )
|
||||
# self.nextrely -= 1
|
||||
# self.add_widget_intelligent(
|
||||
# CenteredTitleText,
|
||||
# name="Currently installed starter models. Uncheck to delete:",
|
||||
# editable=False,
|
||||
# labelColor="CAUTION",
|
||||
# )
|
||||
# self.nextrely -= 1
|
||||
# columns = self._get_columns()
|
||||
# self.previously_installed_models = self.add_widget_intelligent(
|
||||
# MultiSelectColumns,
|
||||
# columns=columns,
|
||||
# values=self.installed_models,
|
||||
# value=[x for x in range(0, len(self.installed_models))],
|
||||
# max_height=1 + len(self.installed_models) // columns,
|
||||
# relx=4,
|
||||
# slow_scroll=True,
|
||||
# scroll_exit=True,
|
||||
# )
|
||||
# self.purge_deleted = self.add_widget_intelligent(
|
||||
# npyscreen.Checkbox,
|
||||
# name="Purge deleted models from disk",
|
||||
# value=False,
|
||||
# scroll_exit=True,
|
||||
# relx=4,
|
||||
# )
|
||||
# self.nextrely += 1
|
||||
if len(self.starter_model_list) > 0:
|
||||
self.add_widget_intelligent(
|
||||
CenteredTitleText,
|
||||
@ -161,35 +127,14 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
relx=4,
|
||||
scroll_exit=True,
|
||||
)
|
||||
self.add_widget_intelligent(
|
||||
CenteredTitleText,
|
||||
name="== CONTROLNET MODELS ==",
|
||||
editable=False,
|
||||
color="CONTROL",
|
||||
)
|
||||
columns=6
|
||||
self.cn_models_selected = self.add_widget_intelligent(
|
||||
MultiSelectColumns,
|
||||
columns=columns,
|
||||
name="Install ControlNet Models",
|
||||
values=cn_model_list,
|
||||
value=[
|
||||
cn_model_list.index(x)
|
||||
for x in cn_model_list
|
||||
if x in self.installed_cn_models
|
||||
],
|
||||
max_height=len(cn_model_list)//columns + 1,
|
||||
relx=4,
|
||||
scroll_exit=True,
|
||||
)
|
||||
self.nextrely += 1
|
||||
self.purge_deleted = self.add_widget_intelligent(
|
||||
npyscreen.Checkbox,
|
||||
name="Purge unchecked models from disk",
|
||||
name="Purge unchecked diffusers models from disk",
|
||||
value=False,
|
||||
scroll_exit=True,
|
||||
relx=4,
|
||||
)
|
||||
self.nextrely += 1
|
||||
self.add_widget_intelligent(
|
||||
CenteredTitleText,
|
||||
name="== IMPORT LOCAL AND REMOTE MODELS ==",
|
||||
@ -211,7 +156,7 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
)
|
||||
self.nextrely -= 1
|
||||
self.import_model_paths = self.add_widget_intelligent(
|
||||
TextBox, max_height=7, scroll_exit=True, editable=True, relx=4
|
||||
TextBox, max_height=4, scroll_exit=True, editable=True, relx=4
|
||||
)
|
||||
self.nextrely += 1
|
||||
self.show_directory_fields = self.add_widget_intelligent(
|
||||
@ -236,6 +181,47 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
relx=4,
|
||||
scroll_exit=True,
|
||||
)
|
||||
self.add_widget_intelligent(
|
||||
CenteredTitleText,
|
||||
name="== CONTROLNET MODELS ==",
|
||||
editable=False,
|
||||
color="CONTROL",
|
||||
)
|
||||
self.nextrely -= 1
|
||||
self.add_widget_intelligent(
|
||||
CenteredTitleText,
|
||||
name="Select the desired ControlNet models. Unchecked models will be purged from disk.",
|
||||
editable=False,
|
||||
labelColor="CAUTION",
|
||||
)
|
||||
columns=6
|
||||
self.cn_models_selected = self.add_widget_intelligent(
|
||||
MultiSelectColumns,
|
||||
columns=columns,
|
||||
name="Install ControlNet Models",
|
||||
values=cn_model_list,
|
||||
value=[
|
||||
cn_model_list.index(x)
|
||||
for x in cn_model_list
|
||||
if x in self.installed_cn_models
|
||||
],
|
||||
max_height=len(cn_model_list)//columns + 1,
|
||||
relx=4,
|
||||
scroll_exit=True,
|
||||
)
|
||||
self.nextrely += 1
|
||||
self.add_widget_intelligent(
|
||||
npyscreen.TitleFixedText,
|
||||
name='Additional ControlNet HuggingFace repo_ids to install (space separated):',
|
||||
relx=4,
|
||||
color='CONTROL',
|
||||
editable=False,
|
||||
scroll_exit=True
|
||||
)
|
||||
self.nextrely -= 1
|
||||
self.additional_controlnet_ids = self.add_widget_intelligent(
|
||||
TextBox, max_height=2, scroll_exit=True, editable=True, relx=4
|
||||
)
|
||||
self.cancel = self.add_widget_intelligent(
|
||||
npyscreen.ButtonPress,
|
||||
name="CANCEL",
|
||||
@ -300,20 +286,22 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
]
|
||||
|
||||
def _get_installed_cn_models(self)->list[str]:
|
||||
with open('log.txt','w') as file:
|
||||
cn_dir = config.controlnet_path
|
||||
file.write(f'cn_dir={cn_dir}\n')
|
||||
installed_cn_models = set()
|
||||
for root, dirs, files in os.walk(cn_dir):
|
||||
for name in dirs:
|
||||
file.write(f'{root}/{name}/config.json\n')
|
||||
if Path(root, name, 'config.json').exists():
|
||||
installed_cn_models.add(name)
|
||||
inverse_dict = {name.split('/')[1]: key for key, name in self.control_net_models.items()}
|
||||
file.write(f'inverse={inverse_dict}')
|
||||
return [inverse_dict[x] for x in installed_cn_models]
|
||||
|
||||
cn_dir = config.controlnet_path
|
||||
installed_cn_models = set()
|
||||
for root, dirs, files in os.walk(cn_dir):
|
||||
for name in dirs:
|
||||
if Path(root, name, '.download_complete').exists():
|
||||
installed_cn_models.add(name.replace('--','/'))
|
||||
return installed_cn_models
|
||||
|
||||
def _add_additional_cn_models(self, known_models: dict, installed_models: set):
|
||||
for i in installed_models:
|
||||
if i in known_models:
|
||||
continue
|
||||
# translate from name to repo_id
|
||||
repo_id = i.replace('--','/')
|
||||
known_models.update({i: repo_id})
|
||||
|
||||
def _get_columns(self) -> int:
|
||||
window_width, window_height = get_terminal_size()
|
||||
cols = (
|
||||
@ -374,15 +362,20 @@ class addModelsForm(npyscreen.FormMultiPage):
|
||||
selections.install_models = [x for x in starter_models if x not in self.existing_models]
|
||||
selections.remove_models = [x for x in self.starter_model_list if x in self.existing_models and x not in starter_models]
|
||||
|
||||
selections.install_cn_models = [self.control_net_models[self.cn_models_selected.values[x]]
|
||||
selections.control_net_map = self.control_net_models
|
||||
selections.install_cn_models = [self.cn_models_selected.values[x]
|
||||
for x in self.cn_models_selected.value
|
||||
if self.cn_models_selected.values[x] not in self.installed_cn_models
|
||||
]
|
||||
selections.remove_cn_models = [self.control_net_models[x]
|
||||
selections.remove_cn_models = [x
|
||||
for x in self.cn_models_selected.values
|
||||
if x in self.installed_cn_models
|
||||
and self.cn_models_selected.values.index(x) not in self.cn_models_selected.value
|
||||
]
|
||||
if (additional_cns := self.additional_controlnet_ids.value.split()):
|
||||
valid_cns = [x for x in additional_cns if '/' in x]
|
||||
selections.install_cn_models.extend(valid_cns)
|
||||
selections.control_net_map.update({x: x for x in valid_cns})
|
||||
|
||||
# load directory and whether to scan on startup
|
||||
if self.show_directory_fields.value:
|
||||
@ -406,6 +399,7 @@ class AddModelApplication(npyscreen.NPSAppManaged):
|
||||
purge_deleted_models=False,
|
||||
install_cn_models = None,
|
||||
remove_cn_models = None,
|
||||
control_net_map = None,
|
||||
scan_directory=None,
|
||||
autoscan_on_startup=None,
|
||||
import_model_paths=None,
|
||||
@ -425,24 +419,24 @@ def process_and_execute(opt: Namespace, selections: Namespace):
|
||||
directory_to_scan = selections.scan_directory
|
||||
scan_at_startup = selections.autoscan_on_startup
|
||||
potential_models_to_install = selections.import_model_paths
|
||||
|
||||
print('NOT INSTALLING MODELS DURING DEBUGGING')
|
||||
print('models to install:',models_to_install)
|
||||
print('models to remove:',models_to_remove)
|
||||
print('CN models to install:',selections.install_cn_models)
|
||||
print('CN models to remove:',selections.remove_cn_models)
|
||||
# install_requested_models(
|
||||
# install_initial_models=models_to_install,
|
||||
# remove_models=models_to_remove,
|
||||
# scan_directory=Path(directory_to_scan) if directory_to_scan else None,
|
||||
# external_models=potential_models_to_install,
|
||||
# scan_at_startup=scan_at_startup,
|
||||
# precision="float32"
|
||||
# if opt.full_precision
|
||||
# else choose_precision(torch.device(choose_torch_device())),
|
||||
# purge_deleted=selections.purge_deleted_models,
|
||||
# config_file_path=Path(opt.config_file) if opt.config_file else None,
|
||||
# )
|
||||
print(f'selections.install_cn_models={selections.install_cn_models}')
|
||||
print(f'selections.remove_cn_models={selections.remove_cn_models}')
|
||||
print(f'selections.cn_model_map={selections.control_net_map}')
|
||||
install_requested_models(
|
||||
install_initial_models=models_to_install,
|
||||
remove_models=models_to_remove,
|
||||
install_cn_models=selections.install_cn_models,
|
||||
remove_cn_models=selections.remove_cn_models,
|
||||
cn_model_map=selections.control_net_map,
|
||||
scan_directory=Path(directory_to_scan) if directory_to_scan else None,
|
||||
external_models=potential_models_to_install,
|
||||
scan_at_startup=scan_at_startup,
|
||||
precision="float32"
|
||||
if opt.full_precision
|
||||
else choose_precision(torch.device(choose_torch_device())),
|
||||
purge_deleted=selections.purge_deleted_models,
|
||||
config_file_path=Path(opt.config_file) if opt.config_file else None,
|
||||
)
|
||||
|
||||
|
||||
# --------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user