in this script, we monitor the clipboard and if there's a new text copied in it, the script will automatically translate it using gpt. the translated text will either be shown on the shell, copied to clipboard, or keep it in a logs depending on the user's settings, the script also create a window and showed only the translated text in there. i want you make the window to also show the untranslated texts (raw from clipboard), separate the untranslated text’s textbox and the translated text’s textbox. make a button that makes the script to retranslate the text in the untranslated textbox if the text in there was edited. do not remove clipboard monitor. here’s the full script:

import time
import pyperclip
import openai
import sys
import keyboard
import json
import tkinter as tk
import threading
from tkinter import font, Menu

# Set your OpenAI API key and base
openai.api_base = “”
openai.api_key = “”

# Toggleable features
PRINT_TRANSLATION = False
COPY_TO_CLIPBOARD = False
KEEP_LOGS = True

# Initialize dictionary to store translated texts
translated_dict = {}

# Define JSON file to store translations
jsonfile = “translations.json”

# Load existing translations from JSON file
try:
with open(jsonfile, “r”, encoding=“utf-8”) as f:
translated_dict = json.load(f)
except FileNotFoundError:
pass

def translate_text(text_to_translate):
# Sends the input text to GPT-3 with a simple translation prompt
prompt = f"Translate the following text:{text_to_translate}“

try:
completion = openai.ChatCompletion.create(
model=“gpt-3.5”,
messages=[
{
“role”: “user”,
“content”: f”{prompt}“,
}
],
)
translated_text = (
completion[“choices”][0]
.get(“message”)
.get(“content”)
.encode(“utf8”)
.decode()
)

except Exception as e:
print(e, f"No credits found in key. Exiting…”)
sys.exit()

return translated_text

def update_display(text):
display.delete(1.0, tk.END)
display.insert(tk.END, text)

def clipboard_monitor():
old_clipboard = “”
exit_flag = False

while not exit_flag:
new_clipboard = pyperclip.paste()

# Compare new_clipboard with old_clipboard to detect changes
if new_clipboard != old_clipboard:
# Update the old_clipboard variable
old_clipboard = new_clipboard
# Check if the copied text has already been translated
if new_clipboard in translated_dict:
translated_text = translated_dict[new_clipboard]
else:
translated_text = translate_text(new_clipboard)
# Store the translation in the dictionary and the JSON file
translated_dict[new_clipboard] = translated_text
with open(jsonfile, “w”, encoding=“utf-8”) as f:
json.dump(translated_dict, f, ensure_ascii=False, indent=4)

if PRINT_TRANSLATION:
print(f"\nTranslation:\n{translated_text}“)

if COPY_TO_CLIPBOARD:
pyperclip.copy(translated_text)

update_display(translated_text)

# Check for keyboard input every 1 second
if keyboard.is_pressed(‘esc’):
exit_flag = True
sys.exit()

time.sleep(1)

def toggle_opacity():
if root.attributes(”-alpha") == 1.0:
root.attributes(“-alpha”, 0.5)
else:
root.attributes(“-alpha”, 1.0)

def toggle_always_on_top():
if root.wm_attributes(“-topmost”) == 0:
root.wm_attributes(“-topmost”, True)
else:
root.wm_attributes(“-topmost”, False)

def create_menu(window):
menu = Menu(window)
window.config(menu=menu)
options_menu = Menu(menu, tearoff=0)
menu.add_cascade(label=“Options”, menu=options_menu)
options_menu.add_checkbutton(label=“Adjust Opacity”, command=toggle_opacity)
options_menu.add_checkbutton(label=“Toggle Always On Top”, command=toggle_always_on_top)

root = tk.Tk()
root.title(“Clipboard Translation Monitor”)
root.geometry(“400x200”)

display_font = font.Font(family=“Arial”, size=12) # Set font and size
display = tk.Text(root, wrap=tk.WORD, font=display_font, bg=“white”, fg=“black”) # Set initial background and font color
display.pack(expand=True, fill=tk.BOTH)

if name == “main”:
print(“Starting Clipboard Translation Monitor…\n”)
print(“Press Esc to exit\n”)
clipboard_monitor_thread = threading.Thread(target=clipboard_monitor)
create_menu(root)
clipboard_monitor_thread.start()
root.mainloop()