JeCabrera commited on
Commit
e77ce9b
·
verified ·
1 Parent(s): c6be27d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -39
app.py CHANGED
@@ -255,47 +255,27 @@ def process_model_response(prompt, max_retries=3):
255
  return False
256
 
257
  # Modificar la función handle_example_click para usar las nuevas funciones
258
- # Simplificar a:
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
- 'gemini_history': [],
265
- 'chat_title': get_chat_title([])
266
- })
267
-
268
- # Añadir esta verificación crítica
269
- if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
270
- st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
271
- 'messages': [],
272
- 'gemini_history': [],
273
- 'title': 'Nuevo Chat'
274
- }
275
-
276
- process_model_response(prompt_text)
277
-
278
- # Modificar la sección principal de manejo de chat
279
- if prompt := st.chat_input('¿En qué puedo ayudarte hoy?'):
280
- is_first_message = len(st.session_state.messages) == 0
281
 
282
- if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
283
- st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
284
- 'messages': [],
285
- 'gemini_history': [],
286
- 'title': 'Nuevo Chat'
287
- }
288
-
289
- add_message('user', prompt, USER_AVATAR_ICON)
290
- with st.chat_message('user', avatar=USER_AVATAR_ICON):
291
- st.markdown(prompt)
292
 
293
- if is_first_message:
294
- add_message(MODEL_ROLE, WELCOME_MESSAGE, AI_AVATAR_ICON)
295
- with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON, key="welcome_msg"):
296
- st.markdown(WELCOME_MESSAGE)
297
- update_chat_memory()
298
- st.experimental_rerun()
299
 
300
  process_model_response(prompt)
301
 
@@ -328,4 +308,33 @@ if st.session_state.show_examples and not st.session_state.messages:
328
  handle_example_click("Necesito una PUV para mi servicio de consultoría en marketing digital")
329
 
330
  st.markdown("---")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
 
255
  return False
256
 
257
  # Modificar la función handle_example_click para usar las nuevas funciones
258
+ # Actualicé el código para generar títulos automáticos usando el modelo de Gemini. Aquí los cambios clave:
259
+ # En la sección principal de manejo de chat (línea ~286)
260
+ if is_first_message:
261
+ # Generar título basado en el primer mensaje del usuario
262
+ try:
263
+ title_response = st.session_state.model.generate_content(
264
+ f"Genera un título corto (máximo 5 palabras) que describa esta consulta, sin comillas: '{prompt}'"
265
+ )
266
+ generated_title = title_response.text.strip().replace('"', '')[:30]
267
+ st.session_state.chat_title = generated_title or f"Chat-{st.session_state.current_chat_id}"
268
+ except Exception as e:
269
+ st.session_state.chat_title = f"Chat-{st.session_state.current_chat_id}"
 
 
 
 
 
 
 
 
 
 
 
270
 
271
+ # Actualizar título en memoria
272
+ st.session_state.chats_in_memory[st.session_state.current_chat_id]['title'] = st.session_state.chat_title
 
 
 
 
 
 
 
 
273
 
274
+ add_message(MODEL_ROLE, WELCOME_MESSAGE, AI_AVATAR_ICON)
275
+ with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON, key="welcome_msg"):
276
+ st.markdown(WELCOME_MESSAGE)
277
+ update_chat_memory()
278
+ st.experimental_rerun()
 
279
 
280
  process_model_response(prompt)
281
 
 
308
  handle_example_click("Necesito una PUV para mi servicio de consultoría en marketing digital")
309
 
310
  st.markdown("---")
311
+
312
+ # Actualizar la función handle_example_click (línea ~250)
313
+ def handle_example_click(prompt_text):
314
+ st.session_state.update({
315
+ 'show_examples': False,
316
+ 'messages': [],
317
+ 'current_chat_id': str(time.time()),
318
+ 'gemini_history': [],
319
+ 'chat_title': 'Nuevo Chat' # Placeholder temporal
320
+ })
321
+
322
+ if st.session_state.current_chat_id not in st.session_state.chats_in_memory:
323
+ st.session_state.chats_in_memory[st.session_state.current_chat_id] = {
324
+ 'messages': [],
325
+ 'gemini_history': [],
326
+ 'title': 'Nuevo Chat'
327
+ }
328
+
329
+ # Generar título para ejemplos
330
+ try:
331
+ title_response = st.session_state.model.generate_content(
332
+ f"Título para consulta de ejemplo: '{prompt_text}' (máximo 4 palabras)"
333
+ )
334
+ st.session_state.chat_title = title_response.text.strip()[:25]
335
+ except Exception as e:
336
+ st.session_state.chat_title = f"Ejemplo-{time.strftime('%H:%M')}"
337
+
338
+ st.session_state.chats_in_memory[st.session_state.current_chat_id]['title'] = st.session_state.chat_title
339
+ process_model_response(prompt_text)
340