Skip to content

Commit

Permalink
Merge pull request #95 from LlmKira/dev
Browse files Browse the repository at this point in the history
✨ feat(generate_image): add mutual exclusion and quality modifier
  • Loading branch information
sudoskys authored Jan 3, 2025
2 parents 03e15d0 + 3a083a1 commit 52ab424
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion playground/generate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def generate(
decrisp_mode=False,
variety_boost=True,
# Checkbox in novelai.net
)
).set_mutual_exclusion(True)
print(f"charge: {agent.calculate_cost(is_opus=True)} if you are vip3")
print(f"charge: {agent.calculate_cost(is_opus=False)} if you are not vip3")
result = await agent.request(
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.7.1"
version = "0.7.2"
description = "NovelAI Python Binding With Pydantic"
authors = [
{ name = "sudoskys", email = "[email protected]" },
Expand Down
51 changes: 48 additions & 3 deletions src/novelai_python/sdk/ai/generate_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,35 @@ def add_image_to_black_background(

class GenerateImageInfer(ApiBaseModel):
_endpoint: str = PrivateAttr("https://image.novelai.net")
_mutual_exclusion: bool = PrivateAttr(False)
"""Positive words and negative words are mutually exclusive, and conflicting negative words are deleted first."""
_quality_modifier: bool = PrivateAttr(True)
"""Add Quality Modifier To Input"""

def set_mutual_exclusion(self, value: bool):
"""
**Enable This will modify the negative prompt.**
Default is False.
Positive words and negative words are mutually exclusive, and conflicting negative words are deleted first.
:param value: bool
:return: self
"""
self._mutual_exclusion = bool(value)
return self

def set_quality_modifier(self, value: bool):
"""
**Enable This will modify the input prompt.**
Default is True.
Add Quality Modifier To Input.
Whether to add the quality vocabulary used by the web application.
:param value:
:return:
"""
self._quality_modifier = bool(value)
return self

@property
def endpoint(self):
Expand Down Expand Up @@ -429,7 +458,19 @@ def enhance_message(_prompt):
)
return _prompt

self.input = enhance_message(self.input)
if self._quality_modifier:
logger.trace("Enhancing input with quality modifier, will modify input prompt.")
self.input = enhance_message(self.input)

if self._mutual_exclusion:
logger.trace("Mutual exclusion is enabled, will modify negative prompt.")
input_prompt = {x.strip(): x for x in self.input.split(",")}
uc_prompt = {x.strip(): x for x in self.parameters.negative_prompt.split(",")}
# Remove conflicting negative words
for key in input_prompt:
if key in uc_prompt:
uc_prompt.pop(key)
self.parameters.negative_prompt = ",".join(uc_prompt.values())

@model_validator(mode="after")
def _build_nai4_prompt(self):
Expand Down Expand Up @@ -467,7 +508,9 @@ def _build_nai4_prompt(self):
@model_validator(mode="after")
def _backend_logic(self):
"""
Backend logic for GenerateImageInfer.
Calibration results.
**NOTE: It is forbidden to write logic that does not belong to novelai.net here**
:return: self
"""
if self.action == Action.GENERATE:
Expand Down Expand Up @@ -949,7 +992,9 @@ async def request(self,
override_headers: Optional[dict] = None,
) -> ImageGenerateResp:
"""
Generate images using NovelAI's diffusion models. According to our Terms of Service, all generation requests must be initiated by a human action. Automating text or image generation to create excessive load on our systems is not allowed.
**Generate images using NovelAI's diffusion models.**
According to our Terms of Service, all generation requests must be initiated by a human action. Automating text or image generation to create excessive load on our systems is not allowed.
:param override_headers: the headers to override
:param session: session
Expand Down

0 comments on commit 52ab424

Please sign in to comment.