aedial / novelai-api Goto Github PK
View Code? Open in Web Editor NEWPython API for the NovelAI REST API
Home Page: https://aedial.github.io/novelai-api/
License: MIT License
Python API for the NovelAI REST API
Home Page: https://aedial.github.io/novelai-api/
License: MIT License
I find that when I generate text on a web page, the endings are all one complete sentence, starting with "." but when I use the api to generate it, it always ends with an incomplete sentence. I have changed "global_settings.generate_until_sentence = True" in the api, because I find that the request in the web page is "True" here but the api defaults to "false". But I found it didn't work. So I would like to know: how can I make the api generate text that always ends with a complete sentence.Can anyone answer my question? Thanks
Translated with www.DeepL.com/Translator (free version)
While debugging my requests against the API and comparing it with the ones from the WebUI, I noticed something weird.
My first thought was that the presets files are outdated, but indeed they do match the ones I can export from the Web UI.
The following properties in red are part of the preset files, but the Web UI does not send them to the API, while this library does.
I don't know, if this affects the generation (because I seem to have other issues atm) or if the API ignores them, but I think we should make sure our requests matches them from the official Web UI.
I donot know it works in “Do”、“Say” or “Story” in default.
I have set the module “theme_textadventure”
Is there a spesific reason for not supporting 3.11, or can it simply be added to the dependencies?
My country access interface is very poor
preset = ImagePreset.from_v3_config()
(from generate_image_test_samplers.py)
return cls.from_file(Path(__file__).parent / "image_presets" / "presets_v3" / "default.preset")
(from ImagePreset.py)
So I modified novelai_api/image_presets/presets_v3/default.preset, but it does not apply. I've tried reinstalling the module, but still does not works.
I have forked the repository and will be submitting a PR with a (mostly) implemented MessagePack decoder, with the additional NovelAi extensions later today.
I don't find any data about "loreBook" in the parameters of the web request or in the parameters of the api. Is there any usage of loreBook in the api? How can I achieve the effect of loreBook in the web page? Is it possible to search for keywords in the context? If the keyword is found, then the text about the keyword is added to the context. Is there a simpler way to do this? For example, a function that has been wrapped and passed the json data of loreBook directly.
That's my question. Thank you very much.
Translated with www.DeepL.com/Translator (free version)
I can't figure it out. Whatever I do it just seems to inpaint random parts of the image and ignore my prompt. txt2img and img2img works fine.
I want to use random nai tags. What is their endpoint?
I'm trying to create Class for run functions in novella inside of flask application. But I have an error.
Traceback (most recent call last):
File "/Users/mirhanmac/Desktop/Projects/noveapi/app.py", line 15, in <module>
webServer = WebServer(flaskLogger, env, novelApi)
File "/Users/mirhanmac/Desktop/Projects/noveapi/web.py", line 13, in __init__
self.novelApi.run()
File "/Users/mirhanmac/Desktop/Projects/noveapi/novel.py", line 26, in run
asyncio.run(self.main())
File "/opt/homebrew/Cellar/[email protected]/3.9.13_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/opt/homebrew/Cellar/[email protected]/3.9.13_3/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/Users/mirhanmac/Desktop/Projects/noveapi/novel.py", line 22, in main
keystore = await api.high_level.get_keystore(key)
File "/Users/mirhanmac/Desktop/Projects/noveapi/novelapi/lib/python3.9/site-packages/novelai_api/_high_level.py", line 78, in get_keystore
keystore.decrypt(key)
File "/Users/mirhanmac/Desktop/Projects/noveapi/novelapi/lib/python3.9/site-packages/novelai_api/Keystore.py", line 90, in decrypt
keystore = loads(b64decode(self.data["keystore"]).decode())
KeyError: 'keystore'
This is my code:
from novelai_api import NovelAI_API
from novelai_api.utils import get_encryption_key, decrypt_user_data
from aiohttp import ClientSession
import asyncio
class NovelAPI:
def __init__(self, logger, env):
if "NAI_USERNAME" not in env or "NAI_PASSWORD" not in env:
raise RuntimeError("Please ensure that NAI_USERNAME and NAI_PASSWORD are set in your environment")
self.logger = logger
self.username = env["NAI_USERNAME"]
self.password = env["NAI_PASSWORD"]
async def main(self):
async with ClientSession() as session:
api = NovelAI_API(session, logger = self.logger)
login = await api.high_level.login(self.username, self.password)
self.logger.info('Bearer ' + login)
key = get_encryption_key(self.username, self.password)
keystore = await api.high_level.get_keystore(key)
self.logger.info(keystore)
def run(self):
asyncio.run(self.main())
If sampler set to ddim for /ai/generation-image
with nai-diffusion-v3
, it generates noised image.
On web page, it generates properly. but in EXIF, sampler shows as "Euler a" on parameters and "ddim_v3" on Comment.
It seems using ddim sampler via API needs more settings or something, but I cannot get additional settings or params for ddim from code.
I checked,
I didn't check,
By the way, on web page, if SMEA+DYN is checked with other sampler then change sampler to DDIM,
the generation costs 28 anlas, not 20 anlas even though DDIM cannot use SMEA or SMEA+DYN.
It would be great to know how to even launch it properly (see other Issue).
I called generate_image with the following code and got a strange error novelai_api.NovelAIError.NovelAIError: 200 - OK
.
Traceback:
Traceback (most recent call last):
File "/root/opt/app.py", line 82, in message_img
run(genarate(prompt))
File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/root/opt/app.py", line 55, in genarate
async for img in api.high_level.generate_image(prompt, ImageModel.Anime_Curated, preset):
File "/usr/local/lib/python3.9/site-packages/novelai_api/_high_level.py", line 355, in generate_image
async for e in self._parent.low_level.generate_image(prompt, model, settings):
File "/usr/local/lib/python3.9/site-packages/novelai_api/_low_level.py", line 657, in generate_image
self._treat_response_object(rsp, content, 201)
File "/usr/local/lib/python3.9/site-packages/novelai_api/_low_level.py", line 48, in _treat_response_object
raise NovelAIError(rsp.status, str(rsp.reason))
novelai_api.NovelAIError.NovelAIError: 200 - OK
Code:
async with API() as api_handler:
api = api_handler.api
preset = ImagePreset()
preset["n_samples"] = 1
preset["resolution"] = (512, 512)
preset["quality_toggle"] = False
async for img in api.high_level.generate_image(prompt, ImageModel.Anime_Curated, preset):
with open(GENERATED_FILEPATH, "wb") as f:
f.write(img)
The entire code is at the following URL
https://github.com/sifue/novelai-slackbot/tree/02419d7d735257294478483753486669620cc384
Since the apparent changes to the NovelAI API a couple of days ago, undesired content no longer seems to be set properly.
All the images I've been able to generate since come back with uc="" (and are notably low quality as a result!). I've confirmed that parameters['uc'] is properly set in _low_level.py when the API call is made. I guess perhaps the naming of the parameters at the NovelAI end has changed?
For the text adventure module, how can I control 'do' or 'say' options, there seems to be no such parameter, there's only prompt.
Is there an example of managing context size that I can look at? I wanted to see how to delete context line by line? Currently I have my prompt as just plain text that I append into as you told, should I make it something like an array of sentences instead so that I can remove by index number?
How does the memory context that you provide in novelAI webapp work? Is it any different?
There is a slight difference in the /ai/annotate-image compared to the frontend, due to some unknown conversion frontend-side.
Base image: image
Base image from frontend: image_input_frontent
Putting the image_input_frontent through the endpoint yields the same result as the frontend.
Creating an API object using
from novelai_api import *
api = NovelAI_API(1, 'properUsername', 'properPassword')
fails with this error:
...\venv\lib\site-packages\novelai_api\NovelAI_API.py:42: RuntimeWarning: coroutine 'High_Level.login' was never awaited
self.high_level.login(username, password)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
I don't have any experience with async, and the lack of documentation makes it hard to figure out what's going on.
https://discord.com/channels/836774308772446268/1093553516078956564
Bug on the backend, nothing that can be done until the devs fix it.
Hello. Thanks so much for working on this. I am new and have just a simple request. Could someone show me how to generate text with a simple example? I am able to log in fine, but I have not been able to figure out how to generate at all. Thanks so much!
D:\gitwork\test>python test.py
Traceback (most recent call last):
File "D:\gitwork\test\test.py", line 89, in <module>
run(main())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "D:\gitwork\test\test.py", line 83, in main
async for _, img in api.high_level.generate_image("1girl", ImageModel.Anime_Full, preset):
File "C:\Users\PC_User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\novelai_api\_high_level.py", line 355, in generate_image
async for e in self._parent.low_level.generate_image(prompt, model, settings):
File "C:\Users\PC_User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\novelai_api\_low_level.py", line 621, in generate_image
async for rsp, content in self.request_stream("post", "/ai/generate-image", args):
File "C:\Users\PC_User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\novelai_api\_low_level.py", line 161, in request_stream
raise e
File "C:\Users\PC_User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\novelai_api\_low_level.py", line 158, in request_stream
async for i in self._request(method, url, session, data, stream):
File "C:\Users\PC_User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\novelai_api\_low_level.py", line 136, in _request
yield rsp, await self._treat_response_stream(rsp, content)
File "C:\Users\PC_User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\novelai_api\_low_level.py", line 88, in _treat_response_stream
data = data.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaa in position 11: invalid start byte
D:\gitwork\test>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.