Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
if selected == "System Prompt":
|
| 2 |
st.title("Configuraci贸n del System Prompt")
|
| 3 |
|
|
@@ -11,15 +88,15 @@ if selected == "System Prompt":
|
|
| 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.
|
| 15 |
height=100,
|
| 16 |
help="Escribe aqu铆 las URLs que el AI puede usar como referencia, separadas por comas."
|
| 17 |
)
|
| 18 |
|
| 19 |
-
|
| 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!")
|
|
@@ -28,7 +105,7 @@ if selected == "System Prompt":
|
|
| 28 |
st.markdown("### System Prompt Actual:")
|
| 29 |
st.info(st.session_state.system_prompt)
|
| 30 |
|
| 31 |
-
if st.session_state.
|
| 32 |
st.markdown("### URLs Guardadas:")
|
| 33 |
st.info(", ".join(st.session_state.cookie_urls))
|
| 34 |
|
|
@@ -75,7 +152,7 @@ elif selected == "Chatbot":
|
|
| 75 |
with st.chat_message("assistant"):
|
| 76 |
st.markdown(gemini_response.text)
|
| 77 |
|
| 78 |
-
|
| 79 |
save_chat_history(st.session_state.chat_session.history)
|
| 80 |
|
| 81 |
# Opci贸n para descargar el historial del chat
|
|
@@ -103,3 +180,4 @@ elif selected == "Image Captioning":
|
|
| 103 |
st.info(caption)
|
| 104 |
|
| 105 |
# Fin del script
|
|
|
|
|
|
| 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 |
+
if "cookie_urls" not in st.session_state:
|
| 55 |
+
st.session_state.cookie_urls = []
|
| 56 |
+
|
| 57 |
+
with st.sidebar:
|
| 58 |
+
selected = option_menu(
|
| 59 |
+
"GD AI",
|
| 60 |
+
["System Prompt", "Chatbot", "Image Captioning"],
|
| 61 |
+
menu_icon="robot",
|
| 62 |
+
icons=['gear', 'chat-dots-fill', 'image-fill'],
|
| 63 |
+
default_index=0
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Bot贸n para borrar historial
|
| 67 |
+
if st.button("Borrar Historial"):
|
| 68 |
+
if 'cookie_chat_history' in st.session_state:
|
| 69 |
+
del st.session_state.cookie_chat_history
|
| 70 |
+
if 'chat_session' in st.session_state:
|
| 71 |
+
del st.session_state.chat_session
|
| 72 |
+
st.success("Historial borrado!")
|
| 73 |
+
|
| 74 |
+
def translate_role_to_streamlit(user_role):
|
| 75 |
+
return "assistant" if user_role == "model" else user_role
|
| 76 |
+
|
| 77 |
+
# Aqu铆 comienza la l贸gica de la aplicaci贸n
|
| 78 |
if selected == "System Prompt":
|
| 79 |
st.title("Configuraci贸n del System Prompt")
|
| 80 |
|
|
|
|
| 88 |
# Campo para ingresar URLs
|
| 89 |
urls_input = st.text_area(
|
| 90 |
"Ingresa URLs de informaci贸n y documentos (separadas por comas)",
|
| 91 |
+
value=", ".join(st.session_state.cookie_urls),
|
| 92 |
height=100,
|
| 93 |
help="Escribe aqu铆 las URLs que el AI puede usar como referencia, separadas por comas."
|
| 94 |
)
|
| 95 |
|
| 96 |
+
if st.button("Guardar System Prompt y URLs"):
|
| 97 |
st.session_state.system_prompt = new_system_prompt
|
| 98 |
st.session_state.cookie_system_prompt = new_system_prompt
|
| 99 |
+
st.session_state.cookie_urls = [url.strip() for url in urls_input.split(",") if url.strip()] # Guardar las URLs en una lista
|
| 100 |
if "chat_session" in st.session_state:
|
| 101 |
del st.session_state.chat_session
|
| 102 |
st.success("System Prompt y URLs actualizados con 茅xito!")
|
|
|
|
| 105 |
st.markdown("### System Prompt Actual:")
|
| 106 |
st.info(st.session_state.system_prompt)
|
| 107 |
|
| 108 |
+
if st.session_state.cookie_urls:
|
| 109 |
st.markdown("### URLs Guardadas:")
|
| 110 |
st.info(", ".join(st.session_state.cookie_urls))
|
| 111 |
|
|
|
|
| 152 |
with st.chat_message("assistant"):
|
| 153 |
st.markdown(gemini_response.text)
|
| 154 |
|
| 155 |
+
# Guardar historial actualizado
|
| 156 |
save_chat_history(st.session_state.chat_session.history)
|
| 157 |
|
| 158 |
# Opci贸n para descargar el historial del chat
|
|
|
|
| 180 |
st.info(caption)
|
| 181 |
|
| 182 |
# Fin del script
|
| 183 |
+
|