Skip to content

Commit

Permalink
Merge pull request #85 from LlmKira/dev
Browse files Browse the repository at this point in the history
✨ refactor(generate_image): update field imports and type aliases
  • Loading branch information
sudoskys authored Dec 21, 2024
2 parents b1a1ab0 + b522fd5 commit f9ab8dc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
65 changes: 35 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,7 +81,7 @@ loop.run_until_complete(main())

```

#### LLM
#### 📦 LLM

```python
import asyncio
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -183,15 +163,40 @@ 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.

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)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]" },
Expand Down
22 changes: 13 additions & 9 deletions src/novelai_python/sdk/ai/generate_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -79,21 +78,21 @@ 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),
gt=0,
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,
Expand All @@ -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")
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f9ab8dc

Please sign in to comment.