AIdeaText commited on
Commit
098b351
1 Parent(s): 8576dc4

Update modules/ui.py

Browse files
Files changed (1) hide show
  1. modules/ui.py +21 -25
modules/ui.py CHANGED
@@ -413,17 +413,16 @@ def display_chatbot_interface(lang_code):
413
  }
414
  t = translations[lang_code]
415
  st.title(t['title'])
 
416
  if 'chatbot' not in st.session_state:
417
  st.session_state.chatbot = initialize_chatbot()
418
  if 'messages' not in st.session_state:
419
  st.session_state.messages = [{"role": "assistant", "content": t['initial_message']}]
420
  if 'message_id' not in st.session_state:
421
  st.session_state.message_id = 0
422
- if 'message_counter' not in st.session_state:
423
- st.session_state.message_counter = 0
424
-
425
  chat_container = st.container()
426
-
427
  # Mostrar mensajes existentes
428
  with chat_container:
429
  for message in st.session_state.messages:
@@ -435,40 +434,37 @@ def display_chatbot_interface(lang_code):
435
 
436
  # Área de entrada del usuario
437
  user_input = st.text_input(t['input_placeholder'], key="user_input")
438
-
439
  # Procesamiento del input del usuario
440
  if user_input:
441
  st.session_state.messages.append({"role": "user", "content": user_input})
442
- st.session_state.message_id += 1
443
- with chat_container:
444
- st.text_area("You:", value=user_input, height=50, key=f"user_msg_{st.session_state.message_id}", disabled=True)
445
 
446
  # Generar respuesta del chatbot
447
- try:
448
  response = get_chatbot_response(st.session_state.chatbot, user_input, lang_code)
449
  except Exception as e:
450
  st.error(f"Error al generar la respuesta del chatbot: {str(e)}")
451
  response = "Lo siento, hubo un error al procesar tu solicitud."
452
-
453
- st.session_state.messages.append({"role": "assistant", "content": response})
454
- st.session_state.message_id += 1
455
 
456
- # Mostrar la respuesta del chatbot con efecto de escritura
457
- with chat_container:
458
- assistant_message = st.empty()
459
- full_response = ""
460
- for char in response:
461
- full_response += char
462
- assistant_message.text_area("Assistant:", value=full_response + "▌", height=50, key=f"assistant_msg_{st.session_state.message_id}_typing_{st.session_state.message_counter}", disabled=True)
463
- st.session_state.message_counter += 1
464
- time.sleep(0.01)
465
- assistant_message.text_area("Assistant:", value=full_response, height=50, key=f"assistant_msg_{st.session_state.message_id}", disabled=True)
466
 
467
  # Guardar la conversación en la base de datos
468
  store_chat_history(st.session_state.username, st.session_state.messages)
469
 
470
  # Limpiar el input del usuario
471
  st.session_state.user_input = ""
472
-
473
- # Forzar una actualización de la interfaz
474
- st.experimental_rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  }
414
  t = translations[lang_code]
415
  st.title(t['title'])
416
+
417
  if 'chatbot' not in st.session_state:
418
  st.session_state.chatbot = initialize_chatbot()
419
  if 'messages' not in st.session_state:
420
  st.session_state.messages = [{"role": "assistant", "content": t['initial_message']}]
421
  if 'message_id' not in st.session_state:
422
  st.session_state.message_id = 0
423
+
 
 
424
  chat_container = st.container()
425
+
426
  # Mostrar mensajes existentes
427
  with chat_container:
428
  for message in st.session_state.messages:
 
434
 
435
  # Área de entrada del usuario
436
  user_input = st.text_input(t['input_placeholder'], key="user_input")
437
+
438
  # Procesamiento del input del usuario
439
  if user_input:
440
  st.session_state.messages.append({"role": "user", "content": user_input})
 
 
 
441
 
442
  # Generar respuesta del chatbot
443
+ try:
444
  response = get_chatbot_response(st.session_state.chatbot, user_input, lang_code)
445
  except Exception as e:
446
  st.error(f"Error al generar la respuesta del chatbot: {str(e)}")
447
  response = "Lo siento, hubo un error al procesar tu solicitud."
 
 
 
448
 
449
+ st.session_state.messages.append({"role": "assistant", "content": response})
 
 
 
 
 
 
 
 
 
450
 
451
  # Guardar la conversación en la base de datos
452
  store_chat_history(st.session_state.username, st.session_state.messages)
453
 
454
  # Limpiar el input del usuario
455
  st.session_state.user_input = ""
456
+
457
+ # Forzar una actualización de la interfaz
458
+ st.experimental_rerun()
459
+
460
+ # Mostrar el último mensaje del asistente con efecto de escritura
461
+ if st.session_state.messages and st.session_state.messages[-1]["role"] == "assistant":
462
+ last_message = st.session_state.messages[-1]["content"]
463
+ with chat_container:
464
+ assistant_message = st.empty()
465
+ full_response = ""
466
+ for char in last_message:
467
+ full_response += char
468
+ assistant_message.text_area("Assistant:", value=full_response + "▌", height=50, key=f"assistant_msg_typing", disabled=True)
469
+ time.sleep(0.01)
470
+ assistant_message.text_area("Assistant:", value=full_response, height=50, key=f"assistant_msg_final", disabled=True)