Shubham170793 commited on
Commit
361f68a
Β·
verified Β·
1 Parent(s): b8a5c60

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -9
src/streamlit_app.py CHANGED
@@ -102,6 +102,7 @@ Output: Write each question on a new line. Do not invent facts β€” base question
102
 
103
  # --- Function to activate a document from the registry ---
104
  def activate_document(doc_name):
 
105
  registry = st.session_state.registry
106
  doc = registry.get_doc(doc_name)
107
 
@@ -109,21 +110,35 @@ def activate_document(doc_name):
109
  st.warning(f"Document {doc_name} not found in registry.")
110
  return
111
 
112
- # Load this document's preprocessed data into session
113
  st.session_state.active_doc = doc
114
- st.session_state.chunks = doc["chunks"]
115
- st.session_state.embeddings = doc["embeddings"]
116
- st.session_state.index = doc["index"]
117
 
118
- # Regenerate suggestions for this document
119
- st.session_state.query_suggestions_fixed = generate_dynamic_suggestions_from_toc(
120
- doc["toc"], doc["chunks"], doc["name"]
121
- )
 
122
 
123
- # Trigger a clean rerun so UI updates with new context
 
 
 
 
 
 
 
 
 
 
 
 
124
  st.rerun()
125
 
126
 
 
127
  # ==========================================================
128
  # 🎨 STYLING β€” REVERT TO ORIGINAL
129
  # ==========================================================
 
102
 
103
  # --- Function to activate a document from the registry ---
104
  def activate_document(doc_name):
105
+ """Load an existing registered document into the active context."""
106
  registry = st.session_state.registry
107
  doc = registry.get_doc(doc_name)
108
 
 
110
  st.warning(f"Document {doc_name} not found in registry.")
111
  return
112
 
113
+ # Load all available preprocessed data
114
  st.session_state.active_doc = doc
115
+ st.session_state.chunks = doc.get("chunks", [])
116
+ st.session_state.embeddings = doc.get("embeddings")
117
+ st.session_state.index = doc.get("index")
118
 
119
+ # --- Handle TOC safely ---
120
+ # Some docs may not have toc saved in registry; fallback to empty list
121
+ toc_data = doc.get("toc", [])
122
+ if not toc_data:
123
+ st.info(f"⚠️ No TOC found for {doc_name}. Using empty TOC for suggestions.")
124
 
125
+ # --- Regenerate suggestions safely ---
126
+ try:
127
+ st.session_state.query_suggestions_fixed = generate_dynamic_suggestions_from_toc(
128
+ toc_data,
129
+ st.session_state.chunks,
130
+ doc.get("name", "Unknown Document")
131
+ )
132
+ except Exception as e:
133
+ # Defensive guard: even if suggestion generator fails, app won't break
134
+ st.warning(f"Suggestion generation failed: {e}")
135
+ st.session_state.query_suggestions_fixed = []
136
+
137
+ # --- Trigger a clean UI rerun so the switch shows immediately ---
138
  st.rerun()
139
 
140
 
141
+
142
  # ==========================================================
143
  # 🎨 STYLING β€” REVERT TO ORIGINAL
144
  # ==========================================================