Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import json
|
| 4 |
-
from streamlit_option_menu import option_menu
|
| 5 |
-
from gemini_utility import (load_gemini_pro, gemini_pro_vision_responce)
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
# Configuraci贸n de la p谩gina
|
| 9 |
-
st.set_page_config(
|
| 10 |
-
page_title="GnosticDev AI",
|
| 11 |
-
page_icon="馃",
|
| 12 |
-
layout="centered",
|
| 13 |
-
initial_sidebar_state="expanded",
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
# Funci贸n para guardar el historial en cookies
|
| 17 |
-
def save_chat_history(history):
|
| 18 |
-
serializable_history = []
|
| 19 |
-
for message in history:
|
| 20 |
-
serializable_history.append({
|
| 21 |
-
"role": message.role,
|
| 22 |
-
"text": message.parts[0].text
|
| 23 |
-
})
|
| 24 |
-
st.session_state.cookie_chat_history = json.dumps(serializable_history)
|
| 25 |
-
|
| 26 |
-
# Funci贸n para cargar el historial desde cookies
|
| 27 |
-
def load_chat_history():
|
| 28 |
-
if 'cookie_chat_history' in st.session_state:
|
| 29 |
-
try:
|
| 30 |
-
history = json.loads(st.session_state.cookie_chat_history)
|
| 31 |
-
model = load_gemini_pro()
|
| 32 |
-
chat = model.start_chat(history=[])
|
| 33 |
-
if st.session_state.system_prompt:
|
| 34 |
-
chat.send_message(st.session_state.system_prompt)
|
| 35 |
-
for message in history:
|
| 36 |
-
if message["role"] != "model" or not message["text"].startswith(st.session_state.system_prompt):
|
| 37 |
-
chat.send_message(message["text"])
|
| 38 |
-
return chat
|
| 39 |
-
except Exception as e:
|
| 40 |
-
st.error(f"Error cargando el historial: {e}")
|
| 41 |
-
return None
|
| 42 |
-
|
| 43 |
-
# Funci贸n para descargar el historial del chat
|
| 44 |
-
def download_chat_history(history):
|
| 45 |
-
chat_text = ""
|
| 46 |
-
for message in history:
|
| 47 |
-
chat_text += f"{message.role}: {message.parts[0].text}\n"
|
| 48 |
-
return chat_text
|
| 49 |
-
|
| 50 |
-
# Inicializar estados
|
| 51 |
-
if "system_prompt" not in st.session_state:
|
| 52 |
-
st.session_state.system_prompt = st.session_state.get('cookie_system_prompt', "")
|
| 53 |
-
|
| 54 |
-
with st.sidebar:
|
| 55 |
-
selected = option_menu(
|
| 56 |
-
"GD AI",
|
| 57 |
-
["System Prompt", "Chatbot", "Image Captioning"],
|
| 58 |
-
menu_icon="robot",
|
| 59 |
-
icons=['gear', 'chat-dots-fill', 'image-fill'],
|
| 60 |
-
default_index=0
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# Bot贸n para borrar historial
|
| 64 |
-
if st.button("Borrar Historial"):
|
| 65 |
-
if 'cookie_chat_history' in st.session_state:
|
| 66 |
-
del st.session_state.cookie_chat_history
|
| 67 |
-
if 'chat_session' in st.session_state:
|
| 68 |
-
del st.session_state.chat_session
|
| 69 |
-
st.success("Historial borrado!")
|
| 70 |
-
|
| 71 |
-
def translate_role_to_streamlit(user_role):
|
| 72 |
-
return "assistant" if user_role == "model" else user_role
|
| 73 |
-
|
| 74 |
if selected == "System Prompt":
|
| 75 |
st.title("Configuraci贸n del System Prompt")
|
| 76 |
|
|
@@ -81,16 +8,29 @@ if selected == "System Prompt":
|
|
| 81 |
help="Escribe aqu铆 las instrucciones que definir谩n el comportamiento del AI"
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
st.session_state.system_prompt = new_system_prompt
|
| 86 |
st.session_state.cookie_system_prompt = new_system_prompt
|
|
|
|
| 87 |
if "chat_session" in st.session_state:
|
| 88 |
del st.session_state.chat_session
|
| 89 |
-
st.success("System Prompt
|
| 90 |
|
| 91 |
if st.session_state.system_prompt:
|
| 92 |
st.markdown("### System Prompt Actual:")
|
| 93 |
st.info(st.session_state.system_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
elif selected == "Chatbot":
|
| 96 |
model = load_gemini_pro()
|
|
@@ -116,15 +56,26 @@ elif selected == "Chatbot":
|
|
| 116 |
with st.chat_message(translate_role_to_streamlit(message.role)):
|
| 117 |
st.markdown(message.parts[0].text)
|
| 118 |
|
| 119 |
-
|
| 120 |
user_prompt = st.chat_input("Preguntame algo...")
|
| 121 |
if user_prompt:
|
| 122 |
st.chat_message("user").markdown(user_prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
| 124 |
with st.chat_message("assistant"):
|
| 125 |
st.markdown(gemini_response.text)
|
| 126 |
|
| 127 |
-
|
| 128 |
save_chat_history(st.session_state.chat_session.history)
|
| 129 |
|
| 130 |
# Opci贸n para descargar el historial del chat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
if selected == "System Prompt":
|
| 2 |
st.title("Configuraci贸n del System Prompt")
|
| 3 |
|
|
|
|
| 8 |
help="Escribe aqu铆 las instrucciones que definir谩n el comportamiento del AI"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Campo para ingresar URLs
|
| 12 |
+
urls_input = st.text_area(
|
| 13 |
+
"Ingresa URLs de informaci贸n y documentos (separadas por comas)",
|
| 14 |
+
value=st.session_state.get('cookie_urls', ""),
|
| 15 |
+
height=100,
|
| 16 |
+
help="Escribe aqu铆 las URLs que el AI puede usar como referencia, separadas por comas."
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if st.button("Guardar System Prompt y URLs"):
|
| 20 |
st.session_state.system_prompt = new_system_prompt
|
| 21 |
st.session_state.cookie_system_prompt = new_system_prompt
|
| 22 |
+
st.session_state.cookie_urls = urls_input.split(",") # Guardar las URLs en una lista
|
| 23 |
if "chat_session" in st.session_state:
|
| 24 |
del st.session_state.chat_session
|
| 25 |
+
st.success("System Prompt y URLs actualizados con 茅xito!")
|
| 26 |
|
| 27 |
if st.session_state.system_prompt:
|
| 28 |
st.markdown("### System Prompt Actual:")
|
| 29 |
st.info(st.session_state.system_prompt)
|
| 30 |
+
|
| 31 |
+
if st.session_state.get('cookie_urls'):
|
| 32 |
+
st.markdown("### URLs Guardadas:")
|
| 33 |
+
st.info(", ".join(st.session_state.cookie_urls))
|
| 34 |
|
| 35 |
elif selected == "Chatbot":
|
| 36 |
model = load_gemini_pro()
|
|
|
|
| 56 |
with st.chat_message(translate_role_to_streamlit(message.role)):
|
| 57 |
st.markdown(message.parts[0].text)
|
| 58 |
|
| 59 |
+
# Campo de entrada
|
| 60 |
user_prompt = st.chat_input("Preguntame algo...")
|
| 61 |
if user_prompt:
|
| 62 |
st.chat_message("user").markdown(user_prompt)
|
| 63 |
+
|
| 64 |
+
# Aqu铆 es donde puedes modificar la l贸gica para que el bot consulte las URLs
|
| 65 |
+
urls = st.session_state.get('cookie_urls', [])
|
| 66 |
+
if urls:
|
| 67 |
+
# L贸gica para consultar las URLs y obtener informaci贸n
|
| 68 |
+
# Esto es un ejemplo, necesitar谩s implementar la l贸gica real para hacer las solicitudes HTTP
|
| 69 |
+
for url in urls:
|
| 70 |
+
# Aqu铆 podr铆as hacer una solicitud HTTP a cada URL y procesar la respuesta
|
| 71 |
+
# Por ejemplo, usando requests.get(url) y luego analizando el contenido
|
| 72 |
+
pass
|
| 73 |
+
|
| 74 |
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
| 75 |
with st.chat_message("assistant"):
|
| 76 |
st.markdown(gemini_response.text)
|
| 77 |
|
| 78 |
+
# Guardar historial actualizado
|
| 79 |
save_chat_history(st.session_state.chat_session.history)
|
| 80 |
|
| 81 |
# Opci贸n para descargar el historial del chat
|