From b3818a9ab1f583d24e57237728c62011c9d20ba3 Mon Sep 17 00:00:00 2001 From: jingrow Date: Fri, 21 Nov 2025 00:29:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(rmbg):=20=E5=B0=86file=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=B5=81=E5=BC=8F=E8=BF=94=E5=9B=9E=EF=BC=8C?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=8E=A5=E5=8F=A3=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将file上传接口从一次性返回改为流式返回 - 使用StreamingResponse和NDJSON格式,与batch接口保持一致 - 提升用户体验,减少响应等待时间 - 统一两种接口的返回格式 --- apps/rmbg/api.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/rmbg/api.py b/apps/rmbg/api.py index b52be95..fce50f6 100644 --- a/apps/rmbg/api.py +++ b/apps/rmbg/api.py @@ -38,15 +38,22 @@ async def remove_background_batch(data: dict, request: Request): @jingrow_api_verify_and_billing(api_name=settings.api_name) async def remove_background_file(file: UploadFile = File(...), request: Request = None): """ - 从上传的文件移除背景 + 从上传的文件移除背景(流式返回) Args: file: 上传的图片文件 request: FastAPI 请求对象 Returns: - 处理后的图片内容 + 流式响应,包含处理后的图片内容 """ content = await file.read() - result = await service.remove_background_from_file(content) - return result + + async def process_and_stream(): + result = await service.remove_background_from_file(content) + yield json.dumps(result) + "\n" + + return StreamingResponse( + process_and_stream(), + media_type="application/x-ndjson" + )