AIdeaText commited on
Commit
9bcf433
verified
1 Parent(s): 390251e

Update modules/chatbot/sidebar_chat.py

Browse files
Files changed (1) hide show
  1. modules/chatbot/sidebar_chat.py +35 -12
modules/chatbot/sidebar_chat.py CHANGED
@@ -11,25 +11,48 @@ def display_sidebar_chat(lang_code: str, chatbot_t: dict):
11
  Muestra el chatbot en el sidebar
12
  Args:
13
  lang_code: C贸digo del idioma
14
- chatbot_t: Traducciones del chatbot del archivo central
15
  """
 
 
 
 
 
 
 
 
 
 
 
 
16
  with st.sidebar:
17
  # Chatbot expandible
18
- with st.expander(chatbot_t.get('expand_chat', 'Open assistant'), expanded=False):
19
  try:
20
  # Inicializar procesador si no existe
21
  if 'chat_processor' not in st.session_state:
22
- st.session_state.chat_processor = ChatProcessor()
 
 
 
 
 
23
 
24
  # Inicializar mensajes si no existen
25
  if 'sidebar_messages' not in st.session_state:
26
  # Intentar recuperar historial previo
27
- history = get_chat_history(st.session_state.username, 'sidebar', 10)
28
- if history:
29
- st.session_state.sidebar_messages = history[0]['messages']
30
- else:
 
 
 
 
 
 
31
  st.session_state.sidebar_messages = [
32
- {"role": "assistant", "content": chatbot_t['initial_message']}
33
  ]
34
 
35
  # Contenedor del chat
@@ -43,7 +66,7 @@ def display_sidebar_chat(lang_code: str, chatbot_t: dict):
43
 
44
  # Input del usuario
45
  user_input = st.text_input(
46
- chatbot_t['input_placeholder'],
47
  key='sidebar_chat_input'
48
  )
49
 
@@ -81,12 +104,12 @@ def display_sidebar_chat(lang_code: str, chatbot_t: dict):
81
  )
82
 
83
  # Bot贸n para limpiar chat
84
- if st.button(chatbot_t['clear_chat']):
85
  st.session_state.sidebar_messages = [
86
- {"role": "assistant", "content": chatbot_t['initial_message']}
87
  ]
88
  st.rerun()
89
 
90
  except Exception as e:
91
  logger.error(f"Error en sidebar chat: {str(e)}")
92
- st.error(chatbot_t['error_message'])
 
11
  Muestra el chatbot en el sidebar
12
  Args:
13
  lang_code: C贸digo del idioma
14
+ chatbot_t: Diccionario de traducciones del chatbot
15
  """
16
+ # Asegurar que tenemos las traducciones necesarias
17
+ default_translations = {
18
+ 'error_message': 'An error occurred',
19
+ 'expand_chat': 'Open Assistant',
20
+ 'initial_message': 'Hi! How can I help?',
21
+ 'input_placeholder': 'Type your message...',
22
+ 'clear_chat': 'Clear chat'
23
+ }
24
+
25
+ # Combinar traducciones por defecto con las proporcionadas
26
+ translations = {**default_translations, **chatbot_t}
27
+
28
  with st.sidebar:
29
  # Chatbot expandible
30
+ with st.expander(translations['expand_chat'], expanded=False):
31
  try:
32
  # Inicializar procesador si no existe
33
  if 'chat_processor' not in st.session_state:
34
+ try:
35
+ st.session_state.chat_processor = ChatProcessor()
36
+ except Exception as e:
37
+ logger.error(f"Error inicializando ChatProcessor: {str(e)}")
38
+ st.error("Error: No se pudo inicializar el chat. Verifica la configuraci贸n.")
39
+ return
40
 
41
  # Inicializar mensajes si no existen
42
  if 'sidebar_messages' not in st.session_state:
43
  # Intentar recuperar historial previo
44
+ try:
45
+ history = get_chat_history(st.session_state.username, 'sidebar', 10)
46
+ if history:
47
+ st.session_state.sidebar_messages = history[0]['messages']
48
+ else:
49
+ st.session_state.sidebar_messages = [
50
+ {"role": "assistant", "content": translations['initial_message']}
51
+ ]
52
+ except Exception as e:
53
+ logger.error(f"Error recuperando historial: {str(e)}")
54
  st.session_state.sidebar_messages = [
55
+ {"role": "assistant", "content": translations['initial_message']}
56
  ]
57
 
58
  # Contenedor del chat
 
66
 
67
  # Input del usuario
68
  user_input = st.text_input(
69
+ translations['input_placeholder'],
70
  key='sidebar_chat_input'
71
  )
72
 
 
104
  )
105
 
106
  # Bot贸n para limpiar chat
107
+ if st.button(translations['clear_chat']):
108
  st.session_state.sidebar_messages = [
109
+ {"role": "assistant", "content": translations['initial_message']}
110
  ]
111
  st.rerun()
112
 
113
  except Exception as e:
114
  logger.error(f"Error en sidebar chat: {str(e)}")
115
+ st.error(translations['error_message'])