From 6523fd07ab2e713260595a74516b390c5552dcdc Mon Sep 17 00:00:00 2001 From: blessedcoolant <54517381+blessedcoolant@users.noreply.github.com> Date: Mon, 26 Dec 2022 00:13:54 +1300 Subject: [PATCH] Model Edit Initial Implementation --- frontend/src/app/invokeai.d.ts | 21 - .../components/ModelManager/AddModel.tsx | 4 +- .../components/ModelManager/ModelEdit.tsx | 375 +++--------------- .../components/ModelManager/ModelList.tsx | 2 +- ldm/invoke/model_cache.py | 20 +- 5 files changed, 68 insertions(+), 354 deletions(-) diff --git a/frontend/src/app/invokeai.d.ts b/frontend/src/app/invokeai.d.ts index 4fa10962e4..af292058c2 100644 --- a/frontend/src/app/invokeai.d.ts +++ b/frontend/src/app/invokeai.d.ts @@ -264,24 +264,3 @@ export declare type UploadOutpaintingMergeImagePayload = { dataURL: string; name: string; }; - -export declare type InvokeModelConfigProps = { - name: string; - description?: string; - config: string; - weights: string; - vae?: string; - width: number; - height: number; - default?: boolean; -}; - -export declare type OnFoundModelResponse = { - search_folder: string; - found_models: FoundModel[]; -}; - -export declare type FoundModel = { - name: string; - location: string; -}; diff --git a/frontend/src/features/system/components/ModelManager/AddModel.tsx b/frontend/src/features/system/components/ModelManager/AddModel.tsx index 42ec59cd93..82f2e03d46 100644 --- a/frontend/src/features/system/components/ModelManager/AddModel.tsx +++ b/frontend/src/features/system/components/ModelManager/AddModel.tsx @@ -26,9 +26,9 @@ import { import React from 'react'; import { FaPlus } from 'react-icons/fa'; import { Field, FieldInputProps, Formik, FormikProps } from 'formik'; -import { RootState } from 'app/store'; +import type { RootState } from 'app/store'; import { addNewModel } from 'app/socketio/actions'; -import { InvokeModelConfigProps } from 'app/invokeai'; +import type { InvokeModelConfigProps } from 'app/invokeai'; import IAICheckbox from 'common/components/IAICheckbox'; import IAIButton from 'common/components/IAIButton'; import SearchModels from './SearchModels'; diff --git a/frontend/src/features/system/components/ModelManager/ModelEdit.tsx b/frontend/src/features/system/components/ModelManager/ModelEdit.tsx index 1b70745e18..4738b0a2a7 100644 --- a/frontend/src/features/system/components/ModelManager/ModelEdit.tsx +++ b/frontend/src/features/system/components/ModelManager/ModelEdit.tsx @@ -1,331 +1,60 @@ -import { createSelector } from '@reduxjs/toolkit'; -import { Field, FieldInputProps, Formik, FormikProps } from 'formik'; -import { RootState } from 'app/store'; -import { useAppDispatch, useAppSelector } from 'app/storeHooks' -import { addNewModel, } from 'app/socketio/actions'; -import { InvokeModelConfigProps } from 'app/invokeai'; -import { SystemState } from 'features/system/store/systemSlice'; -import { - Button, - Flex, - FormControl, - FormErrorMessage, - FormHelperText, - FormLabel, - HStack, - Input, - NumberDecrementStepper, - NumberIncrementStepper, - NumberInput, - NumberInputField, - NumberInputStepper, - Text, - VStack, -} from '@chakra-ui/react'; - - +import { createSelector, Dictionary } from '@reduxjs/toolkit'; +import type { RootState } from 'app/store'; +import { useAppSelector } from 'app/storeHooks'; +import { systemSelector } from 'features/system/store/systemSelectors'; +import React, { useEffect, useState } from 'react'; +import _ from 'lodash'; +import { Model } from 'app/invokeai'; +import { Flex, Spacer, Text } from '@chakra-ui/react'; +const selector = createSelector( + [systemSelector], + (system) => { + const { openModel, model_list } = system; + return { + model_list, + openModel, + }; + }, + { + memoizeOptions: { + resultEqualityCheck: _.isEqual, + }, + } +); export default function ModelEdit() { + const { openModel, model_list } = useAppSelector(selector); + const [openedModel, setOpenedModel] = useState(); - const MIN_MODEL_SIZE = 64; - const MAX_MODEL_SIZE = 2048; - const modelListSelector = createSelector( - (state: RootState) => state.system, - (system: SystemState) => { - const models = _.map(system.model_list, (model, key) => { - return { name: key, ...model }; + useEffect(() => { + if (openModel) { + const retrievedModel = _.pickBy(model_list, (val, key) => { + return _.isEqual(key, openModel); }); - - const selectedModel = models.find((model) => model.name === system.openModel); - - return { - openedModel: { - name:selectedModel?.name, - description:selectedModel?.description, - config:selectedModel?.config, - weights:selectedModel?.weights, - vae:selectedModel?.vae, - width:selectedModel?.width, - height:selectedModel?.height, - default:selectedModel?.default, - } - }; + setOpenedModel(retrievedModel[openModel]); } + }, [model_list, openModel]); + + console.log(openedModel); + + return ( + + + + {openModel} + + {openedModel?.status} + + + {openedModel?.config} + {openedModel?.description} + {openedModel?.height} + {openedModel?.width} + {openedModel?.default} + {openedModel?.vae} + {openedModel?.weights} + + ); - - - const { openedModel } = useAppSelector(modelListSelector); - - - const viewModelFormValues: InvokeModelConfigProps = { - name: openedModel.name || '', - description: openedModel.description || '', - config: openedModel.config || '', - weights: openedModel.weights || '', - vae: openedModel.vae || '', - width: openedModel.width || 0, - height: openedModel.height || 0, - default: openedModel.default || false, - }; - - const dispatch = useAppDispatch(); - - const addModelFormSubmitHandler = (values: InvokeModelConfigProps) => { - dispatch(addNewModel(values)); - onClose(); - }; - - function hasWhiteSpace(s: string) { - return /\\s/g.test(s); - } - - function baseValidation(value: string) { - let error; - if (hasWhiteSpace(value)) error = 'Cannot use spaces'; - return error; - } - - if (!openedModel) { - return ( - 'No model selected' - - )} else { - - return ( - - {({ handleSubmit, errors, touched }) => ( -
- - - Manual - - {/* Name */} - - Name - - - {!!errors.name && touched.name ? ( - {errors.name} - ) : ( - - Enter a name for your model - - )} - - - - {/* Description */} - - Description - - - {!!errors.description && touched.description ? ( - - {errors.description} - - ) : ( - - Add a description for your model - - )} - - - - {/* Config */} - - Config - - - {!!errors.config && touched.config ? ( - {errors.config} - ) : ( - - Path to the config file of your model. - - )} - - - - {/* Weights */} - - Model Location - - - {!!errors.weights && touched.weights ? ( - - {errors.weights} - - ) : ( - - Path to where your model is located. - - )} - - - - {/* VAE */} - - VAE Location - - - {!!errors.vae && touched.vae ? ( - {errors.vae} - ) : ( - - Path to where your VAE is located. - - )} - - - - - {/* Width */} - - Width - - - {({ - field, - form, - }: { - field: FieldInputProps; - form: FormikProps; - }) => ( - - form.setFieldValue( - field.name, - Number(value) - ) - } - > - - - - - - - )} - - - {!!errors.width && touched.width ? ( - - {errors.width} - - ) : ( - - Default width of your model. - - )} - - - - {/* Height */} - - Height - - - {({ - field, - form, - }: { - field: FieldInputProps; - form: FormikProps; - }) => ( - - form.setFieldValue( - field.name, - Number(value) - ) - } - > - - - - - - - )} - - - {!!errors.height && touched.height ? ( - - {errors.height} - - ) : ( - - Default height of your model. - - )} - - - - - - -
- )} -
-)} } diff --git a/frontend/src/features/system/components/ModelManager/ModelList.tsx b/frontend/src/features/system/components/ModelManager/ModelList.tsx index 8569ad3e62..6e118d1a9f 100644 --- a/frontend/src/features/system/components/ModelManager/ModelList.tsx +++ b/frontend/src/features/system/components/ModelManager/ModelList.tsx @@ -68,7 +68,7 @@ const ModelList = () => { }; return ( - + {t('modelmanager:availableModels')} diff --git a/ldm/invoke/model_cache.py b/ldm/invoke/model_cache.py index 2f670eac62..623160e67d 100644 --- a/ldm/invoke/model_cache.py +++ b/ldm/invoke/model_cache.py @@ -134,12 +134,13 @@ class ModelCache(object): ''' models = {} for name in self.config: - try: - description = self.config[name].description - weights = self.config[name].weights - except ConfigAttributeError: - description = '' - weights = '' + description = self.config[name].description if 'description' in self.config[name] else '' + weights = self.config[name].weights if 'weights' in self.config[name] else '' + config = self.config[name].config if 'config' in self.config[name] else '' + width = self.config[name].width if 'width' in self.config[name] else 512 + height = self.config[name].height if 'height' in self.config[name] else 512 + default = self.config[name].default if 'default' in self.config[name] else False + vae = self.config[name].vae if 'vae' in self.config[name] else '' if self.current_model == name: status = 'active' @@ -151,7 +152,12 @@ class ModelCache(object): models[name]={ 'status' : status, 'description' : description, - 'weights': weights + 'weights': weights, + 'config': config, + 'width': width, + 'height': height, + 'vae': vae, + 'default': default } return models