JeCabrera commited on
Commit
c8a5a7f
verified
1 Parent(s): 2ff4f9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -3,18 +3,26 @@ import os
3
  import joblib
4
  import streamlit as st
5
  from dotenv import load_dotenv
 
6
  from system_prompts import get_unified_reel_prompt # Cambiar de get_unified_puv_prompt a get_unified_reel_prompt
7
  from reel_formulas import reel_formulas
8
  from session_state import (
9
  SessionState,
10
  DEFAULT_GEMINI_MODEL,
11
  DATA_DIR,
12
- PAST_CHATS_LIST_PATH,
13
  )
14
 
15
  # Inicializar el estado de la sesi贸n
16
  state = SessionState()
17
  STREAM_SETTINGS = {'batch_size': 1, 'delay_seconds': 0.01}
 
 
 
 
 
 
 
 
18
 
19
  # Funci贸n para detectar saludos y generar respuestas personalizadas
20
  def is_greeting(text):
@@ -71,7 +79,7 @@ def handle_chat_title(prompt):
71
  past_chats[state.chat_id] = state.chat_title
72
  else:
73
  state.chat_title = past_chats[state.chat_id]
74
- joblib.dump(past_chats, PAST_CHATS_LIST_PATH)
75
 
76
  def detect_formula_selection(prompt):
77
  """Detecta si el usuario eligi贸 una f贸rmula por nombre o por n煤mero."""
@@ -254,20 +262,19 @@ if not GOOGLE_API_KEY:
254
  st.stop()
255
 
256
  # Configuraci贸n de la aplicaci贸n
 
 
257
  new_chat_id = f'{time.time()}'
258
  MODEL_ROLE = 'ai'
259
  AI_AVATAR_ICON = '馃' # Cambia el emoji por uno de robot para coincidir con tu logo
260
  USER_AVATAR_ICON = '馃懁' # A帽ade un avatar para el usuario
261
 
262
  # Crear carpeta de datos si no existe
263
- try:
264
- os.mkdir(DATA_DIR)
265
- except FileExistsError:
266
- pass
267
 
268
  # Cargar chats anteriores
269
  try:
270
- past_chats = joblib.load(PAST_CHATS_LIST_PATH)
271
  except (FileNotFoundError, EOFError):
272
  past_chats = {}
273
 
 
3
  import joblib
4
  import streamlit as st
5
  from dotenv import load_dotenv
6
+ from streamlit.runtime.scriptrunner import get_script_run_ctx
7
  from system_prompts import get_unified_reel_prompt # Cambiar de get_unified_puv_prompt a get_unified_reel_prompt
8
  from reel_formulas import reel_formulas
9
  from session_state import (
10
  SessionState,
11
  DEFAULT_GEMINI_MODEL,
12
  DATA_DIR,
 
13
  )
14
 
15
  # Inicializar el estado de la sesi贸n
16
  state = SessionState()
17
  STREAM_SETTINGS = {'batch_size': 1, 'delay_seconds': 0.01}
18
+ user_past_chats_list_path = None
19
+
20
+ def get_user_namespace():
21
+ """Obtiene un namespace por sesi贸n de Streamlit para aislar historial entre usuarios."""
22
+ context = get_script_run_ctx()
23
+ if context and getattr(context, 'session_id', None):
24
+ return context.session_id
25
+ return 'default'
26
 
27
  # Funci贸n para detectar saludos y generar respuestas personalizadas
28
  def is_greeting(text):
 
79
  past_chats[state.chat_id] = state.chat_title
80
  else:
81
  state.chat_title = past_chats[state.chat_id]
82
+ joblib.dump(past_chats, user_past_chats_list_path)
83
 
84
  def detect_formula_selection(prompt):
85
  """Detecta si el usuario eligi贸 una f贸rmula por nombre o por n煤mero."""
 
262
  st.stop()
263
 
264
  # Configuraci贸n de la aplicaci贸n
265
+ state.user_namespace = get_user_namespace()
266
+ user_past_chats_list_path = f'{DATA_DIR}/{state.user_namespace}/past_chats_list'
267
  new_chat_id = f'{time.time()}'
268
  MODEL_ROLE = 'ai'
269
  AI_AVATAR_ICON = '馃' # Cambia el emoji por uno de robot para coincidir con tu logo
270
  USER_AVATAR_ICON = '馃懁' # A帽ade un avatar para el usuario
271
 
272
  # Crear carpeta de datos si no existe
273
+ os.makedirs(f'{DATA_DIR}/{state.user_namespace}', exist_ok=True)
 
 
 
274
 
275
  # Cargar chats anteriores
276
  try:
277
+ past_chats = joblib.load(user_past_chats_list_path)
278
  except (FileNotFoundError, EOFError):
279
  past_chats = {}
280