Ary-007 commited on
Commit
7c11c25
Β·
verified Β·
1 Parent(s): 630354a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -0
app.py CHANGED
@@ -2,12 +2,27 @@ import streamlit as st
2
  import uuid
3
  import os
4
  import base64
 
5
  from langchain_community.retrievers import BM25Retriever
6
  from langchain_core.messages import HumanMessage, AIMessage
7
 
8
  from data_processing import process_and_ingest, SessionDocStore, cleanup_session_index
9
  from rag_engine import run_advanced_rag
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # ──────────────────────────────────────────────────────────────────
12
  # 1. PAGE CONFIG & STATE INITIALIZATION
13
  # ──────────────────────────────────────────────────────────────────
@@ -21,6 +36,7 @@ st.set_page_config(
21
  # Initialize Session State
22
  if "session_id" not in st.session_state:
23
  st.session_state.session_id = str(uuid.uuid4())
 
24
 
25
  if "doc_store" not in st.session_state:
26
  st.session_state.doc_store = SessionDocStore()
 
2
  import uuid
3
  import os
4
  import base64
5
+ import weakref
6
  from langchain_community.retrievers import BM25Retriever
7
  from langchain_core.messages import HumanMessage, AIMessage
8
 
9
  from data_processing import process_and_ingest, SessionDocStore, cleanup_session_index
10
  from rag_engine import run_advanced_rag
11
 
12
+ # ──────────────────────────────────────────────────────────────────
13
+ # AUTOMATIC CLEANUP HANDLER (The Janitor)
14
+ # ──────────────────────────────────────────────────────────────────
15
+
16
+ class SessionJanitor:
17
+ """
18
+ This object lives in the session state.
19
+ When the session ends (Refresh/Close), this object is destroyed.
20
+ The 'weakref.finalize' automatically calls the cleanup function.
21
+ """
22
+ def __init__(self, session_id):
23
+ self.session_id = session_id
24
+ # Register the cleanup function to run when this object dies
25
+ weakref.finalize(self, cleanup_session_index, session_id)
26
  # ──────────────────────────────────────────────────────────────────
27
  # 1. PAGE CONFIG & STATE INITIALIZATION
28
  # ──────────────────────────────────────────────────────────────────
 
36
  # Initialize Session State
37
  if "session_id" not in st.session_state:
38
  st.session_state.session_id = str(uuid.uuid4())
39
+ st.session_state.janitor = SessionJanitor(st.session_state.session_id)
40
 
41
  if "doc_store" not in st.session_state:
42
  st.session_state.doc_store = SessionDocStore()