JeCabrera commited on
Commit
58645bb
·
verified ·
1 Parent(s): 549961d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -31
app.py CHANGED
@@ -150,9 +150,12 @@ def handle_example_click(prompt_text):
150
  'current_chat_id': str(time.time()),
151
  'gemini_history': [],
152
  'chat_title': 'Nuevo Chat',
153
- 'user_input': prompt_text # Añadir el texto al estado
154
  })
 
155
 
 
 
156
  if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
157
  st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
158
  'messages': [],
@@ -292,43 +295,55 @@ if st.session_state.show_examples and not st.session_state.messages:
292
  # === ENTRADA DEL USUARIO ===
293
  if 'user_input' in st.session_state:
294
  prompt = st.session_state.user_input
295
- del st.session_state.user_input # Limpiar después de usar
296
- else:
297
- prompt = st.chat_input('¿En qué puedo ayudarte hoy?')
298
-
299
- if prompt:
300
  # Simular el envío del mensaje
301
  with st.chat_message("user", avatar=USER_AVATAR_ICON):
302
- st.markdown(prompt) # Cambiado de prompt_text a prompt
303
- add_message("user", prompt, USER_AVATAR_ICON) # Cambiado de prompt_text a prompt
304
-
305
- # Procesar la respuesta
306
- process_model_response(prompt) # Cambiado de prompt_text a prompt
307
-
308
- is_first_message = len(st.session_state.messages) == 0
309
-
310
- if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
311
- st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
312
- 'messages': [],
313
- 'gemini_history': [],
314
- 'title': 'Nuevo Chat'
315
- }
316
 
 
317
  try:
318
  title_response = st.session_state.model.generate_content(
319
- f"Genera un título corto (máximo 5 palabras) que describa esta consulta, sin comillas: '{prompt}'"
320
  )
321
- generated_title = title_response.text.strip().replace('"', '')[:30]
322
- st.session_state.chat_title = generated_title or f"Chat-{st.session_state.current_chat_id}"
323
  except Exception as e:
324
- st.session_state.chat_title = f"Chat-{st.session_state.current_chat_id}"
325
 
326
- st.session_state.chats_in_memory[st.session_state.current_chat_id]['title'] = st.session_state.chat_title
 
 
 
 
327
 
328
- if is_first_message:
329
- add_message(MODEL_ROLE, WELCOME_MESSAGE, AI_AVATAR_ICON)
330
- with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON):
331
- st.markdown(WELCOME_MESSAGE)
332
  update_chat_memory()
333
-
334
- process_model_response(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  'current_chat_id': str(time.time()),
151
  'gemini_history': [],
152
  'chat_title': 'Nuevo Chat',
153
+ 'user_input': prompt_text
154
  })
155
+ st.rerun() # Forzar recarga de la página
156
 
157
+ # Eliminar todo el código restante de esta función
158
+ # El procesamiento se hará en la sección de entrada principal
159
  if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
160
  st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
161
  'messages': [],
 
295
  # === ENTRADA DEL USUARIO ===
296
  if 'user_input' in st.session_state:
297
  prompt = st.session_state.user_input
298
+ del st.session_state.user_input
299
+
 
 
 
300
  # Simular el envío del mensaje
301
  with st.chat_message("user", avatar=USER_AVATAR_ICON):
302
+ st.markdown(prompt)
303
+ add_message("user", prompt, USER_AVATAR_ICON)
 
 
 
 
 
 
 
 
 
 
 
 
304
 
305
+ # Actualizar título antes de procesar
306
  try:
307
  title_response = st.session_state.model.generate_content(
308
+ f"Título para consulta: '{prompt}' (máximo 4 palabras)"
309
  )
310
+ st.session_state.chat_title = title_response.text.strip()[:25]
 
311
  except Exception as e:
312
+ st.session_state.chat_title = f"Ejemplo-{time.strftime('%H:%M')}"
313
 
314
+ st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
315
+ 'messages': st.session_state.messages,
316
+ 'gemini_history': st.session_state.gemini_history,
317
+ 'title': st.session_state.chat_title
318
+ }
319
 
320
+ # Procesar la respuesta una sola vez
321
+ process_model_response(prompt)
 
 
322
  update_chat_memory()
323
+
324
+ else:
325
+ prompt = st.chat_input('¿En qué puedo ayudarte hoy?')
326
+ if prompt:
327
+ # Simular el envío del mensaje
328
+ with st.chat_message("user", avatar=USER_AVATAR_ICON):
329
+ st.markdown(prompt)
330
+ add_message("user", prompt, USER_AVATAR_ICON)
331
+
332
+ # Actualizar título antes de procesar
333
+ try:
334
+ title_response = st.session_state.model.generate_content(
335
+ f"Título para consulta: '{prompt}' (máximo 4 palabras)"
336
+ )
337
+ st.session_state.chat_title = title_response.text.strip()[:25]
338
+ except Exception as e:
339
+ st.session_state.chat_title = f"Chat-{time.strftime('%H:%M')}"
340
+
341
+ st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
342
+ 'messages': st.session_state.messages,
343
+ 'gemini_history': st.session_state.gemini_history,
344
+ 'title': st.session_state.chat_title
345
+ }
346
+
347
+ # Procesar la respuesta una sola vez
348
+ process_model_response(prompt)
349
+ update_chat_memory()