JeCabrera commited on
Commit
8bcda54
verified
1 Parent(s): 5ce2734

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -128
app.py CHANGED
@@ -126,20 +126,16 @@ WELCOME_MESSAGE = """
126
  """
127
 
128
  # Inicializar el estado de la sesi贸n
 
129
  if 'chats_in_memory' not in st.session_state:
130
- st.session_state.chats_in_memory = {}
131
-
132
- if 'current_chat_id' not in st.session_state:
133
- st.session_state.current_chat_id = new_chat_id
134
-
135
- if 'chat_title' not in st.session_state:
136
- st.session_state.chat_title = 'Nuevo Chat'
137
-
138
- if 'messages' not in st.session_state:
139
- st.session_state.messages = []
140
-
141
- if 'show_examples' not in st.session_state:
142
- st.session_state.show_examples = True
143
 
144
  # Sidebar allows a list of past chats
145
  with st.sidebar:
@@ -154,12 +150,13 @@ with st.sidebar:
154
  chat_options = [new_chat_id] + list(st.session_state.chats_in_memory.keys())
155
  current_index = chat_options.index(st.session_state.current_chat_id) if st.session_state.current_chat_id in chat_options else 0
156
 
 
157
  st.session_state.current_chat_id = st.selectbox(
158
- label='Selecciona un chat anterior',
159
- options=chat_options,
160
- index=current_index,
161
- format_func=lambda x: st.session_state.chats_in_memory.get(x, {}).get('title', 'Nuevo Chat') if x != new_chat_id else 'Nuevo Chat',
162
- key='chat_selector'
163
  )
164
 
165
  # Bot贸n para borrar el historial
@@ -169,22 +166,7 @@ with st.sidebar:
169
  st.session_state.messages = []
170
  st.session_state.gemini_history = []
171
  st.session_state.chat_title = 'Nuevo Chat'
172
- st.rerun()
173
-
174
- # Inicializar mensajes y cargar historial
175
- if not st.session_state.get('messages'):
176
- st.session_state.messages = []
177
-
178
- # Cargar historial de chat si existe en memoria
179
- if st.session_state.current_chat_id in st.session_state.chats_in_memory:
180
- chat_data = st.session_state.chats_in_memory[st.session_state.current_chat_id]
181
- st.session_state.messages = chat_data.get('messages', [])
182
- st.session_state.gemini_history = chat_data.get('gemini_history', [])
183
- st.session_state.chat_title = chat_data.get('title', 'Nuevo Chat')
184
- else:
185
- st.session_state.messages = []
186
- st.session_state.gemini_history = []
187
- st.session_state.chat_title = 'Nuevo Chat'
188
 
189
  # Configuraci贸n del modelo
190
  model = genai.GenerativeModel(model_name='gemini-2.0-flash')
@@ -256,24 +238,15 @@ def process_model_response(prompt, max_retries=3):
256
  return False
257
 
258
  # Modificar la funci贸n handle_example_click para usar las nuevas funciones
 
259
  def handle_example_click(prompt_text):
260
  st.session_state.update({
261
  'show_examples': False,
262
  'messages': [],
263
- 'current_chat_id': str(time.time())
 
 
264
  })
265
-
266
- add_message('user', prompt_text, USER_AVATAR_ICON)
267
- with st.chat_message('user', avatar=USER_AVATAR_ICON):
268
- st.markdown(prompt_text)
269
-
270
- if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
271
- st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
272
- 'messages': st.session_state.messages,
273
- 'gemini_history': st.session_state.gemini_history,
274
- 'title': 'Nuevo Chat'
275
- }
276
-
277
  process_model_response(prompt_text)
278
 
279
  # Modificar la secci贸n principal de manejo de chat
@@ -293,10 +266,10 @@ if prompt := st.chat_input('驴En qu茅 puedo ayudarte hoy?'):
293
 
294
  if is_first_message:
295
  add_message(MODEL_ROLE, WELCOME_MESSAGE, AI_AVATAR_ICON)
296
- with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON):
297
  st.markdown(WELCOME_MESSAGE)
298
  update_chat_memory()
299
- st.rerun()
300
 
301
  process_model_response(prompt)
302
 
@@ -330,82 +303,3 @@ if st.session_state.show_examples and not st.session_state.messages:
330
 
331
  st.markdown("---")
332
 
333
- # Procesar el prompt temporal si existe
334
- if hasattr(st.session_state, 'temp_prompt') and st.session_state.temp_prompt:
335
- prompt = st.session_state.temp_prompt
336
- delattr(st.session_state, 'temp_prompt')
337
-
338
- # Mostrar mensaje del usuario
339
- with st.chat_message('user', avatar=USER_AVATAR_ICON):
340
- st.markdown(prompt)
341
-
342
- # A帽adir mensaje del usuario al historial
343
- st.session_state.messages.append({
344
- 'role': 'user',
345
- 'content': prompt,
346
- 'avatar': USER_AVATAR_ICON
347
- })
348
-
349
- # Asegurarnos de que el chat existe en memoria antes de actualizarlo
350
- if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
351
- st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
352
- 'messages': [],
353
- 'gemini_history': [],
354
- 'title': 'Nuevo Chat'
355
- }
356
-
357
- # Actualizar chat en memoria
358
- st.session_state.chats_in_memory[st.session_state.current_chat_id].update({
359
- 'messages': st.session_state.messages,
360
- 'gemini_history': st.session_state.gemini_history,
361
- 'title': st.session_state.chat_title
362
- })
363
- st.rerun()
364
- st.rerun()
365
-
366
- # Modificar la secci贸n de historial de chat
367
- with st.sidebar:
368
- # Centrar el logo y eliminar el t铆tulo de RoboCopy
369
- col1, col2, col3 = st.columns([1, 2, 1])
370
- with col2:
371
- st.image("assets/robocopy_logo.png", width=300)
372
-
373
- st.write('# Chats Anteriores')
374
- if st.session_state.get('current_chat_id') is None:
375
- st.session_state.current_chat_id = st.selectbox(
376
- label='Selecciona un chat anterior',
377
- options=[new_chat_id] + list(st.session_state.chats_in_memory.keys()),
378
- format_func=lambda x: st.session_state.chats_in_memory.get(x, {}).get('title', 'Nuevo Chat') if isinstance(x, str) else 'Nuevo Chat',
379
- placeholder='_',
380
- )
381
- else:
382
- st.session_state.current_chat_id = st.selectbox(
383
- label='Selecciona un chat anterior',
384
- options=[new_chat_id, st.session_state.current_chat_id] + list(st.session_state.chats_in_memory.keys()),
385
- index=1,
386
- format_func=lambda x: st.session_state.chats_in_memory.get(x, {}).get('title', 'Nuevo Chat') if isinstance(x, str) else 'Nuevo Chat',
387
- placeholder='_',
388
- )
389
-
390
- # Modificar el selectbox para el historial
391
- chat_options = [new_chat_id] + list(st.session_state.chats_in_memory.keys())
392
- selected_chat = st.selectbox(
393
- label='Selecciona un chat anterior',
394
- options=chat_options,
395
- format_func=lambda x: st.session_state.chats_in_memory.get(x, {}).get('title', 'Nuevo Chat'),
396
- index=chat_options.index(st.session_state.current_chat_id) if st.session_state.current_chat_id in chat_options else 0
397
- )
398
-
399
- # Actualizar el chat actual si cambia la selecci贸n
400
- if selected_chat != st.session_state.current_chat_id:
401
- st.session_state.current_chat_id = selected_chat
402
- if selected_chat in st.session_state.chats_in_memory:
403
- st.session_state.messages = st.session_state.chats_in_memory[selected_chat].get('messages', [])
404
- st.session_state.gemini_history = st.session_state.chats_in_memory[selected_chat].get('gemini_history', [])
405
- st.session_state.chat_title = st.session_state.chats_in_memory[selected_chat].get('title', 'Nuevo Chat')
406
- else:
407
- st.session_state.messages = []
408
- st.session_state.gemini_history = []
409
- st.session_state.chat_title = 'Nuevo Chat'
410
- st.rerun()
411
- st.rerun()
 
126
  """
127
 
128
  # Inicializar el estado de la sesi贸n
129
+ # Inicializaci贸n unificada
130
  if 'chats_in_memory' not in st.session_state:
131
+ st.session_state.update({
132
+ 'chats_in_memory': {},
133
+ 'current_chat_id': new_chat_id,
134
+ 'chat_title': 'Nuevo Chat',
135
+ 'messages': [],
136
+ 'show_examples': True,
137
+ 'gemini_history': []
138
+ })
 
 
 
 
 
139
 
140
  # Sidebar allows a list of past chats
141
  with st.sidebar:
 
150
  chat_options = [new_chat_id] + list(st.session_state.chats_in_memory.keys())
151
  current_index = chat_options.index(st.session_state.current_chat_id) if st.session_state.current_chat_id in chat_options else 0
152
 
153
+ # Modificar a:
154
  st.session_state.current_chat_id = st.selectbox(
155
+ label='Selecciona un chat anterior',
156
+ options=chat_options,
157
+ index=current_index,
158
+ format_func=lambda x: st.session_state.chats_in_memory.get(x, {}).get('title', 'Nuevo Chat'),
159
+ key='chat_selector_unique'
160
  )
161
 
162
  # Bot贸n para borrar el historial
 
166
  st.session_state.messages = []
167
  st.session_state.gemini_history = []
168
  st.session_state.chat_title = 'Nuevo Chat'
169
+ st.experimental_rerun() # Evitar loops
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  # Configuraci贸n del modelo
172
  model = genai.GenerativeModel(model_name='gemini-2.0-flash')
 
238
  return False
239
 
240
  # Modificar la funci贸n handle_example_click para usar las nuevas funciones
241
+ # Simplificar a:
242
  def handle_example_click(prompt_text):
243
  st.session_state.update({
244
  'show_examples': False,
245
  'messages': [],
246
+ 'current_chat_id': str(time.time()),
247
+ 'gemini_history': [],
248
+ 'chat_title': get_chat_title([])
249
  })
 
 
 
 
 
 
 
 
 
 
 
 
250
  process_model_response(prompt_text)
251
 
252
  # Modificar la secci贸n principal de manejo de chat
 
266
 
267
  if is_first_message:
268
  add_message(MODEL_ROLE, WELCOME_MESSAGE, AI_AVATAR_ICON)
269
+ with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON, key="welcome_msg"):
270
  st.markdown(WELCOME_MESSAGE)
271
  update_chat_memory()
272
+ st.experimental_rerun()
273
 
274
  process_model_response(prompt)
275
 
 
303
 
304
  st.markdown("---")
305