Skip to content

Commit

Permalink
Several interface changes for testing and preparing for the new version
Browse files Browse the repository at this point in the history
  • Loading branch information
Odrec committed Jun 11, 2024
1 parent ffa3dd4 commit 09224c0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 66 deletions.
2 changes: 2 additions & 0 deletions pages/chatbot_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from src.ai_client import AIClient
from src.sidepanel_manager import SidepanelManager
import src.styling as styling
from src.language_utils import initialize_language, language_controls


# Load environment variables
Expand Down Expand Up @@ -36,6 +37,7 @@ def initialize_app(self):
# Set and manage sidebar interface controls
self.sidebar_manager.initialize_session_variables()
self.sidebar_manager.display_logo()
language_controls()
self.sidebar_manager.display_sidebar_controls()

# Initialize the client
Expand Down
59 changes: 12 additions & 47 deletions src/chat_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _clear_photo_callback():
session_state['photo_to_use'] = []

@staticmethod
def _toggle_camera_callback():
def _activate_camera_callback():
"""
Toggle the camera activation within the session state.
Expand All @@ -68,16 +68,6 @@ def _toggle_camera_callback():
session_state['activate_camera'] = False
session_state['photo_to_use'] = []

@staticmethod
def _delete_conversation_callback():
"""
Callback function to delete the current conversation history.
This method clears the conversation history for the selected chatbot path stored in the session state.
It sets the conversation history to an empty list.
"""
session_state['conversation_histories'][session_state['selected_chatbot_path_serialized']] = []

@staticmethod
def _fetch_chatbot_description():
"""
Expand Down Expand Up @@ -410,50 +400,34 @@ def _display_camera():
if session_state['your_photo']:
if session_state['use_photo_button']:
session_state['photo_to_use'] = session_state['your_photo']
session_state['toggle_key'] -= 1
session_state['activate_camera'] = False
st.rerun()

def _display_delete_conversation_button(self):
"""
Render a button in the specified column that allows the user to delete the active chatbot's conversation history.
On button click, this method removes the conversation history from the session state for the current chatbot path
and triggers a page rerun to refresh the state.
"""
delete_label = session_state['_']("Delete Conversation")
st.button("🗑️", on_click=self._delete_conversation_callback, help=delete_label)

def _display_chat_buttons(self):
"""
Displays a set of interactive buttons within the chat interface, allowing users to:
- Activate or deactivate the camera.
- Upload images.
- Delete the conversation history.
"""
conversation_key = session_state['selected_chatbot_path_serialized']

#container_camera = st.container()
container_camera = st.container()
container_images_controls = st.container()
container_delete_conversation = st.container()

left_delete_button_margin = 0

if 'selected_model' in session_state and session_state['selected_model'] == self.advanced_model:
left_delete_button_margin = 6
#with container_camera:
# float_parent("margin-left: 0rem; bottom: 6.9rem;background-color: var(--default-backgroundColor); "
# "padding-top: 0.9rem;")
with container_camera:
float_parent("margin-left: 0rem; bottom: 6.9rem;background-color: var(--default-backgroundColor); "
"padding-top: 0.9rem;")

st.button("📷",
key='activate_camera_key',
on_click=self._activate_camera_callback,
help=session_state['_']("Activate camera"))

with container_images_controls:
float_parent("margin-left: 0rem; bottom: 6.9rem;background-color: var(--default-backgroundColor); "
float_parent("margin-left: 6rem; bottom: 6.9rem;background-color: var(--default-backgroundColor); "
"padding-top: 0.9rem;")

with st.popover("📷️", help=session_state['_']("Images")):
st.toggle(session_state['_']("Activate camera"),
key=session_state['toggle_key'],
value=False,
on_change=self._toggle_camera_callback)
with st.popover("🖼️", help=session_state['_']("Images")):

session_state['uploaded_images'] = st.file_uploader(session_state['_']("Upload Images"),
type=['png', 'jpeg', 'jpg', 'gif', 'webp'],
Expand All @@ -472,15 +446,6 @@ def _display_chat_buttons(self):
url_list = urls.strip().split('\n')
session_state['image_urls'].extend([url.strip() for url in url_list if url.strip()])

with container_delete_conversation:
float_parent(
f"margin-left: {left_delete_button_margin}rem; bottom: 6.9rem;background-color: "
"var(--default-backgroundColor); padding-top: 0.9rem;")

if conversation_key in session_state['conversation_histories'] and session_state[
'conversation_histories'][conversation_key]:
self._display_delete_conversation_button()

def display_chat_interface(self):
"""
Displays the chat interface, manages the display of conversation history, and handles user input.
Expand Down
19 changes: 10 additions & 9 deletions src/language_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ def initialize_language():


def language_controls():
st.radio(
"Language",
options=languages,
horizontal=True,
key="selected_language",
on_change=change_language,
index=1,
label_visibility='hidden'
)
with st.sidebar:
st.radio(
"Language",
options=languages,
horizontal=True,
key="selected_language",
on_change=change_language,
index=1,
label_visibility='hidden'
)


def translate():
Expand Down
39 changes: 29 additions & 10 deletions src/sidepanel_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import streamlit as st
from streamlit import session_state
from streamlit_cookies_manager import EncryptedCookieManager
from streamlit_option_menu import option_menu

from src.language_utils import initialize_language, language_controls
from src import menu_utils
Expand All @@ -23,7 +22,6 @@ def __init__(self, advanced_model):
st.info(session_state['_']("The outputs of the chat assistant may be erroneous - therefore, "
"always check the answers for their accuracy. Remember not to enter "
"any personal information and copyrighted materials."))
language_controls()
self.advanced_model = advanced_model

@staticmethod
Expand Down Expand Up @@ -73,7 +71,6 @@ def initialize_session_variables(self):
'edited_prompts': {},
'disable_custom': False,
'images_key': 0,
'toggle_key': -1,
'image_urls': [],
'uploaded_images': [],
'image_content': [],
Expand All @@ -86,6 +83,16 @@ def initialize_session_variables(self):
if key not in session_state:
session_state[key] = default_value

@staticmethod
def _delete_conversation_callback():
"""
Callback function to delete the current conversation history.
This method clears the conversation history for the selected chatbot path stored in the session state.
It sets the conversation history to an empty list.
"""
session_state['conversation_histories'][session_state['selected_chatbot_path_serialized']] = []

@staticmethod
def display_logo():
"""
Expand Down Expand Up @@ -139,11 +146,14 @@ def _display_chatbots_menu(self, options, path=[]):
if isinstance(options, dict) and options: # Verify options is a dictionary and not empty
next_level = list(options.keys())

with st.sidebar:
choice = option_menu("", next_level,
# icons=['chat-dots'] * len(next_level),
# menu_icon="cast",
default_index=0)
#with st.sidebar:
#choice = option_menu("", next_level,
# # icons=['chat-dots'] * len(next_level),
# # menu_icon="cast",
# default_index=0)

# For now keep the choice always on the first level since we don't have more chatbots or features
choice = next_level[0]

if choice:
new_path = path + [choice]
Expand Down Expand Up @@ -262,14 +272,23 @@ def _display_model_information(self):
session_state['uploaded_images'] or
session_state['photo_to_use']):
session_state['images_key'] += 1
session_state['toggle_key'] -= 1
session_state['image_urls'] = []
session_state['uploaded_images'] = []
session_state['image_content'] = []
session_state['photo_to_use'] = []
session_state['activate_camera'] = False
st.rerun()

def _display_delete_conversation_button(self, container):
"""
Render a button in the specified column that allows the user to delete the active chatbot's conversation history.
On button click, this method removes the conversation history from the session state for the current chatbot path
and triggers a page rerun to refresh the state.
"""
delete_label = session_state['_']("Delete Conversation")
container.button("🗑️", on_click=self._delete_conversation_callback, help=delete_label)

def _show_conversation_controls(self):
"""
Display buttons for conversation management, including deleting and downloading conversation history,
Expand All @@ -287,7 +306,7 @@ def _show_conversation_controls(self):
if conversation_key in session_state['conversation_histories'] and session_state[
'conversation_histories'][conversation_key]:
self._download_conversation_button(col2, conversation_key)
# self._delete_conversation_button(col3)
self._display_delete_conversation_button(col3)

def _upload_conversation_button(self, container, conversation_key):
"""
Expand Down

0 comments on commit 09224c0

Please sign in to comment.