From b522fd58fb73ecbce76b5958871f522a211d1f37 Mon Sep 17 00:00:00 2001 From: sudoskys Date: Sun, 22 Dec 2024 00:20:55 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20refactor(generate=5Fimage):=20updat?= =?UTF-8?q?e=20field=20imports=20and=20type=20aliases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔄 chore(README): reorganize sections and update roadmap 🔖 release: bump version to 0.6.1 in pyproject.toml --- README.md | 65 ++++++++++--------- pyproject.toml | 2 +- .../sdk/ai/generate_image/__init__.py | 22 ++++--- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 6e1d78c..4b77f39 100755 --- a/README.md +++ b/README.md @@ -11,32 +11,12 @@ The goal of this repository is to use Pydantic to build legitimate requests to a > Python >= 3.9 is required. -### Roadmap 🚧 +### 📰 News -- [x] tool.random_prompt -- [x] tool.paint_mask -- [x] tool.image_metadata -- [x] tokenizer -- [x] /ai/generate-image -- [x] /user/subscription -- [x] /user/login -- [x] /user/information -- [x] /ai/upscale -- [x] /ai/generate-image/suggest-tags -- [x] /ai/generate-voice -- [x] /ai/generate-stream -- [x] /ai/generate -- [x] /ai/augment-image -- [ ] /ai/annotate-image -- [ ] /ai/classify -- [ ] /ai/generate-prompt +- [Image Generation Model Release — NovelAI Anime Diffusion V4 Curated Preview (EN)](https://blog.novelai.net/release-novelai-anime-diffusion-v4-curated-preview-en-ca4b0b11e671) +- [Tutorial: Creating Consistent Characters with NovelAI Diffusion Anime [Female]](https://blog.novelai.net/tutorial-en-creating-consistent-characters-with-novelai-diffusion-anime-female-538b4b678a4e) -> GenerateImageInfer.calculate_cost is correct in most cases, but please request account information to get accurate -> consumption information. - -> This repo is maintained by me personally now. If you have any questions, please feel free to open an issue. - -### Usage 🖥️ +### 📦 Usage ```shell pip -U install novelai-python @@ -101,7 +81,7 @@ loop.run_until_complete(main()) ``` -#### LLM +#### 📦 LLM ```python import asyncio @@ -152,7 +132,7 @@ loop = asyncio.get_event_loop() loop.run_until_complete(chat("Hello")) ``` -#### Random Prompt +#### 📦 Random Prompt ```python from novelai_python.tool.random_prompt import RandomPromptGenerator @@ -161,14 +141,14 @@ prompt = RandomPromptGenerator(nsfw_enabled=False).random_prompt() print(prompt) ``` -#### Run A Server +#### 📦 Run A Server ```shell pip install novelai_python python3 -m novelai_python.server -h '127.0.0.1' -p 7888 ``` -#### Tokenizer +#### 📦 Tokenizer ```python from novelai_python._enum import get_tokenizer_model, TextLLMModel @@ -183,7 +163,32 @@ print(tokenizer_package.decode(tokenizer_package.encode(t_text))) ``` -## About Nsfw 🚫 +### 🔨 Roadmap + +- [x] tool.random_prompt +- [x] tool.paint_mask +- [x] tool.image_metadata +- [x] tokenizer +- [x] /ai/generate-image +- [x] /user/subscription +- [x] /user/login +- [x] /user/information +- [x] /ai/upscale +- [x] /ai/generate-image/suggest-tags +- [x] /ai/generate-voice +- [x] /ai/generate-stream +- [x] /ai/generate +- [x] /ai/augment-image +- [ ] /ai/annotate-image +- [ ] /ai/classify +- [ ] /ai/generate-prompt + +> GenerateImageInfer.calculate_cost is correct in most cases, but please request account information to get accurate +> consumption information. + +> This repo is maintained by me personally now. If you have any questions, please feel free to open an issue. + +## 🚫 About Nsfw You might need some solutions for identifying NSFW content and adding a mosaic to prevent operational mishaps. @@ -191,7 +196,7 @@ https://dghs-imgutils.deepghs.org/main/api_doc/detect/nudenet.html https://dghs-imgutils.deepghs.org/main/api_doc/operate/censor.html -## Acknowledgements 🙏 +## 🙏 Acknowledgements [BackEnd](https://api.novelai.net/docs) diff --git a/pyproject.toml b/pyproject.toml index bffb529..a00b0a3 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "novelai-python" -version = "0.6.0" +version = "0.6.1" description = "NovelAI Python Binding With Pydantic" authors = [ { name = "sudoskys", email = "coldlando@hotmail.com" }, diff --git a/src/novelai_python/sdk/ai/generate_image/__init__.py b/src/novelai_python/sdk/ai/generate_image/__init__.py index b8a5650..e3a2dfe 100755 --- a/src/novelai_python/sdk/ai/generate_image/__init__.py +++ b/src/novelai_python/sdk/ai/generate_image/__init__.py @@ -7,7 +7,6 @@ import math import random from copy import deepcopy -from dataclasses import field from io import BytesIO from typing import Optional, Union, Tuple, List from urllib.parse import urlparse @@ -28,7 +27,7 @@ from novelai_python.sdk.ai._enum import Model, Sampler, NoiseSchedule, ControlNetModel, Action, UCPreset, \ INPAINTING_MODEL_LIST, get_default_uc_preset, \ ModelTypeAlias, UCPresetTypeAlias, get_default_noise_schedule, get_supported_noise_schedule, \ - get_model_group, ModelGroups, get_supported_params, get_modifiers + get_model_group, ModelGroups, get_supported_params, get_modifiers, ImageBytesTypeAlias from .schema import Character, V4Prompt, V4NegativePrompt from ...schema import ApiBaseModel from ...._exceptions import APIError, AuthError, ConcurrentGenerationError, SessionHttpError @@ -79,7 +78,7 @@ class Params(BaseModel): """Noise Schedule""" legacy_v3_extend: Optional[bool] = False """Legacy V3 Extend""" - mask: Union[bytes, str] = None + mask: ImageBytesTypeAlias = None """Mask for Inpainting""" seed: int = Field( default_factory=lambda: random.randint(0, 4294967295 - 7), @@ -87,13 +86,13 @@ class Params(BaseModel): le=4294967295 - 7, ) """Seed""" - image: Union[bytes, str] = None + image: ImageBytesTypeAlias = None """Image for img2img""" negative_prompt: Optional[str] = '' """Negative Prompt""" - reference_image_multiple: Optional[List[Union[str, bytes]]] = field(default_factory=list) + reference_image_multiple: Optional[List[Union[str, bytes]]] = Field(default_factory=list) """Reference Image For Vibe Mode""" - reference_information_extracted_multiple: Optional[List[float]] = field(default_factory=list) + reference_information_extracted_multiple: Optional[List[float]] = Field(default_factory=list) """Reference Information Extracted For Vibe Mode""" reference_strength_multiple: Optional[List[float]] = Field( default=list, @@ -113,7 +112,7 @@ class Params(BaseModel): """ use_coords: bool = Field(True, description="Use Coordinates") """Use Coordinates""" - characterPrompts: List[Character] = field(default_factory=list) + characterPrompts: List[Character] = Field(default_factory=list) """Character Prompts""" v4_prompt: Optional[V4Prompt] = Field(None, description="V4 Prompt") @@ -136,6 +135,12 @@ class Params(BaseModel): uncond_scale: Optional[float] = Field(None, ge=0, le=1.5, multiple_of=0.05) """Undesired Content Strength""" + @model_validator(mode="after") + def v_character(self): + if len(self.characterPrompts) > 6: + raise ValueError("Too many character given") + return self + @field_validator('uncond_scale') def v_uncond_scale(cls, v: float): """ @@ -684,10 +689,9 @@ def build_img2img( If you need to define more parameters, you should initialize the Param class yourself. - :param controlnet_strength: :param strength: :param image: - :param noise: For img2img + :param noise: For image to image :param extra_noise_seed: Get extra_noise_seed :param reference_information_extracted_multiple: :param reference_strength_multiple: