Spaces:
Sleeping
Sleeping
Update session_state.py
Browse files- session_state.py +42 -50
session_state.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import joblib
|
| 3 |
import os
|
| 4 |
-
import uuid
|
| 5 |
from google import genai
|
| 6 |
from google.genai import types
|
| 7 |
|
| 8 |
-
DEFAULT_GEMINI_MODEL = 'gemini-
|
| 9 |
DATA_DIR = 'data'
|
| 10 |
PAST_CHATS_LIST_PATH = f'{DATA_DIR}/past_chats_list'
|
| 11 |
|
|
@@ -43,12 +42,6 @@ class SessionState:
|
|
| 43 |
|
| 44 |
if 'system_instruction' not in st.session_state:
|
| 45 |
st.session_state.system_instruction = None
|
| 46 |
-
|
| 47 |
-
if 'user_id' not in st.session_state:
|
| 48 |
-
st.session_state.user_id = f'local-{uuid.uuid4()}'
|
| 49 |
-
|
| 50 |
-
if 'session_store' not in st.session_state:
|
| 51 |
-
st.session_state.session_store = None
|
| 52 |
|
| 53 |
# Getters y setters para cada propiedad
|
| 54 |
@property
|
|
@@ -123,22 +116,6 @@ class SessionState:
|
|
| 123 |
def system_instruction(self, value):
|
| 124 |
st.session_state.system_instruction = value
|
| 125 |
|
| 126 |
-
@property
|
| 127 |
-
def user_id(self):
|
| 128 |
-
return st.session_state.user_id
|
| 129 |
-
|
| 130 |
-
@user_id.setter
|
| 131 |
-
def user_id(self, value):
|
| 132 |
-
st.session_state.user_id = value
|
| 133 |
-
|
| 134 |
-
@property
|
| 135 |
-
def session_store(self):
|
| 136 |
-
return st.session_state.session_store
|
| 137 |
-
|
| 138 |
-
@session_store.setter
|
| 139 |
-
def session_store(self, value):
|
| 140 |
-
st.session_state.session_store = value
|
| 141 |
-
|
| 142 |
# Métodos de utilidad
|
| 143 |
def add_message(self, role, content, avatar=None):
|
| 144 |
"""Añade un mensaje al historial"""
|
|
@@ -154,11 +131,6 @@ class SessionState:
|
|
| 154 |
"""Limpia el prompt del estado de la sesión"""
|
| 155 |
self.prompt = None
|
| 156 |
|
| 157 |
-
def set_storage(self, user_id, session_store=None):
|
| 158 |
-
"""Configura el usuario actual y el store opcional (Firebase)."""
|
| 159 |
-
self.user_id = user_id
|
| 160 |
-
self.session_store = session_store
|
| 161 |
-
|
| 162 |
def initialize_model(self, model_name=None, api_key=None):
|
| 163 |
"""Inicializa el modelo de IA"""
|
| 164 |
if model_name is None:
|
|
@@ -242,13 +214,10 @@ class SessionState:
|
|
| 242 |
if chat_id is None:
|
| 243 |
chat_id = self.chat_id
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
return
|
| 248 |
-
|
| 249 |
-
os.makedirs(self._user_data_dir(), exist_ok=True)
|
| 250 |
joblib.dump(self.messages, self._st_messages_path(chat_id))
|
| 251 |
-
joblib.dump(
|
| 252 |
|
| 253 |
def load_chat_history(self, chat_id=None):
|
| 254 |
"""Carga el historial del chat"""
|
|
@@ -256,18 +225,9 @@ class SessionState:
|
|
| 256 |
chat_id = self.chat_id
|
| 257 |
|
| 258 |
try:
|
| 259 |
-
if self.session_store:
|
| 260 |
-
messages, gemini_history = self.session_store.load_chat_history(self.user_id, chat_id)
|
| 261 |
-
if messages is not None and gemini_history is not None:
|
| 262 |
-
self.messages = messages
|
| 263 |
-
self.gemini_history = gemini_history
|
| 264 |
-
return True
|
| 265 |
-
self.messages = []
|
| 266 |
-
self.gemini_history = []
|
| 267 |
-
return False
|
| 268 |
-
|
| 269 |
self.messages = joblib.load(self._st_messages_path(chat_id))
|
| 270 |
-
|
|
|
|
| 271 |
return True
|
| 272 |
except (FileNotFoundError, EOFError):
|
| 273 |
self.messages = []
|
|
@@ -275,13 +235,45 @@ class SessionState:
|
|
| 275 |
return False
|
| 276 |
|
| 277 |
def _st_messages_path(self, chat_id):
|
| 278 |
-
return f'{
|
| 279 |
|
| 280 |
def _gemini_messages_path(self, chat_id):
|
| 281 |
-
return f'{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
def
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
def has_messages(self):
|
| 287 |
"""Verifica si hay mensajes en el historial"""
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import joblib
|
| 3 |
import os
|
|
|
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
|
| 7 |
+
DEFAULT_GEMINI_MODEL = 'gemini-3.1-flash-lite-preview'
|
| 8 |
DATA_DIR = 'data'
|
| 9 |
PAST_CHATS_LIST_PATH = f'{DATA_DIR}/past_chats_list'
|
| 10 |
|
|
|
|
| 42 |
|
| 43 |
if 'system_instruction' not in st.session_state:
|
| 44 |
st.session_state.system_instruction = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Getters y setters para cada propiedad
|
| 47 |
@property
|
|
|
|
| 116 |
def system_instruction(self, value):
|
| 117 |
st.session_state.system_instruction = value
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# Métodos de utilidad
|
| 120 |
def add_message(self, role, content, avatar=None):
|
| 121 |
"""Añade un mensaje al historial"""
|
|
|
|
| 131 |
"""Limpia el prompt del estado de la sesión"""
|
| 132 |
self.prompt = None
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
def initialize_model(self, model_name=None, api_key=None):
|
| 135 |
"""Inicializa el modelo de IA"""
|
| 136 |
if model_name is None:
|
|
|
|
| 214 |
if chat_id is None:
|
| 215 |
chat_id = self.chat_id
|
| 216 |
|
| 217 |
+
serialized_history = self._serialize_gemini_history(self.gemini_history)
|
| 218 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
| 219 |
joblib.dump(self.messages, self._st_messages_path(chat_id))
|
| 220 |
+
joblib.dump(serialized_history, self._gemini_messages_path(chat_id))
|
| 221 |
|
| 222 |
def load_chat_history(self, chat_id=None):
|
| 223 |
"""Carga el historial del chat"""
|
|
|
|
| 225 |
chat_id = self.chat_id
|
| 226 |
|
| 227 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
self.messages = joblib.load(self._st_messages_path(chat_id))
|
| 229 |
+
loaded_history = joblib.load(self._gemini_messages_path(chat_id))
|
| 230 |
+
self.gemini_history = self._deserialize_gemini_history(loaded_history)
|
| 231 |
return True
|
| 232 |
except (FileNotFoundError, EOFError):
|
| 233 |
self.messages = []
|
|
|
|
| 235 |
return False
|
| 236 |
|
| 237 |
def _st_messages_path(self, chat_id):
|
| 238 |
+
return f'{DATA_DIR}/{chat_id}-st_messages'
|
| 239 |
|
| 240 |
def _gemini_messages_path(self, chat_id):
|
| 241 |
+
return f'{DATA_DIR}/{chat_id}-gemini_messages'
|
| 242 |
+
|
| 243 |
+
def _serialize_gemini_history(self, history):
|
| 244 |
+
"""Convierte tipos del SDK (Content/Part) a diccionarios serializables."""
|
| 245 |
+
serialized = []
|
| 246 |
+
for item in history or []:
|
| 247 |
+
if isinstance(item, dict):
|
| 248 |
+
serialized.append(item)
|
| 249 |
+
continue
|
| 250 |
+
if hasattr(item, "model_dump"):
|
| 251 |
+
serialized.append(item.model_dump(mode="python"))
|
| 252 |
+
continue
|
| 253 |
+
if hasattr(item, "to_dict"):
|
| 254 |
+
serialized.append(item.to_dict())
|
| 255 |
+
continue
|
| 256 |
+
serialized.append(item)
|
| 257 |
+
return serialized
|
| 258 |
|
| 259 |
+
def _deserialize_gemini_history(self, history):
|
| 260 |
+
"""Reconstruye Content para rehidratar chat history en google-genai."""
|
| 261 |
+
deserialized = []
|
| 262 |
+
for item in history or []:
|
| 263 |
+
if isinstance(item, dict) and "role" in item and "parts" in item:
|
| 264 |
+
role = item.get("role")
|
| 265 |
+
parts_data = item.get("parts", [])
|
| 266 |
+
parts = []
|
| 267 |
+
for part in parts_data:
|
| 268 |
+
if isinstance(part, dict) and "text" in part:
|
| 269 |
+
parts.append(types.Part(text=part["text"]))
|
| 270 |
+
elif isinstance(part, str):
|
| 271 |
+
parts.append(types.Part(text=part))
|
| 272 |
+
if parts:
|
| 273 |
+
deserialized.append(types.Content(role=role, parts=parts))
|
| 274 |
+
continue
|
| 275 |
+
deserialized.append(item)
|
| 276 |
+
return deserialized
|
| 277 |
|
| 278 |
def has_messages(self):
|
| 279 |
"""Verifica si hay mensajes en el historial"""
|