Giter VIP home page Giter VIP logo

novelai-python's Introduction

banner


PyPI version Downloads

✨ 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 🙏

BackEnd

novelai-api

NovelAI-API

novelai-python's People

Contributors

sudoskys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

novelai-python's Issues

OSError nerdstash_v1.model": No such file or directory Error #2

Traceback (most recent call last):
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\Альоша\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\Альоша\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\Альоша\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\Альоша\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path    
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\Альоша\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\Альоша\.vscode\extensions\ms-python.debugpy-2024.4.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code   
    exec(code, run_globals)
  File "C:\stable-diffusion-webui\outpust\OneDrive - iicx\sd_nai_ref\work_copy_server\sd_bot_1_6_4.py", line 29, in <module>
    from danboorutags_1_7 import cmd_dan, cmd_tt, cmd_tags, cmd_deep, sfw_prompt_generator, nsfw_prompt_generator
  File "C:\stable-diffusion-webui\outpust\OneDrive - iicx\sd_nai_ref\work_copy_server\danboorutags_1_7.py", line 20, in <module>
    from novelai_python.tool.random_prompt import RandomPromptGenerator
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\__init__.py", line 14, in <module>
    from .sdk import GenerateImageInfer, ImageGenerateResp
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\sdk\__init__.py", line 7, in <module>
    from .ai.generate import LLM, LLMResp  # noqa 401
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\sdk\ai\generate\__init__.py", line 19, in <module>
    from ...._response.ai.generate import LLMResp
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\_response\ai\generate.py", line 6, in <module>
    from novelai_python.tokenizer import LLMTokenizer
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\tokenizer\__init__.py", line 121, in <module>
    class LLMTokenizer:
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\tokenizer\__init__.py", line 139, in LLMTokenizer
    _NERDSTASH_TOKENIZER_v1 = SentencePiece(_NERDSTASH_TOKENIZER_v1_PATH)
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\tokenizer\__init__.py", line 31, in __init__
    self.Load(model_path)
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\sentencepiece\__init__.py", line 961, in Load
    return self.LoadFromFile(model_file)
  File "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\sentencepiece\__init__.py", line 316, in LoadFromFile
    return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
OSError: Not found: "c:\Users\Альоша\AppData\Local\Programs\Python\Python310\lib\site-packages\novelai_python\tokenizer\nerdstash_v1.model": No such file or directory Error #2 

but I have this file!

image

Vibe Transfer inconsistent

Generate Result inconsistent
0240320_010827


I have no way of knowing the special handling of the image.
Already Known

  • black background
  • 448*448
  • INTER_LINEAR

After it was updated to a certain version, an unknown problem occurred.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.