From b94115837b178b30327a3b1202dd922d784c9666 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 17 Feb 2024 11:21:04 +0800 Subject: [PATCH 1/9] fix(logger): Change error log to warning log in SDK files --- src/novelai_python/sdk/ai/generate_image/__init__.py | 2 +- src/novelai_python/sdk/ai/generate_image/suggest_tags.py | 2 +- src/novelai_python/sdk/ai/upscale.py | 2 +- src/novelai_python/sdk/user/information.py | 2 +- src/novelai_python/sdk/user/login.py | 2 +- src/novelai_python/sdk/user/subscription.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index cde9550..8ed62c2 100644 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -383,7 +383,7 @@ async def request(self, data=json.dumps(request_data).encode("utf-8") ) if response.headers.get('Content-Type') not in ['binary/octet-stream', 'application/x-zip-compressed']: - logger.error( + logger.warning( f"Error with content type: {response.headers.get('Content-Type')} and code: {response.status_code}" ) try: diff --git a/src/novelai_python/sdk/ai/generate_image/suggest_tags.py b/src/novelai_python/sdk/ai/generate_image/suggest_tags.py index a56cb65..84fc8b5 100644 --- a/src/novelai_python/sdk/ai/generate_image/suggest_tags.py +++ b/src/novelai_python/sdk/ai/generate_image/suggest_tags.py @@ -84,7 +84,7 @@ async def request(self, url=self.base_url + "?" + "&".join([f"{k}={v}" for k, v in request_data.items()]) ) if "application/json" not in response.headers.get('Content-Type') or response.status_code != 200: - logger.error( + logger.warning( f"Error with content type: {response.headers.get('Content-Type')} and code: {response.status_code}" ) try: diff --git a/src/novelai_python/sdk/ai/upscale.py b/src/novelai_python/sdk/ai/upscale.py index f21fb65..1b36724 100644 --- a/src/novelai_python/sdk/ai/upscale.py +++ b/src/novelai_python/sdk/ai/upscale.py @@ -131,7 +131,7 @@ async def request(self, data=json.dumps(request_data).encode("utf-8") ) if response.headers.get('Content-Type') not in ['binary/octet-stream', 'application/x-zip-compressed']: - logger.error( + logger.warning( f"Error with content type: {response.headers.get('Content-Type')} and code: {response.status_code}" ) try: diff --git a/src/novelai_python/sdk/user/information.py b/src/novelai_python/sdk/user/information.py index 3cfbb90..6dc053b 100644 --- a/src/novelai_python/sdk/user/information.py +++ b/src/novelai_python/sdk/user/information.py @@ -73,7 +73,7 @@ async def request(self, self.base_url ) if "application/json" not in response.headers.get('Content-Type') or response.status_code != 200: - logger.error( + logger.warning( f"Error with content type: {response.headers.get('Content-Type')} and code: {response.status_code}" ) try: diff --git a/src/novelai_python/sdk/user/login.py b/src/novelai_python/sdk/user/login.py index a623ae1..f0dfc33 100644 --- a/src/novelai_python/sdk/user/login.py +++ b/src/novelai_python/sdk/user/login.py @@ -92,7 +92,7 @@ async def request(self, data=json.dumps(request_data).encode("utf-8") ) if "application/json" not in response.headers.get('Content-Type') or response.status_code != 201: - logger.error( + logger.warning( f"Error with content type: {response.headers.get('Content-Type')} and code: {response.status_code}" ) try: diff --git a/src/novelai_python/sdk/user/subscription.py b/src/novelai_python/sdk/user/subscription.py index 4fd9ccc..d1e893b 100644 --- a/src/novelai_python/sdk/user/subscription.py +++ b/src/novelai_python/sdk/user/subscription.py @@ -80,7 +80,7 @@ async def request(self, url=self.base_url, ) if "application/json" not in response.headers.get('Content-Type') or response.status_code != 200: - logger.error( + logger.warning( f"Error with content type: {response.headers.get('Content-Type')} and code: {response.status_code}" ) try: From d8351925832fa9144497bef09d5c6a77458418cb Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 17 Feb 2024 12:02:39 +0800 Subject: [PATCH 2/9] fix(logger): Change error log to warning log in SDK files --- playground/generate_image.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/generate_image.py b/playground/generate_image.py index c16b703..9cb1260 100644 --- a/playground/generate_image.py +++ b/playground/generate_image.py @@ -11,7 +11,7 @@ from novelai_python import APIError, Login from novelai_python import GenerateImageInfer, ImageGenerateResp, JwtCredential -from novelai_python.sdk.ai.generate_image import Action +from novelai_python.sdk.ai.generate_image import Action, Sampler from novelai_python.utils.useful import enum_to_list load_dotenv() @@ -30,6 +30,7 @@ async def main(): gen = GenerateImageInfer.build( prompt=f"1girl, winter, jacket, sfw, angel, flower,{enhance}", action=Action.GENERATE, + sampler=Sampler.DDIM_V3 ) cost = gen.calculate_cost(is_opus=True) print(f"charge: {cost} if you are vip3") From 6bef7506c4b060f61693307772a6571077ea1f84 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 18:29:03 +0800 Subject: [PATCH 3/9] fix(logger): Change error log to warning log in SDK files --- .github/workflows/publish.yml | 0 .github/workflows/python_test.yml | 0 .gitignore | 0 .pre-commit-config.yaml | 0 LICENSE | 0 NOTICE.MD | 0 README.md | 0 pdm.lock | 0 playground/enhance.py | 0 playground/generate_image.py | 0 playground/generate_image_img2img.py | 0 playground/information.py | 0 playground/login.py | 0 playground/mask/__init__.py | 0 playground/queue_select.py | 0 playground/random_prompt.py | 0 playground/read_nai_tag.py | 0 playground/subscription.py | 0 playground/suggest_tag.py | 0 playground/upscale.py | 0 pyproject.toml | 0 src/novelai_python/__init__.py | 0 src/novelai_python/_exceptions.py | 0 src/novelai_python/_response/__init__.py | 0 src/novelai_python/_response/ai/__init__.py | 0 src/novelai_python/_response/ai/generate_image.py | 0 src/novelai_python/_response/ai/upscale.py | 0 src/novelai_python/_response/schema.py | 0 src/novelai_python/_response/user/__init__.py | 0 src/novelai_python/_response/user/information.py | 0 src/novelai_python/_response/user/login.py | 0 src/novelai_python/_response/user/subscription.py | 0 src/novelai_python/credential/ApiToken.py | 0 src/novelai_python/credential/JwtToken.py | 0 src/novelai_python/credential/UserAuth.py | 0 src/novelai_python/credential/__init__.py | 0 src/novelai_python/credential/_base.py | 0 src/novelai_python/sdk/__init__.py | 0 src/novelai_python/sdk/ai/LICENSE | 0 src/novelai_python/sdk/ai/__init__.py | 0 src/novelai_python/sdk/ai/generate_image/__init__.py | 0 src/novelai_python/sdk/ai/generate_image/_enum.py | 0 src/novelai_python/sdk/ai/generate_image/suggest_tags.py | 0 src/novelai_python/sdk/ai/upscale.py | 0 src/novelai_python/sdk/schema.py | 0 src/novelai_python/sdk/user/__init__.py | 0 src/novelai_python/sdk/user/information.py | 0 src/novelai_python/sdk/user/login.py | 0 src/novelai_python/sdk/user/subscription.py | 0 src/novelai_python/server.py | 0 src/novelai_python/utils/__init__.py | 0 src/novelai_python/utils/encode.py | 0 src/novelai_python/utils/hash.py | 0 src/novelai_python/utils/random_prompt/__init__.py | 0 src/novelai_python/utils/random_prompt/tag.py | 0 src/novelai_python/utils/random_prompt/tag_character.py | 0 src/novelai_python/utils/random_prompt/tag_nsfw.py | 0 src/novelai_python/utils/useful.py | 0 tests/__init__.py | 0 tests/test_random_prompt.py | 0 tests/test_server.py | 0 tests/test_server_run.py | 0 tests/test_upscale.py | 0 tests/test_user_information.py | 0 tests/test_user_login.py | 0 tests/test_user_subscription.py | 0 66 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 .github/workflows/publish.yml mode change 100644 => 100755 .github/workflows/python_test.yml mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .pre-commit-config.yaml mode change 100644 => 100755 LICENSE mode change 100644 => 100755 NOTICE.MD mode change 100644 => 100755 README.md mode change 100644 => 100755 pdm.lock mode change 100644 => 100755 playground/enhance.py mode change 100644 => 100755 playground/generate_image.py mode change 100644 => 100755 playground/generate_image_img2img.py mode change 100644 => 100755 playground/information.py mode change 100644 => 100755 playground/login.py mode change 100644 => 100755 playground/mask/__init__.py mode change 100644 => 100755 playground/queue_select.py mode change 100644 => 100755 playground/random_prompt.py mode change 100644 => 100755 playground/read_nai_tag.py mode change 100644 => 100755 playground/subscription.py mode change 100644 => 100755 playground/suggest_tag.py mode change 100644 => 100755 playground/upscale.py mode change 100644 => 100755 pyproject.toml mode change 100644 => 100755 src/novelai_python/__init__.py mode change 100644 => 100755 src/novelai_python/_exceptions.py mode change 100644 => 100755 src/novelai_python/_response/__init__.py mode change 100644 => 100755 src/novelai_python/_response/ai/__init__.py mode change 100644 => 100755 src/novelai_python/_response/ai/generate_image.py mode change 100644 => 100755 src/novelai_python/_response/ai/upscale.py mode change 100644 => 100755 src/novelai_python/_response/schema.py mode change 100644 => 100755 src/novelai_python/_response/user/__init__.py mode change 100644 => 100755 src/novelai_python/_response/user/information.py mode change 100644 => 100755 src/novelai_python/_response/user/login.py mode change 100644 => 100755 src/novelai_python/_response/user/subscription.py mode change 100644 => 100755 src/novelai_python/credential/ApiToken.py mode change 100644 => 100755 src/novelai_python/credential/JwtToken.py mode change 100644 => 100755 src/novelai_python/credential/UserAuth.py mode change 100644 => 100755 src/novelai_python/credential/__init__.py mode change 100644 => 100755 src/novelai_python/credential/_base.py mode change 100644 => 100755 src/novelai_python/sdk/__init__.py mode change 100644 => 100755 src/novelai_python/sdk/ai/LICENSE mode change 100644 => 100755 src/novelai_python/sdk/ai/__init__.py mode change 100644 => 100755 src/novelai_python/sdk/ai/generate_image/__init__.py mode change 100644 => 100755 src/novelai_python/sdk/ai/generate_image/_enum.py mode change 100644 => 100755 src/novelai_python/sdk/ai/generate_image/suggest_tags.py mode change 100644 => 100755 src/novelai_python/sdk/ai/upscale.py mode change 100644 => 100755 src/novelai_python/sdk/schema.py mode change 100644 => 100755 src/novelai_python/sdk/user/__init__.py mode change 100644 => 100755 src/novelai_python/sdk/user/information.py mode change 100644 => 100755 src/novelai_python/sdk/user/login.py mode change 100644 => 100755 src/novelai_python/sdk/user/subscription.py mode change 100644 => 100755 src/novelai_python/server.py mode change 100644 => 100755 src/novelai_python/utils/__init__.py mode change 100644 => 100755 src/novelai_python/utils/encode.py mode change 100644 => 100755 src/novelai_python/utils/hash.py mode change 100644 => 100755 src/novelai_python/utils/random_prompt/__init__.py mode change 100644 => 100755 src/novelai_python/utils/random_prompt/tag.py mode change 100644 => 100755 src/novelai_python/utils/random_prompt/tag_character.py mode change 100644 => 100755 src/novelai_python/utils/random_prompt/tag_nsfw.py mode change 100644 => 100755 src/novelai_python/utils/useful.py mode change 100644 => 100755 tests/__init__.py mode change 100644 => 100755 tests/test_random_prompt.py mode change 100644 => 100755 tests/test_server.py mode change 100644 => 100755 tests/test_server_run.py mode change 100644 => 100755 tests/test_upscale.py mode change 100644 => 100755 tests/test_user_information.py mode change 100644 => 100755 tests/test_user_login.py mode change 100644 => 100755 tests/test_user_subscription.py diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml old mode 100644 new mode 100755 diff --git a/.github/workflows/python_test.yml b/.github/workflows/python_test.yml old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/NOTICE.MD b/NOTICE.MD old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/pdm.lock b/pdm.lock old mode 100644 new mode 100755 diff --git a/playground/enhance.py b/playground/enhance.py old mode 100644 new mode 100755 diff --git a/playground/generate_image.py b/playground/generate_image.py old mode 100644 new mode 100755 diff --git a/playground/generate_image_img2img.py b/playground/generate_image_img2img.py old mode 100644 new mode 100755 diff --git a/playground/information.py b/playground/information.py old mode 100644 new mode 100755 diff --git a/playground/login.py b/playground/login.py old mode 100644 new mode 100755 diff --git a/playground/mask/__init__.py b/playground/mask/__init__.py old mode 100644 new mode 100755 diff --git a/playground/queue_select.py b/playground/queue_select.py old mode 100644 new mode 100755 diff --git a/playground/random_prompt.py b/playground/random_prompt.py old mode 100644 new mode 100755 diff --git a/playground/read_nai_tag.py b/playground/read_nai_tag.py old mode 100644 new mode 100755 diff --git a/playground/subscription.py b/playground/subscription.py old mode 100644 new mode 100755 diff --git a/playground/suggest_tag.py b/playground/suggest_tag.py old mode 100644 new mode 100755 diff --git a/playground/upscale.py b/playground/upscale.py old mode 100644 new mode 100755 diff --git a/pyproject.toml b/pyproject.toml old mode 100644 new mode 100755 diff --git a/src/novelai_python/__init__.py b/src/novelai_python/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_exceptions.py b/src/novelai_python/_exceptions.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/__init__.py b/src/novelai_python/_response/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/ai/__init__.py b/src/novelai_python/_response/ai/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/ai/generate_image.py b/src/novelai_python/_response/ai/generate_image.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/ai/upscale.py b/src/novelai_python/_response/ai/upscale.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/schema.py b/src/novelai_python/_response/schema.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/user/__init__.py b/src/novelai_python/_response/user/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/user/information.py b/src/novelai_python/_response/user/information.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/user/login.py b/src/novelai_python/_response/user/login.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/_response/user/subscription.py b/src/novelai_python/_response/user/subscription.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/credential/ApiToken.py b/src/novelai_python/credential/ApiToken.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/credential/JwtToken.py b/src/novelai_python/credential/JwtToken.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/credential/UserAuth.py b/src/novelai_python/credential/UserAuth.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/credential/__init__.py b/src/novelai_python/credential/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/credential/_base.py b/src/novelai_python/credential/_base.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/__init__.py b/src/novelai_python/sdk/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/ai/LICENSE b/src/novelai_python/sdk/ai/LICENSE old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/ai/__init__.py b/src/novelai_python/sdk/ai/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/ai/generate_image/_enum.py b/src/novelai_python/sdk/ai/generate_image/_enum.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/ai/generate_image/suggest_tags.py b/src/novelai_python/sdk/ai/generate_image/suggest_tags.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/ai/upscale.py b/src/novelai_python/sdk/ai/upscale.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/schema.py b/src/novelai_python/sdk/schema.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/user/__init__.py b/src/novelai_python/sdk/user/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/user/information.py b/src/novelai_python/sdk/user/information.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/user/login.py b/src/novelai_python/sdk/user/login.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/sdk/user/subscription.py b/src/novelai_python/sdk/user/subscription.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/server.py b/src/novelai_python/server.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/__init__.py b/src/novelai_python/utils/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/encode.py b/src/novelai_python/utils/encode.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/hash.py b/src/novelai_python/utils/hash.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/random_prompt/__init__.py b/src/novelai_python/utils/random_prompt/__init__.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/random_prompt/tag.py b/src/novelai_python/utils/random_prompt/tag.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/random_prompt/tag_character.py b/src/novelai_python/utils/random_prompt/tag_character.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/random_prompt/tag_nsfw.py b/src/novelai_python/utils/random_prompt/tag_nsfw.py old mode 100644 new mode 100755 diff --git a/src/novelai_python/utils/useful.py b/src/novelai_python/utils/useful.py old mode 100644 new mode 100755 diff --git a/tests/__init__.py b/tests/__init__.py old mode 100644 new mode 100755 diff --git a/tests/test_random_prompt.py b/tests/test_random_prompt.py old mode 100644 new mode 100755 diff --git a/tests/test_server.py b/tests/test_server.py old mode 100644 new mode 100755 diff --git a/tests/test_server_run.py b/tests/test_server_run.py old mode 100644 new mode 100755 diff --git a/tests/test_upscale.py b/tests/test_upscale.py old mode 100644 new mode 100755 diff --git a/tests/test_user_information.py b/tests/test_user_information.py old mode 100644 new mode 100755 diff --git a/tests/test_user_login.py b/tests/test_user_login.py old mode 100644 new mode 100755 diff --git a/tests/test_user_subscription.py b/tests/test_user_subscription.py old mode 100644 new mode 100755 From 4f1f110720687800114667de46781e266c7ea037 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 19:03:49 +0800 Subject: [PATCH 4/9] fix(sdk): Add reference_image reference_strength etc. --- .../sdk/ai/generate_image/__init__.py | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index 8ed62c2..e4e57aa 100755 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -34,7 +34,7 @@ class GenerateImageInfer(ApiBaseModel): class Params(BaseModel): # Inpaint - add_original_image: Optional[bool] = False + add_original_image: Optional[bool] = True # FIXME: 未知作用 mask: Optional[Union[str, bytes]] = None # img2img,base64 cfg_rescale: Optional[float] = Field(0, ge=0, le=1, multiple_of=0.02) @@ -57,6 +57,20 @@ class Params(BaseModel): # Misc params_version: Optional[int] = 1 + reference_image: Optional[Union[str, bytes]] = None + reference_information_extracted: Optional[float] = Field(1, + ge=0, + le=1, + multiple_of=0.01, + description="extracting concepts or features" + ) + reference_strength: Optional[float] = Field(0.6, + ge=0, + le=1, + multiple_of=0.01, + description="the stronger the AI will try to emulate visual cues." + ) + legacy: Optional[bool] = False legacy_v3_extend: Optional[bool] = False @@ -108,6 +122,14 @@ def image_validator(cls, v: Union[str, bytes]): return base64.b64encode(v).decode("utf-8") return v + @field_validator('reference_image') + def image_validator(cls, v: Union[str, bytes]): + if isinstance(v, str) and v.startswith("data:image/"): + raise ValueError("Invalid image format, must be base64 encoded.") + if isinstance(v, bytes): + return base64.b64encode(v).decode("utf-8") + return v + @field_validator('width') def width_validator(cls, v: int): """ @@ -183,6 +205,8 @@ def validate_model(self): if self.action == Action.IMG2IMG: self.parameters.sm = False self.parameters.sm_dyn = False + if self.parameters.image and self.parameters.reference_image: + raise ValueError("image and reference_image cannot be used together.") return self @property @@ -247,6 +271,7 @@ def build(cls, qualityToggle: bool = True, ucPreset: Union[UCPreset, int] = UCPreset.TYPE0, image: Union[str, bytes] = None, + reference_mode: bool = False, add_original_image: bool = None, strength: float = None, mask: Union[str, bytes] = None, @@ -270,6 +295,7 @@ def build(cls, :param width: 宽 :param height: 高 :param image: 图片 + :param reference_mode: 是否是参考模式 :param add_original_image: 是否添加原始图片 :param strength: IMG2IMG 强度 :param mask: Inpainting mask @@ -290,13 +316,21 @@ def build(cls, "height": height, "qualityToggle": qualityToggle, "ucPreset": ucPreset, - "image": image, "add_original_image": add_original_image, - "strength": strength, "mask": mask, "controlnet_model": controlnet_model, "controlnet_condition": controlnet_condition }) + if reference_mode: + kwargs.update({ + "reference_image": image, + "reference_strength": strength, + }) + else: + kwargs.update({ + "image": image, + "strength": strength, + }) # 清理空值 param = {k: v for k, v in kwargs.items() if v is not None} return cls( From 32acbccd88de8f4c37d4638a65c7ff605f0ab1ef Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 19:15:38 +0800 Subject: [PATCH 5/9] fix(sdk): Add reference_image reference_strength etc. --- playground/viber-transfer.py | 56 +++++++++++++++++++ .../sdk/ai/generate_image/__init__.py | 25 +++++---- 2 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 playground/viber-transfer.py diff --git a/playground/viber-transfer.py b/playground/viber-transfer.py new file mode 100644 index 0000000..59995a3 --- /dev/null +++ b/playground/viber-transfer.py @@ -0,0 +1,56 @@ +import asyncio +import base64 +import os + +from dotenv import load_dotenv +from pydantic import SecretStr + +from novelai_python import APIError, Login +from novelai_python import GenerateImageInfer, ImageGenerateResp, JwtCredential +from novelai_python.sdk.ai.generate_image import Action + +load_dotenv() + +enhance = "year 2023,dynamic angle, best quality, amazing quality, very aesthetic, absurdres" +token = None +jwt = os.getenv("NOVELAI_JWT") or token + + +async def main(): + jwt_class = JwtCredential(jwt_token=SecretStr(jwt)) + _res = await Login.build(user_name=os.getenv("NOVELAI_USER"), password=os.getenv("NOVELAI_PASS") + ).request() + with open("raw_test_image.png", "rb") as f: + data = f.read() + # Base64 encode the data + encoded = base64.b64encode(data).decode() + try: + gen = GenerateImageInfer.build( + prompt=f"1girl, spring, jacket, sfw, angel, flower,{enhance}", + action=Action.GENERATE, + image=encoded, + add_original_image=True, + strength=0.6, + reference_mode=True, # IMPORTANT + width=1088, + height=896 + ) + cost = gen.calculate_cost(is_opus=True) + print(f"charge: {cost} if you are vip3") + print(f"charge: {gen.calculate_cost(is_opus=True)}") + _res = await gen.request( + session=jwt_class, remove_sign=True + ) + except APIError as e: + print(e.response) + return + + _res: ImageGenerateResp + print(_res.meta) + file = _res.files[0] + with open("generate_image_transfer.png", "wb") as f: + f.write(file[1]) + + +loop = asyncio.get_event_loop() +loop.run_until_complete(main()) diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index e4e57aa..b722bb7 100755 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -34,7 +34,7 @@ class GenerateImageInfer(ApiBaseModel): class Params(BaseModel): # Inpaint - add_original_image: Optional[bool] = True # FIXME: 未知作用 + add_original_image: Optional[bool] = False # FIXME: 未知作用 mask: Optional[Union[str, bytes]] = None # img2img,base64 cfg_rescale: Optional[float] = Field(0, ge=0, le=1, multiple_of=0.02) @@ -100,7 +100,7 @@ class Params(BaseModel): @model_validator(mode="after") def validate_img2img(self): - image = True if self.image else False + image = True if self.image or self.reference_image else False add_origin = True if self.add_original_image else False if image != add_origin: raise ValueError('Invalid Model Params For img2img2 mode... image should match add_original_image!') @@ -198,15 +198,18 @@ def model_post_init(self, *args) -> None: @model_validator(mode="after") def validate_model(self): - if self.action == Action.INFILL and not self.parameters.mask: - logger.warning("Mask maybe required for infill mode.") + if self.action == Action.INFILL: + if not self.parameters.mask: + logger.warning("Mask maybe required for infill mode.") if self.action != Action.GENERATE: self.parameters.extra_noise_seed = self.parameters.seed if self.action == Action.IMG2IMG: self.parameters.sm = False self.parameters.sm_dyn = False + if not self.parameters.image: + raise ValueError("image is must required for img2img mode.") if self.parameters.image and self.parameters.reference_image: - raise ValueError("image and reference_image cannot be used together.") + logger.warning("image and reference_image should not be used together.") return self @property @@ -395,17 +398,15 @@ async def request(self, try: _log_data = deepcopy(request_data) if self.action == Action.GENERATE: + _log_data.get("parameters", {}).update({ + "reference_image": "base64 data" if self.parameters.reference_image else None, + }) logger.debug(f"Request Data: {_log_data}") else: _log_data.get("parameters", {}).update({ "image": "base64 data" if self.parameters.image else None, - } - ) - _log_data.get("parameters", {}).update( - { - "mask": "base64 data" if self.parameters.mask else None, - } - ) + "mask": "base64 data" if self.parameters.mask else None, + }) logger.debug(f"Request Data: {_log_data}") del _log_data except Exception as e: From 7b2b442805bc07b50bd8dd3d609f017a2575e2e8 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 19:15:52 +0800 Subject: [PATCH 6/9] fix(sdk): Add reference_image reference_strength etc. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 96e5d75..cfea136 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "novelai-python" -version = "0.3.4" +version = "0.3.5" description = "Novelai Python Binding With Pydantic" authors = [ { name = "sudoskys", email = "coldlando@hotmail.com" }, From 6bc8758461028949d19ece90755385a5faebde2c Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 19:19:22 +0800 Subject: [PATCH 7/9] fix(sdk): Add reference_image reference_strength etc. --- src/novelai_python/sdk/ai/generate_image/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index b722bb7..0e3f9aa 100755 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -100,6 +100,8 @@ class Params(BaseModel): @model_validator(mode="after") def validate_img2img(self): + # Deprecated + return self image = True if self.image or self.reference_image else False add_origin = True if self.add_original_image else False if image != add_origin: From b288ddc9a542461aa9edb637e877ef49a247219e Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 19:20:43 +0800 Subject: [PATCH 8/9] fix(sdk): Add reference_image reference_strength etc. --- src/novelai_python/sdk/ai/generate_image/__init__.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index 0e3f9aa..9f1316e 100755 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -98,16 +98,6 @@ class Params(BaseModel): """Undesired Content Strength""" width: Optional[int] = Field(832, ge=64, le=49152) - @model_validator(mode="after") - def validate_img2img(self): - # Deprecated - return self - image = True if self.image or self.reference_image else False - add_origin = True if self.add_original_image else False - if image != add_origin: - raise ValueError('Invalid Model Params For img2img2 mode... image should match add_original_image!') - return self - @field_validator('mask') def mask_validator(cls, v: Union[str, bytes]): if isinstance(v, str) and v.startswith("data:image/"): @@ -210,6 +200,8 @@ def validate_model(self): self.parameters.sm_dyn = False if not self.parameters.image: raise ValueError("image is must required for img2img mode.") + if not self.parameters.add_original_image: + raise ValueError("add_original_image is must required for img2img mode.") if self.parameters.image and self.parameters.reference_image: logger.warning("image and reference_image should not be used together.") return self From 17e068b992204745df73b5cd86ffe04c51920955 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sat, 24 Feb 2024 19:28:37 +0800 Subject: [PATCH 9/9] fix(sdk): Add reference_image reference_strength etc. --- src/novelai_python/sdk/ai/generate_image/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index 9f1316e..a8269fd 100755 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -286,7 +286,7 @@ def build(cls, :param negative_prompt: The content of negative prompt :param seed: The seed for generate image :param steps: The steps for generate image - :param scale: Prompt Guidance + :param scale: The scale for generate image :param cfg_rescale: Prompt Guidance Rescale 0-1 lower is more creative :param sampler: The sampler for generate image :param width: 宽