so this script monitor the clipboard for any changes and translate copied japanese text…
- User
so this script monitor the clipboard for any changes and translate copied japanese text to english using gpt and then show the translation on a created window. unfortunately the OAI endpoint we are going to use have a filter that rejects any prompts with chinese letters, and this dragged japanese kanjis along so we can’t have any prompts with japanese kanjis. i want you to make the script to encode the japanese text using html encode before sending it, and then giving prompt to gpt to decode the html before translating it.
import time import pyperclip import openai import json import tkinter as tk import threading from tkinter import font, Menu from tkinter import font
Set your OpenAI API key and base
openai.api_base = “” openai.api_key = “”
Toggleable features
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): prompt = f"Translate the following Japanese text to English:{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, “No credits found in key. Close the window to exit…”)
return translated_text
def update_display(display, text): display.delete(1.0, tk.END) display.insert(tk.END, text)
def retranslate(): untranslated_text = untranslated_textbox.get(1.0, tk.END).strip() if untranslated_text: pyperclip.copy(untranslated_text)
def clipboard_monitor(): old_clipboard = “”
while True: new_clipboard = pyperclip.paste()
Compare new_clipboard with old_clipboard to detect changes
if new_clipboard != old_clipboard: old_clipboard = new_clipboard if new_clipboard in translated_dict: translated_text = translated_dict[new_clipboard] else: translated_text = translate_text(new_clipboard)
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)
update_display(untranslated_textbox, new_clipboard) update_display(translated_textbox, translated_text)
time.sleep(1)
def toggle_translated(): current_bg = translated_textbox.cget(“background”) if current_bg == “black”: translated_textbox.configure(background=“white”) translated_textbox.delete(1.0, tk.END) translated_textbox.insert(tk.END, translated_dict[untranslated_textbox.get(1.0, tk.END).strip()]) else: translated_textbox.configure(background=“black”) translated_textbox.delete(1.0, tk.END)
def set_always_on_top(): root.attributes(”-topmost", always_on_top_var.get())
def increase_opacity(): opacity = root.attributes(“-alpha”) opacity = min(opacity + 0.1, 1.0) root.attributes(“-alpha”, opacity)
def decrease_opacity(): opacity = root.attributes(“-alpha”) opacity = max(opacity - 0.1, 0.1) root.attributes(“-alpha”, opacity)
def font_window(): global current_font current_font = { “family”: font.Font(font=untranslated_textbox[“font”]).actual(“family”), “size”: font.Font(font=untranslated_textbox[“font”]).actual(“size”), “color”: untranslated_textbox.cget(“foreground”) }
font_window = tk.Toplevel() font_window.title(“Font Settings”) font_window.geometry(“150x150”)
font_family = tk.StringVar() font_family_label = tk.Label(font_window, text=“Font Family”) font_family_label.pack() font_family_entry = tk.Entry(font_window, textvariable=font_family) font_family_entry.insert(0, current_font[“family”]) font_family_entry.pack()
font_size = tk.StringVar() font_size_label = tk.Label(font_window, text=“Font Size”) font_size_label.pack() font_size_entry = tk.Entry(font_window, textvariable=font_size) font_size_entry.insert(0, current_font[“size”]) font_size_entry.pack()
font_color = tk.StringVar() font_color_label = tk.Label(font_window, text=“Font Color”) font_color_label.pack() font_color_entry = tk.Entry(font_window, textvariable=font_color) font_color_entry.insert(0, current_font[“color”]) font_color_entry.pack()
def apply_changes(): new_font_family = font_family.get() or current_font[“family”] new_font_size = font_size.get() or current_font[“size”] new_font_color = font_color.get() or current_font[“color”] current_font = {“family”: new_font_family, “size”: new_font_size, “color”: new_font_color}
untranslated_textbox.configure(font=(current_font[“family”], current_font[“size”]), fg=current_font[“color”]) translated_textbox.configure(font=(current_font[“family”], current_font[“size”]), fg=current_font[“color”]) font_window.destroy()
apply_button = tk.Button(font_window, text=“Apply”, command=apply_changes) apply_button.pack()
cancel_button = tk.Button(font_window, text=“Cancel”, command=font_window.destroy) cancel_button.pack()
root = tk.Tk() root.title(“Clipboard Translation Monitor”) root.geometry(“400x200”)
menu_bar = Menu(root)
Add Options menu
options_menu = Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label=“Options”, menu=options_menu)
Add Always on top option to Options menu
always_on_top_var = tk.BooleanVar(value=False) options_menu.add_checkbutton(label=“Always on top”, variable=always_on_top_var, command=set_always_on_top)
Add Increase opacity option to Options menu
options_menu.add_command(label=“Increase opacity”, command=increase_opacity)
Add Decrease opacity option to Options menu
options_menu.add_command(label=“Decrease opacity”, command=decrease_opacity)
Add Font option to Options menu
options_menu.add_command(label=“Font”, command=font_window)
root.config(menu=menu_bar)
paned_window = tk.PanedWindow(root, sashrelief=“groove”, sashwidth=5, orient=“horizontal”) paned_window.pack(expand=True, fill=“both”)
untranslated_textbox = tk.Text(paned_window, wrap=“word”) paned_window.add(untranslated_textbox)
translated_textbox = tk.Text(paned_window, wrap=“word”) paned_window.add(translated_textbox)
retranslate_button = tk.Button(root, text=“Copy/Retranslate”, command=retranslate) retranslate_button.place(relx=0, rely=1, anchor=“sw”)
toggle_button = tk.Button(root, text=“Hide/Show Translated Text”, command=toggle_translated) toggle_button.place(relx=1, rely=1, anchor=“se”)
if name == “main”: print(“Starting Clipboard Translation Monitor…\n”) clipboard_monitor_thread = threading.Thread(target=clipboard_monitor) clipboard_monitor_thread.start() root.mainloop()
- Assistant
I've modified the script to encode the Japanese text using HTML encoding before sending it and to give the prompt to GPT to decode the HTML before translating it.
import time import pyperclip import openai import json import tkinter as tk import threading from tkinter import font, Menu from html import unescape, escape # Set your OpenAI API key and base openai.api_base = "" openai.api_key = "" # Toggleable features 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): escaped_text = escape(text_to_translate) prompt = f"Decode following HTML encoded text from Japanese to English: {escaped_text}" 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, "No credits found in key. Close the window to exit...") return translated_text # The rest of the script remains unchanged.This modification first escapes Japanese text using
html.escape()function, sends the HTML encoded text as prompt to GPT-3, and then continues with the rest of the script as it was.