From ccb4db50b1d995d30893cbc2388b03b5cb8e339d Mon Sep 17 00:00:00 2001 From: Sosokker Date: Fri, 27 Jun 2025 22:33:04 +0700 Subject: [PATCH] refactor: streamline file ingestion process using temporary files --- app/api/endpoints.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/api/endpoints.py b/app/api/endpoints.py index 02d3f4b..2417860 100644 --- a/app/api/endpoints.py +++ b/app/api/endpoints.py @@ -1,4 +1,5 @@ import shutil +import tempfile from pathlib import Path from typing import Annotated @@ -39,20 +40,17 @@ async def ingest_file( file: Annotated[UploadFile, File(...)], rag_service: Annotated[RAGService, Depends(get_rag_service)], ): - # Save the uploaded file temporarily - temp_dir = Path("temp_files") - Path.mkdir(temp_dir, exist_ok=True) if not file.filename: raise HTTPException(status_code=400, detail="File name is required") - file_path = temp_dir / Path(file.filename) - with file_path.open("wb") as buffer: - shutil.copyfileobj(file.file, buffer) + with tempfile.NamedTemporaryFile(delete=True, suffix=file.filename) as tmp: + file.file.seek(0) + shutil.copyfileobj(file.file, tmp) + tmp.flush() + background_tasks.add_task( + rag_service.ingest_document, Path(tmp.name), file.filename + ) - # Add the ingestion task to run in the background - background_tasks.add_task(rag_service.ingest_document, file_path, file.filename) - - # Immediately return a response to the user return { "message": "File upload successful. Ingestion has started in the background.", "filename": file.filename,