NovelAI Python Binding With Pydantic
Project description
✨ NovelAI api python sdk with Pydantic, modern and user-friendly.
The goal of this repository is to use Pydantic to build legitimate requests to access the NovelAI API service.
Python >= 3.9 is required.
Roadmap 🚧
- tool.random_prompt
- tool.paint_mask
- tool.image_metadata
- /ai/generate-image
- /user/subscription
- /user/login
- /user/information
- /ai/upscale
- /ai/generate-image/suggest-tags
- /ai/generate-voice
- /ai/generate-stream
- /ai/generate
- /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.
Usage 🖥️
pip install novelai-python
More examples can be found in the playground directory, read code as documentation.
import asyncio
import os
from dotenv import load_dotenv
from pydantic import SecretStr
from novelai_python import GenerateImageInfer, ImageGenerateResp, ApiCredential
load_dotenv()
enhance = "year 2023,dynamic angle, best quality, amazing quality, very aesthetic, absurdres"
session = ApiCredential(api_token=SecretStr(os.getenv("NOVELAI_JWT"))) # pst-***
async def main():
gen = await GenerateImageInfer.build(prompt=f"1girl,{enhance}")
cost = gen.calculate_cost(is_opus=True)
print(f"charge: {cost} if you are vip3")
resp = gen.request(session=session)
resp: ImageGenerateResp
print(resp.meta)
file = resp.files[0]
with open(file[0], "wb") as f:
f.write(file[1])
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
LLM
import asyncio
import os
from dotenv import load_dotenv
from pydantic import SecretStr
from novelai_python import APIError, LoginCredential
from novelai_python.sdk.ai.generate import TextLLMModel, LLM
load_dotenv()
username = os.getenv("NOVELAI_USER", None)
assert username is not None
# credential = JwtCredential(jwt_token=SecretStr(jwt))
login_credential = LoginCredential(
username=os.getenv("NOVELAI_USER"),
password=SecretStr(os.getenv("NOVELAI_PASS"))
)
async def chat(prompt: str):
try:
agent = LLM.build(prompt=prompt, model=TextLLMModel.Kayra)
result = await agent.request(session=login_credential)
except APIError as e:
raise Exception(f"Error: {e.message}")
print(f"Result: \n{result.text}")
loop = asyncio.get_event_loop()
loop.run_until_complete(chat("Hello"))
Random Prompt
from novelai_python.tool.random_prompt import RandomPromptGenerator
prompt = RandomPromptGenerator(nsfw_enabled=False).random_prompt()
print(prompt)
Run A Server
pip install novelai_python
python3 -m novelai_python.server -h '127.0.0.1' -p 7888
Acknowledgements 🙏
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
novelai_python-0.4.10.tar.gz
(5.0 MB
view hashes)
Built Distribution
Close
Hashes for novelai_python-0.4.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b23bc2b06e70e59f0ee4e5d59b289744ad8cb93fd7a506911e50ce04b3bbc74 |
|
MD5 | 8aa7a92d056b3d4ba6970f4ed14da965 |
|
BLAKE2b-256 | c590aa715fa22568e004dd192d0d5c4d7ddac81d2b92fac49659ed65b8716801 |