|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import font as tkfont |
| 3 | +import threading |
| 4 | +import torch |
| 5 | +import yaml |
| 6 | +from gui_table import create_table, create_pro_tip |
| 7 | +from metrics_gpu import GPU_Monitor |
| 8 | +from metrics_system import SystemMonitor |
| 9 | +import platform |
| 10 | + |
| 11 | +METRICS_STYLE = { |
| 12 | + 'font': ("Segoe UI Semibold", 12), |
| 13 | + 'bg': "#202123" |
| 14 | +} |
| 15 | +BUTTON_STYLE = { |
| 16 | + 'width': 29, |
| 17 | + 'bg': "#323842", |
| 18 | + 'fg': "light gray", |
| 19 | + 'font': ("Segoe UI Historic", 10) |
| 20 | +} |
| 21 | +FRAME_STYLE = { |
| 22 | + 'highlightthickness': 0, |
| 23 | + 'relief': 'flat', |
| 24 | + 'borderwidth': 0 |
| 25 | +} |
| 26 | + |
| 27 | +def determine_compute_device(): |
| 28 | + if torch.cuda.is_available(): |
| 29 | + COMPUTE_DEVICE = "cuda" |
| 30 | + elif torch.backends.mps.is_available(): |
| 31 | + COMPUTE_DEVICE = "mps" |
| 32 | + else: |
| 33 | + COMPUTE_DEVICE = "cpu" |
| 34 | + |
| 35 | + with open("config.yaml", 'r') as stream: |
| 36 | + config_data = yaml.safe_load(stream) |
| 37 | + |
| 38 | + config_data['COMPUTE_DEVICE'] = COMPUTE_DEVICE |
| 39 | + |
| 40 | + with open("config.yaml", 'w') as stream: |
| 41 | + yaml.safe_dump(config_data, stream) |
| 42 | + |
| 43 | + return COMPUTE_DEVICE |
| 44 | + |
| 45 | +def is_nvidia_gpu(): |
| 46 | + if torch.cuda.is_available(): |
| 47 | + gpu_name = torch.cuda.get_device_name(0) |
| 48 | + return "nvidia" in gpu_name.lower() |
| 49 | + return False |
| 50 | + |
| 51 | + |
| 52 | +class DocQA_GUI: |
| 53 | + def __init__(self, root): |
| 54 | + self.root = root |
| 55 | + |
| 56 | + main_pane = tk.PanedWindow(root, orient=tk.HORIZONTAL, bg="#202123") |
| 57 | + main_pane.pack(fill=tk.BOTH, expand=1) |
| 58 | + |
| 59 | + # LEFT FRAME |
| 60 | + left_frame = tk.Frame(main_pane, bg="#202123", **FRAME_STYLE) |
| 61 | + |
| 62 | + # 1. TABLE |
| 63 | + create_table(left_frame) |
| 64 | + |
| 65 | + # 2. PRO TIP |
| 66 | + create_pro_tip(left_frame) |
| 67 | + |
| 68 | + # 3. METRICS |
| 69 | + self.gpu_info_label = tk.Label( |
| 70 | + left_frame, **METRICS_STYLE, fg='#00c600') |
| 71 | + self.gpu_info_label.pack(pady=0, padx=1) |
| 72 | + |
| 73 | + self.vram_info_label = tk.Label( |
| 74 | + left_frame, **METRICS_STYLE, fg='violet red') |
| 75 | + self.vram_info_label.pack(pady=0, padx=1) |
| 76 | + |
| 77 | + self.ram_usage_label = tk.Label( |
| 78 | + left_frame, **METRICS_STYLE, fg='sky blue') |
| 79 | + self.ram_usage_label.pack(pady=0, padx=1) |
| 80 | + |
| 81 | + self.ram_used_label = tk.Label( |
| 82 | + left_frame, **METRICS_STYLE, fg='medium purple') |
| 83 | + self.ram_used_label.pack(pady=0, padx=1) |
| 84 | + |
| 85 | + self.cpu_usage_label = tk.Label( |
| 86 | + left_frame, **METRICS_STYLE, fg='gold') |
| 87 | + self.cpu_usage_label.pack(pady=0, padx=1) |
| 88 | + |
| 89 | + compute_device = determine_compute_device() |
| 90 | + os_name = platform.system().lower() |
| 91 | + |
| 92 | + if compute_device != "mps" and os_name == "windows" and is_nvidia_gpu(): |
| 93 | + self.cuda_logic = GPU_Monitor( |
| 94 | + self.vram_info_label, self.gpu_info_label, self.root) |
| 95 | + self.system_monitor = SystemMonitor( |
| 96 | + self.cpu_usage_label, self.ram_used_label, self.ram_usage_label, self.root) |
| 97 | + else: |
| 98 | + self.cuda_logic = None |
| 99 | + self.system_monitor = None |
| 100 | + |
| 101 | + # 4. BUTTONS |
| 102 | + self.download_embedding_model_button = tk.Button( |
| 103 | + left_frame, text="Download Embedding Model", **BUTTON_STYLE) |
| 104 | + self.download_embedding_model_button.pack(pady=2) |
| 105 | + |
| 106 | + self.select_embedding_model_button = tk.Button( |
| 107 | + left_frame, text="Select Embedding Model Directory", **BUTTON_STYLE) |
| 108 | + self.select_embedding_model_button.pack(pady=2) |
| 109 | + |
| 110 | + self.choose_documents_button = tk.Button( |
| 111 | + left_frame, text="Choose Documents for Database", **BUTTON_STYLE) |
| 112 | + self.choose_documents_button.pack(pady=2) |
| 113 | + |
| 114 | + self.create_chromadb_button = tk.Button( |
| 115 | + left_frame, text="Create Vector Database", **BUTTON_STYLE) |
| 116 | + self.create_chromadb_button.pack(pady=2) |
| 117 | + main_pane.add(left_frame) |
| 118 | + |
| 119 | + # RIGHT FRAME |
| 120 | + right_frame = tk.Frame(main_pane, bg="#202123", **FRAME_STYLE) |
| 121 | + main_pane.add(right_frame) |
| 122 | + |
| 123 | + # For displaying the answer |
| 124 | + middle_frame = tk.Frame(right_frame, bg="#1e263c", **FRAME_STYLE) |
| 125 | + middle_frame.pack(pady=5, fill=tk.BOTH, expand=1) |
| 126 | + |
| 127 | + self.read_only_text = tk.Text( |
| 128 | + middle_frame, wrap=tk.WORD, state=tk.DISABLED, bg="#1e263c", fg="light gray") |
| 129 | + self.read_only_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=1) |
| 130 | + self.read_only_text.configure(font=("Segoe UI Historic", 12)) |
| 131 | + |
| 132 | + scroll2 = tk.Scrollbar( |
| 133 | + middle_frame, command=self.read_only_text.yview) |
| 134 | + scroll2.pack(side=tk.RIGHT, fill=tk.Y) |
| 135 | + self.read_only_text.config(yscrollcommand=scroll2.set) |
| 136 | + |
| 137 | + # For inputting the question |
| 138 | + bottom_frame = tk.Frame(right_frame) |
| 139 | + bottom_frame.pack(pady=5, fill=tk.BOTH, expand=1) |
| 140 | + |
| 141 | + self.text_input = tk.Text(bottom_frame, wrap=tk.WORD, height=5, bg="#2a2b2e", fg="light gray") |
| 142 | + self.text_input.pack(side=tk.LEFT, fill=tk.BOTH, expand=1) |
| 143 | + self.text_input.configure(font=("Segoe UI Historic", 13)) |
| 144 | + |
| 145 | + scroll1 = tk.Scrollbar(bottom_frame, command=self.text_input.yview) |
| 146 | + scroll1.pack(side=tk.RIGHT, fill=tk.Y) |
| 147 | + self.text_input.config(yscrollcommand=scroll1.set) |
| 148 | + |
| 149 | + self.submit_query_button = tk.Button( |
| 150 | + right_frame, text="Submit Question", width=29, bg="#323842", fg="light gray", font=("Segoe UI Historic", 10)) |
| 151 | + self.submit_query_button.pack(pady=5, side=tk.TOP) |
| 152 | + |
| 153 | + def center_window(self, root): |
| 154 | + root.withdraw() |
| 155 | + root.update_idletasks() |
| 156 | + |
| 157 | + width = root.winfo_width() |
| 158 | + height = root.winfo_height() |
| 159 | + x = (root.winfo_screenwidth() // 2) - (width // 2) |
| 160 | + y = (root.winfo_screenheight() // 2) - (height // 2) |
| 161 | + |
| 162 | + root.geometry('{}x{}+{}+{}'.format(width, height, x, y)) |
| 163 | + root.deiconify() |
| 164 | + |
| 165 | + def stop_and_exit(self): |
| 166 | + if self.cuda_logic: |
| 167 | + self.cuda_logic.stop_and_exit_gpu_monitor() |
| 168 | + if self.system_monitor: |
| 169 | + self.system_monitor.stop_and_exit_system_monitor() |
| 170 | + self.root.quit() |
| 171 | + self.root.destroy() |
| 172 | + |
| 173 | + def start_up(self): |
| 174 | + self.center_window(self.root) |
| 175 | + self.root.protocol("WM_DELETE_WINDOW", self.stop_and_exit) |
| 176 | + self.root.mainloop() |
| 177 | + |
| 178 | +if __name__ == "__main__": |
| 179 | + from gui_logic import DocQA_Logic |
| 180 | + root = tk.Tk() |
| 181 | + root.title("LM Studio ChromaDB Plugin - www.chintellalaw.com") |
| 182 | + root.geometry("850x910") |
| 183 | + root.minsize(850, 910) |
| 184 | + |
| 185 | + app = DocQA_GUI(root) |
| 186 | + logic = DocQA_Logic(app) |
| 187 | + app.start_up() |
0 commit comments