forked from chigkim/VOLlama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPISettingsDialog.py
79 lines (66 loc) · 2.89 KB
/
APISettingsDialog.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
from Settings import settings
import wx
class APISettingsDialog(wx.Dialog):
def __init__(self, parent, title):
super().__init__(parent, title=title, size=(400, 400))
self.panel = wx.Panel(self)
self.grid_sizer = wx.GridBagSizer(5, 5) # Specify vertical and horizontal gap between widgets
# Define parameters and their tooltips
self.parameters = {
"llm_name": {"label": "LLM", "control": "Choice", "choices": ['Ollama', 'OpenAI', 'Gemini'], "initial": settings.llm_name},
"api_key": {"label": "API Key", "control": "Text", "initial": self.get_api_key()},
}
self.controls = {}
row_index = 0
for key, value in self.parameters.items():
self.add_parameter_control(row_index, key, **value)
row_index += 1
# Add OK and Cancel buttons at the bottom
ok_button = wx.Button(self.panel, label='OK', id=wx.ID_OK)
ok_button.Bind(wx.EVT_BUTTON, self.on_ok)
cancel_button = wx.Button(self.panel, label='Cancel', id=wx.ID_CANCEL)
self.grid_sizer.Add(ok_button, pos=(row_index, 0), flag=wx.ALL, border=5)
self.grid_sizer.Add(cancel_button, pos=(row_index, 1), flag=wx.ALL, border=5)
self.panel.SetSizer(self.grid_sizer)
def add_parameter_control(self, row, name, label, control, **kwargs):
label_widget = wx.StaticText(self.panel, label=label)
self.grid_sizer.Add(label_widget, pos=(row, 0), flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=5)
if control == "SpinCtrl":
widget = wx.SpinCtrl(self.panel, value=str(kwargs["initial"]), min=kwargs["min"], max=kwargs["max"])
elif control == "CheckBox":
widget = wx.CheckBox(self.panel)
widget.SetValue(kwargs["initial"])
elif control == "SpinCtrlDouble":
widget = wx.SpinCtrlDouble(self.panel, value=str(kwargs["initial"]), min=kwargs["min"], max=kwargs["max"], inc=kwargs["inc"])
widget.SetValue(kwargs["initial"])
elif control == "Choice":
widget = wx.Choice(self.panel, choices=kwargs["choices"])
widget.SetStringSelection(kwargs["initial"])
elif control == "Text":
widget = wx.TextCtrl(self.panel, value=kwargs["initial"])
else:
raise ValueError("Unsupported control type")
widget.SetName(label)
if name == "llm_name":
widget.Bind(wx.EVT_CHOICE, self.onSelectLlm)
self.controls[name] = widget
self.grid_sizer.Add(widget, pos=(row, 1), flag=wx.EXPAND | wx.ALL, border=5)
def on_ok(self, event):
for key, widget in self.controls.items():
if isinstance(widget, wx.Choice):
value = widget.GetStringSelection()
else:
value = widget.GetValue()
if key == "api_key":
key = settings.llm_name+"_"+key
setattr(settings, key.lower(), value)
self.Close()
def get_api_key(self):
if settings.llm_name == "OpenAI":
return settings.openai_api_key
elif settings.llm_name == "Gemini":
return settings.gemini_api_key
else: return ""
def onSelectLlm(self, event):
settings.llm_name = self.controls['llm_name'].GetStringSelection()
self.controls['api_key'].SetValue(self.get_api_key())