fix: Make the name extraction work for both ckpts and folders

This commit is contained in:
blessedcoolant 2023-09-04 10:52:27 +12:00
parent f9c3c07d98
commit 95f44ff343
3 changed files with 21 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import { setAdvancedAddScanModel } from '../../store/modelManagerSlice';
import BaseModelSelect from '../shared/BaseModelSelect'; import BaseModelSelect from '../shared/BaseModelSelect';
import CheckpointConfigsSelect from '../shared/CheckpointConfigsSelect'; import CheckpointConfigsSelect from '../shared/CheckpointConfigsSelect';
import ModelVariantSelect from '../shared/ModelVariantSelect'; import ModelVariantSelect from '../shared/ModelVariantSelect';
import { getModelName } from './util';
type AdvancedAddCheckpointProps = { type AdvancedAddCheckpointProps = {
model_path?: string; model_path?: string;
@ -28,7 +29,7 @@ export default function AdvancedAddCheckpoint(
const advancedAddCheckpointForm = useForm<CheckpointModelConfig>({ const advancedAddCheckpointForm = useForm<CheckpointModelConfig>({
initialValues: { initialValues: {
model_name: model_path?.match(/[^\\/]+$/)?.[0]?.split('.')[0] ?? '', model_name: model_path ? getModelName(model_path) : '',
base_model: 'sd-1', base_model: 'sd-1',
model_type: 'main', model_type: 'main',
path: model_path ? model_path : '', path: model_path ? model_path : '',
@ -102,10 +103,7 @@ export default function AdvancedAddCheckpoint(
{...advancedAddCheckpointForm.getInputProps('path')} {...advancedAddCheckpointForm.getInputProps('path')}
onBlur={(e) => { onBlur={(e) => {
if (advancedAddCheckpointForm.values['model_name'] === '') { if (advancedAddCheckpointForm.values['model_name'] === '') {
const modelName = e.currentTarget.value const modelName = getModelName(e.currentTarget.value);
.match(/[^\\/]+$/)?.[0]
?.split('.')[0];
if (modelName) { if (modelName) {
advancedAddCheckpointForm.setFieldValue( advancedAddCheckpointForm.setFieldValue(
'model_name', 'model_name',

View File

@ -11,6 +11,7 @@ import { DiffusersModelConfig } from 'services/api/types';
import { setAdvancedAddScanModel } from '../../store/modelManagerSlice'; import { setAdvancedAddScanModel } from '../../store/modelManagerSlice';
import BaseModelSelect from '../shared/BaseModelSelect'; import BaseModelSelect from '../shared/BaseModelSelect';
import ModelVariantSelect from '../shared/ModelVariantSelect'; import ModelVariantSelect from '../shared/ModelVariantSelect';
import { getModelName } from './util';
type AdvancedAddDiffusersProps = { type AdvancedAddDiffusersProps = {
model_path?: string; model_path?: string;
@ -25,7 +26,7 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) {
const advancedAddDiffusersForm = useForm<DiffusersModelConfig>({ const advancedAddDiffusersForm = useForm<DiffusersModelConfig>({
initialValues: { initialValues: {
model_name: model_path?.match(/[^\\/]+$/)?.[0] ?? '', model_name: model_path ? getModelName(model_path, false) : '',
base_model: 'sd-1', base_model: 'sd-1',
model_type: 'main', model_type: 'main',
path: model_path ? model_path : '', path: model_path ? model_path : '',
@ -94,7 +95,7 @@ export default function AdvancedAddDiffusers(props: AdvancedAddDiffusersProps) {
{...advancedAddDiffusersForm.getInputProps('path')} {...advancedAddDiffusersForm.getInputProps('path')}
onBlur={(e) => { onBlur={(e) => {
if (advancedAddDiffusersForm.values['model_name'] === '') { if (advancedAddDiffusersForm.values['model_name'] === '') {
const modelName = e.currentTarget.value.match(/[^\\/]+$/)?.[0]; const modelName = getModelName(e.currentTarget.value, false);
if (modelName) { if (modelName) {
advancedAddDiffusersForm.setFieldValue( advancedAddDiffusersForm.setFieldValue(
'model_name', 'model_name',

View File

@ -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 '';
}
}