gnosticdev commited on
Commit
2b28b66
verified
1 Parent(s): 7eda8e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -17
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
- # Configuraci贸n de la p谩gina
9
  st.set_page_config(
10
  page_title="GnosticDev AI",
11
  page_icon="馃",
@@ -13,23 +13,26 @@ st.set_page_config(
13
  initial_sidebar_state="expanded",
14
  )
15
 
16
- # Funci贸n para guardar el historial en el estado de la sesi贸n
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.chat_history = serializable_history
 
25
 
26
- # Funci贸n para cargar el historial desde el estado de la sesi贸n
27
  def load_chat_history():
28
- if 'chat_history' in st.session_state:
29
  try:
30
- history = st.session_state.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:
@@ -40,26 +43,32 @@ def load_chat_history():
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 = ""
46
-
47
- if "chat_history" not in st.session_state:
48
- st.session_state.chat_history = []
49
 
50
  with st.sidebar:
51
  selected = option_menu(
52
  "GD AI",
53
- ["Chatbot", "Image Captioning"], # Eliminamos "System Prompt"
54
  menu_icon="robot",
55
- icons=['chat-dots-fill', 'image-fill'],
56
  default_index=0
57
  )
58
 
59
  # Bot贸n para borrar historial
60
  if st.button("Borrar Historial"):
61
- st.session_state.chat_history = []
62
- st.session_state.chat_session = None
 
 
63
  st.success("Historial borrado!")
64
 
65
  def translate_role_to_streamlit(user_role):
@@ -68,7 +77,28 @@ def translate_role_to_streamlit(user_role):
68
  else:
69
  return user_role
70
 
71
- if selected == "Chatbot":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  model = load_gemini_pro()
73
 
74
  # Inicializar o cargar sesi贸n de chat
@@ -92,7 +122,7 @@ if selected == "Chatbot":
92
  with st.chat_message(translate_role_to_streamlit(message.role)):
93
  st.markdown(message.parts[0].text)
94
 
95
- # Campo de entrada
96
  user_prompt = st.chat_input("Preguntame algo...")
97
  if user_prompt:
98
  st.chat_message("user").markdown(user_prompt)
@@ -105,7 +135,7 @@ if selected == "Chatbot":
105
 
106
  # Opci贸n para descargar el historial del chat
107
  if st.button("Descargar Historial del Chat"):
108
- chat_history = "\n".join([f"{msg['role']}: {msg['text']}" for msg in st.session_state.chat_history])
109
  st.download_button(
110
  label="Descargar",
111
  data=chat_history,
 
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:
 
43
  st.error(f"Error cargando el historial: {e}")
44
  return None
45
 
46
+ # Funci贸n para descargar el historial del chat
47
+ def download_chat_history(history):
48
+ chat_text = ""
49
+ for message in history:
50
+ chat_text += f"{message.role}: {message.parts[0].text}\n"
51
+ return chat_text
52
+
53
  # Inicializar estados
54
  if "system_prompt" not in st.session_state:
55
+ st.session_state.system_prompt = st.session_state.get('cookie_system_prompt', "")
 
 
 
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):
 
77
  else:
78
  return user_role
79
 
80
+ if selected == "System Prompt":
81
+ st.title("Configuraci贸n del System Prompt")
82
+
83
+ new_system_prompt = st.text_area(
84
+ "Ingresa las instrucciones para el AI (System Prompt)",
85
+ value=st.session_state.system_prompt,
86
+ height=300,
87
+ help="Escribe aqu铆 las instrucciones que definir谩n el comportamiento del AI"
88
+ )
89
+
90
+ if st.button("Guardar System Prompt"):
91
+ st.session_state.system_prompt = new_system_prompt
92
+ st.session_state.cookie_system_prompt = new_system_prompt # Guardar en cookie
93
+ if "chat_session" in st.session_state:
94
+ del st.session_state.chat_session
95
+ st.success("System Prompt actualizado con 茅xito!")
96
+
97
+ if st.session_state.system_prompt:
98
+ st.markdown("### System Prompt Actual:")
99
+ st.info(st.session_state.system_prompt)
100
+
101
+ elif selected == "Chatbot":
102
  model = load_gemini_pro()
103
 
104
  # Inicializar o cargar sesi贸n de chat
 
122
  with st.chat_message(translate_role_to_streamlit(message.role)):
123
  st.markdown(message.parts[0].text)
124
 
125
+ # Campo de entrada
126
  user_prompt = st.chat_input("Preguntame algo...")
127
  if user_prompt:
128
  st.chat_message("user").markdown(user_prompt)
 
135
 
136
  # Opci贸n para descargar el historial del chat
137
  if st.button("Descargar Historial del Chat"):
138
+ chat_history = download_chat_history(st.session_state.chat_session.history)
139
  st.download_button(
140
  label="Descargar",
141
  data=chat_history,