Skip to content

Commit

Permalink
refactoring + chat with chatgpt
Browse files Browse the repository at this point in the history
  • Loading branch information
Azornes committed Apr 9, 2023
1 parent e90aae6 commit 4095b1d
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 76 deletions.
49 changes: 39 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ def onLeftButtonUp(event):
def get_text_from_ocr(self):
def OCRGoogle(test):
with open(assets.path_to_tmp, 'rb') as img:
ocr_result = google_api.ocr_by_google_api(ImageEnhance.Image.open(img))
ocr_result = google_api.run_ocr(ImageEnhance.Image.open(img))
return ocr_result

def OCRBaiduu(test):
with open(assets.path_to_tmp2, 'rb') as img:
ocr_result = baidu.ocr_by_baidu(img.read())
ocr_result = baidu.run_ocr(img.read())
return ocr_result

def OCRCapture2Text(test):
ocr_result = capture2Text.ocr_by_capture2text()
ocr_result = capture2Text.run_ocr()
return ocr_result.decode('UTF-8')

def OCRGoogleFree(test):
with open(assets.path_to_tmp, 'rb') as img:
ocr_result = google_free.ocr_google_free(img)
ocr_result = google_free.run_ocr(img)
return ocr_result

def OCRWindows(test):
Expand All @@ -194,11 +194,11 @@ def OCRWindows(test):
return result_text

def OCRTesseract(test):
ocr_result = tesseract.ocr_by_tesseract(assets.path_to_tmp2)
ocr_result = tesseract.run_ocr(assets.path_to_tmp2)
return ocr_result

def OCRRapid(test):
ocr_result = rapid_ocr.ocr_by_rapid(assets.path_to_tmp2)
ocr_result = rapid_ocr.run_ocr(assets.path_to_tmp2)
return ocr_result

list_functions = [OCRGoogle, OCRBaiduu, OCRCapture2Text, OCRGoogleFree, OCRWindows, OCRTesseract, OCRRapid]
Expand Down Expand Up @@ -391,7 +391,7 @@ async def display_translations_ChatGPT(word, language_to="English"):
root.translation_frame_textbox.insert("0.0", "\n\n")
final_result = ""
line_num = 0
async for response in chatGpt.translate_by_chat_gpt_async(word, language_to):
async for response in chatGpt.run_translate_async(word, language_to):
if "\n" in response:
root.translation_frame_textbox.insert(f"{line_num}.0 lineend", response)
line_num += 2
Expand All @@ -406,13 +406,13 @@ def translate(results):
root.loading_icon.start()
translated = ""
if root.option_menu_translation.get() == "GoogleFree":
translated = google_free.translate_by_special_point_google(results, root.combobox_to_language.get())
translated = google_free.run_translate(results, root.combobox_to_language.get())
elif root.option_menu_translation.get() == "DeepL":
translated = deepL.translate_by_special_point_deepL(results, root.combobox_from_language.get(), root.combobox_to_language.get())
translated = deepL.run_translate(results, root.combobox_from_language.get(), root.combobox_to_language.get())
elif root.option_menu_translation.get() == "ChatGPT":
translated = asyncio.run(display_translations_ChatGPT(results, root.combobox_to_language.get()))
elif root.option_menu_translation.get() in ['alibaba', 'argos', 'baidu', 'bing', 'caiyun', 'google', 'iciba', 'iflytek', 'iflyrec', 'itranslate', 'lingvanex', 'mglip', 'modernMt', 'myMemory', 'niutrans', 'papago', 'qqFanyi', 'qqTranSmart', 'reverso', 'sogou', 'translateCom', 'utibet', 'volcEngine', 'yandex', 'youdao']:
translated = multi_translators.translate(results, root.combobox_from_language.get(), root.combobox_to_language.get(), root.option_menu_translation.get())
translated = multi_translators.run_translate(results, root.combobox_from_language.get(), root.combobox_to_language.get(), root.option_menu_translation.get())
if root.switch_results_to_clipboard.get() == 1:
root.clipboard_clear()
root.clipboard_append(translated)
Expand Down Expand Up @@ -486,6 +486,35 @@ def load_hotkey():
load_hotkey()
root.button_start.configure(command=buttonCaptureClick)


async def display_chat_ChatGPT(word):
async for response in chatGpt.run_chat_ai_async(word):
root.textbox_chatgpt_frame.insert('end', response)
root.textbox_chatgpt_frame.insert('end', "\n\n")

def send_message_chat_ai():
# root.loading_icon.start()
message = root.textbox_chat_frame.get(0.0, 'end')
print(message)
root.textbox_chat_frame.delete(0.0, 'end')

root.textbox_chatgpt_frame.insert('end', f"You:\n")
root.textbox_chatgpt_frame.insert('end', f"{message}\n")
root.textbox_chatgpt_frame.insert('end', f"ChatGPT:\n")
asyncio.run(display_chat_ChatGPT(message))
# root.loading_icon.stop()


def send_message_chat_ai_button():
thread = Thread(target=send_message_chat_ai)
thread.start()



root.button_send_message_chat_ai.configure(command=send_message_chat_ai_button)



try:
root.mainloop()
except Exception as e:
Expand Down
6 changes: 3 additions & 3 deletions ocrTranslate/config_files.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from configparser import RawConfigParser

from ocrTranslate.services.baidu import Baidu
from ocrTranslate.services.capture2text import Capture2Text
from ocrTranslate.services.capture_2_text import Capture2Text
from ocrTranslate.services.google_free import GoogleFree
from ocrTranslate.services.google_api import GoogleAPI
from ocrTranslate.services.chatGPT_free3 import ChatGPTFree
from ocrTranslate.services.DeepL import DeepL
from ocrTranslate.services.mult_translators import MultiTranslators
from ocrTranslate.services.deepL import DeepL
from ocrTranslate.services.multi_translators import MultiTranslators
from ocrTranslate.assets import Assets as assets
from ocrTranslate.services.rapid_ocr import RapidOcr

Expand Down
6 changes: 3 additions & 3 deletions ocrTranslate/gui/complex_tk_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ def on_mouse_press(event):
def change_size_textbox(self, event):
cursor_index = self.textbox_chat_frame._textbox.count('1.0', 'end', 'displaylines')[0]
new_height = (cursor_index * 15)+15
print(cursor_index)
print(self.textbox_chat_frame.cget('height'))
#print(cursor_index)
#print(self.textbox_chat_frame.cget('height'))
if new_height != self.textbox_chat_frame.cget('height') and self.textbox_chat_frame.cget('height') <= 105:
if new_height >= 105:
self.textbox_chat_frame.configure(height=105)
Expand Down Expand Up @@ -569,7 +569,7 @@ def load_mass(self, child, config, settings_name):
child.delete(0, "end")
child.insert(0, config[settings_name][self.get_key(str(child))])
if "textbox" in child.winfo_name():
child.delete("1.0", "end")
child.delete("0.0", "end")
child.insert("0.0", base64.b64decode(config[settings_name][self.get_key(str(child))]).decode("utf-8"))
if "radiobutton" in child.winfo_name():
print("radiobutton")
Expand Down
2 changes: 1 addition & 1 deletion ocrTranslate/services/baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def renew_chatbot_session_password(self):
print(e)
print("appid or apikey or secretkey are invalid")

def ocr_by_baidu(self, img) -> str:
def run_ocr(self, img) -> str:
result = self.baidu_client.basicGeneral(img)
result = self.get_result_text(result)
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, path_to_Capture2Text_CLI_exe="", ) -> None:
else:
self.is_active = False

def ocr_by_capture2text(self) -> str:
def run_ocr(self) -> str:
result = subprocess.check_output(self.path_to_Capture2Text_CLI_exe + ' --image {path_to_tmp} '.format(path_to_tmp=assets.path_to_tmp) + '--output-format ${capture}')
return result

59 changes: 40 additions & 19 deletions ocrTranslate/services/chatGPT_free3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import uuid

from revChatGPT.V1 import Chatbot as ChatbotFree
from secrets import compare_digest

from ocrTranslate.utils import format_words, format_words2


class ChatGPTFree:
def __init__(self, email="", password="", session_token="", access_token="") -> None:
Expand Down Expand Up @@ -45,47 +49,64 @@ def renew_chatbot_access_token(self):
print(e)
print("access token has been out of date, please renew token")

async def translate_by_chat_gpt_async(self, word, language_to="English"):
async def run_translate_async(self, word, language_to="English"):
if self.is_active:
words = ""
if type(word) != str:
for i in word:
if i != "":
words = words + i + "\n"
else:
words = word
words = format_words(word)
prev_text = ""
for data in self.chat_gpt_free.ask("Just translate the following sentence into {languageInText}, without any explanation and write only the translated sentence: {wordInText}".format(wordInText=words, languageInText=language_to)):
for data in self.chat_gpt_free.ask("Just translate the following sentence into {languageInText}, without any explanation and write only the translated sentence:\n{wordInText}".format(wordInText=words, languageInText=language_to)):
response = data["message"][len(prev_text):]
prev_text = data["message"]
yield response
else:
yield "chatgpt non valid user date"

def translate_by_chat_gpt(self, word, language_to="English", prompt=""):
def run_translate(self, word, language_to="English", prompt=""):
print("translate_by_chat_gpt")
if self.is_active:
words = ""
if type(word) != str:
for i in word:
if i != "":
words = words + i + "\n"
else:
words = word

words = format_words(word)
if prompt == "":
prompt = "Just translate the following sentence into {languageInText}, without any explanation and write only the translated sentence: {wordInText}".format(wordInText=words, languageInText=language_to)
else:
prompt = prompt + " " + words

response = ""
prev_text = ""
for data in self.chat_gpt_free.ask(prompt):
#print(self.chat_gpt_free.conversation_id)
#print(self.chat_gpt_free.parent_id)

for data in self.chat_gpt_free.ask(prompt=prompt, conversation_id=str(uuid.uuid4())):
response = data["message"]
message = data["message"][len(prev_text):]
print(message, end="", flush=True)
prev_text = data["message"]
print("")
return response
else:
return "chatgpt non valid user date"

def run_chat_ai(self, prompt=""):
print("run_chat_ai")
if self.is_active:
response = ""
prev_text = ""

for data in self.chat_gpt_free.ask(prompt=prompt, conversation_id=str(uuid.uuid4())):
response = data["message"]
message = data["message"][len(prev_text):]
print(message, end="", flush=True)
prev_text = data["message"]
print("")
return response
else:
return "chatgpt non valid user date"

async def run_chat_ai_async(self, prompt=""):
if self.is_active:
prev_text = ""
for data in self.chat_gpt_free.ask(prompt=prompt):
response = data["message"][len(prev_text):]
prev_text = data["message"]
yield response
print(self.chat_gpt_free.conversation_id)
else:
yield "chatgpt non valid user date"
21 changes: 12 additions & 9 deletions ocrTranslate/services/DeepL.py → ocrTranslate/services/deepL.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import deepl
from ocrTranslate.langs import _langs
from ocrTranslate.utils import format_words


class DeepL:
Expand All @@ -14,7 +15,7 @@ def __init__(self) -> None:
print(e)

# deepl-translate library
def translate_by_special_point_deepL_old(self, word, language_from="english", language_to="polish"):
def run_translate_old(self, word, language_from="english", language_to="polish"):
words = ""
if type(word) != str:
for i in word:
Expand All @@ -28,20 +29,22 @@ def translate_by_special_point_deepL_old(self, word, language_from="english", la
return result

# deepl-cli library
def translate_by_special_point_deepL(self, word, language_from="english", language_to="polish"):
def run_translate(self, word, language_from="english", language_to="polish"):
_language_to = list(_langs.keys())[list(_langs.values()).index(language_to)]
_language_from = list(_langs.keys())[list(_langs.values()).index(language_from)]
words = ""
if type(word) != str:
for i in word:
if i != "":
words = words + i + "\n"
else:
words = word
words = format_words(word)
self.deepl_init.to_lang = _language_to
self.deepl_init.from_lang = _language_from
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
result = self.deepl_init.translate(words)
return result

async def run_translate_async(self, word, language_from="english", language_to="polish"):
_language_to = list(_langs.keys())[list(_langs.values()).index(language_to)]
_language_from = list(_langs.keys())[list(_langs.values()).index(language_from)]
words = format_words(word)
self.deepl_init.to_lang = _language_to
self.deepl_init.from_lang = _language_from
return await self.deepl_init.translate_async(words)

4 changes: 2 additions & 2 deletions ocrTranslate/services/google_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def text_to_wav(self, voice_name: str, text: str):

# text_to_wav("pl-PL-Wavenet-B", "jakis tekst do powiedzenia a co?")

def ocr_by_google_api(self, image: Image) -> str:
def run_ocr(self, image: Image) -> str:
"""Detect text from PIL.Image data using Google Cloud Translate."""

# Create bytestream of the given image
Expand Down Expand Up @@ -88,7 +88,7 @@ def _calc_height(word):
'''
return "{}".format(text)

def translate_by_google_api(self, text: str, target_language: str = 'en', source_language: str = 'ja') -> str:
def run_translate(self, text: str, target_language: str = 'en', source_language: str = 'ja') -> str:
"""
from google.cloud import translate_v2 as translate2
translate_client2 = translate2.Client(credentials=credentials)
Expand Down
15 changes: 4 additions & 11 deletions ocrTranslate/services/google_free.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pyshadow.main import Shadow

from ocrTranslate.assets import Assets as assets
from ocrTranslate.utils import list_to_string
from ocrTranslate.utils import list_to_string, format_words
from ocrTranslate.langs import _langs


Expand Down Expand Up @@ -100,17 +100,10 @@ def refresh_session(self):

self.show_captcha(driver)

def translate_by_special_point_google(self, word, language_to: str = "English") -> str:

def run_translate(self, word, language_to: str = "English") -> str:
_language_to = list(_langs.keys())[list(_langs.values()).index(language_to)]
words = ""
if type(word) != str:
for i in word:
if i != "":
words = words + i + "%0A"
else:
words = word

words = format_words(word)
url = "https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl={language_to}&q=".format(language_to=_language_to) + words
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}

Expand Down Expand Up @@ -143,7 +136,7 @@ def translate_by_special_point_google(self, word, language_to: str = "English")
result = list_to_string(list_of_words_translated)
return result

def ocr_google_free(self, img) -> str:
def run_ocr(self, img) -> str:
image = base64.b64encode(img.read()).decode('utf-8')

token = "03AG"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
from ocrTranslate.langs import _langs
import translators as ts

from ocrTranslate.utils import format_words


class MultiTranslators:
def __init__(self) -> None:
print(ts.translators_pool)
#ts.preaccelerate()

def translate(self, word, language_from="auto", language_to="english", translator="bing"):
def run_translate(self, word, language_from="auto", language_to="english", translator="bing"):
_language_to = list(_langs.keys())[list(_langs.values()).index(language_to)]
_language_from = list(_langs.keys())[list(_langs.values()).index(language_from)]
words = ""
if type(word) != str:
for i in word:
if i != "":
words = words + i + "\n"
else:
words = word

print(ts.translators_pool)
words = format_words(word)
#print(ts.translators_pool)
return ts.translate_text(query_text=words, translator=translator, from_language=_language_from, to_language=_language_to)
2 changes: 1 addition & 1 deletion ocrTranslate/services/rapid_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class RapidOcr:
def __init__(self) -> None:
self.rapid_ocr = RapidOCR()

def ocr_by_rapid(self, path: str) -> str:
def run_ocr(self, path: str) -> str:
result, elapse = self.rapid_ocr(path)
text_result = ""

Expand Down

0 comments on commit 4095b1d

Please sign in to comment.