mirror of
https://github.com/Sosokker/plain-rag.git
synced 2025-12-19 14:54:05 +01:00
refactor: streamline file ingestion process using temporary files
This commit is contained in:
parent
2f13d8c3ce
commit
ccb4db50b1
@ -1,4 +1,5 @@
|
|||||||
import shutil
|
import shutil
|
||||||
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
|
||||||
@ -39,20 +40,17 @@ async def ingest_file(
|
|||||||
file: Annotated[UploadFile, File(...)],
|
file: Annotated[UploadFile, File(...)],
|
||||||
rag_service: Annotated[RAGService, Depends(get_rag_service)],
|
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:
|
if not file.filename:
|
||||||
raise HTTPException(status_code=400, detail="File name is required")
|
raise HTTPException(status_code=400, detail="File name is required")
|
||||||
file_path = temp_dir / Path(file.filename)
|
|
||||||
|
|
||||||
with file_path.open("wb") as buffer:
|
with tempfile.NamedTemporaryFile(delete=True, suffix=file.filename) as tmp:
|
||||||
shutil.copyfileobj(file.file, buffer)
|
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 {
|
return {
|
||||||
"message": "File upload successful. Ingestion has started in the background.",
|
"message": "File upload successful. Ingestion has started in the background.",
|
||||||
"filename": file.filename,
|
"filename": file.filename,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user