Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Sep 28, 2023
1 parent a7f149e commit fd7bdd5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
/.vscode
.env.local
debug.log
data/configs.json
3 changes: 2 additions & 1 deletion core/CAIAssistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class CAIAssistant:
def __init__(self, promptsFolder=None, openai_api_key=None):
if promptsFolder is None:
promptsFolder = os.path.join(os.path.dirname(__file__), '../prompts')
promptsFolder = os.path.join(os.path.dirname(__file__), '../data')
self._promptsFolder = promptsFolder
self.bindAPI(openai_api_key=openai_api_key)
return
Expand Down Expand Up @@ -41,6 +41,7 @@ def bindAPI(self, openai_api_key):
),
]),
)
self._connected = True
except Exception as e:
logging.error('Failed to bind API: ' + str(e))
return
Expand Down
7 changes: 6 additions & 1 deletion core/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ def _updateLocalization(self, languageName, languageCode):
res = [x for x in res if len(x) > 0]
assert len(strings) == len(res)
res = {k: v for k, v in zip(strings, res)}
return res
return res

def bindAPI(self, key):
self._assistant.bindAPI(key)
return

23 changes: 23 additions & 0 deletions data/languages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"sk": "Slovak",
"en": "English",
"es": "Spanish",
"fr": "French",
"de": "German",
"it": "Italian",
"pt": "Portuguese",
"nl": "Dutch",
"sv": "Swedish",
"no": "Norwegian",
"da": "Danish",
"fi": "Finnish",
"pl": "Polish",
"hu": "Hungarian",
"ru": "Russian",
"ar": "Arabic",
"ja": "Japanese",
"zh": "Chinese",
"ko": "Korean",
"ua": "Ukrainian",
"tr": "Turkish"
}
File renamed without changes.
File renamed without changes.
40 changes: 26 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tkinter import ttk
from tkinter import simpledialog
import tkinter.scrolledtext as tkst
import json
from core.worker import CWorker
# set up logging
import logging
Expand All @@ -21,16 +22,17 @@
# TODO: add translation history
# TODO: add simple translation diff
class App(tk.Frame):
def __init__(self, master, languages, currentLanguage=None):
def __init__(self, master, languages, configs):
super().__init__(master)
self._configs = configs
self._localizationMap = {}
# predefine messages
self._localization('Processing...')
self._localization('Translation is not accurate and will be updated soon.')
# set up UI
self._master = master
self._languages = languages
self._currentLanguage = currentLanguage or self._languages.keys()[0]
self._currentLanguage = self._configs.get('language') or 'en'
# set global font
self._master.option_add("*Font", ("Arial", 14))
self._master.title("AI Enhanced Translator")
Expand Down Expand Up @@ -194,6 +196,7 @@ def onSelectLanguage(self, event):
if code is None: return

self._currentLanguage = code
self._configs['language'] = code
self._worker.forceTranslate() # hack to force translation
return

Expand All @@ -211,18 +214,27 @@ def onSwitchAPIKey(self):
parent=self._master
)
if newKey is None: return
# self._worker.switchAPIKey(newKey)
self._worker.bindAPI(newKey)
return

def configs(self): return self._configs
# End of class

def main():
# load languages from data/languages.json
with open('data/languages.json', 'r') as f: languages = json.load(f)
# load configs
configs = {}
try:
with open('data/configs.json', 'r') as f: configs = json.load(f)
except: pass

app = App(master=tk.Tk(), languages=languages, configs=configs)
app.mainloop()
# save configs on exit
configs = app.configs()
with open('data/configs.json', 'w') as f: json.dump(configs, f, indent=2)
return

if '__main__' == __name__:
app = App(
master=tk.Tk(),
# TODO: load languages from config
languages={
'sk': 'Slovak',
'en': 'English',
'de': 'German',
},
currentLanguage='sk'
)
app.mainloop()
main()

0 comments on commit fd7bdd5

Please sign in to comment.