Initialize fileItem first to speed up file display

This commit is contained in:
Clivia 2024-07-31 07:25:53 +08:00
parent 9d58bb1c66
commit 6681df29d2

View File

@ -1,5 +1,6 @@
<script lang="ts"> <script lang="ts">
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
import { v4 as uuidv4 } from 'uuid';
import { onMount, tick, getContext } from 'svelte'; import { onMount, tick, getContext } from 'svelte';
import { import {
type Model, type Model,
@ -98,6 +99,8 @@
const uploadFileHandler = async (file) => { const uploadFileHandler = async (file) => {
console.log(file); console.log(file);
const fileId = uuidv4();
// Check if the file is an audio file and transcribe/convert it to text file // Check if the file is an audio file and transcribe/convert it to text file
if (['audio/mpeg', 'audio/wav'].includes(file['type'])) { if (['audio/mpeg', 'audio/wav'].includes(file['type'])) {
const res = await transcribeAudio(localStorage.token, file).catch((error) => { const res = await transcribeAudio(localStorage.token, file).catch((error) => {
@ -112,40 +115,48 @@
} }
} }
// Upload the file to the server const fileItem = {
const uploadedFile = await uploadFile(localStorage.token, file).catch((error) => { type: 'file',
toast.error(error); file: '',
return null; id: fileId,
}); url: '',
name: file.name,
collection_name: '',
status: 'uploaded',
size: file.size,
error: ''
};
files = [...files, fileItem];
if (uploadedFile) { try {
const fileItem = { const uploadedFile = await uploadFile(localStorage.token, file);
type: 'file',
file: uploadedFile,
id: uploadedFile.id,
url: `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`,
name: file.name,
collection_name: '',
status: 'uploaded',
error: ''
};
files = [...files, fileItem];
// TODO: Check if tools & functions have files support to skip this step to delegate file processing if (uploadedFile) {
// Default Upload to VectorDB fileItem.file = uploadedFile;
if ( fileItem.id = uploadedFile.id;
SUPPORTED_FILE_TYPE.includes(file['type']) || fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
) { // TODO: Check if tools & functions have files support to skip this step to delegate file processing
processFileItem(fileItem); // Default Upload to VectorDB
if (
SUPPORTED_FILE_TYPE.includes(file['type']) ||
SUPPORTED_FILE_EXTENSIONS.includes(file.name.split('.').at(-1))
) {
processFileItem(fileItem);
} else {
toast.error(
$i18n.t(`Unknown file type '{{file_type}}'. Proceeding with the file upload anyway.`, {
file_type: file['type']
})
);
processFileItem(fileItem);
}
} else { } else {
toast.error( files = files.filter((item) => item.id !== fileId);
$i18n.t(`Unknown file type '{{file_type}}'. Proceeding with the file upload anyway.`, {
file_type: file['type']
})
);
processFileItem(fileItem);
} }
} catch (error) {
toast.error(error);
files = files.filter((item) => item.id !== fileId);
} }
}; };