From b66881a3718d6e601997ee3821af3a6622b7088a Mon Sep 17 00:00:00 2001 From: Stephen Hu <812791840@qq.com> Date: Mon, 8 Dec 2025 10:18:46 +0800 Subject: [PATCH] Refactor:book parser use with to handle bytesIO (#11800) ### What problem does this PR solve? book parser use with to handle bytesIO ### Type of change - [x] Refactoring --- rag/app/book.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rag/app/book.py b/rag/app/book.py index ca91be149..4b59fdc86 100644 --- a/rag/app/book.py +++ b/rag/app/book.py @@ -143,13 +143,14 @@ def chunk(filename, binary=None, from_page=0, to_page=100000, elif re.search(r"\.doc$", filename, re.IGNORECASE): callback(0.1, "Start to parse.") - binary = BytesIO(binary) - doc_parsed = parser.from_buffer(binary) - sections = doc_parsed['content'].split('\n') - sections = [(line, "") for line in sections if line] - remove_contents_table(sections, eng=is_english( - random_choices([t for t, _ in sections], k=200))) - callback(0.8, "Finish parsing.") + with BytesIO(binary) as binary: + binary = BytesIO(binary) + doc_parsed = parser.from_buffer(binary) + sections = doc_parsed['content'].split('\n') + sections = [(line, "") for line in sections if line] + remove_contents_table(sections, eng=is_english( + random_choices([t for t, _ in sections], k=200))) + callback(0.8, "Finish parsing.") else: raise NotImplementedError(