-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
269 lines (229 loc) · 10.9 KB
/
setup.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import paths
import customtkinter
import modules.persistence as persistence
import modules.stdops as stdops
from PIL import Image
from ctrls import ctkframe
import os
import gui
customtkinter.set_appearance_mode("dark")
class Setup(customtkinter.CTk):
width = 900
height = 600
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.stdops = stdops.StdOps()
self.config = persistence.Persistence()
self.CURRENT_PATH = paths.CURRENT_PATH
self.CONFIG_DIR = paths.CONFIG_DIR
self.LOGS_DIR = paths.LOGS_DIR
self.TMP_DIR = paths.TMP_DIR
self.SYSTEM_LOG_FILE = paths.SYSTEM_LOG_FILE
self.CHAT_LOG_FILE = paths.CHAT_LOG_FILE
self.USER_SETTINGS_FILE = paths.USER_SETTINGS_FILE
self.KEY_CONFIG_FILE = paths.KEY_CONFIG_FILE
self.SETUP_DONE_FLAG_FILE = paths.SETUP_DONE_FLAG_FILE
self.USERNAME = None
self.API_KEY = None
#//////////// SETUP ////////////
if not self.setup():
self.setupFailedEvent()
#///////////////////////////////
#//////////// SETUP FORM ////////////
self.title("Wingtp & OpenAI Setup")
self.geometry(f"{self.width}x{self.height}")
self.resizable(False, False)
self.bg_image = customtkinter.CTkImage(Image.open(f"{paths.SETUP_BG_IMAGE}"), size=(self.width, self.height))
self.bg_image_label = customtkinter.CTkLabel(self, image=self.bg_image)
self.bg_image_label.grid(row=0, column=0)
self.setup_frame = ctkframe.CustomTkFrame(self, bg_color="transparent", fg_color="transparent")
self.setup_frame.grid(row=0, column=0, sticky="ns")
self.setup_label = customtkinter.CTkLabel(self.setup_frame, text="WinGTP 0.1.0", font=customtkinter.CTkFont(size=28, weight="bold"))
self.setup_label.grid(row=0, column=0, padx=30, pady=(150, 15))
self.username_entry = customtkinter.CTkEntry(self.setup_frame, width=200, placeholder_text="username")
self.username_entry.grid(row=1, column=0, padx=30, pady=(15, 15))
self.api_key_entry = customtkinter.CTkEntry(self.setup_frame, width=200, placeholder_text="api key")
self.api_key_entry.grid(row=2, column=0, padx=30, pady=(0, 15))
self.setup_done_button = customtkinter.CTkButton(self.setup_frame, text="Finish Setup", command=self.formDoneEvent, width=200)
self.setup_done_button.grid(row=3, column=0, padx=30, pady=(15, 15))
def createSetupDoneFlagFile(self) -> bool:
if not self.stdops.createFile(self.SETUP_DONE_FLAG_FILE):
return False
else:
return True
def setupConfig(self) -> None:
self.config.openConfig()
self.config.setDefaultSection("DEFAULTS")
self.config.addSection("system")
self.config.addOption("system", "new_user", True)
self.config.addOption("system", "config_dir", f"{self.CONFIG_DIR}")
self.config.addOption("system", "logs_dir", f"{self.LOGS_DIR}")
self.config.addOption("system", "tmp_dir", f"{self.TMP_DIR}")
self.config.addOption("system", "config_file", f"{self.USER_SETTINGS_FILE}")
self.config.addOption("system", "sys_log", f"{self.SYSTEM_LOG_FILE}")
self.config.addSection("user")
self.config.addOption("user", "username", f"{self.USERNAME}")
self.config.addOption("user", "api_key", f"{self.API_KEY}")
self.config.addOption("user", "api_key_path", f"{self.KEY_CONFIG_FILE}")
self.config.addOption("user", "organization", "org-8rsyvRZvyUBHhs4fJqATWK23")
self.config.addSection("ui")
self.config.addOption("ui", "color", "#5ae7d6")
self.config.addOption("ui", "ui_scaling", "100%%")
self.config.addOption("ui", "theme", "System")
self.config.addSection("chat")
self.config.addOption("chat", "chat_to_file", False)
self.config.addOption("chat", "chat_log_path", f"{self.CHAT_LOG_FILE}")
self.config.addOption("chat", "echo_chat", False)
self.config.addOption("chat", "stream_chat", False)
self.config.addOption("chat", "use_stop_list", False)
self.config.addOption("chat", "chat_temperature", 1)
self.config.addOption("chat", "presence_penalty", 0)
self.config.addOption("chat", "frequency_penalty", 0)
self.config.addOption("chat", "best_of", 1)
self.config.addOption("chat", "timeout", 0)
self.config.addOption("chat", "chat_engine", "text-davinci-003")
self.config.addOption("chat", "response_token_limit", 16)
self.config.addOption("chat", "response_count", 1)
self.config.addOption("chat", "api_base", "https://api.openai.com/v1")
self.config.addOption("chat", "api_type", "open_ai")
self.config.addOption("chat", "api_version", None)
self.config.addOption("chat", "request_type", 0)
self.config.addOption("chat", "user_defined_data_file", None)
self.config.addOption("chat", "jsonl_data_file", None)
self.config.addSection("stop_lists")
self.config.addOption("stop_lists", "stop_list1", "a custom stop list")
self.config.addOption("stop_lists", "stop_list2", "another custom stop list")
self.config.addSection("requests")
self.config.addOption("requests", "current_request", None)
self.config.addOption("requests", "previous_request", None)
self.config.addSection("responses")
self.config.addOption("responses", "current_response", None)
self.config.addOption("responses", "previous_response", None)
self.config.addSection("translations")
self.config.addOption("translations", "lang1", None)
self.config.addOption("translations", "lang2", None)
self.config.addSection("qa")
self.config.addOption("qa", "context_1", None )
self.config.addSection("image_requests")
self.config.addOption("image_requests", "use_edit", 0)
self.config.addOption("image_requests", "use_variation", 0)
self.config.addOption("image_requests", "use_new", 1)
self.config.addOption("image_requests", "img_path", None)
self.config.addOption("image_requests", "mask_path", None)
self.config.addOption("image_requests", "img_size", "256x256")
self.config.addOption("image_requests", "response_format", "url")
self.config.addSection("audio_transcription")
self.config.addOption("audio_transcription", "audio_file", None)
self.config.addSection("edits")
self.config.addOption("edits", "instruction", None)
if self.config.saveConfig():
return True
else:
return False
def createConfigFiles(self) -> bool:
if not self.stdops.createDir(self.CONFIG_DIR):
return False
elif not self.stdops.createDir(self.LOGS_DIR):
return False
elif not self.stdops.createDir(self.TMP_DIR):
return False
elif not self.stdops.createFile(self.USER_SETTINGS_FILE):
return False
elif not self.stdops.createFile(self.KEY_CONFIG_FILE):
return False
elif not self.stdops.createFile(self.SYSTEM_LOG_FILE):
return False
elif not self.stdops.createFile(self.CHAT_LOG_FILE):
return False
else:
return True
def validateAPIKey(self, api_key: str) -> bool:
if not (api_key.startswith('sk-') and len(api_key) == 51):
return False
else:
return True
def writeAPIKey(self, api_key: str) -> bool:
if self.stdops.writeTofile(self.KEY_CONFIG_FILE, api_key):
return True
return False
def rollBackSetup(self) -> None:
print("Setup failed. Rolling back changes")
try:
if os.path.exists(self.SETUP_DONE_FLAG_FILE):
os.remove(self.SETUP_DONE_FLAG_FILE)
if os.path.exists(self.USER_SETTINGS_FILE):
os.remove(self.USER_SETTINGS_FILE)
if os.path.exists(self.KEY_CONFIG_FILE):
os.remove(self.KEY_CONFIG_FILE)
if os.path.exists(self.CHAT_LOG_FILE):
os.remove(self.CHAT_LOG_FILE)
if os.path.exists(self.SYSTEM_LOG_FILE):
os.remove(self.SYSTEM_LOG_FILE)
if os.path.exists(self.CONFIG_DIR):
os.rmdir(self.CONFIG_DIR)
if os.path.exists(self.LOGS_DIR):
os.rmdir(self.LOGS_DIR)
if os.path.exists(self.TMP_DIR):
os.rmdir(self.TMP_DIR)
except IOError as ioe:
print(repr(ioe))
except Exception as e:
print(repr(e))
def setupFailedEvent(self) -> None:
self.rollBackSetup()
self.quit()
exit(0)
def badField(self, field: str = "both"):
if field == "both":
self.api_key_entry.configure(border_color="#ff0555")
self.username_entry.configure(border_color="#ff0555")
if field == "username":
self.username_entry.configure(border_color="#ff0555")
if field == "api_key":
self.api_key_entry.configure(border_color="#ff0555")
def runGUI(self):
self._gui = gui.WinGTPGUI()
self.destroy()
self._gui.mainloop()
def formDoneEvent(self) -> None:
_valid = False
# Get the username...
_username = self.username_entry.get()
# Get the api key...
_api_key = self.api_key_entry.get()
if len(_username) == 0 and len(_api_key) == 0:
self.badField("both")
return False
if len(_username) == 0:
self.badField("username")
return False
else:
self.USERNAME = _username
self.config.openConfig()
self.config.setOption("user", "username", f"{self.USERNAME}")
self.config.saveConfig()
# Validate the api key...
if self.validateAPIKey(_api_key):
self.API_KEY = _api_key
self.config.openConfig()
self.config.setOption("user", "api_key", str(_api_key))
self.config.saveConfig()
if not self.writeAPIKey(_api_key):
return False
else:
if not self.createSetupDoneFlagFile():
self.setupFailedEvent()
return False
else:
_valid = True
else:
self.badField("api_key")
return False
if _valid == True:
self.runGUI()
def setup(self) -> bool:
if not self.createConfigFiles():
return False
else:
self.setupConfig()
return True