|
| 1 | +import tkinter as tk |
| 2 | +from tkinter.font import Font |
| 3 | +from tkinter import messagebox |
| 4 | +from tkinter import filedialog |
| 5 | +from ThirdAI import NeuralDBClient as Ndb |
| 6 | + |
| 7 | + |
| 8 | +class ThirdAIApp: |
| 9 | + """ |
| 10 | + A GUI application for using the ThirdAI neural database client to train and query data. |
| 11 | + """ |
| 12 | + def __init__(self, root): |
| 13 | + """ |
| 14 | + Initialize the user interface window. |
| 15 | +
|
| 16 | + Args: |
| 17 | + root (tk.Tk): The main Tkinter window. |
| 18 | + """ |
| 19 | + # Initialize the main window |
| 20 | + self.root = root |
| 21 | + self.root.geometry("600x500") |
| 22 | + self.root.title('ThirdAI - T&C') |
| 23 | + |
| 24 | + # Initialize variables |
| 25 | + self.path = [] |
| 26 | + self.client = Ndb() |
| 27 | + |
| 28 | + # GUI elements |
| 29 | + |
| 30 | + # Labels and buttons |
| 31 | + self.menu = tk.Label(self.root, text="Terms & Conditions", font=self.custom_font(30), fg='black', |
| 32 | + highlightthickness=2, highlightbackground="red") |
| 33 | + self.menu.place(x=125, y=10) |
| 34 | + |
| 35 | + self.insert_button = tk.Button(self.root, text="Insert File!", font=self.custom_font(15), fg='black', bg="grey", |
| 36 | + width=10, command=self.file_input) |
| 37 | + self.insert_button.place(x=245, y=100) |
| 38 | + |
| 39 | + self.text_box = tk.Text(self.root, wrap=tk.WORD, width=30, height=1) |
| 40 | + self.text_box.place(x=165, y=150) |
| 41 | + |
| 42 | + self.training_button = tk.Button(self.root, text="Training", font=self.custom_font(15), fg='black', bg="grey", |
| 43 | + width=10, command=self.training) |
| 44 | + self.training_button.place(x=245, y=195) |
| 45 | + |
| 46 | + self.query_label = tk.Label(self.root, text="Query", font=self.custom_font(20), fg='black') |
| 47 | + self.query_label.place(x=255, y=255) |
| 48 | + |
| 49 | + self.query_entry = tk.Entry(self.root, font=self.custom_font(20), width=30) |
| 50 | + self.query_entry.place(x=70, y=300) |
| 51 | + |
| 52 | + self.processing_button = tk.Button(self.root, text="Processing", font=self.custom_font(15), fg='black', |
| 53 | + bg="grey", width=10, command=self.processing) |
| 54 | + self.processing_button.place(x=245, y=355) |
| 55 | + |
| 56 | + self.clear_button = tk.Button(self.root, text="Clear", font=15, fg='black', bg="grey", width=10, |
| 57 | + command=self.clear_all) |
| 58 | + self.clear_button.place(x=245, y=405) |
| 59 | + |
| 60 | + @staticmethod |
| 61 | + def custom_font(size): |
| 62 | + """ |
| 63 | + Create a custom font with the specified size. |
| 64 | +
|
| 65 | + Args: |
| 66 | + size (int): The font size. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Font: The custom Font object. |
| 70 | + """ |
| 71 | + return Font(size=size) |
| 72 | + |
| 73 | + def file_input(self): |
| 74 | + """ |
| 75 | + Open a file dialog to select a PDF file and display its name in the text box. |
| 76 | + """ |
| 77 | + file_type = dict(defaultextension=".pdf", filetypes=[("pdf file", "*.pdf")]) |
| 78 | + file_path = filedialog.askopenfilename(**file_type) |
| 79 | + |
| 80 | + if file_path: |
| 81 | + self.path.append(file_path) |
| 82 | + file_name = file_path.split("/")[-1] |
| 83 | + self.text_box.delete(1.0, tk.END) |
| 84 | + self.text_box.insert(tk.INSERT, file_name) |
| 85 | + |
| 86 | + def clear_all(self): |
| 87 | + """ |
| 88 | + Clear the query entry, text box, and reset the path. |
| 89 | + """ |
| 90 | + self.query_entry.delete(0, tk.END) |
| 91 | + self.text_box.delete(1.0, tk.END) |
| 92 | + self.path.clear() |
| 93 | + |
| 94 | + def training(self): |
| 95 | + """ |
| 96 | + Train the neural database client with the selected PDF file. |
| 97 | + """ |
| 98 | + if not self.path: |
| 99 | + messagebox.showwarning("No File Selected", "Please select a PDF file before training.") |
| 100 | + return |
| 101 | + |
| 102 | + self.client.train(self.path[0]) |
| 103 | + |
| 104 | + messagebox.showinfo("Training Complete", "Training is done!") |
| 105 | + |
| 106 | + def processing(self): |
| 107 | + """ |
| 108 | + Process a user query and display the output in a new window. |
| 109 | + """ |
| 110 | + question = self.query_entry.get() |
| 111 | + |
| 112 | + # When there is no query submitted by the user |
| 113 | + if not question: |
| 114 | + messagebox.showwarning("No Query", "Please enter a query.") |
| 115 | + return |
| 116 | + |
| 117 | + output = self.client.query(question) |
| 118 | + self.display_output(output) |
| 119 | + |
| 120 | + def display_output(self, output_data): |
| 121 | + """ |
| 122 | + Display the output data in a new window. |
| 123 | +
|
| 124 | + Args: |
| 125 | + output_data (str): The output text to be displayed. |
| 126 | + """ |
| 127 | + output_window = tk.Toplevel(self.root) |
| 128 | + output_window.title("Output Data") |
| 129 | + output_window.geometry("500x500") |
| 130 | + |
| 131 | + output_text = tk.Text(output_window, wrap=tk.WORD, width=50, height=50) |
| 132 | + output_text.pack(padx=10, pady=10) |
| 133 | + output_text.insert(tk.END, output_data) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + """ |
| 138 | + Initializing the main application window |
| 139 | + """ |
| 140 | + |
| 141 | + # Calling the main application window |
| 142 | + win = tk.Tk() |
| 143 | + app = ThirdAIApp(win) |
| 144 | + win.mainloop() |
0 commit comments