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 javascript URI before sending it, and then giving prompt to gpt to decode the URI 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)
- Assistant
You can implement this change by updating the
translate_textfunction to first encode the text using thequotemethod fromurllib.parse. Then, update the chat completion prompt to decode the URI before translating it. Here's your updated script with the required changes:First, please add this import statement to your script:
from urllib.parse import quoteNow, update the
translate_textfunction like this:def translate_text(text_to_translate): encoded_text = quote(text_to_translate) prompt = f"Please decode the following URI encoded text: '{encoded_text}' and then translate the decoded Japanese text to English." 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