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" + )