feat: 添加数据集加载功能,支持选择数据集文件夹并验证meta.json格式
This commit is contained in:
@@ -34,3 +34,25 @@ export type ImageMeta = {
|
||||
sentences: DescribeSentence[];
|
||||
selectedSentences: string[];
|
||||
}
|
||||
|
||||
export function isDatasetMeta(value: unknown): value is DatasetMeta {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const candidate = value as Partial<DatasetMeta>;
|
||||
|
||||
return (
|
||||
typeof candidate.name === 'string' &&
|
||||
typeof candidate.targetModel === 'string' &&
|
||||
typeof candidate.loraType === 'string' &&
|
||||
typeof candidate.unifiedImageSize === 'boolean' &&
|
||||
typeof candidate.unifiedImageRatio === 'boolean' &&
|
||||
Array.isArray(candidate.imageSize) &&
|
||||
candidate.imageSize.length === 2 &&
|
||||
typeof candidate.imageSize[0] === 'number' &&
|
||||
typeof candidate.imageSize[1] === 'number' &&
|
||||
Array.isArray(candidate.triggerWords) &&
|
||||
candidate.triggerWords.every((word) => typeof word === 'string')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,72 @@
|
||||
<script lang="ts">
|
||||
import { afterNavigate } from '$app/navigation';
|
||||
import { afterNavigate, goto } from '$app/navigation';
|
||||
import NewFolder from '$lib/components/icons/NewFolder.svelte';
|
||||
import OpenFolder from '$lib/components/icons/OpenFolder.svelte';
|
||||
import { openedDatasetDir } from '$lib/stores/dataset';
|
||||
import { currentActivate } from '$lib/stores/navigate';
|
||||
import { isDatasetMeta } from '$lib/types/meta';
|
||||
import { join } from '@tauri-apps/api/path';
|
||||
import { message, open } from '@tauri-apps/plugin-dialog';
|
||||
import { readDir, readTextFile } from '@tauri-apps/plugin-fs';
|
||||
|
||||
afterNavigate(() => {
|
||||
currentActivate.set('dataset');
|
||||
});
|
||||
|
||||
async function loadDataset() {
|
||||
try {
|
||||
const selectResult = await open({
|
||||
directory: true,
|
||||
multiple: false,
|
||||
title: 'Select Dataset Folder',
|
||||
});
|
||||
|
||||
let selectedPath = '';
|
||||
if (Array.isArray(selectResult)) {
|
||||
selectedPath = selectResult[0] ?? '';
|
||||
} else if (typeof selectResult === 'string') {
|
||||
selectedPath = selectResult;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!selectedPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entries = await readDir(selectedPath);
|
||||
const hasMetaFile = entries.some((entry) => entry.name === 'meta.json');
|
||||
|
||||
if (!hasMetaFile) {
|
||||
await message('The selected folder does not contain meta.json.', {
|
||||
kind: 'warning',
|
||||
title: 'Invalid Dataset Folder',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const metaPath = await join(selectedPath, 'meta.json');
|
||||
const metaText = await readTextFile(metaPath);
|
||||
const parsedMeta: unknown = JSON.parse(metaText);
|
||||
|
||||
if (!isDatasetMeta(parsedMeta)) {
|
||||
await message('meta.json format is invalid and cannot be parsed as DatasetMeta.', {
|
||||
kind: 'warning',
|
||||
title: 'Invalid meta.json',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
openedDatasetDir.set(selectedPath);
|
||||
await goto('/dataset');
|
||||
} catch (error) {
|
||||
console.error('Failed to load dataset:', error);
|
||||
await message('Failed to load dataset folder.', {
|
||||
kind: 'error',
|
||||
title: 'Load Dataset Failed',
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<main class="flex flex-col justify-center items-center overflow-hidden size-full">
|
||||
@@ -15,7 +75,7 @@ afterNavigate(() => {
|
||||
<NewFolder width="20" />
|
||||
New Dataset
|
||||
</a>
|
||||
<button class="btn">
|
||||
<button class="btn" onclick={loadDataset}>
|
||||
<OpenFolder width="20" />
|
||||
Open Dataset
|
||||
</button>
|
||||
|
||||
@@ -66,7 +66,6 @@ async function createDataset() {
|
||||
const metaPath = await join(storePath, 'meta.json');
|
||||
await writeTextFile(metaPath, JSON.stringify(datasetMeta, null, 2));
|
||||
|
||||
localStorage.setItem('#sym:openedDatasetDir', storePath);
|
||||
openedDatasetDir.set(storePath);
|
||||
|
||||
await goto('/dataset');
|
||||
|
||||
Reference in New Issue
Block a user