Skip to content

Commit

Permalink
Merge pull request #4 from Seinuve/attempt_async_implementation_for_k…
Browse files Browse the repository at this point in the history
…ijiku

Attempt async implementation for kijiku merge
  • Loading branch information
Seinu authored Sep 3, 2023
2 parents 9ab5ef7 + 9f1f575 commit c1f6c45
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 772 deletions.
654 changes: 0 additions & 654 deletions GUI.py

This file was deleted.

74 changes: 47 additions & 27 deletions Kudasai.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## built in modules
import os
import sys
import asyncio

## third party modules

Expand Down Expand Up @@ -121,11 +122,11 @@ def setup_kairyou_for_console(self) -> None:

os.system("title " + "Kudasai Console")

replacement_json = input("Please enter the path to the replacement json file:\n")
replacement_json = input("Please enter the path to the replacement json file:\n").strip('"')

self.preloader.toolkit.clear_console()

input_file = input("Please enter the path to the input file to be processed:\n")
input_file = input("Please enter the path to the input file to be processed:\n").strip('"')

self.preloader.toolkit.clear_console()

Expand All @@ -151,7 +152,7 @@ def setup_kairyou_for_console(self) -> None:

##-------------------start-of-run_kudasai_console()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def run_kudasai_console(self) -> None:
async def run_kudasai_console(self) -> None:

"""
Expand All @@ -175,13 +176,13 @@ def run_kudasai_console(self) -> None:

self.preloader.toolkit.pause_console("\nPress any key to continue to Auto-Translation...")

self.determine_autotranslation_module()
await self.determine_autotranslation_module()

self.preloader.toolkit.pause_console("\nPress any key to exit...")

##-------------------start-of-run_kudasai_cli()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def run_kudasai_cli(self) -> None:
async def run_kudasai_cli(self) -> None:

"""
Expand All @@ -205,7 +206,7 @@ def run_kudasai_cli(self) -> None:

self.preloader.toolkit.pause_console("\nPress any key to continue to Auto-Translation...")

self.determine_autotranslation_module()
await self.determine_autotranslation_module()

##-------------------start-of-handle_update_check_from_cli_or_console()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Expand All @@ -223,7 +224,7 @@ def handle_update_check_from_cli_or_console(self) -> None:
"""

self.connection, update_prompt = self.preloader.toolkit.check_update()
self.connection, update_prompt = self.preloader.toolkit.check_update() # type: ignore

if(update_prompt):

Expand All @@ -235,7 +236,7 @@ def handle_update_check_from_cli_or_console(self) -> None:

##-------------------start-of-determine_autotranslation_module()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def determine_autotranslation_module(self) -> None:
async def determine_autotranslation_module(self) -> None:

"""
Expand Down Expand Up @@ -270,7 +271,7 @@ def determine_autotranslation_module(self) -> None:
if(pathing == "1"):
self.run_kaiseki()
elif(pathing == "2"):
self.run_kijiku()
await self.run_kijiku()
else:
self.preloader.toolkit.clear_console()

Expand Down Expand Up @@ -306,7 +307,7 @@ def run_kaiseki(self) -> None:

##-------------------start-of-run_kijiku()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

def run_kijiku(self) -> None:
async def run_kijiku(self) -> None:

"""
Expand All @@ -328,7 +329,7 @@ def run_kijiku(self) -> None:

self.kijiku_client = Kijiku(self.kairyou_client.text_to_preprocess,self.preloader) ## type: ignore (we know it's not None)

self.kijiku_client.translate()
await self.kijiku_client.translate()

self.preloader.toolkit.clear_console()

Expand All @@ -338,30 +339,49 @@ def run_kijiku(self) -> None:

##-------------------start-of-main()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

client = Kudasai()
async def main() -> None:

try:
client = Kudasai()

## determines if we will run the Kudasai CLI or the Kudasai Console
if(__name__ == '__main__'):
try:

## to be implemented later, for now we will just run the CLI version
if(len(sys.argv) < 3):

client.setup_kairyou_for_console()
## determines if we will run the Kudasai CLI or the Kudasai Console
if(__name__ == '__main__'):

client.run_kudasai_console()
## if running console version
if(len(sys.argv) <= 1):

client.setup_kairyou_for_console()

client.preloader.file_handler.logger.push_batch()
await client.run_kudasai_console()

else:
client.preloader.file_handler.logger.push_batch()

## if running cli version
elif(len(sys.argv) == 3):

client.setup_kairyou_for_cli(sys.argv[1], sys.argv[2])

await client.run_kudasai_cli()

client.setup_kairyou_for_cli(sys.argv[1], sys.argv[2])
client.preloader.file_handler.logger.push_batch()

## print usage statement
else:

print("Usage: python Kudasai.py <input_file> <replacement_json>\n\n")
print("or run Kudasai.py without any arguments to run the console version.\n\n")

client.preloader.file_handler.logger.log_action("Usage: python Kudasai.py <input_file> <replacement_json>")

client.preloader.toolkit.pause_console()

exit()

client.run_kudasai_cli()
except Exception as e:

client.preloader.file_handler.logger.push_batch()
client.preloader.file_handler.handle_critical_exception(e)

except Exception as e:
##---------------------------------/

client.preloader.file_handler.handle_critical_exception(e)
asyncio.run(main())
39 changes: 14 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ Built for Windows, should work on Linux/MacOS but is untested.

Python version: 3.8+

It should work with 3.8, although it is untested. I recommend using 3.9+

Used to make (Japanese - English) translation easier by preprocessing the Japanese text (optional auto translation using deepL/openai API).

Preprocessor originally derived from https://github.com/Atreyagaurav/mtl-related-scripts
Expand All @@ -40,6 +38,8 @@ Kijiku.py - openai translation module - 基軸 - Foundation
---------------------------------------------------------------------------------------------------------------------------------------------------
**Dependencies**<a name="dependencies"></a>

spacy

spacy[jp]

spacy[en]
Expand All @@ -65,27 +65,29 @@ or see requirements.txt

Please note that issues can occur when trying to install these dependencies:

python -m spacy download ja_core_news_lg
pip install en_core_web_lg

python -m spacy download en_core_web_lg
pip install ja_core_news_lg

if these do not work, either reinstall spacy or try:

pip install en_core_web_lg
python -m spacy download ja_core_news_lg

pip install ja_core_news_lg
python -m spacy download en_core_web_lg

---------------------------------------------------------------------------------------------------------------------------------------------------

**Kairyou**<a name="kairyou"></a>

Kairyou is the preprocessing module, it is used to preprocess Japanese text to make it easier to translate. It is the first step in the process.

To run Kairyou and by extension Kudasai, you must use the command line. You can do so by opening a command prompt and entering the following command:
To run Kairyou and by extension Kudasai, you may use the CLI or the Console.

```python Path to Kudasai.py Path to the text you are preprocessing Path to the replacement json file```
You can run the console by simply clicking on Kudasai.py, this will open the console and you can follow the prompts from there.

If you wish to use the CLI, you can do so by opening a command prompt and entering the following:

Where the following is replaced with the appropriate information:
```python Path to Kudasai.py Path to the text you are preprocessing Path to the replacement json file```

i.e.

Expand All @@ -101,7 +103,7 @@ See an example of a command line entry below

Many replacement json files are included in the replacements jsons folder, you can also make your own if you wish provided it follows the same format. See an example below

![Example JSON](https://i.imgur.com/qgCI9w9.png)
![Example JSON](https://i.imgur.com/u3FnUia.jpg)

If you do not wish to use a replacement json file, you can use the blank replacement json file provided in the replacements folder. This also serves as a template for making your own replacement json files.

Expand All @@ -127,7 +129,7 @@ After preprocessing is completed, you will be prompted to run the translation mo

**Kaiseki**<a name="kaiseki"></a>

Kaiseki is a deepl translation module, it is used to translate Japanese to English. It is flawed and not very accurate compared to Kijiku although plans are in place to develop a better version.
Kaiseki is a deepl translation module, it is used to translate Japanese to English. It is flawed and not very accurate compared to Kijiku although plans are in place to develop a better version *eventually*.

Please note an api key is required for Kaiseki to work, you can get one here: https://www.deepl.com/pro#developer.

Expand Down Expand Up @@ -155,12 +157,10 @@ Kijiku will store your obfuscated api key locally under KudasaiConfig under your

You will also be prompted if you'd like to change these settings, if you choose to do so, you'll be asked for which setting you'd like to change, and what to change it too, until you choose to exit. Multiple things can be done in this menu. If you want to change anything about the settings, you do it here.

You can also choose upload your own settings file in the setting change menu, this is useful if you want to use someone else's settings file. You would do so by placing the json file in the same directory as Kudasai.py and then selecting 'c' in the settings change menu. This will load the file in and use it as your settings.
You can also choose to upload your own settings file in the settings change menu, this is useful if you want to use someone else's settings file. You would do so by placing the json file in the same directory as Kudasai.py and then selecting 'c' in the settings change menu. This will load the file in and use it as your settings.

After that you will be shown an estimated cost of translation, this is based on the number of tokens in the preprocessed text. Kijiku will then run and translate the preprocessed text and no other input is required.

Please note that translation with Kijiku can take a very long time depending on the length of the text and the number of tokens. It is not uncommon for it to take hours or more for long texts. Plans are in place to make Kijiku asynchronous to speed up translation.

Also note that Kijiku's settings are very complex, please see the section below for more information on them.

---------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -201,17 +201,6 @@ See https://platform.openai.com/docs/api-reference/chat/create for further detai

Please note that while logit_bias and max_tokens can be changed, Kijiku does not currently do anything with them.

---------------------------------------------------------------------------------------------------------------------------------------------------
**GUI**<a name="gui"></a>

GUI.py is a GUI Interface for Kudasai, it also provides visuals for Kaiseki and Kijiku.

Please note that the GUI can appear unresponsive while translating or preprocessing, this is normal.

A console also launches with the gui, it is mostly for visuals but you will be required to confirm your settings if you choose to run Kijiku.

![Example GUI](https://i.imgur.com/JqIj5bE.png)

---------------------------------------------------------------------------------------------------------------------------------------------------
**License**<a name="license"></a>

Expand Down
Loading

0 comments on commit c1f6c45

Please sign in to comment.