From cbb72ff0dbe44965920a460614a4c957583bbeeb Mon Sep 17 00:00:00 2001 From: FangYin Cheng Date: Sat, 2 Sep 2023 11:18:55 +0800 Subject: [PATCH 1/2] fix: Fix ChatExcel upload file error on windows --- pilot/openapi/api_v1/api_v1.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pilot/openapi/api_v1/api_v1.py b/pilot/openapi/api_v1/api_v1.py index 515af75bb..e6cc6a365 100644 --- a/pilot/openapi/api_v1/api_v1.py +++ b/pilot/openapi/api_v1/api_v1.py @@ -16,7 +16,7 @@ from fastapi import ( from fastapi.responses import StreamingResponse from fastapi.exceptions import RequestValidationError from typing import List -from tempfile import NamedTemporaryFile +import tempfile from pilot.openapi.api_view_model import ( Result, @@ -222,17 +222,17 @@ async def params_load(conv_uid: str, chat_mode: str, doc_file: UploadFile = File ## file save if not os.path.exists(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode)): os.makedirs(os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode)) - with NamedTemporaryFile( - dir=os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode), delete=False - ) as tmp: + # We can not move temp file in windows system when we open file in context of `with` + tmp_fd, tmp_path = tempfile.mkstemp( + dir=os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode) + ) + # TODO Use no noblocking file save with aiofiles + with os.fdopen(tmp_fd, "wb") as tmp: tmp.write(await doc_file.read()) - tmp_path = tmp.name - shutil.move( - tmp_path, - os.path.join( - KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode, doc_file.filename - ), - ) + shutil.move( + tmp_path, + os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode, doc_file.filename), + ) ## chat prepare dialogue = ConversationVo( conv_uid=conv_uid, chat_mode=chat_mode, select_param=doc_file.filename From aa85a0111b0a74f5e5da1f3b5f6d88e3ef57440c Mon Sep 17 00:00:00 2001 From: FangYin Cheng Date: Sat, 2 Sep 2023 11:31:33 +0800 Subject: [PATCH 2/2] fix: Fix comment error --- pilot/openapi/api_v1/api_v1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pilot/openapi/api_v1/api_v1.py b/pilot/openapi/api_v1/api_v1.py index e6cc6a365..fdbfcf2b1 100644 --- a/pilot/openapi/api_v1/api_v1.py +++ b/pilot/openapi/api_v1/api_v1.py @@ -226,7 +226,7 @@ async def params_load(conv_uid: str, chat_mode: str, doc_file: UploadFile = File tmp_fd, tmp_path = tempfile.mkstemp( dir=os.path.join(KNOWLEDGE_UPLOAD_ROOT_PATH, chat_mode) ) - # TODO Use no noblocking file save with aiofiles + # TODO Use noblocking file save with aiofiles with os.fdopen(tmp_fd, "wb") as tmp: tmp.write(await doc_file.read()) shutil.move(