|
|
|
import streamlit as st
|
|
from .chat_process import ChatProcessor
|
|
from ..database.chat_mongo_db import store_chat_history, get_chat_history
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def display_sidebar_chat(lang_code: str, chatbot_t: dict):
|
|
"""
|
|
Muestra el chatbot en el sidebar
|
|
Args:
|
|
lang_code: C贸digo del idioma
|
|
chatbot_t: Diccionario de traducciones del chatbot
|
|
"""
|
|
|
|
default_translations = {
|
|
'error_message': 'An error occurred',
|
|
'expand_chat': 'Open Assistant',
|
|
'initial_message': 'Hi! How can I help?',
|
|
'input_placeholder': 'Type your message...',
|
|
'clear_chat': 'Clear chat'
|
|
}
|
|
|
|
|
|
translations = {**default_translations, **chatbot_t}
|
|
|
|
with st.sidebar:
|
|
|
|
with st.expander(translations['expand_chat'], expanded=False):
|
|
try:
|
|
|
|
if 'chat_processor' not in st.session_state:
|
|
try:
|
|
st.session_state.chat_processor = ChatProcessor()
|
|
except Exception as e:
|
|
logger.error(f"Error inicializando ChatProcessor: {str(e)}")
|
|
st.error("Error: No se pudo inicializar el chat. Verifica la configuraci贸n.")
|
|
return
|
|
|
|
|
|
if 'sidebar_messages' not in st.session_state:
|
|
|
|
try:
|
|
history = get_chat_history(st.session_state.username, 'sidebar', 10)
|
|
if history:
|
|
st.session_state.sidebar_messages = history[0]['messages']
|
|
else:
|
|
st.session_state.sidebar_messages = [
|
|
{"role": "assistant", "content": translations['initial_message']}
|
|
]
|
|
except Exception as e:
|
|
logger.error(f"Error recuperando historial: {str(e)}")
|
|
st.session_state.sidebar_messages = [
|
|
{"role": "assistant", "content": translations['initial_message']}
|
|
]
|
|
|
|
|
|
chat_container = st.container()
|
|
|
|
|
|
with chat_container:
|
|
for message in st.session_state.sidebar_messages:
|
|
with st.chat_message(message["role"]):
|
|
st.markdown(message["content"])
|
|
|
|
|
|
user_input = st.text_input(
|
|
translations['input_placeholder'],
|
|
key='sidebar_chat_input'
|
|
)
|
|
|
|
if user_input:
|
|
|
|
st.session_state.sidebar_messages.append(
|
|
{"role": "user", "content": user_input}
|
|
)
|
|
|
|
|
|
with chat_container:
|
|
with st.chat_message("assistant"):
|
|
message_placeholder = st.empty()
|
|
full_response = ""
|
|
|
|
for chunk in st.session_state.chat_processor.process_chat_input(
|
|
user_input,
|
|
lang_code
|
|
):
|
|
full_response += chunk
|
|
message_placeholder.markdown(full_response)
|
|
|
|
|
|
st.session_state.sidebar_messages.append(
|
|
{"role": "assistant", "content": full_response.strip()}
|
|
)
|
|
|
|
|
|
store_chat_history(
|
|
username=st.session_state.username,
|
|
messages=st.session_state.sidebar_messages,
|
|
analysis_type='sidebar'
|
|
)
|
|
|
|
|
|
if st.button(translations['clear_chat']):
|
|
st.session_state.sidebar_messages = [
|
|
{"role": "assistant", "content": translations['initial_message']}
|
|
]
|
|
st.rerun()
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error en sidebar chat: {str(e)}")
|
|
st.error(translations['error_message']) |