From b4bdea6d85047e58aeab80941d9a1ac78e61f0b7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 18 Jun 2024 14:33:44 -0700 Subject: [PATCH] fix: files --- backend/apps/webui/models/files.py | 2 +- backend/apps/webui/routers/files.py | 39 ++++++++++++++++- src/lib/apis/files/index.ts | 28 +++++++++++++ src/lib/components/chat/MessageInput.svelte | 8 +++- .../chat/Messages/UserMessage.svelte | 42 +++++++++++++++++++ 5 files changed, 116 insertions(+), 3 deletions(-) diff --git a/backend/apps/webui/models/files.py b/backend/apps/webui/models/files.py index c62fa4019..6459ad725 100644 --- a/backend/apps/webui/models/files.py +++ b/backend/apps/webui/models/files.py @@ -42,7 +42,7 @@ class FileModel(BaseModel): #################### -class FileResponse(BaseModel): +class FileModelResponse(BaseModel): id: str user_id: str filename: str diff --git a/backend/apps/webui/routers/files.py b/backend/apps/webui/routers/files.py index 471079e4c..37cee6580 100644 --- a/backend/apps/webui/routers/files.py +++ b/backend/apps/webui/routers/files.py @@ -12,12 +12,20 @@ from fastapi import ( from datetime import datetime, timedelta from typing import List, Union, Optional +from pathlib import Path from fastapi import APIRouter +from fastapi.responses import StreamingResponse, JSONResponse, FileResponse + from pydantic import BaseModel import json -from apps.webui.models.files import Files, FileForm, FileModel, FileResponse +from apps.webui.models.files import ( + Files, + FileForm, + FileModel, + FileModelResponse, +) from utils.utils import get_verified_user, get_admin_user from constants import ERROR_MESSAGES @@ -121,6 +129,35 @@ async def get_file_by_id(id: str, user=Depends(get_verified_user)): ) +############################ +# Get File Content By Id +############################ + + +@router.get("/{id}/content", response_model=Optional[FileModel]) +async def get_file_content_by_id(id: str, user=Depends(get_verified_user)): + file = Files.get_file_by_id(id) + + if file: + file_path = Path(file.meta["path"]) + + # Check if the file already exists in the cache + if file_path.is_file(): + + print(f"file_path: {file_path}") + return FileResponse(file_path) + else: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.NOT_FOUND, + ) + else: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.NOT_FOUND, + ) + + ############################ # Delete File By Id ############################ diff --git a/src/lib/apis/files/index.ts b/src/lib/apis/files/index.ts index d467d2889..352fa509b 100644 --- a/src/lib/apis/files/index.ts +++ b/src/lib/apis/files/index.ts @@ -92,6 +92,34 @@ export const getFileById = async (token: string, id: string) => { return res; }; +export const getFileContentById = async (token: string, id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}/content`, { + method: 'GET', + headers: { + Accept: 'application/json', + authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return await res.blob(); + }) + .catch((err) => { + error = err.detail; + console.log(err); + + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + export const deleteFileById = async (token: string, id: string) => { let error = null; diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index ce619e31d..f199b9154 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -22,7 +22,12 @@ } from '$lib/apis/rag'; import { uploadFile } from '$lib/apis/files'; - import { SUPPORTED_FILE_TYPE, SUPPORTED_FILE_EXTENSIONS, WEBUI_BASE_URL } from '$lib/constants'; + import { + SUPPORTED_FILE_TYPE, + SUPPORTED_FILE_EXTENSIONS, + WEBUI_BASE_URL, + WEBUI_API_BASE_URL + } from '$lib/constants'; import Prompts from './MessageInput/PromptCommands.svelte'; import Suggestions from './MessageInput/Suggestions.svelte'; @@ -116,6 +121,7 @@ type: 'file', file: uploadedFile, id: uploadedFile.id, + url: `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`, name: file.name, collection_name: '', status: 'uploaded', diff --git a/src/lib/components/chat/Messages/UserMessage.svelte b/src/lib/components/chat/Messages/UserMessage.svelte index 04591a14f..f0b27d5d5 100644 --- a/src/lib/components/chat/Messages/UserMessage.svelte +++ b/src/lib/components/chat/Messages/UserMessage.svelte @@ -8,6 +8,7 @@ import Tooltip from '$lib/components/common/Tooltip.svelte'; import { user as _user } from '$lib/stores'; + import { getFileContentById } from '$lib/apis/files'; const i18n = getContext('i18n'); @@ -97,6 +98,47 @@
{#if file.type === 'image'} input + {:else if file.type === 'file'} + {:else if file.type === 'doc'}