From c91ba2dbe793d38ff253c3b3b829b6b74ffd77be Mon Sep 17 00:00:00 2001 From: blessedcoolant <54517381+blessedcoolant@users.noreply.github.com> Date: Mon, 4 Sep 2023 01:36:48 +1200 Subject: [PATCH 1/3] feat: Infer Model Name automatically if empty in Model Forms --- .../AddModelsPanel/AdvancedAddCheckpoint.tsx | 13 ++++++++++++- .../AddModelsPanel/AdvancedAddDiffusers.tsx | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx index 5f82483cd3..4f452b0727 100644 --- a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx +++ b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx @@ -1,11 +1,11 @@ import { Flex } from '@chakra-ui/react'; import { useForm } from '@mantine/form'; -import { makeToast } from 'features/system/util/makeToast'; import { useAppDispatch } from 'app/store/storeHooks'; import IAIButton from 'common/components/IAIButton'; import IAIMantineTextInput from 'common/components/IAIMantineInput'; import IAISimpleCheckbox from 'common/components/IAISimpleCheckbox'; import { addToast } from 'features/system/store/systemSlice'; +import { makeToast } from 'features/system/util/makeToast'; import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAddMainModelsMutation } from 'services/api/endpoints/models'; @@ -100,6 +100,17 @@ export default function AdvancedAddCheckpoint( label="Model Location" required {...advancedAddCheckpointForm.getInputProps('path')} + onBlur={(e) => { + if (advancedAddCheckpointForm.values['model_name'] === '') { + advancedAddCheckpointForm.setFieldValue( + 'model_name', + e.currentTarget.value + .split('\\') + .splice(-1)[0] + ?.split('.')[0] as string + ); + } + }} /> { + if (advancedAddDiffusersForm.values['model_name'] === '') { + advancedAddDiffusersForm.setFieldValue( + 'model_name', + e.currentTarget.value.split('\\').splice(-1)[0] as string + ); + } + }} /> Date: Mon, 4 Sep 2023 10:16:57 +1200 Subject: [PATCH 2/3] fix: Support UNIX paths --- .../AddModelsPanel/AdvancedAddCheckpoint.tsx | 19 +++++++++++-------- .../AddModelsPanel/AdvancedAddDiffusers.tsx | 13 ++++++++----- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx index 4f452b0727..8a525c543b 100644 --- a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx +++ b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx @@ -28,7 +28,7 @@ export default function AdvancedAddCheckpoint( const advancedAddCheckpointForm = useForm({ initialValues: { - model_name: model_path?.split('\\').splice(-1)[0]?.split('.')[0] ?? '', + model_name: model_path?.match(/[^\\/]+$/)?.[0]?.split('.')[0] ?? '', base_model: 'sd-1', model_type: 'main', path: model_path ? model_path : '', @@ -102,13 +102,16 @@ export default function AdvancedAddCheckpoint( {...advancedAddCheckpointForm.getInputProps('path')} onBlur={(e) => { if (advancedAddCheckpointForm.values['model_name'] === '') { - advancedAddCheckpointForm.setFieldValue( - 'model_name', - e.currentTarget.value - .split('\\') - .splice(-1)[0] - ?.split('.')[0] as string - ); + const modelName = e.currentTarget.value + .match(/[^\\/]+$/)?.[0] + ?.split('.')[0]; + + if (modelName) { + advancedAddCheckpointForm.setFieldValue( + 'model_name', + modelName as string + ); + } } }} /> diff --git a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx index f576f5a61f..469c0c7061 100644 --- a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx +++ b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx @@ -25,7 +25,7 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) { const advancedAddDiffusersForm = useForm({ initialValues: { - model_name: model_path?.split('\\').splice(-1)[0] ?? '', + model_name: model_path?.match(/[^\\/]+$/)?.[0] ?? '', base_model: 'sd-1', model_type: 'main', path: model_path ? model_path : '', @@ -94,10 +94,13 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) { {...advancedAddDiffusersForm.getInputProps('path')} onBlur={(e) => { if (advancedAddDiffusersForm.values['model_name'] === '') { - advancedAddDiffusersForm.setFieldValue( - 'model_name', - e.currentTarget.value.split('\\').splice(-1)[0] as string - ); + const modelName = e.currentTarget.value.match(/[^\\/]+$/)?.[0]; + if (modelName) { + advancedAddDiffusersForm.setFieldValue( + 'model_name', + modelName as string + ); + } } }} /> From 95f44ff3435766b42a227968d052b8dc84d0cc67 Mon Sep 17 00:00:00 2001 From: blessedcoolant <54517381+blessedcoolant@users.noreply.github.com> Date: Mon, 4 Sep 2023 10:52:27 +1200 Subject: [PATCH 3/3] fix: Make the name extraction work for both ckpts and folders --- .../AddModelsPanel/AdvancedAddCheckpoint.tsx | 8 +++----- .../AddModelsPanel/AdvancedAddDiffusers.tsx | 5 +++-- .../ModelManager/subpanels/AddModelsPanel/util.ts | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/util.ts diff --git a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx index 8a525c543b..85dfb0ed8b 100644 --- a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx +++ b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx @@ -14,6 +14,7 @@ import { setAdvancedAddScanModel } from '../../store/modelManagerSlice'; import BaseModelSelect from '../shared/BaseModelSelect'; import CheckpointConfigsSelect from '../shared/CheckpointConfigsSelect'; import ModelVariantSelect from '../shared/ModelVariantSelect'; +import { getModelName } from './util'; type AdvancedAddCheckpointProps = { model_path?: string; @@ -28,7 +29,7 @@ export default function AdvancedAddCheckpoint( const advancedAddCheckpointForm = useForm({ initialValues: { - model_name: model_path?.match(/[^\\/]+$/)?.[0]?.split('.')[0] ?? '', + model_name: model_path ? getModelName(model_path) : '', base_model: 'sd-1', model_type: 'main', path: model_path ? model_path : '', @@ -102,10 +103,7 @@ export default function AdvancedAddCheckpoint( {...advancedAddCheckpointForm.getInputProps('path')} onBlur={(e) => { if (advancedAddCheckpointForm.values['model_name'] === '') { - const modelName = e.currentTarget.value - .match(/[^\\/]+$/)?.[0] - ?.split('.')[0]; - + const modelName = getModelName(e.currentTarget.value); if (modelName) { advancedAddCheckpointForm.setFieldValue( 'model_name', diff --git a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx index 469c0c7061..181a6f4234 100644 --- a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx +++ b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx @@ -11,6 +11,7 @@ import { DiffusersModelConfig } from 'services/api/types'; import { setAdvancedAddScanModel } from '../../store/modelManagerSlice'; import BaseModelSelect from '../shared/BaseModelSelect'; import ModelVariantSelect from '../shared/ModelVariantSelect'; +import { getModelName } from './util'; type AdvancedAddDiffusersProps = { model_path?: string; @@ -25,7 +26,7 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) { const advancedAddDiffusersForm = useForm({ initialValues: { - model_name: model_path?.match(/[^\\/]+$/)?.[0] ?? '', + model_name: model_path ? getModelName(model_path, false) : '', base_model: 'sd-1', model_type: 'main', path: model_path ? model_path : '', @@ -94,7 +95,7 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) { {...advancedAddDiffusersForm.getInputProps('path')} onBlur={(e) => { if (advancedAddDiffusersForm.values['model_name'] === '') { - const modelName = e.currentTarget.value.match(/[^\\/]+$/)?.[0]; + const modelName = getModelName(e.currentTarget.value, false); if (modelName) { advancedAddDiffusersForm.setFieldValue( 'model_name', diff --git a/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/util.ts b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/util.ts new file mode 100644 index 0000000000..7314a86908 --- /dev/null +++ b/invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/util.ts @@ -0,0 +1,15 @@ +export function getModelName(filepath: string, isCheckpoint: boolean = true) { + let regex; + if (isCheckpoint) { + regex = new RegExp('[^\\\\/]+(?=\\.)'); + } else { + regex = new RegExp('[^\\\\/]+(?=[\\\\/]?$)'); + } + + const match = filepath.match(regex); + if (match) { + return match[0]; + } else { + return ''; + } +}