Novelai Image Generation API輕巧的異步Python包裝器。
Metadata類,以輕鬆地設置具有類型驗證的生成參數。asyncio運行生成任務並有效返回輸出。 重要的
不幸的是,Novelai從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參數設置為基本圖像。基本圖像需要轉換為基本64編碼的格式。這可以使用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.INPAINTING ,並將image參數設置為基本圖像,然後mask參數為黑白掩碼圖像,在該圖像中,白色是塗漆和黑色的區域,以保持原樣。基本圖像和掩碼都需要轉換為基本64編碼的格式。這可以使用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轉移沒有自己的動作類型。取而代之的是,通過在元數據中添加reference_image_multiple參數來實現。參考圖像需要轉換為基本64編碼的格式。這可以使用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天。令牌可以用作向Novelai請求的身份驗證標頭。
# Replace argument values with your actual account credentials
python3 -m novelai login < username > < password > Novelai後端
田園/小說
Novelai非官方知識基礎