Spaces:
Running
Running
# modules/chatbot/sidebar_chat.py | |
import streamlit as st | |
from .chatbot import ClaudeAPIChat, initialize_chatbot, get_chatbot_response | |
from ..database.chat_mongo_db import store_chat_history, get_chat_history | |
import logging | |
logger = logging.getLogger(__name__) | |
def display_sidebar_chat(lang_code): | |
""" | |
Muestra el chatbot en el sidebar | |
""" | |
translations = { | |
'es': { | |
'chat_title': "Asistente AIdeaText", | |
'input_placeholder': "¿Tienes alguna pregunta?", | |
'initial_message': "¡Hola! Soy tu asistente. ¿En qué puedo ayudarte?", | |
'expand_chat': "Abrir asistente", | |
'clear_chat': "Limpiar chat" | |
}, | |
'en': { | |
'chat_title': "AIdeaText Assistant", | |
'input_placeholder': "Any questions?", | |
'initial_message': "Hi! I'm your assistant. How can I help?", | |
'expand_chat': "Open assistant", | |
'clear_chat': "Clear chat" | |
} | |
} | |
t = translations.get(lang_code, translations['en']) | |
with st.sidebar: | |
# Chatbot expandible | |
with st.expander(t['expand_chat'], expanded=False): | |
# Inicializar chatbot si no existe | |
if 'sidebar_chatbot' not in st.session_state: | |
st.session_state.sidebar_chatbot = initialize_chatbot() | |
# Inicializar mensajes si no existen | |
if 'sidebar_messages' not in st.session_state: | |
# Intentar recuperar historial previo | |
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": t['initial_message']} | |
] | |
# Contenedor del chat | |
chat_container = st.container() | |
# Mostrar mensajes | |
with chat_container: | |
for message in st.session_state.sidebar_messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
# Input del usuario | |
user_input = st.text_input( | |
t['input_placeholder'], | |
key='sidebar_chat_input' | |
) | |
# Procesar input | |
if user_input: | |
# Agregar mensaje del usuario | |
st.session_state.sidebar_messages.append( | |
{"role": "user", "content": user_input} | |
) | |
# Generar y mostrar respuesta | |
with chat_container: | |
with st.chat_message("assistant"): | |
message_placeholder = st.empty() | |
full_response = "" | |
for chunk in get_chatbot_response( | |
st.session_state.sidebar_chatbot, | |
user_input, | |
lang_code | |
): | |
full_response += chunk | |
message_placeholder.markdown(full_response + "▌") | |
message_placeholder.markdown(full_response) | |
# Guardar respuesta | |
st.session_state.sidebar_messages.append( | |
{"role": "assistant", "content": full_response} | |
) | |
# Guardar conversación | |
store_chat_history( | |
st.session_state.username, | |
'sidebar', | |
st.session_state.sidebar_messages | |
) | |
# Botón para limpiar chat | |
if st.button(t['clear_chat']): | |
st.session_state.sidebar_messages = [ | |
{"role": "assistant", "content": t['initial_message']} | |
] | |
st.rerun() |