JeCabrera commited on
Commit
923a333
·
verified ·
1 Parent(s): b194d60

Upload 12 files

Browse files
Files changed (1) hide show
  1. app.py +35 -21
app.py CHANGED
@@ -66,41 +66,55 @@ def process_message(prompt, is_example=False):
66
 
67
  with st.chat_message(MODEL_ROLE, avatar=AI_AVATAR_ICON):
68
  try:
69
- # Forzar streaming siempre
 
 
 
 
 
70
  response = state.chat.send_message(
71
  enhanced_prompt,
72
- stream=True, # Asegurar que siempre sea True
73
- generation_config={"temperature": 0.9} # Mantener configuración consistente
74
  )
75
 
76
- message_placeholder = st.empty()
77
  full_response = ''
78
 
79
- # Añadir indicador de "escribiendo..."
80
- typing_indicator = st.empty()
81
- typing_indicator.markdown("*Generando respuesta...*")
82
-
83
- # Mostrar respuesta por fragmentos
84
- for chunk in response:
85
- for ch in chunk.text:
86
- full_response += ch
87
- time.sleep(0.01) # Mantener delay consistente
88
- message_placeholder.write(full_response + '▌')
 
 
 
89
 
90
- # Eliminar indicador de escritura
91
  typing_indicator.empty()
92
 
93
- # Mostrar respuesta completa
94
- message_placeholder.write(full_response)
95
 
96
- # Añadir respuesta al historial
97
  state.add_message(
98
  role=MODEL_ROLE,
99
- content=state.chat.history[-1].parts[0].text,
100
- avatar=AI_AVATAR_ICON,
101
  )
102
 
103
- state.gemini_history = state.chat.history
 
 
 
 
 
 
 
104
 
105
  # Guardar historial actualizado
106
  state.save_chat_history()
 
66
 
67
  with st.chat_message(MODEL_ROLE, avatar=AI_AVATAR_ICON):
68
  try:
69
+ # Configurar el placeholder y el indicador de escritura
70
+ message_placeholder = st.empty()
71
+ typing_indicator = st.empty()
72
+ typing_indicator.markdown("*Generando respuesta...*")
73
+
74
+ # Asegurar que siempre usamos streaming
75
  response = state.chat.send_message(
76
  enhanced_prompt,
77
+ stream=True,
78
+ generation_config={"temperature": 0.9}
79
  )
80
 
 
81
  full_response = ''
82
 
83
+ # Mejorar el manejo del streaming
84
+ try:
85
+ for chunk in response:
86
+ if chunk.text: # Verificar que hay texto en el chunk
87
+ for ch in chunk.text:
88
+ full_response += ch
89
+ time.sleep(0.01)
90
+ # Mantener el indicador mientras se escribe
91
+ typing_indicator.markdown("*Generando respuesta...*")
92
+ message_placeholder.markdown(full_response + '▌')
93
+ except Exception as e:
94
+ st.error(f"Error en el streaming: {str(e)}")
95
+ return
96
 
97
+ # Limpiar el indicador de escritura al finalizar
98
  typing_indicator.empty()
99
 
100
+ # Mostrar respuesta final
101
+ message_placeholder.markdown(full_response)
102
 
103
+ # Actualizar el historial
104
  state.add_message(
105
  role=MODEL_ROLE,
106
+ content=full_response,
107
+ avatar=AI_AVATAR_ICON
108
  )
109
 
110
+ # Guardar el historial actualizado
111
+ state.save_chat_history()
112
+
113
+ except Exception as e:
114
+ st.error(f"Error: {str(e)}")
115
+ return
116
+
117
+ state.gemini_history = state.chat.history
118
 
119
  # Guardar historial actualizado
120
  state.save_chat_history()