Update src/streamlit_app.py
Browse files- src/streamlit_app.py +23 -25
src/streamlit_app.py
CHANGED
|
@@ -212,7 +212,7 @@ st.caption("Query SAP documentation and enterprise PDFs β powered by reasoning
|
|
| 212 |
doc_choice = st.radio("Select a document:", ["-- Select --", "Sample PDF", "Upload Custom PDF"], index=0)
|
| 213 |
|
| 214 |
# ==========================================================
|
| 215 |
-
# π DOCUMENT HANDLING β CLEAN + ACCURATE UI FLOW
|
| 216 |
# ==========================================================
|
| 217 |
if doc_choice == "-- Select --":
|
| 218 |
st.info("β¬
οΈ Select or upload a document to begin.")
|
|
@@ -234,17 +234,14 @@ else:
|
|
| 234 |
with open(temp_path, "wb") as f:
|
| 235 |
f.write(uploaded_file.getbuffer())
|
| 236 |
else:
|
| 237 |
-
st.stop()
|
| 238 |
|
| 239 |
# --- Real processing begins here ---
|
| 240 |
if temp_path:
|
| 241 |
doc_name = os.path.basename(temp_path)
|
| 242 |
|
| 243 |
-
#
|
| 244 |
-
if (
|
| 245 |
-
"doc_ready" not in st.session_state
|
| 246 |
-
or st.session_state.get("last_doc") != doc_name
|
| 247 |
-
):
|
| 248 |
status = st.empty()
|
| 249 |
status.info("π€ Upload complete β reading document...")
|
| 250 |
|
|
@@ -253,12 +250,16 @@ else:
|
|
| 253 |
chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
|
| 254 |
|
| 255 |
status.info("π§ Building embeddings and search index...")
|
| 256 |
-
embeddings = cache_embeddings(
|
| 257 |
index = build_faiss_index(embeddings)
|
| 258 |
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
#
|
| 262 |
st.session_state.update({
|
| 263 |
"text": text,
|
| 264 |
"toc": toc,
|
|
@@ -267,30 +268,26 @@ else:
|
|
| 267 |
"index": index,
|
| 268 |
"doc_ready": True,
|
| 269 |
"last_doc": doc_name,
|
|
|
|
| 270 |
})
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
else:
|
| 273 |
-
#
|
| 274 |
text = st.session_state["text"]
|
| 275 |
toc = st.session_state["toc"]
|
| 276 |
chunks = st.session_state["chunks"]
|
| 277 |
embeddings = st.session_state["embeddings"]
|
| 278 |
index = st.session_state["index"]
|
| 279 |
-
st.info(f"π {st.session_state['last_doc']} is ready for queries.")
|
| 280 |
-
|
| 281 |
-
# --- Suggestion setup ---
|
| 282 |
-
if st.session_state.get("last_doc") != doc_name:
|
| 283 |
-
query_suggestions = generate_dynamic_suggestions_from_toc(toc, chunks, doc_name)
|
| 284 |
-
st.session_state.update({
|
| 285 |
-
"query_suggestions_fixed": query_suggestions,
|
| 286 |
-
"last_doc": doc_name,
|
| 287 |
-
"user_query_input": "",
|
| 288 |
-
"selected_suggestion": None,
|
| 289 |
-
"show_more": False
|
| 290 |
-
})
|
| 291 |
-
st.rerun()
|
| 292 |
-
else:
|
| 293 |
query_suggestions = st.session_state.get("query_suggestions_fixed", [])
|
|
|
|
| 294 |
|
| 295 |
# --- Ask section ---
|
| 296 |
st.markdown("### π¬ Ask the Assistant")
|
|
@@ -322,6 +319,7 @@ else:
|
|
| 322 |
answer = re.sub(r"(^|\n)-\s*", r"\1<br>β’ ", answer)
|
| 323 |
st.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
|
| 324 |
|
|
|
|
| 325 |
# ==========================================================
|
| 326 |
# π¨ Optional Sidebar Scroll Styling (keeps it clean)
|
| 327 |
# ==========================================================
|
|
|
|
| 212 |
doc_choice = st.radio("Select a document:", ["-- Select --", "Sample PDF", "Upload Custom PDF"], index=0)
|
| 213 |
|
| 214 |
# ==========================================================
|
| 215 |
+
# π DOCUMENT HANDLING β CLEAN + ACCURATE UI FLOW (final stable)
|
| 216 |
# ==========================================================
|
| 217 |
if doc_choice == "-- Select --":
|
| 218 |
st.info("β¬
οΈ Select or upload a document to begin.")
|
|
|
|
| 234 |
with open(temp_path, "wb") as f:
|
| 235 |
f.write(uploaded_file.getbuffer())
|
| 236 |
else:
|
| 237 |
+
st.stop()
|
| 238 |
|
| 239 |
# --- Real processing begins here ---
|
| 240 |
if temp_path:
|
| 241 |
doc_name = os.path.basename(temp_path)
|
| 242 |
|
| 243 |
+
# Process only once per document
|
| 244 |
+
if "doc_ready" not in st.session_state or st.session_state.get("last_doc") != doc_name:
|
|
|
|
|
|
|
|
|
|
| 245 |
status = st.empty()
|
| 246 |
status.info("π€ Upload complete β reading document...")
|
| 247 |
|
|
|
|
| 250 |
chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
|
| 251 |
|
| 252 |
status.info("π§ Building embeddings and search index...")
|
| 253 |
+
embeddings = cache_embeddings(doc_name, chunks, embed_chunks)
|
| 254 |
index = build_faiss_index(embeddings)
|
| 255 |
|
| 256 |
+
final_message = (
|
| 257 |
+
"β
Document processed successfully β hang tight, "
|
| 258 |
+
"your AI assistant is gearing up to help you out!"
|
| 259 |
+
)
|
| 260 |
+
status.success(final_message)
|
| 261 |
|
| 262 |
+
# Store everything so UI stays consistent on rerun
|
| 263 |
st.session_state.update({
|
| 264 |
"text": text,
|
| 265 |
"toc": toc,
|
|
|
|
| 268 |
"index": index,
|
| 269 |
"doc_ready": True,
|
| 270 |
"last_doc": doc_name,
|
| 271 |
+
"status_text": final_message
|
| 272 |
})
|
| 273 |
|
| 274 |
+
# Build suggestions once
|
| 275 |
+
query_suggestions = generate_dynamic_suggestions_from_toc(toc, chunks, doc_name)
|
| 276 |
+
st.session_state["query_suggestions_fixed"] = query_suggestions
|
| 277 |
+
st.session_state["user_query_input"] = ""
|
| 278 |
+
st.session_state["selected_suggestion"] = None
|
| 279 |
+
st.session_state["show_more"] = False
|
| 280 |
+
st.rerun()
|
| 281 |
+
|
| 282 |
else:
|
| 283 |
+
# Reuse cached content and status message
|
| 284 |
text = st.session_state["text"]
|
| 285 |
toc = st.session_state["toc"]
|
| 286 |
chunks = st.session_state["chunks"]
|
| 287 |
embeddings = st.session_state["embeddings"]
|
| 288 |
index = st.session_state["index"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
query_suggestions = st.session_state.get("query_suggestions_fixed", [])
|
| 290 |
+
st.info(st.session_state.get("status_text", f"π {doc_name} is ready for queries."))
|
| 291 |
|
| 292 |
# --- Ask section ---
|
| 293 |
st.markdown("### π¬ Ask the Assistant")
|
|
|
|
| 319 |
answer = re.sub(r"(^|\n)-\s*", r"\1<br>β’ ", answer)
|
| 320 |
st.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
|
| 321 |
|
| 322 |
+
|
| 323 |
# ==========================================================
|
| 324 |
# π¨ Optional Sidebar Scroll Styling (keeps it clean)
|
| 325 |
# ==========================================================
|