Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,125 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
| 3 |
-
|
| 4 |
-
import
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
| 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 |
+
# Inicializar estados
|
| 44 |
+
if "system_prompt" not in st.session_state:
|
| 45 |
+
st.session_state.system_prompt = st.session_state.get('cookie_system_prompt', "")
|
| 46 |
|
| 47 |
+
with st.sidebar:
|
| 48 |
+
selected = option_menu(
|
| 49 |
+
"GD AI",
|
| 50 |
+
["System Prompt", "Chatbot", "Image Captioning"],
|
| 51 |
+
menu_icon="robot",
|
| 52 |
+
icons=['gear', 'chat-dots-fill', 'image-fill'],
|
| 53 |
+
default_index=0
|
| 54 |
)
|
| 55 |
+
|
| 56 |
+
# Bot贸n para borrar historial
|
| 57 |
+
if st.button("Borrar Historial"):
|
| 58 |
+
if 'cookie_chat_history' in st.session_state:
|
| 59 |
+
del st.session_state.cookie_chat_history
|
| 60 |
+
if 'chat_session' in st.session_state:
|
| 61 |
+
del st.session_state.chat_session
|
| 62 |
+
st.success("Historial borrado!")
|
| 63 |
|
| 64 |
+
def translate_role_to_streamlit(user_role):
|
| 65 |
+
return "assistant" if user_role == "model" else user_role
|
| 66 |
|
| 67 |
+
if selected == "System Prompt":
|
| 68 |
+
st.title("Configuraci贸n del System Prompt")
|
| 69 |
+
|
| 70 |
+
new_system_prompt = st.text_area(
|
| 71 |
+
"Ingresa las instrucciones para el AI (System Prompt)",
|
| 72 |
+
value=st.session_state.system_prompt,
|
| 73 |
+
height=300,
|
| 74 |
+
help="Escribe aqu铆 las instrucciones que definir谩n el comportamiento del AI"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if st.button("Guardar System Prompt"):
|
| 78 |
+
st.session_state.system_prompt = new_system_prompt
|
| 79 |
+
st.session_state.cookie_system_prompt = new_system_prompt
|
| 80 |
+
if "chat_session" in st.session_state:
|
| 81 |
+
del st.session_state.chat_session
|
| 82 |
+
st.success("System Prompt actualizado con 茅xito!")
|
| 83 |
+
|
| 84 |
+
if st.session_state.system_prompt:
|
| 85 |
+
st.markdown("### System Prompt Actual:")
|
| 86 |
+
st.info(st.session_state.system_prompt)
|
| 87 |
|
| 88 |
+
elif selected == "Chatbot":
|
| 89 |
+
model = load_gemini_pro()
|
| 90 |
+
|
| 91 |
+
# Inicializar o cargar sesi贸n de chat
|
| 92 |
+
if "chat_session" not in st.session_state:
|
| 93 |
+
loaded_chat = load_chat_history()
|
| 94 |
+
if loaded_chat:
|
| 95 |
+
st.session_state.chat_session = loaded_chat
|
| 96 |
+
else:
|
| 97 |
+
st.session_state.chat_session = model.start_chat(history=[])
|
| 98 |
+
if st.session_state.system_prompt:
|
| 99 |
+
st.session_state.chat_session.send_message(st.session_state.system_prompt)
|
| 100 |
|
| 101 |
+
st.title("Gnosticdev Chatbot")
|
| 102 |
+
|
| 103 |
+
if st.session_state.system_prompt:
|
| 104 |
+
with st.expander("Ver System Prompt actual"):
|
| 105 |
+
st.info(st.session_state.system_prompt)
|
| 106 |
+
|
| 107 |
+
# Mostrar historial
|
| 108 |
+
for message in st.session_state.chat_session.history:
|
| 109 |
+
with st.chat_message(translate_role_to_streamlit(message.role)):
|
| 110 |
+
st.markdown(message.parts[0].text)
|
| 111 |
|
| 112 |
+
# Campo de entrada
|
| 113 |
+
user_prompt = st.chat_input("Preguntame algo...")
|
| 114 |
+
if user_prompt:
|
| 115 |
+
st.chat_message("user").markdown(user_prompt)
|
| 116 |
+
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
| 117 |
+
with st.chat_message("assistant"):
|
| 118 |
+
st.markdown(gemini_response.text)
|
| 119 |
+
|
| 120 |
+
# Guardar historial actualizado
|
| 121 |
+
save_chat_history(st.session_state.chat_session.history)
|
| 122 |
|
| 123 |
+
elif selected == "Image Captioning":
|
| 124 |
+
st.title("Image Caption Generation馃摳")
|
| 125 |
+
upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "
|