Create, Use a Labelled Text Field for the Conversation Input Field

- This fixes the field expanding when configure screen is expanded
- Allows for reusability of the labelled text field
- Simplifies the logic to save settings for conversation processor
This commit is contained in:
Debanjum Singh Solanky 2022-08-12 16:59:15 +03:00
parent fa7e36cada
commit a1c58a9470
2 changed files with 39 additions and 30 deletions

View file

@ -8,6 +8,7 @@ from PyQt6.QtCore import Qt
# Internal Packages # Internal Packages
from src.configure import configure_server from src.configure import configure_server
from src.interface.desktop.file_browser import FileBrowser from src.interface.desktop.file_browser import FileBrowser
from src.interface.desktop.labelled_text_field import LabelledTextField
from src.utils import constants, state, yaml as yaml_utils from src.utils import constants, state, yaml as yaml_utils
from src.utils.cli import cli from src.utils.cli import cli
from src.utils.config import SearchType, ProcessorType from src.utils.config import SearchType, ProcessorType
@ -86,32 +87,26 @@ class ConfigureScreen(QtWidgets.QDialog):
def add_processor_panel(self, current_conversation_config: dict, processor_type: ProcessorType, parent_layout: QtWidgets.QLayout): def add_processor_panel(self, current_conversation_config: dict, processor_type: ProcessorType, parent_layout: QtWidgets.QLayout):
"Add Conversation Processor Panel" "Add Conversation Processor Panel"
# Get current settings from config for given processor type
current_openai_api_key = current_conversation_config.get('openai-api-key', None) current_openai_api_key = current_conversation_config.get('openai-api-key', None)
# Create widgets to display settings for given processor type
processor_type_settings = QtWidgets.QWidget() processor_type_settings = QtWidgets.QWidget()
processor_type_layout = QtWidgets.QVBoxLayout(processor_type_settings) processor_type_layout = QtWidgets.QVBoxLayout(processor_type_settings)
enable_conversation = ProcessorCheckBox(f"Conversation", processor_type) enable_conversation = ProcessorCheckBox(f"Conversation", processor_type)
# Add file browser to set input files for given processor type
input_field = LabelledTextField("OpenAI API Key", processor_type, current_openai_api_key)
# Set enabled/disabled based on checkbox state
enable_conversation.setChecked(current_openai_api_key is not None) enable_conversation.setChecked(current_openai_api_key is not None)
conversation_settings = QtWidgets.QWidget()
conversation_settings_layout = QtWidgets.QHBoxLayout(conversation_settings)
input_label = QtWidgets.QLabel()
input_label.setText("OpenAI API Key")
input_label.setFixedWidth(95)
input_field = ProcessorLineEdit(current_openai_api_key, processor_type)
input_field.setFixedWidth(245)
input_field.setEnabled(enable_conversation.isChecked()) input_field.setEnabled(enable_conversation.isChecked())
enable_conversation.stateChanged.connect(lambda _: input_field.setEnabled(enable_conversation.isChecked())) enable_conversation.stateChanged.connect(lambda _: input_field.setEnabled(enable_conversation.isChecked()))
conversation_settings_layout.addWidget(input_label) # Add setting widgets for given processor type to panel
conversation_settings_layout.addWidget(input_field)
processor_type_layout.addWidget(enable_conversation) processor_type_layout.addWidget(enable_conversation)
processor_type_layout.addWidget(conversation_settings) processor_type_layout.addWidget(input_field)
parent_layout.addWidget(processor_type_settings) parent_layout.addWidget(processor_type_settings)
return processor_type_settings return processor_type_settings
def add_action_panel(self, parent_layout: QtWidgets.QLayout): def add_action_panel(self, parent_layout: QtWidgets.QLayout):
@ -165,9 +160,7 @@ class ConfigureScreen(QtWidgets.QDialog):
"Update config with conversation settings from UI" "Update config with conversation settings from UI"
for settings_panel in self.processor_settings_panels: for settings_panel in self.processor_settings_panels:
for child in settings_panel.children(): for child in settings_panel.children():
if isinstance(child, QtWidgets.QWidget) and child.findChild(ProcessorLineEdit): if not isinstance(child, (ProcessorCheckBox, LabelledTextField)):
child = child.findChild(ProcessorLineEdit)
elif not isinstance(child, ProcessorCheckBox):
continue continue
if isinstance(child, ProcessorCheckBox): if isinstance(child, ProcessorCheckBox):
# Processor Type Disabled # Processor Type Disabled
@ -178,9 +171,9 @@ class ConfigureScreen(QtWidgets.QDialog):
current_processor_config = self.current_config['processor'].get(child.processor_type, {}) current_processor_config = self.current_config['processor'].get(child.processor_type, {})
default_processor_config = self.get_default_config(processor_type = child.processor_type) default_processor_config = self.get_default_config(processor_type = child.processor_type)
self.new_config['processor'][child.processor_type.value] = merge_dicts(current_processor_config, default_processor_config) self.new_config['processor'][child.processor_type.value] = merge_dicts(current_processor_config, default_processor_config)
elif isinstance(child, ProcessorLineEdit) and child.processor_type in self.new_config['processor']: elif isinstance(child, LabelledTextField) and child.processor_type in self.new_config['processor']:
if child.processor_type == ProcessorType.Conversation: if child.processor_type == ProcessorType.Conversation:
self.new_config['processor'][child.processor_type.value]['openai-api-key'] = child.text() if child.text() != '' else None self.new_config['processor'][child.processor_type.value]['openai-api-key'] = child.input_field.text() if child.input_field.text() != '' else None
def save_settings_to_file(self) -> bool: def save_settings_to_file(self) -> bool:
# Validate config before writing to file # Validate config before writing to file
@ -230,12 +223,3 @@ class ProcessorCheckBox(QtWidgets.QCheckBox):
def __init__(self, text, processor_type: ProcessorType, parent=None): def __init__(self, text, processor_type: ProcessorType, parent=None):
self.processor_type = processor_type self.processor_type = processor_type
super(ProcessorCheckBox, self).__init__(text, parent=parent) super(ProcessorCheckBox, self).__init__(text, parent=parent)
class ProcessorLineEdit(QtWidgets.QLineEdit):
def __init__(self, text, processor_type: ProcessorType, parent=None):
self.processor_type = processor_type
if text is None:
super(ProcessorLineEdit, self).__init__(parent=parent)
else:
super(ProcessorLineEdit, self).__init__(text, parent=parent)

View file

@ -0,0 +1,25 @@
# External Packages
from PyQt6 import QtWidgets
# Internal Packages
from src.utils.config import ProcessorType
class LabelledTextField(QtWidgets.QWidget):
def __init__(self, title, processor_type: ProcessorType=None, default_value: str=None):
QtWidgets.QWidget.__init__(self)
layout = QtWidgets.QHBoxLayout()
self.setLayout(layout)
self.processor_type = processor_type
self.label = QtWidgets.QLabel()
self.label.setText(title)
self.label.setFixedWidth(95)
layout.addWidget(self.label)
self.input_field = QtWidgets.QLineEdit(self)
self.input_field.setFixedWidth(250)
self.input_field.setText(default_value)
layout.addWidget(self.input_field)
layout.addStretch()