Skip to content

Commit

Permalink
[TOOL] Move decode into CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Aedial committed May 21, 2023
1 parent 2977a13 commit 7b3dc23
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Run a sanity check on your user content. It will print what content couldn't be
python -m novelai_api sanity_check <username> <password>
```

### Decode
Decode a b64 encoded tokenized text. This will print the tokens and the decoded text.
```bash
python -m novelai_api decode <model> <data>
```

## Using the module in your code
A full list of examples is available in the [example](example) directory

Expand Down
6 changes: 6 additions & 0 deletions README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Run a sanity check on your user content. It will print what content couldn't be
python -m novelai_api sanity_check <username> <password>
```

### Decode
Decode a b64 encoded tokenized text. This will print the tokens and the decoded text.
```bash
python -m novelai_api decode <model> <data>
```

## Using the module in your code
A full list of examples is available in the [example](example) directory

Expand Down
44 changes: 38 additions & 6 deletions novelai_api/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import asyncio
import base64
import inspect
import sys
from argparse import ArgumentParser
from logging import Logger, StreamHandler
from typing import Any, Dict, List, NoReturn, Optional, Set, Tuple

from aiohttp import ClientSession

from novelai_api import NovelAIAPI
from novelai_api.Preset import Model
from novelai_api.Tokenizer import Tokenizer
from novelai_api.utils import decompress_user_data, decrypt_user_data, get_access_key, get_encryption_key


Expand Down Expand Up @@ -158,32 +163,59 @@ async def sanity_checker_func(username: str, password: str):
_check_duplicate_meta(stories, story_contents, presets, modules)


# decode
async def decode_func(model: str, data: str):
model = Model(model)

tokens = base64.b64decode(data)
tokens = [int.from_bytes(tokens[i * 2 : (i + 1) * 2], "little") for i in range(len(tokens) // 2)]
print(f"Tokens = {tokens}")

text = Tokenizer.decode(model, tokens)
print(f"Text = {text}")


if __name__ == "__main__":
parser = ArgumentParser()

def add_credentials_arguments(p: ArgumentParser):
p.add_argument("username", help="NovelAI username")
p.add_argument("password", help="NovelAI password")

subparser = parser.add_subparsers(help="Function to call")

# Get access key
access_key_parser = subparser.add_parser("access_key", help="Get access key")
access_key_parser.set_defaults(func=get_access_key_func)
add_credentials_arguments(access_key_parser)

# Get access token
access_token_parser = subparser.add_parser("access_token", help="Get access token")
access_token_parser.set_defaults(func=get_access_token_func)
add_credentials_arguments(access_token_parser)

# Sanity check
sanity_check_parser = subparser.add_parser("sanity_check", help="Sanity check")
sanity_check_parser.set_defaults(func=sanity_checker_func)
add_credentials_arguments(sanity_check_parser)

# Generic arguments
parser.add_argument("username", help="NovelAI username")
parser.add_argument("password", help="NovelAI password")
# Decode
decode_parser = subparser.add_parser("decode", help="Decode")
decode_parser.add_argument("model", help="Model to use")
decode_parser.add_argument("data", help="Data to decode")
decode_parser.set_defaults(func=decode_func)

# Parse arguments
args = parser.parse_args()
if getattr(args, "func", None) is None:
parser.print_help()
elif asyncio.iscoroutinefunction(args.func):
asyncio.run(args.func(args.username, args.password))
sys.exit(1)

# Get the values of the arguments
arg_names = inspect.getfullargspec(args.func).args
args_values = {name: getattr(args, name) for name in arg_names}

if asyncio.iscoroutinefunction(args.func):
asyncio.run(args.func(**args_values))
else:
args.func(args.username, args.password)
args.func(**args_values)
17 changes: 0 additions & 17 deletions tools/decode.py

This file was deleted.

0 comments on commit 7b3dc23

Please sign in to comment.