Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
|
|
3 |
from streamlit_option_menu import option_menu
|
4 |
from gemini_utility import (load_gemini_pro, gemini_pro_vision_responce)
|
5 |
from PIL import Image
|
@@ -12,9 +13,39 @@ st.set_page_config(
|
|
12 |
initial_sidebar_state="expanded",
|
13 |
)
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
if "system_prompt" not in st.session_state:
|
17 |
-
st.session_state.system_prompt = ""
|
18 |
|
19 |
with st.sidebar:
|
20 |
selected = option_menu(
|
@@ -24,6 +55,14 @@ with st.sidebar:
|
|
24 |
icons=['gear', 'chat-dots-fill', 'image-fill'],
|
25 |
default_index=0
|
26 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def translate_role_to_streamlit(user_role):
|
29 |
if user_role == "model":
|
@@ -34,7 +73,6 @@ def translate_role_to_streamlit(user_role):
|
|
34 |
if selected == "System Prompt":
|
35 |
st.title("Configuraci贸n del System Prompt")
|
36 |
|
37 |
-
# 脕rea para editar el system prompt
|
38 |
new_system_prompt = st.text_area(
|
39 |
"Ingresa las instrucciones para el AI (System Prompt)",
|
40 |
value=st.session_state.system_prompt,
|
@@ -44,11 +82,11 @@ if selected == "System Prompt":
|
|
44 |
|
45 |
if st.button("Guardar System Prompt"):
|
46 |
st.session_state.system_prompt = new_system_prompt
|
|
|
47 |
if "chat_session" in st.session_state:
|
48 |
-
del st.session_state.chat_session
|
49 |
st.success("System Prompt actualizado con 茅xito!")
|
50 |
|
51 |
-
# Mostrar el prompt actual
|
52 |
if st.session_state.system_prompt:
|
53 |
st.markdown("### System Prompt Actual:")
|
54 |
st.info(st.session_state.system_prompt)
|
@@ -56,31 +94,37 @@ if selected == "System Prompt":
|
|
56 |
elif selected == "Chatbot":
|
57 |
model = load_gemini_pro()
|
58 |
|
59 |
-
#
|
60 |
if "chat_session" not in st.session_state:
|
61 |
-
|
62 |
-
if
|
63 |
-
st.session_state.chat_session
|
|
|
|
|
|
|
|
|
64 |
|
65 |
st.title("Gnosticdev Chatbot")
|
66 |
|
67 |
-
# Mostrar el system prompt actual en un expander
|
68 |
if st.session_state.system_prompt:
|
69 |
with st.expander("Ver System Prompt actual"):
|
70 |
st.info(st.session_state.system_prompt)
|
71 |
|
72 |
-
#
|
73 |
for message in st.session_state.chat_session.history:
|
74 |
with st.chat_message(translate_role_to_streamlit(message.role)):
|
75 |
st.markdown(message.parts[0].text)
|
76 |
|
77 |
-
#
|
78 |
user_prompt = st.chat_input("Preguntame algo...")
|
79 |
if user_prompt:
|
80 |
st.chat_message("user").markdown(user_prompt)
|
81 |
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
82 |
with st.chat_message("assistant"):
|
83 |
st.markdown(gemini_response.text)
|
|
|
|
|
|
|
84 |
|
85 |
elif selected == "Image Captioning":
|
86 |
st.title("Image Caption Generation馃摳")
|
@@ -94,4 +138,4 @@ elif selected == "Image Captioning":
|
|
94 |
default_prompt = "Write a caption for this image"
|
95 |
caption = gemini_pro_vision_responce(default_prompt, image)
|
96 |
with col2:
|
97 |
-
st.info(caption)
|
|
|
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
|
|
|
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:
|
39 |
+
if message["role"] != "model" or not message["text"].startswith(st.session_state.system_prompt):
|
40 |
+
chat.send_message(message["text"])
|
41 |
+
return chat
|
42 |
+
except Exception as e:
|
43 |
+
st.error(f"Error cargando el historial: {e}")
|
44 |
+
return None
|
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(
|
|
|
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":
|
|
|
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,
|
|
|
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)
|
|
|
94 |
elif selected == "Chatbot":
|
95 |
model = load_gemini_pro()
|
96 |
|
97 |
+
# Inicializar o cargar sesi贸n de chat
|
98 |
if "chat_session" not in st.session_state:
|
99 |
+
loaded_chat = load_chat_history()
|
100 |
+
if loaded_chat:
|
101 |
+
st.session_state.chat_session = loaded_chat
|
102 |
+
else:
|
103 |
+
st.session_state.chat_session = model.start_chat(history=[])
|
104 |
+
if st.session_state.system_prompt:
|
105 |
+
st.session_state.chat_session.send_message(st.session_state.system_prompt)
|
106 |
|
107 |
st.title("Gnosticdev Chatbot")
|
108 |
|
|
|
109 |
if st.session_state.system_prompt:
|
110 |
with st.expander("Ver System Prompt actual"):
|
111 |
st.info(st.session_state.system_prompt)
|
112 |
|
113 |
+
# Mostrar historial
|
114 |
for message in st.session_state.chat_session.history:
|
115 |
with st.chat_message(translate_role_to_streamlit(message.role)):
|
116 |
st.markdown(message.parts[0].text)
|
117 |
|
118 |
+
# Campo de entrada
|
119 |
user_prompt = st.chat_input("Preguntame algo...")
|
120 |
if user_prompt:
|
121 |
st.chat_message("user").markdown(user_prompt)
|
122 |
gemini_response = st.session_state.chat_session.send_message(user_prompt)
|
123 |
with st.chat_message("assistant"):
|
124 |
st.markdown(gemini_response.text)
|
125 |
+
|
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馃摳")
|
|
|
138 |
default_prompt = "Write a caption for this image"
|
139 |
caption = gemini_pro_vision_responce(default_prompt, image)
|
140 |
with col2:
|
141 |
+
st.info(caption)
|