Ver Fonte

refac: file upload

Timothy J. Baek há 1 ano atrás
pai
commit
d6a1bf1406
2 ficheiros alterados com 11 adições e 7 exclusões
  1. 7 3
      backend/apps/rag/main.py
  2. 4 4
      src/lib/components/chat/MessageInput.svelte

+ 7 - 3
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":

+ 4 - 4
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 = [