Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import openai
- import os
- import tkinter as tk
- from tkinter import filedialog, scrolledtext
- from tkinter.ttk import Progressbar
- from typing import Tuple
- openai.api_key = "your_api_key_here"
- def check_text(text: str) -> Tuple[str, str]:
- response = openai.Completion.create(
- engine="text-davinci-002",
- prompt=f"Please correct the following text for grammar, punctuation, and typos:\n\n{text}",
- max_tokens=100,
- n=1,
- stop=None,
- temperature=0.7,
- )
- corrected_text = response.choices[0].text.strip()
- changes = "Changes:\n"
- if corrected_text == text:
- changes += "No changes were made.\n"
- else:
- changes += f"Original: {text}\nCorrected: {corrected_text}\n"
- return corrected_text, changes
- def process_file(file_path: str, progress_bar, status_label, changes_textbox):
- with open(file_path, "r") as file:
- text = file.read()
- output_file_path = os.path.splitext(file_path)[0] + "_corrected.txt"
- changes_file_path = os.path.splitext(file_path)[0] + "_changes.txt"
- corrected_sections = []
- changes_list = []
- total_sections = (len(text) - 1) // 500 + 1
- progress_bar["maximum"] = total_sections
- for i in range(0, len(text), 500):
- section = text[i : i + 500]
- corrected_section, changes = check_text(section)
- corrected_sections.append(corrected_section)
- changes_list.append(changes)
- progress_bar["value"] += 1
- progress_bar.update()
- changes_textbox.insert(tk.END, changes)
- changes_textbox.yview(tk.END)
- with open(output_file_path, "w") as output_file:
- output_file.write("".join(corrected_sections))
- with open(changes_file_path, "w") as changes_file:
- changes_file.write("\n".join(changes_list))
- status_label.config(text="Done!")
- def browse_file(progress_bar, status_label, changes_textbox):
- file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
- if file_path:
- status_label.config(text="Changes loading...")
- progress_bar["value"] = 0
- progress_bar.update()
- process_file(file_path, progress_bar, status_label, changes_textbox)
- def main():
- root = tk.Tk()
- root.title("Grammar and Typo Checker")
- load_button = tk.Button(root, text="Load Text File", command=lambda: browse_file(progress_bar, status_label, changes_textbox))
- load_button.pack(pady=10)
- status_label = tk.Label(root, text="")
- status_label.pack(pady=5)
- progress_bar = Progressbar(root, orient="horizontal", mode="determinate")
- progress_bar.pack(pady=5, padx=10, fill=tk.X)
- changes_label = tk.Label(root, text="Changes:")
- changes_label.pack(pady=5)
- changes_textbox = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=80, height=20)
- changes_textbox.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
- root.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Comments
-
- This script requires you to have an open ai key
- And python installed aswell as the open ai library
-
- thank you
Add Comment
Please, Sign In to add comment
Advertisement