Update app.py
Browse files
app.py
CHANGED
@@ -1,141 +1,44 @@
|
|
1 |
-
import os
|
2 |
import streamlit as st
|
3 |
import json
|
4 |
-
|
5 |
-
|
6 |
-
from PIL import Image
|
7 |
|
8 |
-
# Setting the page config
|
9 |
-
st.set_page_config(
|
10 |
-
page_title="GnosticDev AI",
|
11 |
-
page_icon="馃",
|
12 |
-
layout="centered",
|
13 |
-
initial_sidebar_state="expanded",
|
14 |
-
)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
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 |
-
|
51 |
-
|
52 |
-
"
|
53 |
-
|
54 |
-
|
55 |
-
|
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 |
-
|
68 |
-
|
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 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
130 |
-
|
131 |
-
upload_image = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
|
132 |
-
|
133 |
-
if upload_image and st.button("Generate"):
|
134 |
-
image = Image.open(upload_image)
|
135 |
-
col1, col2 = st.columns(2)
|
136 |
-
with col1:
|
137 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
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)
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
+
import pandas as pd
|
4 |
+
import datetime
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def download_chat_history(chat_history):
|
8 |
+
if not chat_history:
|
9 |
+
st.info("No hay historial de chat para descargar.")
|
10 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Convertir a DataFrame para facilitar la exportaci贸n a CSV o Excel
|
13 |
+
df = pd.DataFrame(chat_history)
|
14 |
+
now = datetime.datetime.now()
|
15 |
+
df['timestamp'] = now
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
17 |
|
18 |
+
csv = df.to_csv(index=False)
|
19 |
+
st.download_button(
|
20 |
+
label="Descargar conversaci贸n (CSV)",
|
21 |
+
data=csv,
|
22 |
+
file_name=f"chat_history_{now.strftime('%Y%m%d_%H%M%S')}.csv",
|
23 |
+
mime="text/csv",
|
|
|
24 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Opcional: descarga en Excel (requiere openpyxl)
|
27 |
+
# ... (mismo c贸digo que en la respuesta anterior) ...
|
|
|
|
|
|
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# ... (resto de tu c贸digo Streamlit) ...
|
31 |
+
|
32 |
+
# Dentro de la secci贸n del chatbot:
|
33 |
+
|
34 |
+
chat_history = [] # Inicializa la lista para almacenar el historial del chat
|
35 |
+
|
36 |
+
# ... (tu c贸digo para manejar el chat) ...
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Dentro del bucle donde procesas los mensajes:
|
39 |
+
chat_history.append({"role": message["role"], "content": message["content"]}) #Asumiendo que tus mensajes tienen 'role' y 'content'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
# Bot贸n para descargar (al final de la secci贸n del chatbot):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
if st.button("Descargar conversaci贸n"):
|
44 |
+
download_chat_history(chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|