-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_handler.py
More file actions
64 lines (49 loc) · 2.01 KB
/
command_handler.py
File metadata and controls
64 lines (49 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import json
from tkinter import messagebox
from chat import ask_chat_gpt
QA_STORAGE_FILE = "qa_storage.json"
def load_stored_answers():
"""Loads stored Q&A pairs from the JSON file."""
try:
with open(QA_STORAGE_FILE, "r") as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
return {}
def save_stored_answers(data):
"""Saves Q&A pairs to the JSON file."""
with open(QA_STORAGE_FILE, "w") as file:
json.dump(data, file, indent=4)
def execute_command(event, command_entry, text_area, response_box):
"""Processes and executes commands entered in the command line."""
command = command_entry.get().strip().lower()
if command == "search":
content = text_area.get(1.0, "end")
matches = re.findall(r"~\((.*?)\)~", content) # Find all questions
if not matches:
messagebox.showinfo("Search", "No questions found in the text.")
return
stored_answers = load_stored_answers()
response_box.config(state="normal")
response_box.delete("1.0", "end")
for question in matches:
if question in stored_answers:
response = stored_answers[question]
else:
response = ask_chat_gpt(question, response_box)
stored_answers[question] = response # Save new answer
# Display each question-answer pair
response_box.insert("end", f"Q: {question}\nA: {response}\n\n")
response_box.config(state="disabled")
save_stored_answers(stored_answers) # Save updated data
elif command == "clear":
text_area.delete(1.0, "end")
messagebox.showinfo("Command Executed", "Text cleared!")
elif command == "help":
print("Still working on this")
pass
elif command == "exit":
text_area.quit()
else:
messagebox.showerror("Invalid Command", f"Unknown command: {command}")
command_entry.delete(0, "end") # Clear command input