Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ 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 |
-
#
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="GnosticDev AI",
|
| 11 |
page_icon="馃",
|
|
@@ -13,30 +13,26 @@ st.set_page_config(
|
|
| 13 |
initial_sidebar_state="expanded",
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# Funci贸n para
|
| 17 |
-
def translate_role_to_streamlit(user_role):
|
| 18 |
-
if user_role == "model":
|
| 19 |
-
return "assistant"
|
| 20 |
-
else:
|
| 21 |
-
return user_role
|
| 22 |
-
|
| 23 |
-
# Funci贸n para guardar el historial en el estado de la sesi贸n
|
| 24 |
def save_chat_history(history):
|
|
|
|
| 25 |
serializable_history = []
|
| 26 |
for message in history:
|
| 27 |
serializable_history.append({
|
| 28 |
"role": message.role,
|
| 29 |
"text": message.parts[0].text
|
| 30 |
})
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
# Funci贸n para cargar el historial desde
|
| 34 |
def load_chat_history():
|
| 35 |
-
if '
|
| 36 |
try:
|
| 37 |
-
history = st.session_state.
|
| 38 |
model = load_gemini_pro()
|
| 39 |
chat = model.start_chat(history=[])
|
|
|
|
| 40 |
if st.session_state.system_prompt:
|
| 41 |
chat.send_message(st.session_state.system_prompt)
|
| 42 |
for message in history:
|
|
@@ -49,27 +45,53 @@ def load_chat_history():
|
|
| 49 |
|
| 50 |
# Inicializar estados
|
| 51 |
if "system_prompt" not in st.session_state:
|
| 52 |
-
st.session_state.system_prompt = ""
|
| 53 |
-
|
| 54 |
-
if "chat_history" not in st.session_state:
|
| 55 |
-
st.session_state.chat_history = []
|
| 56 |
|
| 57 |
with st.sidebar:
|
| 58 |
selected = option_menu(
|
| 59 |
"GD AI",
|
| 60 |
-
["Chatbot", "Image Captioning"],
|
| 61 |
menu_icon="robot",
|
| 62 |
-
icons=['chat-dots-fill', 'image-fill'],
|
| 63 |
default_index=0
|
| 64 |
)
|
| 65 |
|
| 66 |
# Bot贸n para borrar historial
|
| 67 |
if st.button("Borrar Historial"):
|
| 68 |
-
st.session_state
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
st.success("Historial borrado!")
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
model = load_gemini_pro()
|
| 74 |
|
| 75 |
# Inicializar o cargar sesi贸n de chat
|
|
@@ -104,16 +126,6 @@ if selected == "Chatbot":
|
|
| 104 |
# Guardar historial actualizado
|
| 105 |
save_chat_history(st.session_state.chat_session.history)
|
| 106 |
|
| 107 |
-
# Opci贸n para descargar el historial del chat
|
| 108 |
-
if st.button("Descargar Historial del Chat"):
|
| 109 |
-
chat_history = "\n".join([f"{msg['role']}: {msg['text']}" for msg in st.session_state.chat_history])
|
| 110 |
-
st.download_button(
|
| 111 |
-
label="Descargar",
|
| 112 |
-
data=chat_history,
|
| 113 |
-
file_name="historial_chat.txt",
|
| 114 |
-
mime="text/plain"
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
elif selected == "Image Captioning":
|
| 118 |
st.title("Image Caption Generation馃摳")
|
| 119 |
upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
| 5 |
from gemini_utility import (load_gemini_pro, gemini_pro_vision_responce)
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
# Setting the page config
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="GnosticDev AI",
|
| 11 |
page_icon="馃",
|
|
|
|
| 13 |
initial_sidebar_state="expanded",
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Funci贸n para guardar el historial en cookies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def save_chat_history(history):
|
| 18 |
+
# Convertir el historial a un formato serializable
|
| 19 |
serializable_history = []
|
| 20 |
for message in history:
|
| 21 |
serializable_history.append({
|
| 22 |
"role": message.role,
|
| 23 |
"text": message.parts[0].text
|
| 24 |
})
|
| 25 |
+
# Guardar en cookie
|
| 26 |
+
st.session_state.cookie_chat_history = json.dumps(serializable_history)
|
| 27 |
|
| 28 |
+
# Funci贸n para cargar el historial desde cookies
|
| 29 |
def load_chat_history():
|
| 30 |
+
if 'cookie_chat_history' in st.session_state:
|
| 31 |
try:
|
| 32 |
+
history = json.loads(st.session_state.cookie_chat_history)
|
| 33 |
model = load_gemini_pro()
|
| 34 |
chat = model.start_chat(history=[])
|
| 35 |
+
# Reconstruir el historial
|
| 36 |
if st.session_state.system_prompt:
|
| 37 |
chat.send_message(st.session_state.system_prompt)
|
| 38 |
for message in history:
|
|
|
|
| 45 |
|
| 46 |
# Inicializar estados
|
| 47 |
if "system_prompt" not in st.session_state:
|
| 48 |
+
st.session_state.system_prompt = st.session_state.get('cookie_system_prompt', "")
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
with st.sidebar:
|
| 51 |
selected = option_menu(
|
| 52 |
"GD AI",
|
| 53 |
+
["System Prompt", "Chatbot", "Image Captioning"],
|
| 54 |
menu_icon="robot",
|
| 55 |
+
icons=['gear', 'chat-dots-fill', 'image-fill'],
|
| 56 |
default_index=0
|
| 57 |
)
|
| 58 |
|
| 59 |
# Bot贸n para borrar historial
|
| 60 |
if st.button("Borrar Historial"):
|
| 61 |
+
if 'cookie_chat_history' in st.session_state:
|
| 62 |
+
del st.session_state.cookie_chat_history
|
| 63 |
+
if 'chat_session' in st.session_state:
|
| 64 |
+
del st.session_state.chat_session
|
| 65 |
st.success("Historial borrado!")
|
| 66 |
|
| 67 |
+
def translate_role_to_streamlit(user_role):
|
| 68 |
+
if user_role == "model":
|
| 69 |
+
return "assistant"
|
| 70 |
+
else:
|
| 71 |
+
return user_role
|
| 72 |
+
|
| 73 |
+
if selected == "System Prompt":
|
| 74 |
+
st.title("Configuraci贸n del System Prompt")
|
| 75 |
+
|
| 76 |
+
new_system_prompt = st.text_area(
|
| 77 |
+
"Ingresa las instrucciones para el AI (System Prompt)",
|
| 78 |
+
value=st.session_state.system_prompt,
|
| 79 |
+
height=300,
|
| 80 |
+
help="Escribe aqu铆 las instrucciones que definir谩n el comportamiento del AI"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if st.button("Guardar System Prompt"):
|
| 84 |
+
st.session_state.system_prompt = new_system_prompt
|
| 85 |
+
st.session_state.cookie_system_prompt = new_system_prompt # Guardar en cookie
|
| 86 |
+
if "chat_session" in st.session_state:
|
| 87 |
+
del st.session_state.chat_session
|
| 88 |
+
st.success("System Prompt actualizado con 茅xito!")
|
| 89 |
+
|
| 90 |
+
if st.session_state.system_prompt:
|
| 91 |
+
st.markdown("### System Prompt Actual:")
|
| 92 |
+
st.info(st.session_state.system_prompt)
|
| 93 |
+
|
| 94 |
+
elif selected == "Chatbot":
|
| 95 |
model = load_gemini_pro()
|
| 96 |
|
| 97 |
# Inicializar o cargar sesi贸n de chat
|
|
|
|
| 126 |
# Guardar historial actualizado
|
| 127 |
save_chat_history(st.session_state.chat_session.history)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
elif selected == "Image Captioning":
|
| 130 |
st.title("Image Caption Generation馃摳")
|
| 131 |
upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|