Skip to content

Commit

Permalink
🐛 fix(sdk): Retry request on 500 error in upscale.py and generate_ima…
Browse files Browse the repository at this point in the history
…ge/__init__.py
  • Loading branch information
sudoskys committed Feb 17, 2024
1 parent 7e0ea99 commit a03a433
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions playground/generate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async def main():
session=globe_s, remove_sign=True
)
except APIError as e:
print(str(e))
print(e.response)
return

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies = [
"argon2-cffi>=23.1.0",
"opencv-python>=4.8.1.78",
"fake-useragent>=1.4.0",
"tenacity>=8.2.3",
]
requires-python = ">=3.8"
readme = "README.md"
Expand Down
7 changes: 7 additions & 0 deletions src/novelai_python/sdk/ai/generate_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from curl_cffi.requests import AsyncSession
from loguru import logger
from pydantic import BaseModel, ConfigDict, PrivateAttr, field_validator, model_validator, Field
from tenacity import retry, stop_after_attempt, wait_random, retry_if_exception
from typing_extensions import override

from ._enum import Model, Sampler, NoiseSchedule, ControlNetModel, Action, UCPreset
Expand Down Expand Up @@ -327,6 +328,12 @@ async def necessary_headers(self, request_data) -> dict:
"Cache-Control": "no-cache",
}

@retry(
wait=wait_random(min=1, max=3),
stop=stop_after_attempt(3),
retry=retry_if_exception(lambda e: hasattr(e, "code") and str(e.code) == "500"),
reraise=True
)
async def request(self,
session: Union[AsyncSession, "CredentialBase"],
*,
Expand Down
7 changes: 7 additions & 0 deletions src/novelai_python/sdk/ai/upscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from curl_cffi.requests import AsyncSession
from loguru import logger
from pydantic import ConfigDict, PrivateAttr, model_validator
from tenacity import wait_random, retry, stop_after_attempt, retry_if_exception

from ..schema import ApiBaseModel
from ..._exceptions import APIError, AuthError, SessionHttpError
Expand Down Expand Up @@ -87,6 +88,12 @@ async def necessary_headers(self, request_data) -> dict:
"Cache-Control": "no-cache",
}

@retry(
wait=wait_random(min=1, max=3),
stop=stop_after_attempt(3),
retry=retry_if_exception(lambda e: hasattr(e, "code") and str(e.code) == "500"),
reraise=True
)
async def request(self,
session: Union[AsyncSession, "CredentialBase"],
*,
Expand Down

0 comments on commit a03a433

Please sign in to comment.