Segizu commited on
Commit
c9b8157
·
1 Parent(s): 095d11a

Añadir cambios al chatbot

Browse files
Files changed (2) hide show
  1. app.py +35 -25
  2. utils.py +1 -1
app.py CHANGED
@@ -4,34 +4,44 @@ from utils import get_bot_response
4
  # Configuración de la página
5
  st.set_page_config(page_title="Chatbot de Servicios Digitales", layout="centered")
6
 
7
- # Título de la app
8
- st.title("Chatbot de Servicios Digitales")
9
- st.write("¡Hola! Soy tu asistente virtual. Estoy aquí para ayudarte con servicios de desarrollo web, apps móviles, ideas de IA y análisis con Power BI.")
10
-
11
- # Inicializar el historial de chat en la sesión
12
  if "chat_history" not in st.session_state:
13
  st.session_state.chat_history = []
14
 
15
- # Función para mostrar el historial de mensajes
16
- def display_chat():
17
- for chat in st.session_state.chat_history:
18
- if chat["role"] == "user":
19
- st.markdown(f"**Tú:** {chat['message']}")
20
- else:
21
- st.markdown(f"**Bot:** {chat['message']}")
22
 
23
- # Mostrar la conversación previa
24
- display_chat()
25
 
26
- # Formulario para enviar nuevos mensajes
27
- with st.form(key="chat_form", clear_on_submit=True):
28
- user_input = st.text_input("Escribe tu mensaje aquí:")
29
- submit_button = st.form_submit_button(label="Enviar")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- if submit_button and user_input:
32
- # Agregar mensaje del usuario al historial
33
- st.session_state.chat_history.append({"role": "user", "message": user_input})
34
- # Obtener respuesta del bot desde utils.py
35
- bot_response = get_bot_response(user_input)
36
- st.session_state.chat_history.append({"role": "bot", "message": bot_response})
37
- # Al no usar experimental_rerun, el script se volverá a ejecutar automáticamente en la siguiente interacción
 
4
  # Configuración de la página
5
  st.set_page_config(page_title="Chatbot de Servicios Digitales", layout="centered")
6
 
7
+ # Inicializar el historial del chat en la sesión
 
 
 
 
8
  if "chat_history" not in st.session_state:
9
  st.session_state.chat_history = []
10
 
11
+ # Título y mensaje de bienvenida
12
+ st.title("Chatbot de Servicios Digitales")
13
+ st.write(
14
+ "¡Hola! Soy tu asistente virtual. Estoy aquí para ayudarte con servicios de desarrollo web, "
15
+ "apps móviles, ideas de IA y análisis con Power BI."
16
+ )
 
17
 
18
+ # Campo de texto para ingresar el mensaje del usuario (usando una key para poder manipular su valor)
19
+ user_input = st.text_input("Escribe tu mensaje aquí:", key="user_input")
20
 
21
+ # Botón de envío
22
+ if st.button("Enviar"):
23
+ if st.session_state.user_input: # Verifica que se haya escrito algo
24
+ # Agregar el mensaje del usuario al historial
25
+ st.session_state.chat_history.append({
26
+ "role": "user",
27
+ "message": st.session_state.user_input
28
+ })
29
+ # Obtener respuesta del bot y agregarla al historial
30
+ response = get_bot_response(st.session_state.user_input)
31
+ st.session_state.chat_history.append({
32
+ "role": "bot",
33
+ "message": response
34
+ })
35
+ # Limpiar el campo de texto
36
+ st.session_state.user_input = ""
37
+ # Forzar la actualización de la interfaz
38
+ st.experimental_rerun()
39
+ else:
40
+ st.warning("Por favor, ingresa un mensaje.")
41
 
42
+ # Mostrar el historial actualizado del chat
43
+ for chat in st.session_state.chat_history:
44
+ if chat["role"] == "user":
45
+ st.markdown(f"**Tú:** {chat['message']}")
46
+ else:
47
+ st.markdown(f"**Bot:** {chat['message']}")
 
utils.py CHANGED
@@ -16,4 +16,4 @@ def get_bot_response(user_message: str) -> str:
16
  elif "hola" in message or "buenos días" in message or "saludos" in message:
17
  return "¡Hola! ¿En qué puedo ayudarte hoy?"
18
  else:
19
- return "Gracias por tu mensaje. ¿Podrías darme más detalles o especificar en qué área estás interesado?"
 
16
  elif "hola" in message or "buenos días" in message or "saludos" in message:
17
  return "¡Hola! ¿En qué puedo ayudarte hoy?"
18
  else:
19
+ return "Gracias por tu mensaje. ¿Podrías darme más detalles o especificar en qué área estás interesado?"