Improve UX (#9)

* Go to chat tab when resignin
* Allow placeholder message configurable
* Hide setting tabs if there aren't any settings
This commit is contained in:
Duc Nguyen (john) 2024-04-04 15:39:24 +07:00 committed by GitHub
parent ecf09b275f
commit e187e23dd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 119 additions and 69 deletions

View File

@ -24,41 +24,57 @@ class App(BaseApp):
"""Render the UI""" """Render the UI"""
self._tabs = {} self._tabs = {}
if self.f_user_management: with gr.Tabs() as self.tabs:
from ktem.pages.login import LoginPage if self.f_user_management:
from ktem.pages.login import LoginPage
with gr.Tab("Welcome", elem_id="login-tab") as self._tabs["login-tab"]: with gr.Tab(
self.login_page = LoginPage(self) "Welcome", elem_id="login-tab", id="login-tab"
) as self._tabs["login-tab"]:
self.login_page = LoginPage(self)
with gr.Tab(
"Chat", elem_id="chat-tab", visible=not self.f_user_management
) as self._tabs["chat-tab"]:
self.chat_page = ChatPage(self)
for index in self.index_manager.indices:
with gr.Tab( with gr.Tab(
f"{index.name} Index", "Chat",
elem_id=f"{index.id}-tab", elem_id="chat-tab",
elem_classes="indices-tab", id="chat-tab",
visible=not self.f_user_management, visible=not self.f_user_management,
) as self._tabs[f"{index.id}-tab"]: ) as self._tabs["chat-tab"]:
page = index.get_index_page_ui() self.chat_page = ChatPage(self)
setattr(self, f"_index_{index.id}", page)
with gr.Tab( for index in self.index_manager.indices:
"Admin", elem_id="admin-tab", visible=not self.f_user_management with gr.Tab(
) as self._tabs["admin-tab"]: f"{index.name} Index",
self.admin_page = AdminPage(self) elem_id=f"{index.id}-tab",
elem_classes="indices-tab",
id=f"{index.id}-tab",
visible=not self.f_user_management,
) as self._tabs[f"{index.id}-tab"]:
page = index.get_index_page_ui()
setattr(self, f"_index_{index.id}", page)
with gr.Tab( with gr.Tab(
"Settings", elem_id="settings-tab", visible=not self.f_user_management "Admin",
) as self._tabs["settings-tab"]: elem_id="admin-tab",
self.settings_page = SettingsPage(self) id="admin-tab",
visible=not self.f_user_management,
) as self._tabs["admin-tab"]:
self.admin_page = AdminPage(self)
with gr.Tab( with gr.Tab(
"Help", elem_id="help-tab", visible=not self.f_user_management "Settings",
) as self._tabs["help-tab"]: elem_id="settings-tab",
self.help_page = HelpPage(self) id="settings-tab",
visible=not self.f_user_management,
) as self._tabs["settings-tab"]:
self.settings_page = SettingsPage(self)
with gr.Tab(
"Help",
elem_id="help-tab",
id="help-tab",
visible=not self.f_user_management,
) as self._tabs["help-tab"]:
self.help_page = HelpPage(self)
def on_subscribe_public_events(self): def on_subscribe_public_events(self):
if self.f_user_management: if self.f_user_management:
@ -75,7 +91,7 @@ class App(BaseApp):
else gr.update(visible=False) else gr.update(visible=False)
) )
for k in self._tabs.keys() for k in self._tabs.keys()
) ) + [gr.update(selected="login-tab")]
with Session(engine) as session: with Session(engine) as session:
user = session.exec(select(User).where(User.id == user_id)).first() user = session.exec(select(User).where(User.id == user_id)).first()
@ -100,6 +116,8 @@ class App(BaseApp):
else: else:
tabs_update.append(gr.update(visible=True)) tabs_update.append(gr.update(visible=True))
tabs_update.append(gr.update(selected="chat-tab"))
return tabs_update return tabs_update
self.subscribe_event( self.subscribe_event(
@ -107,7 +125,7 @@ class App(BaseApp):
definition={ definition={
"fn": signed_in_out, "fn": signed_in_out,
"inputs": [self.user_id], "inputs": [self.user_id],
"outputs": list(self._tabs.values()), "outputs": list(self._tabs.values()) + [self.tabs],
"show_progress": "hidden", "show_progress": "hidden",
}, },
) )
@ -117,7 +135,7 @@ class App(BaseApp):
definition={ definition={
"fn": signed_in_out, "fn": signed_in_out,
"inputs": [self.user_id], "inputs": [self.user_id],
"outputs": list(self._tabs.values()), "outputs": list(self._tabs.values()) + [self.tabs],
"show_progress": "hidden", "show_progress": "hidden",
}, },
) )

View File

@ -304,6 +304,7 @@ class ChatPage(BasePage):
self.chat_control.conversation, self.chat_control.conversation,
self.chat_control.conversation_rn, self.chat_control.conversation_rn,
self.chat_panel.chatbot, self.chat_panel.chatbot,
self.info_panel,
] ]
+ self._indices_input, + self._indices_input,
"show_progress": "hidden", "show_progress": "hidden",
@ -406,13 +407,19 @@ class ChatPage(BasePage):
text, refs = "", "" text, refs = "", ""
len_ref = -1 # for logging purpose len_ref = -1 # for logging purpose
msg_placeholder = getattr(
flowsettings, "KH_CHAT_MSG_PLACEHOLDER", "Thinking ..."
)
print(msg_placeholder)
while True: while True:
try: try:
response = queue.get_nowait() response = queue.get_nowait()
except Exception: except Exception:
state[pipeline.get_info()["id"]] = reasoning_state["pipeline"] state[pipeline.get_info()["id"]] = reasoning_state["pipeline"]
yield chat_history + [(chat_input, text or "Thinking ...")], refs, state yield chat_history + [
(chat_input, text or msg_placeholder)
], refs, state
continue continue
if response is None: if response is None:

View File

@ -72,6 +72,31 @@ class SettingsPage(BasePage):
self._components = {} self._components = {}
self._reasoning_mode = {} self._reasoning_mode = {}
# render application page if there are application settings
self._render_app_tab = False
if self._default_settings.application.settings:
self._render_app_tab = True
# render index page if there are index settings (general and/or specific)
self._render_index_tab = False
if self._default_settings.index.settings:
self._render_index_tab = True
else:
for sig in self._default_settings.index.options.values():
if sig.settings:
self._render_index_tab = True
break
# render reasoning page if there are reasoning settings
self._render_reasoning_tab = False
if len(self._default_settings.reasoning.settings) > 1:
self._render_reasoning_tab = True
else:
for sig in self._default_settings.reasoning.options.values():
if sig.settings:
self._render_reasoning_tab = True
break
self.on_building_ui() self.on_building_ui()
def on_building_ui(self): def on_building_ui(self):
@ -79,12 +104,9 @@ class SettingsPage(BasePage):
if self._app.f_user_management: if self._app.f_user_management:
with gr.Tab("User settings"): with gr.Tab("User settings"):
self.user_tab() self.user_tab()
with gr.Tab("General application settings"): self.app_tab()
self.app_tab() self.index_tab()
with gr.Tab("Index settings"): self.reasoning_tab()
self.index_tab()
with gr.Tab("Reasoning settings"):
self.reasoning_tab()
def on_subscribe_public_events(self): def on_subscribe_public_events(self):
""" """
@ -221,9 +243,10 @@ class SettingsPage(BasePage):
return "", "" return "", ""
def app_tab(self): def app_tab(self):
for n, si in self._default_settings.application.settings.items(): with gr.Tab("General application settings", visible=self._render_app_tab):
obj = render_setting_item(si, si.value) for n, si in self._default_settings.application.settings.items():
self._components[f"application.{n}"] = obj obj = render_setting_item(si, si.value)
self._components[f"application.{n}"] = obj
def index_tab(self): def index_tab(self):
# TODO: double check if we need general # TODO: double check if we need general
@ -232,37 +255,39 @@ class SettingsPage(BasePage):
# obj = render_setting_item(si, si.value) # obj = render_setting_item(si, si.value)
# self._components[f"index.{n}"] = obj # self._components[f"index.{n}"] = obj
for pn, sig in self._default_settings.index.options.items(): with gr.Tab("Index settings", visible=self._render_index_tab):
with gr.Tab(f"Index {pn}"): for pn, sig in self._default_settings.index.options.items():
for n, si in sig.settings.items(): with gr.Tab(f"Index {pn}"):
obj = render_setting_item(si, si.value) for n, si in sig.settings.items():
self._components[f"index.options.{pn}.{n}"] = obj obj = render_setting_item(si, si.value)
self._components[f"index.options.{pn}.{n}"] = obj
def reasoning_tab(self): def reasoning_tab(self):
with gr.Group(): with gr.Tab("Reasoning settings", visible=self._render_reasoning_tab):
for n, si in self._default_settings.reasoning.settings.items(): with gr.Group():
if n == "use": for n, si in self._default_settings.reasoning.settings.items():
continue if n == "use":
obj = render_setting_item(si, si.value) continue
self._components[f"reasoning.{n}"] = obj
gr.Markdown("### Reasoning-specific settings")
self._components["reasoning.use"] = render_setting_item(
self._default_settings.reasoning.settings["use"],
self._default_settings.reasoning.settings["use"].value,
)
for idx, (pn, sig) in enumerate(
self._default_settings.reasoning.options.items()
):
with gr.Group(
visible=idx == 0,
elem_id=pn,
) as self._reasoning_mode[pn]:
gr.Markdown("**Name**: Description")
for n, si in sig.settings.items():
obj = render_setting_item(si, si.value) obj = render_setting_item(si, si.value)
self._components[f"reasoning.options.{pn}.{n}"] = obj self._components[f"reasoning.{n}"] = obj
gr.Markdown("### Reasoning-specific settings")
self._components["reasoning.use"] = render_setting_item(
self._default_settings.reasoning.settings["use"],
self._default_settings.reasoning.settings["use"].value,
)
for idx, (pn, sig) in enumerate(
self._default_settings.reasoning.options.items()
):
with gr.Group(
visible=idx == 0,
elem_id=pn,
) as self._reasoning_mode[pn]:
gr.Markdown("**Name**: Description")
for n, si in sig.settings.items():
obj = render_setting_item(si, si.value)
self._components[f"reasoning.options.{pn}.{n}"] = obj
def change_reasoning_mode(self, value): def change_reasoning_mode(self, value):
output = [] output = []