Skip to main content

A lightweight async wrapper for NovelAI image generation API

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

NovelAI Icon NovelAI-API

A lightweight asynchronous Python wrapper for NovelAI image generation API.

Features

  • Lightweight - Focuses on image generation only, providing a simple and easy-to-use interface.
  • Concurrent - Supports both API and web backend, allowing to run two generating tasks simultaneously.
  • Parameterized - Provides a Metadata class to easily set up generation parameters with type validation.
  • Asynchronous - Utilizes asyncio to run generating tasks and return outputs efficiently.

Installation

pip install novelai

Usage

Initialization

Import required packages and initialize a client with your NovelAI account credentials.

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())

Image Generation

After initializing successfully, you can generate images with the generate_image method. The method takes a Metadata object as the first argument, and an optional host argument to specify the backend to use.

By passing verbose=True, the method will print the estimated Anlas cost each time a generating request is going to be made.

The full parameter list of Metadata can be found in the class definition.

from pathlib import Path
from novelai import Metadata, HOSTS

async def main():
    metadata = Metadata(
        prompt="1girl",
        negative_prompt="bad anatomy",
        width=832,
        height=1216,
        n_samples=1,
    )

    print(f"Estimated Anlas cost: {metadata.calculate_cost(is_opus=False)}")

    # Choose host between "HOSTS.API" and "HOSTS.WEB"
    output = await client.generate_image(
        metadata, host=HOSTS.WEB, verbose=False, is_opus=False
    )

    path = Path("./temp")
    path.mkdir(parents=True, exist_ok=True)

    for filename, data in output.items():
        dest = Path(path / filename)
        dest.write_bytes(data)
        print(f"Image saved as {dest.resolve()}")

asyncio.run(main())

Concurrent Generation

By default, NovelAI only allows one concurrent generating task at a time. However, this wrapper provides the ability to simultaneously run two concurrent generating tasks by sending requests to API and web backend respectively.

Note that API and web backend both have limit on concurrent generation. Therefore, running more than two concurrent tasks will result in a 429 Too Many Requests error.

Full usage example is provided under /docs.

async def task_api():
    await client.generate_image(metadata, host=HOSTS.API)
    print("API task completed")

async def task_web():
    await client.generate_image(metadata, host=HOSTS.WEB)
    print("Web task completed")

async def main():
    tasks = [
        asyncio.create_task(task_api()),
        asyncio.create_task(task_web()),
    ]
    await asyncio.wait(tasks)

asyncio.run(main())

Use in CLI

Optionally, a module function is also provided to directly generate access token in CLI.

Once a access token is generated, it will be valid for 30 days. Token can be used as the authentication header to make requests to NovelAI.

# Replace argument values with your actual account credentials
python3 -m novelai login <username> <password>

References

NovelAI Backend

Aedial/novelai-api

NovelAI Unofficial Knowledgebase

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

novelai-0.2.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

novelai-0.2.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file novelai-0.2.0.tar.gz.

File metadata

  • Download URL: novelai-0.2.0.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for novelai-0.2.0.tar.gz
Algorithm Hash digest
SHA256 59ae2f83cc74544a3f0cf2fb0afef7c8944894712688c97e5801ed5c9a4b3236
MD5 875758b79d8e60b9db71eed67c9c642b
BLAKE2b-256 d5c58e2dee8453c0d80c82bff4127b19f2096a66f063cb12fbee3368a03e7f3d

See more details on using hashes here.

File details

Details for the file novelai-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: novelai-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for novelai-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28f15f751058a8ff129870f4912f8411f7228787eb72c9ead6ba7d2eb7540f9d
MD5 e4074c16e9f7a8dcc0f9b55699c664be
BLAKE2b-256 0a22b5e2dfaa455d0c71fcb6fcd3c3519d7515334cce1eb13333199378572d64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page