Updated readme, added some further privacy things for hugging face
Browse files- README.md +4 -1
- modules/common/file_ensurer.py +15 -0
- util/hugging_face_flag.py +0 -0
- webgui.py +151 -3
README.md
CHANGED
@@ -101,8 +101,11 @@ For Gemini, you'll need an API key, you can get one [here](https://ai.google.dev
|
|
101 |
|
102 |
I'd recommend using GPT for most things, as it's generally better at translation.
|
103 |
|
104 |
-
Once again, mostly straightforward, fill in your API key,
|
105 |
|
|
|
|
|
|
|
106 |
|
107 |
---------------------------------------------------------------------------------------------------------------------------------------------------
|
108 |
|
|
|
101 |
|
102 |
I'd recommend using GPT for most things, as it's generally better at translation.
|
103 |
|
104 |
+
Once again, mostly straightforward, fill in your API key, select your LLM, and select your text. You'll also need to add your settings file if on HuggingFace.
|
105 |
|
106 |
+
You can calculate costs here or just translate. Output will show in the appropriate fields.
|
107 |
+
|
108 |
+
For further details on the settings file, see [here](#translation-with-llms-settings).
|
109 |
|
110 |
---------------------------------------------------------------------------------------------------------------------------------------------------
|
111 |
|
modules/common/file_ensurer.py
CHANGED
@@ -24,6 +24,8 @@ class FileEnsurer():
|
|
24 |
output_dir = os.path.join(script_dir, "output")
|
25 |
archive_dir = os.path.join(output_dir, "archive")
|
26 |
|
|
|
|
|
27 |
## main dirs (config is just under userprofile on windows, and under home on linux); secrets are under appdata on windows, and under .config on linux
|
28 |
if(os.name == 'nt'): ## Windows
|
29 |
config_dir = os.path.join(os.environ['USERPROFILE'],"KudasaiConfig")
|
@@ -143,6 +145,19 @@ class FileEnsurer():
|
|
143 |
do_interrupt = False
|
144 |
need_to_run_kairyou = True
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
##--------------------start-of-exit_kudasai()------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
147 |
|
148 |
@staticmethod
|
|
|
24 |
output_dir = os.path.join(script_dir, "output")
|
25 |
archive_dir = os.path.join(output_dir, "archive")
|
26 |
|
27 |
+
hugging_face_flag = os.path.join(script_dir, "util", "hugging_face_flag.py")
|
28 |
+
|
29 |
## main dirs (config is just under userprofile on windows, and under home on linux); secrets are under appdata on windows, and under .config on linux
|
30 |
if(os.name == 'nt'): ## Windows
|
31 |
config_dir = os.path.join(os.environ['USERPROFILE'],"KudasaiConfig")
|
|
|
145 |
do_interrupt = False
|
146 |
need_to_run_kairyou = True
|
147 |
|
148 |
+
##-------------------start-of-check_if_hugging_space()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
149 |
+
|
150 |
+
@staticmethod
|
151 |
+
def check_if_hugging_space() -> bool:
|
152 |
+
|
153 |
+
"""
|
154 |
+
|
155 |
+
Determines if Kudasai is running on a Hugging Face server.
|
156 |
+
|
157 |
+
"""
|
158 |
+
|
159 |
+
return os.path.exists(FileEnsurer.hugging_face_flag)
|
160 |
+
|
161 |
##--------------------start-of-exit_kudasai()------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
162 |
|
163 |
@staticmethod
|
util/hugging_face_flag.py
ADDED
File without changes
|
webgui.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
## built-in libraries
|
2 |
import typing
|
3 |
import base64
|
|
|
4 |
|
5 |
## third-party libraries
|
6 |
import gradio as gr
|
@@ -89,6 +90,75 @@ class KudasaiGUI:
|
|
89 |
|
90 |
return Logger.current_batch
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
##-------------------start-of-set_kijiku_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
93 |
|
94 |
async def set_kijiku_api_key(api_key) -> None:
|
@@ -121,6 +191,31 @@ class KudasaiGUI:
|
|
121 |
except:
|
122 |
raise gr.Error("Invalid API key")
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
##-------------------start-of-create_new_key_value_tuple_pairs()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
125 |
|
126 |
def create_new_key_value_tuple_pairs(kijiku_settings:typing.List[str]) -> typing.List[typing.Tuple[str, str]]:
|
@@ -268,7 +363,7 @@ class KudasaiGUI:
|
|
268 |
self.input_text_kaiseki = gr.Textbox(label='Japanese Text', placeholder='Use this or the text file input, if you provide both, Kudasai will use the file input.', lines=10, show_label=True, interactive=True)
|
269 |
|
270 |
with gr.Row():
|
271 |
-
self.kaiseki_api_key_input_field = gr.Textbox(label='API Key', lines=1, show_label=True, interactive=True, type='password')
|
272 |
|
273 |
with gr.Row():
|
274 |
self.kaiseki_translate_button = gr.Button('Translate', variant="primary")
|
@@ -301,13 +396,13 @@ class KudasaiGUI:
|
|
301 |
with gr.Column():
|
302 |
self.input_txt_file_kijiku = gr.File(label='TXT file with Japanese Text', file_count='single', file_types=['.txt'], type='filepath', interactive=True)
|
303 |
self.input_text_kijiku = gr.Textbox(label='Japanese Text', placeholder='Use this or the text file input, if you provide both, Kudasai will use the file input.', lines=10, show_label=True, interactive=True, type='text')
|
304 |
-
self.input_kijiku_rules_file = gr.File(value = FileEnsurer.config_kijiku_rules_path, label='Kijiku Rules File', file_count='single', file_types=['.json'], type='filepath')
|
305 |
|
306 |
with gr.Row():
|
307 |
self.llm_option_dropdown = gr.Dropdown(label='LLM Option', choices=["OpenAI", "Gemini"], value="OpenAI", show_label=True, interactive=True)
|
308 |
|
309 |
with gr.Row():
|
310 |
-
self.kijiku_api_key_input = gr.Textbox(label='API Key', lines=1, max_lines=2, show_label=True, interactive=True, type='password')
|
311 |
|
312 |
with gr.Row():
|
313 |
self.kijiku_translate_button = gr.Button('Translate', variant="primary")
|
@@ -711,6 +806,9 @@ class KudasaiGUI:
|
|
711 |
|
712 |
indexed_text = Kudasai.mark_indexed_names(text_to_index, unique_names)
|
713 |
|
|
|
|
|
|
|
714 |
return indexed_text, indexing_log, log_text, log_text, error_log
|
715 |
|
716 |
else:
|
@@ -772,6 +870,9 @@ class KudasaiGUI:
|
|
772 |
|
773 |
log_text = FileEnsurer.standard_read_file(Logger.log_file_path)
|
774 |
|
|
|
|
|
|
|
775 |
return preprocessed_text, preprocessing_log, log_text, log_text, error_log
|
776 |
|
777 |
else:
|
@@ -848,6 +949,13 @@ class KudasaiGUI:
|
|
848 |
|
849 |
error_text = FileEnsurer.standard_read_file(FileEnsurer.error_log_path)
|
850 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
851 |
return translated_text, je_check_text, log_text, error_text
|
852 |
|
853 |
##-------------------start-of-translate_with_kijiku()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
@@ -928,7 +1036,13 @@ class KudasaiGUI:
|
|
928 |
log_text = FileEnsurer.standard_read_file(Logger.log_file_path)
|
929 |
|
930 |
error_text = FileEnsurer.standard_read_file(FileEnsurer.error_log_path)
|
|
|
|
|
|
|
931 |
|
|
|
|
|
|
|
932 |
return translated_text, je_check_text, log_text, error_text
|
933 |
|
934 |
##-------------------start-of-kijiku_calculate_costs_button_click()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
@@ -1001,6 +1115,11 @@ class KudasaiGUI:
|
|
1001 |
|
1002 |
gr.Info(cost_estimation)
|
1003 |
|
|
|
|
|
|
|
|
|
|
|
1004 |
return cost_estimation
|
1005 |
|
1006 |
##-------------------start-of-clear_index_tab()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
@@ -1532,6 +1651,28 @@ class KudasaiGUI:
|
|
1532 |
gr.Info("Preprocessed text copied to Kijiku")
|
1533 |
return input_text
|
1534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1535 |
##-------------------start-of-Listener-Declaration---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
1536 |
|
1537 |
##-------------------start-of-load()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
@@ -1872,6 +2013,13 @@ class KudasaiGUI:
|
|
1872 |
|
1873 |
outputs=[self.logging_tab_debug_log_output_field, self.logging_tab_error_log_output_field])
|
1874 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1875 |
##-------------------start-of-save_to_file_indexed_text_click()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
1876 |
|
1877 |
self.save_to_file_indexed_text.click(lambda text: text, ## save text as is
|
|
|
1 |
## built-in libraries
|
2 |
import typing
|
3 |
import base64
|
4 |
+
import os
|
5 |
|
6 |
## third-party libraries
|
7 |
import gradio as gr
|
|
|
90 |
|
91 |
return Logger.current_batch
|
92 |
|
93 |
+
##-------------------start-of-get_saved_kaiseki_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
94 |
+
|
95 |
+
def get_saved_kaiseki_api_key() -> str:
|
96 |
+
|
97 |
+
"""
|
98 |
+
|
99 |
+
Gets the saved kaiseki api key from the config folder, if it exists.
|
100 |
+
|
101 |
+
Returns:
|
102 |
+
api_key (str) : The api key.
|
103 |
+
|
104 |
+
"""
|
105 |
+
|
106 |
+
try:
|
107 |
+
|
108 |
+
assert FileEnsurer.check_if_hugging_space() == False
|
109 |
+
|
110 |
+
## Api key is encoded in base 64 so we need to decode it before returning
|
111 |
+
return base64.b64decode(FileEnsurer.standard_read_file(FileEnsurer.deepl_api_key_path).encode('utf-8')).decode('utf-8')
|
112 |
+
|
113 |
+
except:
|
114 |
+
return ""
|
115 |
+
|
116 |
+
##-------------------start-of-get_saved_openai_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
117 |
+
|
118 |
+
def get_saved_openai_api_key() -> str:
|
119 |
+
|
120 |
+
"""
|
121 |
+
|
122 |
+
Gets the saved openai api key from the config folder, if it exists.
|
123 |
+
|
124 |
+
Returns:
|
125 |
+
api_key (str) : The api key.
|
126 |
+
|
127 |
+
"""
|
128 |
+
|
129 |
+
try:
|
130 |
+
|
131 |
+
assert FileEnsurer.check_if_hugging_space() == False
|
132 |
+
|
133 |
+
## Api key is encoded in base 64 so we need to decode it before returning
|
134 |
+
return base64.b64decode(FileEnsurer.standard_read_file(FileEnsurer.openai_api_key_path).encode('utf-8')).decode('utf-8')
|
135 |
+
|
136 |
+
except:
|
137 |
+
return ""
|
138 |
+
|
139 |
+
##-------------------start-of-get_saved_gemini_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
140 |
+
|
141 |
+
def get_saved_gemini_api_key() -> str:
|
142 |
+
|
143 |
+
"""
|
144 |
+
|
145 |
+
Gets the saved gemini api key from the config folder, if it exists.
|
146 |
+
|
147 |
+
Returns:
|
148 |
+
api_key (str) : The api key.
|
149 |
+
|
150 |
+
"""
|
151 |
+
|
152 |
+
try:
|
153 |
+
|
154 |
+
assert FileEnsurer.check_if_hugging_space() == False
|
155 |
+
|
156 |
+
## Api key is encoded in base 64 so we need to decode it before returning
|
157 |
+
return base64.b64decode(FileEnsurer.standard_read_file(FileEnsurer.gemini_api_key_path).encode('utf-8')).decode('utf-8')
|
158 |
+
|
159 |
+
except:
|
160 |
+
return ""
|
161 |
+
|
162 |
##-------------------start-of-set_kijiku_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
163 |
|
164 |
async def set_kijiku_api_key(api_key) -> None:
|
|
|
191 |
except:
|
192 |
raise gr.Error("Invalid API key")
|
193 |
|
194 |
+
##-------------------start-of-update_kijiku_api_key()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
195 |
+
|
196 |
+
def update_kijiku_api_key(api_key) -> None:
|
197 |
+
|
198 |
+
"""
|
199 |
+
|
200 |
+
Updates the kijiku api key.
|
201 |
+
|
202 |
+
Parameters:
|
203 |
+
api_key (str) : The api key.
|
204 |
+
|
205 |
+
"""
|
206 |
+
|
207 |
+
if(FileEnsurer.check_if_hugging_space() == True):
|
208 |
+
return
|
209 |
+
|
210 |
+
## also gonna want to update the api key file with the new api key
|
211 |
+
if(Kijiku.LLM_TYPE == "openai"):
|
212 |
+
path_to_api_key = FileEnsurer.openai_api_key_path
|
213 |
+
|
214 |
+
else:
|
215 |
+
path_to_api_key = FileEnsurer.gemini_api_key_path
|
216 |
+
|
217 |
+
FileEnsurer.standard_overwrite_file(path_to_api_key, base64.b64encode(str(api_key).encode('utf-8')).decode('utf-8'), omit=True)
|
218 |
+
|
219 |
##-------------------start-of-create_new_key_value_tuple_pairs()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
220 |
|
221 |
def create_new_key_value_tuple_pairs(kijiku_settings:typing.List[str]) -> typing.List[typing.Tuple[str, str]]:
|
|
|
363 |
self.input_text_kaiseki = gr.Textbox(label='Japanese Text', placeholder='Use this or the text file input, if you provide both, Kudasai will use the file input.', lines=10, show_label=True, interactive=True)
|
364 |
|
365 |
with gr.Row():
|
366 |
+
self.kaiseki_api_key_input_field = gr.Textbox(label='API Key', value=get_saved_kaiseki_api_key, lines=1, show_label=True, interactive=True, type='password')
|
367 |
|
368 |
with gr.Row():
|
369 |
self.kaiseki_translate_button = gr.Button('Translate', variant="primary")
|
|
|
396 |
with gr.Column():
|
397 |
self.input_txt_file_kijiku = gr.File(label='TXT file with Japanese Text', file_count='single', file_types=['.txt'], type='filepath', interactive=True)
|
398 |
self.input_text_kijiku = gr.Textbox(label='Japanese Text', placeholder='Use this or the text file input, if you provide both, Kudasai will use the file input.', lines=10, show_label=True, interactive=True, type='text')
|
399 |
+
self.input_kijiku_rules_file = gr.File(value = FileEnsurer.config_kijiku_rules_path if not FileEnsurer.check_if_hugging_space() else FileEnsurer.blank_rules_path, label='Kijiku Rules File', file_count='single', file_types=['.json'], type='filepath')
|
400 |
|
401 |
with gr.Row():
|
402 |
self.llm_option_dropdown = gr.Dropdown(label='LLM Option', choices=["OpenAI", "Gemini"], value="OpenAI", show_label=True, interactive=True)
|
403 |
|
404 |
with gr.Row():
|
405 |
+
self.kijiku_api_key_input = gr.Textbox(label='API Key', value=get_saved_openai_api_key, lines=1, max_lines=2, show_label=True, interactive=True, type='password')
|
406 |
|
407 |
with gr.Row():
|
408 |
self.kijiku_translate_button = gr.Button('Translate', variant="primary")
|
|
|
806 |
|
807 |
indexed_text = Kudasai.mark_indexed_names(text_to_index, unique_names)
|
808 |
|
809 |
+
if(FileEnsurer.check_if_hugging_space == True):
|
810 |
+
os.remove(FileEnsurer.output_dir)
|
811 |
+
|
812 |
return indexed_text, indexing_log, log_text, log_text, error_log
|
813 |
|
814 |
else:
|
|
|
870 |
|
871 |
log_text = FileEnsurer.standard_read_file(Logger.log_file_path)
|
872 |
|
873 |
+
if(FileEnsurer.check_if_hugging_space == True):
|
874 |
+
os.remove(FileEnsurer.output_dir)
|
875 |
+
|
876 |
return preprocessed_text, preprocessing_log, log_text, log_text, error_log
|
877 |
|
878 |
else:
|
|
|
949 |
|
950 |
error_text = FileEnsurer.standard_read_file(FileEnsurer.error_log_path)
|
951 |
|
952 |
+
if(FileEnsurer.check_if_hugging_space == True):
|
953 |
+
os.remove(FileEnsurer.output_dir)
|
954 |
+
|
955 |
+
else:
|
956 |
+
## also gonna want to update the api key file with the new api key
|
957 |
+
FileEnsurer.standard_overwrite_file(FileEnsurer.deepl_api_key_path, base64.b64encode(str(api_key_input).encode('utf-8')).decode('utf-8'), omit=True)
|
958 |
+
|
959 |
return translated_text, je_check_text, log_text, error_text
|
960 |
|
961 |
##-------------------start-of-translate_with_kijiku()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
1036 |
log_text = FileEnsurer.standard_read_file(Logger.log_file_path)
|
1037 |
|
1038 |
error_text = FileEnsurer.standard_read_file(FileEnsurer.error_log_path)
|
1039 |
+
|
1040 |
+
if(FileEnsurer.check_if_hugging_space == True):
|
1041 |
+
os.remove(FileEnsurer.output_dir)
|
1042 |
|
1043 |
+
## then overwrite the api key file with the new api key
|
1044 |
+
update_kijiku_api_key(api_key)
|
1045 |
+
|
1046 |
return translated_text, je_check_text, log_text, error_text
|
1047 |
|
1048 |
##-------------------start-of-kijiku_calculate_costs_button_click()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
1115 |
|
1116 |
gr.Info(cost_estimation)
|
1117 |
|
1118 |
+
if(FileEnsurer.check_if_hugging_space == True):
|
1119 |
+
os.remove(FileEnsurer.output_dir)
|
1120 |
+
|
1121 |
+
update_kijiku_api_key(api_key)
|
1122 |
+
|
1123 |
return cost_estimation
|
1124 |
|
1125 |
##-------------------start-of-clear_index_tab()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
1651 |
gr.Info("Preprocessed text copied to Kijiku")
|
1652 |
return input_text
|
1653 |
|
1654 |
+
##-------------------start-of-switch_kijiku_api_key_value()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
1655 |
+
|
1656 |
+
def switch_kijiku_api_key_value(llm_type) -> str:
|
1657 |
+
|
1658 |
+
"""
|
1659 |
+
|
1660 |
+
Switches the LLM type value.
|
1661 |
+
|
1662 |
+
Parameters:
|
1663 |
+
llm_type (str) : The LLM type.
|
1664 |
+
|
1665 |
+
Returns:
|
1666 |
+
llm_type (str) : The LLM type.
|
1667 |
+
|
1668 |
+
"""
|
1669 |
+
|
1670 |
+
if(llm_type == "OpenAI"):
|
1671 |
+
return get_saved_openai_api_key()
|
1672 |
+
|
1673 |
+
else:
|
1674 |
+
return get_saved_gemini_api_key()
|
1675 |
+
|
1676 |
##-------------------start-of-Listener-Declaration---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
1677 |
|
1678 |
##-------------------start-of-load()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
2013 |
|
2014 |
outputs=[self.logging_tab_debug_log_output_field, self.logging_tab_error_log_output_field])
|
2015 |
|
2016 |
+
##-------------------start-of-kijiku_api_key_input.change()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
2017 |
+
|
2018 |
+
self.llm_option_dropdown.change(switch_kijiku_api_key_value,
|
2019 |
+
inputs=[self.llm_option_dropdown],
|
2020 |
+
|
2021 |
+
outputs=[self.kijiku_api_key_input])
|
2022 |
+
|
2023 |
##-------------------start-of-save_to_file_indexed_text_click()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
2024 |
|
2025 |
self.save_to_file_indexed_text.click(lambda text: text, ## save text as is
|