Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -102,11 +102,50 @@ def update_chat_memory():
|
|
102 |
def handle_model_error(error, retry_count, max_retries):
|
103 |
if retry_count >= max_retries:
|
104 |
error_message = f"Lo siento, estoy experimentando problemas para procesar tu solicitud. Por favor, intenta de nuevo más tarde. Error: {str(error)}"
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
return False
|
111 |
|
112 |
def mostrar_con_efecto_escritura(texto, velocidad=0.05):
|
|
|
102 |
def handle_model_error(error, retry_count, max_retries):
|
103 |
if retry_count >= max_retries:
|
104 |
error_message = f"Lo siento, estoy experimentando problemas para procesar tu solicitud. Por favor, intenta de nuevo más tarde. Error: {str(error)}"
|
105 |
+
return error_message
|
106 |
+
return None
|
107 |
+
|
108 |
+
def process_model_response(prompt, max_retries=3):
|
109 |
+
retry_count = 0
|
110 |
+
while retry_count < max_retries:
|
111 |
+
try:
|
112 |
+
# Crear el mensaje del modelo primero
|
113 |
+
with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON):
|
114 |
+
# Añade un indicador de "escribiendo..."
|
115 |
+
typing_indicator = st.empty()
|
116 |
+
typing_indicator.markdown("*🤖 RoboCopy está escribiendo...*")
|
117 |
+
|
118 |
+
response = st.session_state.chat.send_message(prompt, stream=True)
|
119 |
+
mensaje_completo = ""
|
120 |
+
mensaje_actual = ""
|
121 |
+
|
122 |
+
# Procesar la respuesta por chunks con efecto de escritura
|
123 |
+
for chunk in response:
|
124 |
+
mensaje_completo += chunk.text
|
125 |
+
for caracter in chunk.text:
|
126 |
+
mensaje_actual += caracter
|
127 |
+
typing_indicator.markdown(mensaje_actual + "▌")
|
128 |
+
time.sleep(0.02) # Velocidad de escritura ajustada
|
129 |
+
|
130 |
+
# Mostrar mensaje final y limpiar indicador
|
131 |
+
typing_indicator.markdown(mensaje_completo)
|
132 |
+
|
133 |
+
add_message(MODEL_ROLE, mensaje_completo, AI_AVATAR_ICON)
|
134 |
+
st.session_state.gemini_history = st.session_state.chat.history
|
135 |
+
update_chat_memory()
|
136 |
+
return True
|
137 |
+
|
138 |
+
except Exception as e:
|
139 |
+
retry_count += 1
|
140 |
+
error_msg = handle_model_error(e, retry_count, max_retries)
|
141 |
+
if error_msg:
|
142 |
+
with st.chat_message(name=MODEL_ROLE, avatar=AI_AVATAR_ICON):
|
143 |
+
st.error(error_msg)
|
144 |
+
add_message(MODEL_ROLE, error_msg, AI_AVATAR_ICON)
|
145 |
+
update_chat_memory()
|
146 |
+
return False
|
147 |
+
wait_time = (1.5 ** retry_count)
|
148 |
+
time.sleep(wait_time)
|
149 |
return False
|
150 |
|
151 |
def mostrar_con_efecto_escritura(texto, velocidad=0.05):
|