Update src/streamlit_app.py
Browse files- src/streamlit_app.py +71 -6
src/streamlit_app.py
CHANGED
|
@@ -7,6 +7,21 @@ import streamlit as st
|
|
| 7 |
import torch
|
| 8 |
from document_registry import DocumentRegistry
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# ==========================================================
|
| 11 |
# β
PAGE CONFIGS
|
| 12 |
# ==========================================================
|
|
@@ -85,6 +100,30 @@ Output: Write each question on a new line. Do not invent facts β base question
|
|
| 85 |
except Exception:
|
| 86 |
return ["How do I start using this guide?", "What does this document cover?"]
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# ==========================================================
|
| 89 |
# π¨ STYLING β REVERT TO ORIGINAL
|
| 90 |
# ==========================================================
|
|
@@ -149,13 +188,36 @@ with st.sidebar:
|
|
| 149 |
if "registry" in st.session_state:
|
| 150 |
registry = st.session_state["registry"]
|
| 151 |
registered_docs = registry.list_docs() if hasattr(registry, "list_docs") else []
|
|
|
|
| 152 |
if registered_docs:
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
else:
|
| 160 |
st.caption("π No documents registered yet.")
|
| 161 |
else:
|
|
@@ -163,6 +225,7 @@ with st.sidebar:
|
|
| 163 |
|
| 164 |
st.markdown("---")
|
| 165 |
|
|
|
|
| 166 |
show_dev = st.checkbox("Show advanced settings (for developers)", value=False)
|
| 167 |
if show_dev:
|
| 168 |
st.markdown("### βοΈ Developer Options")
|
|
@@ -175,6 +238,7 @@ with st.sidebar:
|
|
| 175 |
st.markdown("---")
|
| 176 |
st.caption("β¨ Built by Shubham Sharma")
|
| 177 |
|
|
|
|
| 178 |
if show_dev:
|
| 179 |
st.markdown("---")
|
| 180 |
with st.expander("π§© Developer Insights", expanded=False):
|
|
@@ -196,6 +260,7 @@ with st.sidebar:
|
|
| 196 |
st.text_area("", doc_text[:1000], height=120)
|
| 197 |
st.caption(f"{len(st.session_state.get('chunks', []))} chunks processed.")
|
| 198 |
|
|
|
|
| 199 |
# ==========================================================
|
| 200 |
# π§ SESSION STATE SAFETY INITIALIZATION
|
| 201 |
# ==========================================================
|
|
|
|
| 7 |
import torch
|
| 8 |
from document_registry import DocumentRegistry
|
| 9 |
|
| 10 |
+
# --- Initialize session keys to avoid rerun issues ---
|
| 11 |
+
if "registry" not in st.session_state:
|
| 12 |
+
st.session_state.registry = DocumentRegistry()
|
| 13 |
+
if "active_doc" not in st.session_state:
|
| 14 |
+
st.session_state.active_doc = None
|
| 15 |
+
if "chunks" not in st.session_state:
|
| 16 |
+
st.session_state.chunks = []
|
| 17 |
+
if "embeddings" not in st.session_state:
|
| 18 |
+
st.session_state.embeddings = None
|
| 19 |
+
if "index" not in st.session_state:
|
| 20 |
+
st.session_state.index = None
|
| 21 |
+
if "query_suggestions_fixed" not in st.session_state:
|
| 22 |
+
st.session_state.query_suggestions_fixed = []
|
| 23 |
+
|
| 24 |
+
|
| 25 |
# ==========================================================
|
| 26 |
# β
PAGE CONFIGS
|
| 27 |
# ==========================================================
|
|
|
|
| 100 |
except Exception:
|
| 101 |
return ["How do I start using this guide?", "What does this document cover?"]
|
| 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 |
+
|
| 108 |
+
if not doc:
|
| 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 |
# ==========================================================
|
|
|
|
| 188 |
if "registry" in st.session_state:
|
| 189 |
registry = st.session_state["registry"]
|
| 190 |
registered_docs = registry.list_docs() if hasattr(registry, "list_docs") else []
|
| 191 |
+
|
| 192 |
if registered_docs:
|
| 193 |
+
st.markdown("### π Registered Documents")
|
| 194 |
+
|
| 195 |
+
# Loop through registered documents
|
| 196 |
+
for i, doc in enumerate(registered_docs, start=1):
|
| 197 |
+
doc_name = doc.get("name", f"Document {i}")
|
| 198 |
+
chunks = doc.get("num_chunks", "?")
|
| 199 |
+
toc_source = doc.get("toc_source", "β")
|
| 200 |
+
|
| 201 |
+
# Use an expander for each document
|
| 202 |
+
with st.expander(f"{i}. {doc_name}", expanded=False):
|
| 203 |
+
st.markdown(f"- π§© **Chunks:** {chunks}")
|
| 204 |
+
st.markdown(f"- ποΈ **TOC Source:** {toc_source}")
|
| 205 |
+
|
| 206 |
+
# If this document is currently active, show status
|
| 207 |
+
if (
|
| 208 |
+
"active_doc" in st.session_state
|
| 209 |
+
and st.session_state.active_doc
|
| 210 |
+
and st.session_state.active_doc.get("name") == doc_name
|
| 211 |
+
):
|
| 212 |
+
st.success("β
Active Document")
|
| 213 |
+
else:
|
| 214 |
+
# Button to activate this document
|
| 215 |
+
st.button(
|
| 216 |
+
"Activate",
|
| 217 |
+
key=f"activate_{doc_name}",
|
| 218 |
+
on_click=activate_document,
|
| 219 |
+
args=(doc_name,),
|
| 220 |
+
)
|
| 221 |
else:
|
| 222 |
st.caption("π No documents registered yet.")
|
| 223 |
else:
|
|
|
|
| 225 |
|
| 226 |
st.markdown("---")
|
| 227 |
|
| 228 |
+
# π§ Developer Options Section
|
| 229 |
show_dev = st.checkbox("Show advanced settings (for developers)", value=False)
|
| 230 |
if show_dev:
|
| 231 |
st.markdown("### βοΈ Developer Options")
|
|
|
|
| 238 |
st.markdown("---")
|
| 239 |
st.caption("β¨ Built by Shubham Sharma")
|
| 240 |
|
| 241 |
+
# π§© Developer Insights
|
| 242 |
if show_dev:
|
| 243 |
st.markdown("---")
|
| 244 |
with st.expander("π§© Developer Insights", expanded=False):
|
|
|
|
| 260 |
st.text_area("", doc_text[:1000], height=120)
|
| 261 |
st.caption(f"{len(st.session_state.get('chunks', []))} chunks processed.")
|
| 262 |
|
| 263 |
+
|
| 264 |
# ==========================================================
|
| 265 |
# π§ SESSION STATE SAFETY INITIALIZATION
|
| 266 |
# ==========================================================
|