소설 이미지 생성 API를위한 가벼운 비동기 파이썬 래퍼.
Metadata 클래스를 제공합니다.asyncio 사용하여 작업 생성 작업을 실행하고 출력을 효율적으로 반환합니다. 중요한
불행히도, Neverai는 2024 년 3 월 21 일부터 API 종말점의 이미지 생성 기능을 감가 상각했습니다. 결과적으로 동시 생성 기능은 더 이상 사용할 수 없습니다.
원천
메모
이 패키지에는 Python 3.12 이상이 필요합니다.
PIP로 설치/업데이트 :
pip install -U novelai필요한 패키지를 가져오고 Novelai 계정 자격 증명으로 클라이언트를 초기화하십시오.
import asyncio
from novelai import NAIClient
# Replace argument values with your actual account credentials
username = "Your NovelAI username"
password = "Your NovelAI password"
async def main ():
client = NAIClient ( username , password , proxy = None )
await client . init ( timeout = 30 )
asyncio . run ( main ()) 성공적으로 초기화 한 후 generate_image 메소드로 이미지를 생성 할 수 있습니다. 이 방법은 Metadata 객체를 첫 번째 인수로, 그리고 사용할 백엔드를 지정하기위한 선택적인 host 인수를 취합니다.
verbose=True 를 통과 시킴으로써이 방법은 생성 요청이 이루어질 때마다 추정 된 Anlas 비용을 인쇄합니다.
Metadata 의 전체 매개 변수 목록은 클래스 정의에서 찾을 수 있습니다.
from novelai import Metadata , Resolution
async def main ():
metadata = Metadata (
prompt = "1girl" ,
negative_prompt = "bad anatomy" ,
res_preset = Resolution . NORMAL_PORTRAIT ,
n_samples = 1 ,
)
print ( f"Estimated Anlas cost: { metadata . calculate_cost ( is_opus = False ) } " )
output = await client . generate_image (
metadata , verbose = False , is_opus = False
)
for image in output :
image . save ( path = "output images" , verbose = True )
asyncio . run ( main ()) img2img 작업을 수행하려면 메타 데이터에서 action 매개 변수를 Action.IMG2IMG 로 설정하고 image 매개 변수를 기본 이미지로 설정하십시오. 기본 이미지는 Base64- 인코딩 형식으로 변환해야합니다. 이것은 base64 모듈을 사용하여 달성 할 수 있습니다.
import base64
from novelai import Metadata , Action
async def main ():
with open ( "tests/images/portrait.jpg" , "rb" ) as f :
base_image = base64 . b64encode ( f . read ()). decode ( "utf-8" )
metadata = Metadata (
prompt = "1girl" ,
negative_prompt = "bad anatomy" ,
action = Action . IMG2IMG ,
width = 832 ,
height = 1216 ,
n_samples = 1 ,
image = base_image ,
strength = 0.5 ,
noise = 0.1 ,
)
output = await client . generate_image ( metadata , verbose = True )
for image in output :
image . save ( path = "output images" , verbose = True )
asyncio . run ( main ()) inpaint action을 수행하려면 메타 데이터에서 action 매개 변수를 Action.INPAINTING 으로 설정하고 image 매개 변수를 기본 이미지로 설정하고 매개 변수를 검은 색 및 흰색 마스크 이미지로 mask . 기본 이미지와 마스크는 모두 Base64에 인코딩 된 형식으로 변환해야합니다. 이것은 base64 모듈을 사용하여 달성 할 수 있습니다.
import base64
from novelai import Metadata , Model , Action , Resolution
async def main ():
with open ( "tests/images/portrait.jpg" , "rb" ) as f :
base_image = base64 . b64encode ( f . read ()). decode ( "utf-8" )
with open ( "tests/images/inpaint_left.jpg" , "rb" ) as f :
mask = base64 . b64encode ( f . read ()). decode ( "utf-8" )
metadata = Metadata (
prompt = "1girl" ,
negative_prompt = "bad anatomy" ,
model = Model . V3INP ,
action = Action . INPAINT ,
res_preset = Resolution . NORMAL_PORTRAIT ,
image = base_image ,
mask = mask ,
)
output = await client . generate_image ( metadata , verbose = True )
for image in output :
image . save ( path = "output images" , verbose = True )
asyncio . run ( main ()) Vibe Transfer에는 자체 동작 유형이 없습니다. 대신, 메타 데이터에서 reference_image_multiple 매개 변수를 추가하여 달성됩니다. 참조 이미지는 Base64- 인코딩 형식으로 변환해야합니다. 이것은 base64 모듈을 사용하여 달성 할 수 있습니다.
import base64
from novelai import Metadata , Resolution
async def main ():
with open ( "tests/images/portrait.jpg" , "rb" ) as f :
base_image = base64 . b64encode ( f . read ()). decode ( "utf-8" )
metadata = Metadata (
prompt = "1girl" ,
negative_prompt = "bad anatomy" ,
res_preset = Resolution . NORMAL_PORTRAIT ,
reference_image_multiple = [ base_image ],
reference_information_extracted_multiple = [ 1 ],
reference_strength_multiple = [ 0.6 ],
)
output = await client . generate_image ( metadata , verbose = True )
for image in output :
image . save ( path = "output images" , verbose = True )
asyncio . run ( main ())선택적으로 CLI에서 액세스 토큰을 직접 생성하기 위해 모듈 기능이 제공됩니다.
액세스 토큰이 생성되면 30 일 동안 유효합니다. 토큰은 소설에 요청을하기 위해 인증 헤더로 사용할 수 있습니다.
# Replace argument values with your actual account credentials
python3 -m novelai login < username > < password > Nevelai 백엔드
aedial/novelai-api
Nevelai 비공식 지식베이스