From 8e7b333fea8b40bdd52e3bc95a774dfef56d8205 Mon Sep 17 00:00:00 2001 From: awwaawwa <8493196+awwaawwa@users.noreply.github.com> Date: Thu, 27 Feb 2025 20:21:53 +0800 Subject: [PATCH 1/3] feat(docvision): add retry mechanism to predict_layout --- babeldoc/docvision/rpc_doclayout.py | 101 +++++++++++++++------------- pyproject.toml | 3 +- 2 files changed, 58 insertions(+), 46 deletions(-) diff --git a/babeldoc/docvision/rpc_doclayout.py b/babeldoc/docvision/rpc_doclayout.py index 8e068ab..05cee0c 100644 --- a/babeldoc/docvision/rpc_doclayout.py +++ b/babeldoc/docvision/rpc_doclayout.py @@ -5,6 +5,10 @@ import httpx import msgpack import numpy as np +from tenacity import retry +from tenacity import retry_if_exception_type +from tenacity import stop_after_attempt +from tenacity import wait_exponential from babeldoc.docvision.doclayout import DocLayoutModel from babeldoc.docvision.doclayout import YoloBox @@ -34,6 +38,17 @@ def encode_image(image) -> bytes: return encoded +@retry( + stop=stop_after_attempt(3), # 最多重试3次 + wait=wait_exponential( + multiplier=1, min=1, max=10 + ), # 指数退避策略,初始1秒,最大10秒 + retry=retry_if_exception_type((httpx.HTTPError, Exception)), # 针对哪些异常重试 + before_sleep=lambda retry_state: logger.warning( + f"Request failed, retrying in {retry_state.next_action.sleep} seconds... " + f"(Attempt {retry_state.attempt_number}/3)" + ), +) def predict_layout( image, host: str = "http://localhost:8000", @@ -50,53 +65,49 @@ def predict_layout( Returns: List of predictions containing bounding boxes and classes """ - try: - # Prepare request data - if not isinstance(image, list): - image = [image] - image_data = [encode_image(image) for image in image] - data = { - "image": image_data, - "imgsz": imgsz, - } - - # Pack data using msgpack - packed_data = msgpack.packb(data, use_bin_type=True) - logger.debug(f"Packed data size: {len(packed_data)} bytes") - - # Send request - logger.debug(f"Sending request to {host}/inference") - response = httpx.post( - f"{host}/inference", - data=packed_data, - headers={ - "Content-Type": "application/msgpack", - "Accept": "application/msgpack", - }, - timeout=300, - follow_redirects=True, + # Prepare request data + if not isinstance(image, list): + image = [image] + image_data = [encode_image(image) for image in image] + data = { + "image": image_data, + "imgsz": imgsz, + } + + # Pack data using msgpack + packed_data = msgpack.packb(data, use_bin_type=True) + logger.debug(f"Packed data size: {len(packed_data)} bytes") + + # Send request + logger.debug(f"Sending request to {host}/inference") + response = httpx.post( + f"{host}/inference", + data=packed_data, + headers={ + "Content-Type": "application/msgpack", + "Accept": "application/msgpack", + }, + timeout=300, + follow_redirects=True, + ) + + logger.debug(f"Response status: {response.status_code}") + logger.debug(f"Response headers: {response.headers}") + + if response.status_code == 200: + try: + result = msgpack.unpackb(response.content, raw=False) + return result + except Exception as e: + logger.exception(f"Failed to unpack response: {e!s}") + raise + else: + logger.error(f"Request failed with status {response.status_code}") + logger.error(f"Response content: {response.content}") + raise Exception( + f"Request failed with status {response.status_code}: {response.text}", ) - logger.debug(f"Response status: {response.status_code}") - logger.debug(f"Response headers: {response.headers}") - - if response.status_code == 200: - try: - result = msgpack.unpackb(response.content, raw=False) - return result - except Exception as e: - logger.exception(f"Failed to unpack response: {e!s}") - raise - else: - logger.error(f"Request failed with status {response.status_code}") - logger.error(f"Response content: {response.content}") - raise Exception( - f"Request failed with status {response.status_code}: {response.text}", - ) - except Exception as e: - logger.exception(f"Unexpected error: {e!s}") - raise - class RpcDocLayoutModel(DocLayoutModel): """DocLayoutModel implementation that uses RPC service.""" diff --git a/pyproject.toml b/pyproject.toml index 19c23a9..b4a9ba4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ dependencies = [ "xsdata[cli,lxml,soap]>=24.12", "msgpack>=1.1.0", "pydantic>=2.10.6", + "tenacity>=9.0.0", ] [project.urls] @@ -147,4 +148,4 @@ version_pattern = "MAJOR.MINOR.PATCH[.PYTAGNUM]" ] "babeldoc/const.py" = [ '__version__ = "{version}"' -] \ No newline at end of file +] From af2467e06439b038727153e67a8ba3c7224f54a3 Mon Sep 17 00:00:00 2001 From: awwaawwa <8493196+awwaawwa@users.noreply.github.com> Date: Thu, 27 Feb 2025 20:22:11 +0800 Subject: [PATCH 2/3] chore(dependencies): Update uv.lock with package version bumps - Bump babeldoc version to 0.1.14 - Update package version specifiers for various dependencies - Refresh lock file with latest package versions --- uv.lock | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/uv.lock b/uv.lock index 28b5861..461fa40 100644 --- a/uv.lock +++ b/uv.lock @@ -48,7 +48,7 @@ wheels = [ [[package]] name = "babeldoc" -version = "0.1.13" +version = "0.1.14" source = { editable = "." } dependencies = [ { name = "bitstring" }, @@ -67,6 +67,7 @@ dependencies = [ { name = "pydantic" }, { name = "pymupdf" }, { name = "rich" }, + { name = "tenacity" }, { name = "toml" }, { name = "tqdm" }, { name = "xsdata", extra = ["cli", "lxml", "soap"] }, @@ -105,6 +106,7 @@ requires-dist = [ { name = "pydantic", specifier = ">=2.10.6" }, { name = "pymupdf", specifier = ">=1.25.1" }, { name = "rich", specifier = ">=13.9.4" }, + { name = "tenacity", specifier = ">=9.0.0" }, { name = "toml", specifier = ">=0.10.2" }, { name = "tqdm", specifier = ">=4.67.1" }, { name = "xsdata", extras = ["cli", "lxml", "soap"], specifier = ">=24.12" }, @@ -1753,6 +1755,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/99/ff/c87e0622b1dadea79d2fb0b25ade9ed98954c9033722eb707053d310d4f3/sympy-1.13.3-py3-none-any.whl", hash = "sha256:54612cf55a62755ee71824ce692986f23c88ffa77207b30c1368eda4a7060f73", size = 6189483 }, ] +[[package]] +name = "tenacity" +version = "9.0.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/cd/94/91fccdb4b8110642462e653d5dcb27e7b674742ad68efd146367da7bdb10/tenacity-9.0.0.tar.gz", hash = "sha256:807f37ca97d62aa361264d497b0e31e92b8027044942bfa756160d908320d73b", size = 47421 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b6/cb/b86984bed139586d01532a587464b5805f12e397594f19f931c4c2fbfa61/tenacity-9.0.0-py3-none-any.whl", hash = "sha256:93de0c98785b27fcf659856aa9f54bfbd399e29969b0621bc7f762bd441b4539", size = 28169 }, +] + [[package]] name = "toml" version = "0.10.2" From 4e33f9a8f015df48594073057e99c8ac5824c4b6 Mon Sep 17 00:00:00 2001 From: awwaawwa <8493196+awwaawwa@users.noreply.github.com> Date: Thu, 27 Feb 2025 20:24:46 +0800 Subject: [PATCH 3/3] chore: Bump version to 0.1.15 Update version number across project files: - Increment version in pyproject.toml - Update __version__ in __init__.py, const.py, and main.py --- babeldoc/__init__.py | 2 +- babeldoc/const.py | 2 +- babeldoc/main.py | 2 +- pyproject.toml | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/babeldoc/__init__.py b/babeldoc/__init__.py index fb69db9..f3b4574 100644 --- a/babeldoc/__init__.py +++ b/babeldoc/__init__.py @@ -1 +1 @@ -__version__ = "0.1.14" +__version__ = "0.1.15" diff --git a/babeldoc/const.py b/babeldoc/const.py index 04e1522..7457118 100644 --- a/babeldoc/const.py +++ b/babeldoc/const.py @@ -2,7 +2,7 @@ import subprocess from pathlib import Path -__version__ = "0.1.14" +__version__ = "0.1.15" CACHE_FOLDER = Path.home() / ".cache" / "babeldoc" diff --git a/babeldoc/main.py b/babeldoc/main.py index 429f407..a44ddfc 100644 --- a/babeldoc/main.py +++ b/babeldoc/main.py @@ -23,7 +23,7 @@ from babeldoc.translation_config import TranslationConfig logger = logging.getLogger(__name__) -__version__ = "0.1.14" +__version__ = "0.1.15" def create_parser(): diff --git a/pyproject.toml b/pyproject.toml index b4a9ba4..ae26155 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "BabelDOC" -version = "0.1.14" +version = "0.1.15" description = "Yet Another Document Translator" license = "AGPL-3.0" readme = "README.md" @@ -132,7 +132,7 @@ pythonpath = [".", "src"] testpaths = ["tests"] [bumpver] -current_version = "0.1.14" +current_version = "0.1.15" version_pattern = "MAJOR.MINOR.PATCH[.PYTAGNUM]" [bumpver.file_patterns]