Spaces:
Runtime error
Runtime error
Upload 10 files
Browse files
app.py
CHANGED
@@ -2,9 +2,9 @@ import time
|
|
2 |
import os
|
3 |
import joblib
|
4 |
import streamlit as st
|
5 |
-
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
-
from google.generativeai import types
|
8 |
from system_prompts import get_puv_system_prompt # Importar el prompt iterativo
|
9 |
|
10 |
# Función para cargar CSS personalizado
|
@@ -33,6 +33,9 @@ load_dotenv()
|
|
33 |
GOOGLE_API_KEY=os.environ.get('GOOGLE_API_KEY')
|
34 |
genai.configure(api_key=GOOGLE_API_KEY)
|
35 |
|
|
|
|
|
|
|
36 |
new_chat_id = f'{time.time()}'
|
37 |
MODEL_ROLE = 'ai'
|
38 |
AI_AVATAR_ICON = '🤖' # Cambia el emoji por uno de robot para coincidir con tu logo
|
@@ -156,27 +159,31 @@ if prompt := st.chat_input('¿En qué puedo ayudarte con tu Propuesta Única de
|
|
156 |
# Obtener el system prompt iterativo
|
157 |
system_prompt = get_puv_system_prompt()
|
158 |
|
159 |
-
#
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
164 |
|
165 |
-
#
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
# Enviar mensaje al AI con system prompt
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
172 |
)
|
173 |
|
174 |
-
# Eliminar la línea duplicada que usa st.session_state.chat
|
175 |
-
# response = st.session_state.chat.send_message(
|
176 |
-
# prompt,
|
177 |
-
# stream=True,
|
178 |
-
# )
|
179 |
-
|
180 |
# Display assistant response in chat message container
|
181 |
with st.chat_message(
|
182 |
name=MODEL_ROLE,
|
@@ -184,24 +191,25 @@ if prompt := st.chat_input('¿En qué puedo ayudarte con tu Propuesta Única de
|
|
184 |
):
|
185 |
message_placeholder = st.empty()
|
186 |
full_response = ''
|
187 |
-
assistant_response = response
|
188 |
|
189 |
# Añade un indicador de "escribiendo..."
|
190 |
typing_indicator = st.empty()
|
191 |
typing_indicator.markdown("*RoboCopy está escribiendo...*")
|
192 |
|
193 |
# Streams in a chunk at a time
|
194 |
-
for chunk in
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
|
|
205 |
# Elimina el indicador de escritura
|
206 |
typing_indicator.empty()
|
207 |
# Write full message with placeholder
|
@@ -211,11 +219,25 @@ if prompt := st.chat_input('¿En qué puedo ayudarte con tu Propuesta Única de
|
|
211 |
st.session_state.messages.append(
|
212 |
dict(
|
213 |
role=MODEL_ROLE,
|
214 |
-
content=
|
215 |
avatar=AI_AVATAR_ICON,
|
216 |
)
|
217 |
)
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
# Save to file
|
220 |
joblib.dump(
|
221 |
st.session_state.messages,
|
|
|
2 |
import os
|
3 |
import joblib
|
4 |
import streamlit as st
|
5 |
+
from google import genai
|
6 |
+
from google.genai import types
|
7 |
from dotenv import load_dotenv
|
|
|
8 |
from system_prompts import get_puv_system_prompt # Importar el prompt iterativo
|
9 |
|
10 |
# Función para cargar CSS personalizado
|
|
|
33 |
GOOGLE_API_KEY=os.environ.get('GOOGLE_API_KEY')
|
34 |
genai.configure(api_key=GOOGLE_API_KEY)
|
35 |
|
36 |
+
# Inicializar el cliente de Gemini
|
37 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
38 |
+
|
39 |
new_chat_id = f'{time.time()}'
|
40 |
MODEL_ROLE = 'ai'
|
41 |
AI_AVATAR_ICON = '🤖' # Cambia el emoji por uno de robot para coincidir con tu logo
|
|
|
159 |
# Obtener el system prompt iterativo
|
160 |
system_prompt = get_puv_system_prompt()
|
161 |
|
162 |
+
# Preparar el contenido para la API de Gemini
|
163 |
+
contents = [
|
164 |
+
types.Content(
|
165 |
+
role="user",
|
166 |
+
parts=[
|
167 |
+
types.Part.from_text(text=prompt),
|
168 |
+
],
|
169 |
+
),
|
170 |
+
]
|
171 |
|
172 |
+
# Configurar el system prompt
|
173 |
+
generate_content_config = types.GenerateContentConfig(
|
174 |
+
response_mime_type="text/plain",
|
175 |
+
system_instruction=[
|
176 |
+
types.Part.from_text(text=system_prompt),
|
177 |
+
],
|
178 |
+
)
|
179 |
|
180 |
# Enviar mensaje al AI con system prompt
|
181 |
+
response_stream = client.models.generate_content_stream(
|
182 |
+
model="gemini-2.0-flash",
|
183 |
+
contents=contents,
|
184 |
+
config=generate_content_config,
|
185 |
)
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
# Display assistant response in chat message container
|
188 |
with st.chat_message(
|
189 |
name=MODEL_ROLE,
|
|
|
191 |
):
|
192 |
message_placeholder = st.empty()
|
193 |
full_response = ''
|
|
|
194 |
|
195 |
# Añade un indicador de "escribiendo..."
|
196 |
typing_indicator = st.empty()
|
197 |
typing_indicator.markdown("*RoboCopy está escribiendo...*")
|
198 |
|
199 |
# Streams in a chunk at a time
|
200 |
+
for chunk in response_stream:
|
201 |
+
if hasattr(chunk, 'text'):
|
202 |
+
# Simulate stream of chunk
|
203 |
+
words = chunk.text.split(' ')
|
204 |
+
for i, word in enumerate(words):
|
205 |
+
full_response += word
|
206 |
+
# Añadir espacio solo si no es la última palabra del chunk
|
207 |
+
if i < len(words) - 1:
|
208 |
+
full_response += ' '
|
209 |
+
time.sleep(0.1) # Velocidad ajustada para mejor legibilidad
|
210 |
+
# Rewrites with a cursor at end
|
211 |
+
message_placeholder.write(full_response + '▌')
|
212 |
+
|
213 |
# Elimina el indicador de escritura
|
214 |
typing_indicator.empty()
|
215 |
# Write full message with placeholder
|
|
|
219 |
st.session_state.messages.append(
|
220 |
dict(
|
221 |
role=MODEL_ROLE,
|
222 |
+
content=full_response,
|
223 |
avatar=AI_AVATAR_ICON,
|
224 |
)
|
225 |
)
|
226 |
+
|
227 |
+
# Actualizar el historial de Gemini (adaptado para la nueva API)
|
228 |
+
if not hasattr(st.session_state, 'gemini_history'):
|
229 |
+
st.session_state.gemini_history = []
|
230 |
+
|
231 |
+
st.session_state.gemini_history.append({
|
232 |
+
'role': 'user',
|
233 |
+
'parts': [{'text': prompt}]
|
234 |
+
})
|
235 |
+
|
236 |
+
st.session_state.gemini_history.append({
|
237 |
+
'role': 'model',
|
238 |
+
'parts': [{'text': full_response}]
|
239 |
+
})
|
240 |
+
|
241 |
# Save to file
|
242 |
joblib.dump(
|
243 |
st.session_state.messages,
|