JeCabrera commited on
Commit
5a16f21
verified
1 Parent(s): 5ec6171

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -31,28 +31,13 @@ def process_message(prompt, is_example=False):
31
  st.markdown(prompt)
32
 
33
  state.add_message('user', prompt, USER_AVATAR_ICON)
34
-
35
- # Obtener el prompt mejorado primero
36
  enhanced_prompt = get_enhanced_prompt(prompt, is_example)
37
 
38
- # Mover la respuesta del modelo despu茅s del mensaje del usuario
39
- with st.chat_message(MODEL_ROLE, avatar=AI_AVATAR_ICON):
40
- try:
41
- message_placeholder = st.empty()
42
- typing_indicator = st.empty()
43
- typing_indicator.markdown("*Generando respuesta...*")
44
-
45
- response = state.send_message(enhanced_prompt)
46
- full_response = stream_response(response, message_placeholder, typing_indicator)
47
-
48
- if full_response:
49
- state.add_message(MODEL_ROLE, full_response, AI_AVATAR_ICON)
50
- state.gemini_history = state.chat.history
51
- state.save_chat_history()
52
-
53
- except Exception as e:
54
- st.error(f"Error en el streaming: {str(e)}")
55
- return
56
 
57
  def handle_chat_title(prompt):
58
  """Maneja la l贸gica del t铆tulo del chat"""
@@ -81,13 +66,17 @@ def process_model_response(enhanced_prompt):
81
  typing_indicator = st.empty()
82
  typing_indicator.markdown("*Generando respuesta...*")
83
 
 
 
 
84
  response = state.send_message(enhanced_prompt)
85
- full_response = stream_response(response, message_placeholder, typing_indicator)
 
86
 
87
- # Actualizar historial
88
- state.add_message(role=MODEL_ROLE, content=full_response, avatar=AI_AVATAR_ICON)
89
- state.gemini_history = state.chat.history
90
- state.save_chat_history()
91
 
92
  except Exception as e:
93
  st.error(f"Error: {str(e)}")
@@ -277,3 +266,35 @@ if state.has_prompt():
277
  process_message(prompt, is_example=True)
278
  # Limpiar el prompt
279
  state.clear_prompt()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  st.markdown(prompt)
32
 
33
  state.add_message('user', prompt, USER_AVATAR_ICON)
 
 
34
  enhanced_prompt = get_enhanced_prompt(prompt, is_example)
35
 
36
+ # Aplicar el efecto de desvanecimiento
37
+ apply_fade_in_effect()
38
+
39
+ # Procesar respuesta del modelo con el nuevo efecto
40
+ process_model_response(enhanced_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def handle_chat_title(prompt):
43
  """Maneja la l贸gica del t铆tulo del chat"""
 
66
  typing_indicator = st.empty()
67
  typing_indicator.markdown("*Generando respuesta...*")
68
 
69
+ # Aplicar el efecto de desvanecimiento
70
+ apply_fade_in_effect()
71
+
72
  response = state.send_message(enhanced_prompt)
73
+ # Usar la nueva funci贸n con efecto de desvanecimiento
74
+ full_response = stream_response_with_fade(response, message_placeholder, typing_indicator)
75
 
76
+ if full_response:
77
+ state.add_message(MODEL_ROLE, full_response, AI_AVATAR_ICON)
78
+ state.gemini_history = state.chat.history
79
+ state.save_chat_history()
80
 
81
  except Exception as e:
82
  st.error(f"Error: {str(e)}")
 
266
  process_message(prompt, is_example=True)
267
  # Limpiar el prompt
268
  state.clear_prompt()
269
+
270
+ def apply_fade_in_effect():
271
+ """Aplica un efecto de desvanecimiento para el texto"""
272
+ st.markdown("""
273
+ <style>
274
+ @keyframes fadeIn {
275
+ from { opacity: 0; }
276
+ to { opacity: 1; }
277
+ }
278
+ .fade-in-text {
279
+ animation: fadeIn 0.5s ease-in;
280
+ }
281
+ </style>
282
+ """, unsafe_allow_html=True)
283
+
284
+ def stream_response_with_fade(response, message_placeholder, typing_indicator):
285
+ """Maneja el streaming de la respuesta con efecto de desvanecimiento"""
286
+ full_response = ''
287
+ try:
288
+ for chunk in response:
289
+ if chunk.text:
290
+ full_response += chunk.text
291
+ typing_indicator.markdown("*Generando respuesta...*")
292
+ message_placeholder.markdown(f'<div class="fade-in-text">{full_response}</div>', unsafe_allow_html=True)
293
+ time.sleep(0.3)
294
+
295
+ typing_indicator.empty()
296
+ message_placeholder.markdown(f'<div class="fade-in-text">{full_response}</div>', unsafe_allow_html=True)
297
+ return full_response
298
+ except Exception as e:
299
+ st.error(f"Error en el streaming: {str(e)}")
300
+ return ''