From c694e6d626658a9668aa3ba9178bf4172c375afc Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Sun, 10 Sep 2023 20:22:20 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- imaginepy/async_imagine.py | 20 +++++++++----------- imaginepy/sync_imagine.py | 20 +++++++++----------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/imaginepy/async_imagine.py b/imaginepy/async_imagine.py index a1d3f2f..6d434f7 100644 --- a/imaginepy/async_imagine.py +++ b/imaginepy/async_imagine.py @@ -33,12 +33,11 @@ async def close(self): async def _request(self, **kwargs) -> Response: headers = {"accept": "*/*", "user-agent": "okhttp/4.10.0"} - headers.update(kwargs.get("headers") or {}) + headers |= (kwargs.get("headers") or {}) data = clear_dict(kwargs.get("data")) if data: - prompt = data.get("prompt", "").lower().split(" ") - if prompt: + if prompt := data.get("prompt", "").lower().split(" "): for i, word in enumerate(prompt): word = re.sub(r'[^a-zA-Z]', "", word) if word in BANNED_WORDS: @@ -76,8 +75,7 @@ async def _request(self, **kwargs) -> Response: return r async def thumb(self, item: Union[Model, Style, Inspiration, Mode]) -> bytes: - href = item.value[2 if isinstance( - item, Model) or isinstance(item, Style) else 1] + href = item.value[2 if isinstance(item, (Model, Style)) else 1] async with httpx.AsyncClient() as client: response = await client.get(f"{self.cdn}/{href}") response.raise_for_status() @@ -113,7 +111,7 @@ async def variate( "image": ("image.jpeg", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -152,7 +150,7 @@ async def sdprem( "steps": get_steps(steps) } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -170,7 +168,7 @@ async def upscale(self, content: bytes, asbase64: bool = False) -> bytes: "image": ("_", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -239,7 +237,7 @@ async def sdimg( "priority": int(priority) } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -276,7 +274,7 @@ async def controlnet( "image": ("temp_314.1353898439128.jpg", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -294,7 +292,7 @@ async def codeformer(self, content: bytes, asbase64: bool = False) -> bytes: "image": ("tempImage.jpg", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) diff --git a/imaginepy/sync_imagine.py b/imaginepy/sync_imagine.py index 7989f41..8115782 100644 --- a/imaginepy/sync_imagine.py +++ b/imaginepy/sync_imagine.py @@ -29,12 +29,11 @@ def __init__(self, restricted: bool = True): def _request(self, **kwargs) -> Response: headers = {"accept": "*/*", "user-agent": "okhttp/4.10.0"} - headers.update(kwargs.get("headers") or {}) + headers |= (kwargs.get("headers") or {}) data = clear_dict(kwargs.get("data")) if data: - prompt = data.get("prompt", "").lower().split(" ") - if prompt: + if prompt := data.get("prompt", "").lower().split(" "): for i, word in enumerate(prompt): word = re.sub(r'[^a-zA-Z]', "", word) if word in BANNED_WORDS: @@ -72,8 +71,7 @@ def _request(self, **kwargs) -> Response: return r def thumb(self, item: Union[Model, Style, Inspiration, Mode]) -> bytes: - href = item.value[2 if isinstance( - item, Model) or isinstance(item, Style) else 1] + href = item.value[2 if isinstance(item, (Model, Style)) else 1] with httpx.Client() as client: response = client.get(f"{self.cdn}/{href}") response.raise_for_status() @@ -109,7 +107,7 @@ def variate( "image": ("image.jpeg", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -148,7 +146,7 @@ def sdprem( "steps": get_steps(steps) } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -166,7 +164,7 @@ def upscale(self, content: bytes, asbase64: bool = False) -> bytes: "image": ("_", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -235,7 +233,7 @@ def sdimg( "priority": int(priority) } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -272,7 +270,7 @@ def controlnet( "image": ("temp_314.1353898439128.jpg", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content) @@ -290,7 +288,7 @@ def codeformer(self, content: bytes, asbase64: bool = False) -> bytes: "image": ("tempImage.jpg", content, "image/jpg") } ) - if asbase64 == True: + if asbase64: return pybase64.b64encode(response.content).decode('utf-8') else: return bytes2png(response.content)