Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -56,28 +56,28 @@ with st.sidebar:
|
|
56 |
with col2:
|
57 |
st.image("assets/robocopy_logo.png", width=300)
|
58 |
|
59 |
-
st.write('#
|
60 |
if st.session_state.get('chat_id') is None:
|
61 |
st.session_state.chat_id = st.selectbox(
|
62 |
-
label='
|
63 |
options=[new_chat_id] + list(past_chats.keys()),
|
64 |
-
format_func=lambda x: past_chats.get(x, '
|
65 |
placeholder='_',
|
66 |
)
|
67 |
else:
|
68 |
# This will happen the first time AI response comes in
|
69 |
st.session_state.chat_id = st.selectbox(
|
70 |
-
label='
|
71 |
options=[new_chat_id, st.session_state.chat_id] + list(past_chats.keys()),
|
72 |
index=1,
|
73 |
-
format_func=lambda x: past_chats.get(x, '
|
74 |
placeholder='_',
|
75 |
)
|
76 |
# Save new chats after a message has been sent to AI
|
77 |
# TODO: Give user a chance to name chat
|
78 |
-
st.session_state.chat_title = f'
|
79 |
|
80 |
-
st.write('#
|
81 |
|
82 |
# Chat history (allows to ask multiple questions)
|
83 |
try:
|
@@ -109,8 +109,36 @@ for message in st.session_state.messages:
|
|
109 |
if prompt := st.chat_input('¿En qué puedo ayudarte hoy?'): # Mensaje más amigable
|
110 |
# Save this as a chat for later
|
111 |
if st.session_state.chat_id not in past_chats.keys():
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Display user message in chat message container
|
115 |
with st.chat_message('user', avatar=USER_AVATAR_ICON): # Añade el avatar del usuario
|
116 |
st.markdown(prompt)
|
|
|
56 |
with col2:
|
57 |
st.image("assets/robocopy_logo.png", width=300)
|
58 |
|
59 |
+
st.write('# Chats Anteriores')
|
60 |
if st.session_state.get('chat_id') is None:
|
61 |
st.session_state.chat_id = st.selectbox(
|
62 |
+
label='Selecciona un chat anterior',
|
63 |
options=[new_chat_id] + list(past_chats.keys()),
|
64 |
+
format_func=lambda x: past_chats.get(x, 'Nuevo Chat'),
|
65 |
placeholder='_',
|
66 |
)
|
67 |
else:
|
68 |
# This will happen the first time AI response comes in
|
69 |
st.session_state.chat_id = st.selectbox(
|
70 |
+
label='Selecciona un chat anterior',
|
71 |
options=[new_chat_id, st.session_state.chat_id] + list(past_chats.keys()),
|
72 |
index=1,
|
73 |
+
format_func=lambda x: past_chats.get(x, 'Nuevo Chat' if x != st.session_state.chat_id else st.session_state.chat_title),
|
74 |
placeholder='_',
|
75 |
)
|
76 |
# Save new chats after a message has been sent to AI
|
77 |
# TODO: Give user a chance to name chat
|
78 |
+
st.session_state.chat_title = f'SesiónChat-{st.session_state.chat_id}'
|
79 |
|
80 |
+
st.write('# Chatea con Gemini')
|
81 |
|
82 |
# Chat history (allows to ask multiple questions)
|
83 |
try:
|
|
|
109 |
if prompt := st.chat_input('¿En qué puedo ayudarte hoy?'): # Mensaje más amigable
|
110 |
# Save this as a chat for later
|
111 |
if st.session_state.chat_id not in past_chats.keys():
|
112 |
+
# Es una nueva conversación, generemos un título basado en el primer mensaje
|
113 |
+
# Primero, guardamos un título temporal
|
114 |
+
temp_title = f'SesiónChat-{st.session_state.chat_id}'
|
115 |
+
past_chats[st.session_state.chat_id] = temp_title
|
116 |
+
|
117 |
+
# Generamos un título basado en el contenido del mensaje
|
118 |
+
try:
|
119 |
+
# Usamos el mismo modelo para generar un título corto
|
120 |
+
title_generator = genai.GenerativeModel('gemini-2.0-flash')
|
121 |
+
title_response = title_generator.generate_content(
|
122 |
+
f"Genera un título corto (máximo 5 palabras) que describa de qué trata esta consulta, sin usar comillas ni puntuación: '{prompt}'")
|
123 |
+
|
124 |
+
# Obtenemos el título generado
|
125 |
+
generated_title = title_response.text.strip()
|
126 |
+
|
127 |
+
# Actualizamos el título en past_chats
|
128 |
+
if generated_title:
|
129 |
+
st.session_state.chat_title = generated_title
|
130 |
+
past_chats[st.session_state.chat_id] = generated_title
|
131 |
+
else:
|
132 |
+
st.session_state.chat_title = temp_title
|
133 |
+
except Exception as e:
|
134 |
+
print(f"Error al generar título: {e}")
|
135 |
+
st.session_state.chat_title = temp_title
|
136 |
+
else:
|
137 |
+
# Ya existe esta conversación, usamos el título guardado
|
138 |
+
st.session_state.chat_title = past_chats[st.session_state.chat_id]
|
139 |
+
|
140 |
+
joblib.dump(past_chats, 'data/past_chats_list')
|
141 |
+
|
142 |
# Display user message in chat message container
|
143 |
with st.chat_message('user', avatar=USER_AVATAR_ICON): # Añade el avatar del usuario
|
144 |
st.markdown(prompt)
|