From d6a1bf14062588745c6cb4e1fc08d91e5a225353 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 7 Jan 2024 09:00:30 -0800 Subject: [PATCH] refac: file upload --- backend/apps/rag/main.py | 10 +++++++--- src/lib/components/chat/MessageInput.svelte | 8 ++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index 5bf74b09..b3844d69 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -24,7 +24,7 @@ from typing import Optional import uuid - +from utils.misc import calculate_sha256 from utils.utils import get_current_user from config import UPLOAD_DIR, EMBED_MODEL, CHROMA_CLIENT, CHUNK_SIZE, CHUNK_OVERLAP from constants import ERROR_MESSAGES @@ -123,12 +123,11 @@ def store_web(form_data: StoreWebForm, user=Depends(get_current_user)): @app.post("/doc") def store_doc( - collection_name: str = Form(...), + collection_name: Optional[str] = Form(None), file: UploadFile = File(...), user=Depends(get_current_user), ): # "https://www.gutenberg.org/files/1727/1727-h/1727-h.htm" - file.filename = f"{collection_name}-{file.filename}" if file.content_type not in ["application/pdf", "text/plain"]: raise HTTPException( @@ -144,6 +143,11 @@ def store_doc( f.write(contents) f.close() + f = open(file_path, "rb") + if collection_name == None: + collection_name = calculate_sha256(f)[:63] + f.close() + if file.content_type == "application/pdf": loader = PyPDFLoader(file_path) elif file.content_type == "text/plain": diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 36511e59..9653f81e 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -124,8 +124,8 @@ reader.readAsDataURL(file); } else if (['application/pdf', 'text/plain'].includes(file['type'])) { console.log(file); - const hash = (await calculateSHA256(file)).substring(0, 63); - const res = await uploadDocToVectorDB(localStorage.token, hash, file); + // const hash = (await calculateSHA256(file)).substring(0, 63); + const res = await uploadDocToVectorDB(localStorage.token, '', file); if (res) { files = [ @@ -243,8 +243,8 @@ reader.readAsDataURL(file); } else if (['application/pdf', 'text/plain'].includes(file['type'])) { console.log(file); - const hash = (await calculateSHA256(file)).substring(0, 63); - const res = await uploadDocToVectorDB(localStorage.token, hash, file); + // const hash = (await calculateSHA256(file)).substring(0, 63); + const res = await uploadDocToVectorDB(localStorage.token, '', file); if (res) { files = [