Skip to content

Commit d69f3a0

Browse files
feat: Infer Model Name automatically if empty in Model Forms (#4445)
## What type of PR is this? (check all applicable) - [x] Feature ## Have you discussed this change with the InvokeAI team? - [x] No ## Description Automatically infer the name of the model from the path supplied IF the model name slot is empty. If the model name is not empty, we presume that the user has entered a model name or made changes to it and we do not touch it in order to not override user changes. ## Related Tickets & Documents - Addresses: #4443
2 parents b0cce80 + 95f44ff commit d69f3a0

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddCheckpoint.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Flex } from '@chakra-ui/react';
22
import { useForm } from '@mantine/form';
3-
import { makeToast } from 'features/system/util/makeToast';
43
import { useAppDispatch } from 'app/store/storeHooks';
54
import IAIButton from 'common/components/IAIButton';
65
import IAIMantineTextInput from 'common/components/IAIMantineInput';
76
import IAISimpleCheckbox from 'common/components/IAISimpleCheckbox';
87
import { addToast } from 'features/system/store/systemSlice';
8+
import { makeToast } from 'features/system/util/makeToast';
99
import { useState } from 'react';
1010
import { useTranslation } from 'react-i18next';
1111
import { useAddMainModelsMutation } from 'services/api/endpoints/models';
@@ -14,6 +14,7 @@ import { setAdvancedAddScanModel } from '../../store/modelManagerSlice';
1414
import BaseModelSelect from '../shared/BaseModelSelect';
1515
import CheckpointConfigsSelect from '../shared/CheckpointConfigsSelect';
1616
import ModelVariantSelect from '../shared/ModelVariantSelect';
17+
import { getModelName } from './util';
1718

1819
type AdvancedAddCheckpointProps = {
1920
model_path?: string;
@@ -28,7 +29,7 @@ export default function AdvancedAddCheckpoint(
2829

2930
const advancedAddCheckpointForm = useForm<CheckpointModelConfig>({
3031
initialValues: {
31-
model_name: model_path?.split('\\').splice(-1)[0]?.split('.')[0] ?? '',
32+
model_name: model_path ? getModelName(model_path) : '',
3233
base_model: 'sd-1',
3334
model_type: 'main',
3435
path: model_path ? model_path : '',
@@ -100,6 +101,17 @@ export default function AdvancedAddCheckpoint(
100101
label="Model Location"
101102
required
102103
{...advancedAddCheckpointForm.getInputProps('path')}
104+
onBlur={(e) => {
105+
if (advancedAddCheckpointForm.values['model_name'] === '') {
106+
const modelName = getModelName(e.currentTarget.value);
107+
if (modelName) {
108+
advancedAddCheckpointForm.setFieldValue(
109+
'model_name',
110+
modelName as string
111+
);
112+
}
113+
}
114+
}}
103115
/>
104116
<IAIMantineTextInput
105117
label="Description"

invokeai/frontend/web/src/features/ui/components/tabs/ModelManager/subpanels/AddModelsPanel/AdvancedAddDiffusers.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Flex } from '@chakra-ui/react';
22
import { useForm } from '@mantine/form';
3-
import { makeToast } from 'features/system/util/makeToast';
43
import { useAppDispatch } from 'app/store/storeHooks';
54
import IAIButton from 'common/components/IAIButton';
65
import IAIMantineTextInput from 'common/components/IAIMantineInput';
76
import { addToast } from 'features/system/store/systemSlice';
7+
import { makeToast } from 'features/system/util/makeToast';
88
import { useTranslation } from 'react-i18next';
99
import { useAddMainModelsMutation } from 'services/api/endpoints/models';
1010
import { DiffusersModelConfig } from 'services/api/types';
1111
import { setAdvancedAddScanModel } from '../../store/modelManagerSlice';
1212
import BaseModelSelect from '../shared/BaseModelSelect';
1313
import ModelVariantSelect from '../shared/ModelVariantSelect';
14+
import { getModelName } from './util';
1415

1516
type AdvancedAddDiffusersProps = {
1617
model_path?: string;
@@ -25,7 +26,7 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) {
2526

2627
const advancedAddDiffusersForm = useForm<DiffusersModelConfig>({
2728
initialValues: {
28-
model_name: model_path?.split('\\').splice(-1)[0] ?? '',
29+
model_name: model_path ? getModelName(model_path, false) : '',
2930
base_model: 'sd-1',
3031
model_type: 'main',
3132
path: model_path ? model_path : '',
@@ -92,6 +93,17 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) {
9293
label="Model Location"
9394
placeholder="Provide the path to a local folder where your Diffusers Model is stored"
9495
{...advancedAddDiffusersForm.getInputProps('path')}
96+
onBlur={(e) => {
97+
if (advancedAddDiffusersForm.values['model_name'] === '') {
98+
const modelName = getModelName(e.currentTarget.value, false);
99+
if (modelName) {
100+
advancedAddDiffusersForm.setFieldValue(
101+
'model_name',
102+
modelName as string
103+
);
104+
}
105+
}
106+
}}
95107
/>
96108
<IAIMantineTextInput
97109
label="Description"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function getModelName(filepath: string, isCheckpoint: boolean = true) {
2+
let regex;
3+
if (isCheckpoint) {
4+
regex = new RegExp('[^\\\\/]+(?=\\.)');
5+
} else {
6+
regex = new RegExp('[^\\\\/]+(?=[\\\\/]?$)');
7+
}
8+
9+
const match = filepath.match(regex);
10+
if (match) {
11+
return match[0];
12+
} else {
13+
return '';
14+
}
15+
}

0 commit comments

Comments
 (0)