Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -11,22 +11,6 @@ from session_state import SessionState
|
|
11 |
# Inicializar el estado de la sesión
|
12 |
state = SessionState()
|
13 |
|
14 |
-
# Función para aplicar efecto de desvanecimiento
|
15 |
-
def apply_fade_in_effect():
|
16 |
-
"""Aplica un efecto de desvanecimiento para el texto"""
|
17 |
-
st.markdown("""
|
18 |
-
<style>
|
19 |
-
@keyframes fadeIn {
|
20 |
-
from { opacity: 0; }
|
21 |
-
to { opacity: 1; }
|
22 |
-
}
|
23 |
-
.fade-in-text {
|
24 |
-
animation: fadeIn 3s ease-in; /* Aumentado de 1.5s a 3s */
|
25 |
-
opacity: 1;
|
26 |
-
}
|
27 |
-
</style>
|
28 |
-
""", unsafe_allow_html=True)
|
29 |
-
|
30 |
# Función para detectar saludos y generar respuestas personalizadas
|
31 |
def is_greeting(text):
|
32 |
"""Detecta si el texto es un saludo simple"""
|
@@ -47,13 +31,28 @@ def process_message(prompt, is_example=False):
|
|
47 |
st.markdown(prompt)
|
48 |
|
49 |
state.add_message('user', prompt, USER_AVATAR_ICON)
|
50 |
-
enhanced_prompt = get_enhanced_prompt(prompt, is_example)
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
|
55 |
-
#
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
def handle_chat_title(prompt):
|
59 |
"""Maneja la lógica del título del chat"""
|
@@ -82,17 +81,13 @@ def process_model_response(enhanced_prompt):
|
|
82 |
typing_indicator = st.empty()
|
83 |
typing_indicator.markdown("*Generando respuesta...*")
|
84 |
|
85 |
-
# Aplicar el efecto de desvanecimiento
|
86 |
-
apply_fade_in_effect()
|
87 |
-
|
88 |
response = state.send_message(enhanced_prompt)
|
89 |
-
|
90 |
-
full_response = stream_response_with_fade(response, message_placeholder, typing_indicator)
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
except Exception as e:
|
98 |
st.error(f"Error: {str(e)}")
|
@@ -105,7 +100,7 @@ def stream_response(response, message_placeholder, typing_indicator):
|
|
105 |
if chunk.text:
|
106 |
for ch in chunk.text:
|
107 |
full_response += ch
|
108 |
-
time.sleep(0.
|
109 |
typing_indicator.markdown("*Generando respuesta...*")
|
110 |
message_placeholder.markdown(full_response + '▌')
|
111 |
except Exception as e:
|
@@ -116,33 +111,6 @@ def stream_response(response, message_placeholder, typing_indicator):
|
|
116 |
message_placeholder.markdown(full_response)
|
117 |
return full_response
|
118 |
|
119 |
-
def stream_response_with_fade(response, message_placeholder, typing_indicator):
|
120 |
-
"""Maneja el streaming de la respuesta con efecto de desvanecimiento"""
|
121 |
-
full_response = ''
|
122 |
-
try:
|
123 |
-
for chunk in response:
|
124 |
-
if chunk.text:
|
125 |
-
# Añadir el texto del chunk actual
|
126 |
-
full_response += chunk.text
|
127 |
-
typing_indicator.markdown("*Generando respuesta...*")
|
128 |
-
|
129 |
-
# Aplicar el efecto de desvanecimiento con un ID único para forzar la re-renderización
|
130 |
-
unique_id = int(time.time() * 1000) # Timestamp en milisegundos
|
131 |
-
message_placeholder.markdown(
|
132 |
-
f'<div id="fade-{unique_id}" class="fade-in-text">{full_response}</div>',
|
133 |
-
unsafe_allow_html=True
|
134 |
-
)
|
135 |
-
|
136 |
-
# Esperar más tiempo entre actualizaciones
|
137 |
-
time.sleep(1.5) # Aumentado de 0.8 a 1.5 segundos
|
138 |
-
|
139 |
-
typing_indicator.empty()
|
140 |
-
message_placeholder.markdown(f'<div class="fade-in-text">{full_response}</div>', unsafe_allow_html=True)
|
141 |
-
return full_response
|
142 |
-
except Exception as e:
|
143 |
-
st.error(f"Error en el streaming: {str(e)}")
|
144 |
-
return ''
|
145 |
-
|
146 |
# Función para cargar CSS personalizado
|
147 |
def load_css(file_path):
|
148 |
with open(file_path) as f:
|
|
|
11 |
# Inicializar el estado de la sesión
|
12 |
state = SessionState()
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Función para detectar saludos y generar respuestas personalizadas
|
15 |
def is_greeting(text):
|
16 |
"""Detecta si el texto es un saludo simple"""
|
|
|
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 |
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)}")
|
|
|
100 |
if chunk.text:
|
101 |
for ch in chunk.text:
|
102 |
full_response += ch
|
103 |
+
time.sleep(0.01)
|
104 |
typing_indicator.markdown("*Generando respuesta...*")
|
105 |
message_placeholder.markdown(full_response + '▌')
|
106 |
except Exception as e:
|
|
|
111 |
message_placeholder.markdown(full_response)
|
112 |
return full_response
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
# Función para cargar CSS personalizado
|
115 |
def load_css(file_path):
|
116 |
with open(file_path) as f:
|