From 0b456a18a3e0fde3f774355ae8d66e13c3a2ae08 Mon Sep 17 00:00:00 2001 From: Stephen Hu Date: Tue, 2 Sep 2025 10:31:37 +0800 Subject: [PATCH] Refactor: Improve the buffer close for vision_llm_chunk (#9845) ### What problem does this PR solve? Improve the buffer close for vision_llm_chunk ### Type of change - [x] Refactoring --- rag/app/picture.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/rag/app/picture.py b/rag/app/picture.py index db9bb3729..a868cf4cc 100644 --- a/rag/app/picture.py +++ b/rag/app/picture.py @@ -78,15 +78,12 @@ def vision_llm_chunk(binary, vision_model, prompt=None, callback=None): txt = "" try: - img_binary = io.BytesIO() - img.save(img_binary, format='JPEG') - img_binary.seek(0) - - ans = clean_markdown_block(vision_model.describe_with_prompt(img_binary.read(), prompt)) - - txt += "\n" + ans - - return txt + with io.BytesIO() as img_binary: + img.save(img_binary, format='JPEG') + img_binary.seek(0) + ans = clean_markdown_block(vision_model.describe_with_prompt(img_binary.read(), prompt)) + txt += "\n" + ans + return txt except Exception as e: callback(-1, str(e))