-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirements_gui.py
184 lines (157 loc) · 7.84 KB
/
requirements_gui.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
File: requirements_gui.py
Description: This script creates a GUI for the requirements generator.
Contributors:
Adrian Hassa
Created: 2024-11-13
Last Modified: 2024-12-09
Project: 3GPP Requirement Tools
URL: https://github.com/Adrian2901/3gpp-requirements-tools
License: MIT License (see LICENSE file for details)
"""
import json
import os
import tkinter as tk
from tkinter import ttk, filedialog
import filter_docs
import generate_req
import csv2xlsx
import threading
import requests.exceptions
class RequirementsGenerator:
def __init__(self, root, config):
self.frame = ttk.Frame(root)
self.config = config
# Get the values from the config files
self.ip_var=tk.StringVar(value="localhost:11435")
self.path_var=tk.StringVar(value=config['folder_path'])
self.output_var=tk.StringVar(value=config['output_folder_path'])
self.keyword_var=tk.StringVar(value=' '.join(config['keywords']).replace(" ", ","))
self.model_var=config['model_name']
# LLM IP address and port input in the GUI
self.ip_label = tk.Label(self.frame, text = 'LLM address:port', font=('arial',10))
self.ip_entry = tk.Entry(self.frame, textvariable = self.ip_var, font=('arial',10,'normal'), width=70)
# Input path input in the GUI
self.path_label = tk.Label(self.frame, text = 'Input folder path', font=('arial',10))
self.path_entry = tk.Entry(self.frame, textvariable = self.path_var, font=('arial',10,'normal'), width=70, state="readonly")
self.path_btn=tk.Button(self.frame,text = '...', command = self.select_path_dir, width=10)
# Output path input in the GUI
self.output_label = tk.Label(self.frame, text = 'Output folder path', font = ('arial',10,'normal'))
self.output_entry=tk.Entry(self.frame, textvariable = self.output_var, font = ('arial',10,'normal'), width=70, state="readonly")
self.output_btn=tk.Button(self.frame,text = '...', command = self.select_output_dir, width=10)
# Keywords input in the GUI
self.keyword_label = tk.Label(self.frame, text = 'Keywords', font = ('arial',10,'normal'))
self.keyword_entry=tk.Entry(self.frame, textvariable = self.keyword_var, font = ('arial',10,'normal'), width=70)
# Model selection in the GUI
self.model_label = tk.Label(self.frame, text = 'Language Model', font = ('arial',10,'normal'))
self.model_entry=ttk.Combobox(self.frame, values=["llama3.1", "llama3.1:70b", "llama3.1:405b"], width=79)
self.model_entry.set(self.model_var)
# Unit selection in the GUI
self.unit_label = tk.Label(self.frame, text = 'Units', font = ('arial',10,'normal'))
self.unit_frame = tk.Frame(self.frame)
self.unit_vars = {} # Dictionary to store BooleanVar for each unit
# Create a checkbox for each unit type in the config
col = 0
for unit_name in config['units']:
unit_var = tk.BooleanVar(value=unit_name in config.get('checked_units', []))
self.unit_vars[unit_name] = unit_var
self.unit_checkbox = tk.Checkbutton(self.unit_frame, text=unit_name, variable=unit_var)
self.unit_checkbox.grid(row=0, column=col, padx=25)
col += 1
self.run_btn=tk.Button(self.frame,text = 'Run', command = self.run, width=30)
self.status_label = tk.Label(self.frame, text = '', font = ('arial',10,'normal'))
self.progressbar = ttk.Progressbar(self.frame, length=500)
# Place the widgets on the window grid
self.ip_label.grid(row=0,column=0, padx=5)
self.ip_entry.grid(row=0,column=1, pady=10)
self.model_label.grid(row=1,column=0, padx=5)
self.model_entry.grid(row=1,column=1, pady=10)
self.path_label.grid(row=2,column=0, padx=5)
self.path_entry.grid(row=2,column=1, pady=10)
self.path_btn.grid(row=2,column=2, pady=10)
self.output_label.grid(row=3,column=0, padx=5)
self.output_entry.grid(row=3,column=1, pady=10)
self.output_btn.grid(row=3,column=2, pady=10)
self.keyword_label.grid(row=4,column=0, padx=5)
self.keyword_entry.grid(row=4,column=1, pady=10)
self.unit_label.grid(row=5, column=0, padx=5)
self.unit_frame.grid(row=5, column=1, pady=10)
self.run_btn.grid(row=6,column=1)
self.status_label.grid(row=7,column=1)
# Function to save the configuration to the JSON file
def save_config(self):
checked_units = [unit for unit, var in self.unit_vars.items() if var.get()]
self.config['llm_address'] = self.ip_var.get()
self.config['folder_path'] = self.path_var.get()
self.config['output_folder_path'] = self.output_var.get()
self.config['keywords'] = self.keyword_var.get().split(",")
self.config['model_name'] = self.model_entry.get()
self.config['checked_units'] = checked_units
with open('config.json', 'w') as f:
json.dump(self.config, f)
return self.config
# Function to update the status label and progress bar
def update_status(self, message, progress=None):
self.status_label.config(text=message)
if progress is not None:
self.progressbar.grid(row=8,column=1)
self.progressbar.config(value=progress*100)
else:
self.progressbar.grid_forget()
# Function to start the filtering on button press
def run(self):
self.config = self.save_config()
# Validate the input fields
if not self.config['llm_address'] or not self.config['folder_path'] or not self.config['output_folder_path'] or not self.config['keywords']:
self.update_status("Please fill in all the fields!")
return
self.run_btn.config(state=tk.DISABLED)
self.update_status("Starting the filtering...")
# Function to run on a separate thread
def run_tasks():
try:
filter_docs.execute_filtering(self.config, self.update_status)
generate_req.generate_req(self.config, self.update_status)
csv2xlsx.csv_to_xlsx(self.config, self.update_status)
except requests.exceptions.ConnectionError as e:
print(e)
self.update_status("A connection error occurred! Please check the LLM address and try again.")
except KeyError as e:
print(e)
self.update_status("Error occured while fetching data! Please check the model name and try again.")
except ValueError as e:
print(e)
self.update_status("Error occured while processing data! Please check the input and try again.")
except Exception as e:
print(e)
self.update_status("An error occurred! Please try again.")
self.run_btn.config(state=tk.NORMAL)
# Start the thread
task_thread = threading.Thread(target=run_tasks)
task_thread.start()
def select_path_dir(self):
dir = filedialog.askdirectory()
self.path_var.set(dir)
self.path_entry.delete(0, tk.END)
self.path_entry.insert(0, dir)
def select_output_dir(self):
dir = filedialog.askdirectory()
self.output_var.set(dir)
self.output_entry.delete(0, tk.END)
self.output_entry.insert(0, dir)
if __name__ == "__main__":
# File path for the configuration file
CONFIG_FILE = "config.json"
# Load the configuration from the JSON file
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
# Create the main window
root = tk.Tk()
root.title(config.get("title", "Generate Requirements"))
requirements_gui = RequirementsGenerator(root, config)
requirements_gui.frame.pack(expand=True, fill="both")
# Set window size and disable resizing
root.geometry("700x400")
root.resizable(False, False)
# Run the application
root.mainloop()