Skip to content

Commit

Permalink
Merge pull request #81 from LlmKira/dev
Browse files Browse the repository at this point in the history
feat: Novelai 4.0 & Params V3
  • Loading branch information
sudoskys authored Dec 21, 2024
2 parents 8489954 + a09cfab commit 741e0a2
Show file tree
Hide file tree
Showing 74 changed files with 1,896 additions and 947 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,39 @@ from dotenv import load_dotenv
from pydantic import SecretStr

from novelai_python import GenerateImageInfer, ImageGenerateResp, ApiCredential
from novelai_python.sdk.ai.generate_image import Model, Character, Sampler, UCPreset
from novelai_python.sdk.ai.generate_image.schema import PositionMap

load_dotenv()
enhance = "year 2023,dynamic angle, best quality, amazing quality, very aesthetic, absurdres"
session = ApiCredential(api_token=SecretStr(os.getenv("NOVELAI_JWT"))) # pst-***

prompt = "1girl, year 2023,dynamic angle, best quality, amazing quality, very aesthetic, absurdres"


async def main():
gen = await GenerateImageInfer.build(prompt=f"1girl,{enhance}")
gen = GenerateImageInfer.build_generate(
prompt=prompt,
model=Model.NAI_DIFFUSION_4_CURATED_PREVIEW,
character_prompts=[
Character(
prompt="1girl",
uc="red hair",
center=PositionMap.AUTO
),
Character(
prompt="1boy",
center=PositionMap.E5
)
],
sampler=Sampler.K_EULER_ANCESTRAL,
ucPreset=UCPreset.TYPE0,
# Recommended, using preset negative_prompt depends on selected model
qualitySuffix=True,
qualityToggle=True,
decrisp_mode=False,
variety_boost=True,
# Checkbox in novelai.net
)
cost = gen.calculate_cost(is_opus=True)
print(f"charge: {cost} if you are vip3")
resp = gen.request(session=session)
Expand Down
766 changes: 366 additions & 400 deletions pdm.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion playground/augment-image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/1/26 下午12:23
# @Author : sudoskys
# @File : __init__.py.py
# @Software: PyCharm

import asyncio
import os
import pathlib
Expand Down
2 changes: 1 addition & 1 deletion playground/enhance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/13 下午1:58
# @Author : sudoskys
# @File : enhance.py
# @Software: PyCharm

from loguru import logger

# NOTE About Enhance Mode
Expand Down
45 changes: 35 additions & 10 deletions playground/generate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/1/26 下午12:23
# @Author : sudoskys
# @File : __init__.py.py
# @Software: PyCharm

import asyncio
import os
import pathlib
Expand All @@ -12,12 +12,14 @@

from novelai_python import APIError, LoginCredential
from novelai_python import GenerateImageInfer, ImageGenerateResp, JwtCredential
from novelai_python.sdk.ai._enum import UCPreset
from novelai_python.sdk.ai.generate_image import Action, Model, Sampler
from novelai_python.sdk.ai.generate_image import Action, Model, Sampler, Character, UCPreset
from novelai_python.sdk.ai.generate_image.schema import PositionMap
from novelai_python.utils.useful import enum_to_list


async def generate(prompt="1girl, year 2023, dynamic angle, best quality, amazing quality, very aesthetic, absurdres"):
async def generate(
prompt="1girl, year 2023, dynamic angle, best quality, amazing quality, very aesthetic, absurdres"
):
jwt = os.getenv("NOVELAI_JWT", None)
if jwt is None:
raise ValueError("NOVELAI_JWT is not set in `.env` file, please create one and set it")
Expand All @@ -29,16 +31,39 @@ async def generate(prompt="1girl, year 2023, dynamic angle, best quality, amazin
)
# await _login_credential.request()
print(f"Action List:{enum_to_list(Action)}")
print(
"""
.1 .3 .5 .7 .9
A1 B1 C1 D1 E1
A2 B2 C2 D2 E2
A3 B3 C3 D3 E3
..............
A5 B5 C5 D5 E5
"""
)
try:
agent = GenerateImageInfer.build(
agent = GenerateImageInfer.build_generate(
prompt=prompt,
model=Model.NAI_DIFFUSION_3,
action=Action.GENERATE,
sampler=Sampler.K_DPMPP_2M,
ucPreset=UCPreset.TYPE0, # Recommended, using preset negative_prompt depends on selected model
model=Model.NAI_DIFFUSION_4_CURATED_PREVIEW,
character_prompts=[
Character(
prompt="1girl",
uc="red hair",
center=PositionMap.AUTO
),
Character(
prompt="1boy",
center=PositionMap.E5
)
],
sampler=Sampler.K_EULER_ANCESTRAL,
ucPreset=UCPreset.TYPE0,
# Recommended, using preset negative_prompt depends on selected model
qualitySuffix=True,
qualityToggle=True,
decrisp_mode=False,
variety_boost=True, # Checkbox in website
variety_boost=True,
# Checkbox in novelai.net
)
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")
Expand Down
11 changes: 4 additions & 7 deletions playground/generate_image_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/9 下午10:04
# @Author : sudoskys
# @File : generate_image_img2img.py
# @Software: PyCharm

import asyncio
import base64
import os
Expand Down Expand Up @@ -41,15 +41,12 @@ async def generate(
# Base64 encode the image
image = base64.b64encode(f.read()).decode()
# image = f.read() # Or you can use the raw bytes
agent = GenerateImageInfer.build(
agent = GenerateImageInfer.build_img2img(
prompt=prompt,
action=Action.IMG2IMG,
sampler=Sampler.K_DPMPP_SDE,
image=image,
add_original_image=False,
strength=0.9,
noise=0.1,
qualityToggle=True,
seed=123456789,
extra_noise_seed=123123123,
)
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")
Expand Down
2 changes: 1 addition & 1 deletion playground/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/8 下午4:34
# @Author : sudoskys
# @File : information.py
# @Software: PyCharm

import asyncio
import os

Expand Down
2 changes: 1 addition & 1 deletion playground/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/7 下午12:07
# @Author : sudoskys
# @File : login.py
# @Software: PyCharm

import asyncio
import os

Expand Down
2 changes: 1 addition & 1 deletion playground/paint_mask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/13 下午12:42
# @Author : sudoskys
# @File : __init__.py.py
# @Software: PyCharm

from novelai_python.tool.paint_mask import create_mask_from_sketch

with open('sk.jpg', 'rb') as f:
Expand Down
2 changes: 1 addition & 1 deletion playground/random_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/7 下午12:51
# @Author : sudoskys
# @File : random_prompt.py
# @Software: PyCharm


from novelai_python.tool.random_prompt import RandomPromptGenerator

Expand Down
12 changes: 8 additions & 4 deletions playground/simple.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import asyncio

from novelai_python.sdk.ai.generate_image import Model


async def main():
from pydantic import SecretStr
from novelai_python import GenerateImageInfer, ApiCredential, LoginCredential
from novelai_python import GenerateImageInfer, LoginCredential
credential = LoginCredential(
username="NOVELAI_USERNAME",
password=SecretStr("NOVELAI_PASSWORD")
)
gen = GenerateImageInfer.build(
prompt="1girl, year 2023, dynamic angle, best quality, amazing quality, very aesthetic, absurdres")
gen = GenerateImageInfer.build_generate(
prompt="1girl, year 2023, dynamic angle, best quality, amazing quality, very aesthetic, absurdres",
model=Model.NAI_DIFFUSION_3,
)
print(f"消耗点数: vip3:{gen.calculate_cost(is_opus=True)}, {gen.calculate_cost(is_opus=False)}")
resp = await gen.request(session=credential)
with open("image.png", "wb") as f:
f.write(resp.files[0][1])


loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
2 changes: 1 addition & 1 deletion playground/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/7 上午10:16
# @Author : sudoskys
# @File : subscription.py
# @Software: PyCharm

import asyncio
import os

Expand Down
2 changes: 1 addition & 1 deletion playground/suggest_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Time : 2024/2/13 下午8:25
# @Author : sudoskys
# @File : suggest_tag.py
# @Software: PyCharm

import asyncio
import os

Expand Down
4 changes: 2 additions & 2 deletions playground/upscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# @Time : 2024/2/13 上午11:58
# @Author : sudoskys
# @File : upscale.py
# @Software: PyCharm

# -*- coding: utf-8 -*-
# @Time : 2024/2/14 下午5:20
# @Author : sudoskys
# @File : upscale_demo.py
# @Software: PyCharm

# To run the demo, you need an event loop, for instance by using asyncio
import asyncio
import os
Expand Down
16 changes: 8 additions & 8 deletions playground/vibe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from novelai_python import APIError, Login
from novelai_python import GenerateImageInfer, ImageGenerateResp, JwtCredential
from novelai_python.sdk.ai.generate_image import Action, Sampler
from novelai_python.sdk.ai.generate_image import Action, Sampler, Model
from novelai_python.utils.useful import enum_to_list


Expand All @@ -32,15 +32,15 @@ async def generate(
raise ValueError(f"Image not found: {image_path}")
with open(image_path, "rb") as f:
image = f.read()
agent = GenerateImageInfer.build(
agent = GenerateImageInfer.build_generate(
prompt=prompt,
action=Action.GENERATE,
sampler=Sampler.K_DPMPP_SDE,
reference_image=image,
reference_strength=0.9,
reference_information_extracted=1,
add_original_image=True, # This Not affect the vibe generation
qualityToggle=True,
model=Model.NAI_DIFFUSION_3,
reference_image_multiple=[image],
reference_strength_multiple=[0.9],
reference_information_extracted_multiple=[1],
add_original_image=True,
# This Not affect the vibe generation
)
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")
Expand Down
12 changes: 4 additions & 8 deletions playground/vibe_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,14 @@ async def generate(
image = f.read()
with open(reference_image_path, "rb") as f:
reference_image = f.read()
agent = GenerateImageInfer.build(
agent = GenerateImageInfer.build_img2img(
prompt=prompt,
action=Action.IMG2IMG,
sampler=Sampler.K_DPMPP_SDE,

image=image,
strength=0.6,

reference_image=reference_image,
reference_strength=0.6,
reference_information_extracted=1,

reference_image_multiple=[reference_image],
reference_strength_multiple=[0.9],
reference_information_extracted_multiple=[1],
add_original_image=True, # This Not affect the vibe generation
qualityToggle=True,
)
Expand Down
20 changes: 10 additions & 10 deletions playground/vibe_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import asyncio
import os
import pathlib

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 import APIError, Login, ApiCredential
from novelai_python import GenerateImageInfer, ImageGenerateResp
from novelai_python.sdk.ai.generate_image import Action, Sampler, Model
from novelai_python.utils.useful import enum_to_list
from novelai_python.tool.paint_mask import create_mask_from_sketch
from novelai_python.utils.useful import enum_to_list

with open('static_image.png', 'rb') as f:
ori_bytes = f.read()
Expand All @@ -34,7 +35,7 @@ async def generate(
jwt = os.getenv("NOVELAI_JWT", None)
if jwt is None:
raise ValueError("NOVELAI_JWT is not set in `.env` file, please create one and set it")
credential = JwtCredential(jwt_token=SecretStr(jwt))
credential = ApiCredential(api_token=SecretStr(jwt))
"""Or you can use the login credential to get the jwt token"""
_login_credential = Login.build(
user_name=os.getenv("NOVELAI_USER"),
Expand All @@ -55,18 +56,17 @@ async def generate(
reference_image = f.read()
with open(mask_path, "rb") as f:
mask = f.read()
agent = GenerateImageInfer.build(
agent = GenerateImageInfer.build_infill(
prompt=prompt,
action=Action.INFILL,
model=Model.NAI_DIFFUSION_3_INPAINTING,
sampler=Sampler.K_DPMPP_SDE,
image=image,
mask=mask,
strength=0.6,

reference_image=reference_image,
reference_strength=0.6,
reference_information_extracted=1,
reference_image_multiple=[reference_image],
reference_strength_multiple=[0.9],
reference_information_extracted_multiple=[1],

add_original_image=True, # This Not affect the vibe generation
qualityToggle=True,
Expand All @@ -88,5 +88,5 @@ async def generate(


load_dotenv()
loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
loop.run_until_complete(generate())
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "novelai-python"
version = "0.5.3"
version = "0.6.0"
description = "NovelAI Python Binding With Pydantic"
authors = [
{ name = "sudoskys", email = "[email protected]" },
Expand All @@ -9,7 +9,7 @@ dependencies = [
"elara>=0.5.5",
"loguru>=0.7.2",
"pydantic-settings>=2.1.0",
"pydantic>=2.5.3",
"pydantic>=2.9.0",
"httpx>=0.26.0",
"shortuuid>=1.0.11",
"Pillow>=10.2.0",
Expand Down
1 change: 0 additions & 1 deletion record/ai/generate_image/image2image.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0",
"Accept": "*/*",

"Content-Type": "application/json",
"Authorization": "Secret",
"Sec-Fetch-Dest": "empty",
Expand Down
Loading

0 comments on commit 741e0a2

Please sign in to comment.