Update src/streamlit_app.py
Browse files- src/streamlit_app.py +120 -92
src/streamlit_app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ==========================================================
|
| 2 |
-
# streamlit_app.py β Restored
|
| 3 |
# ==========================================================
|
| 4 |
import os
|
| 5 |
import re
|
|
@@ -8,10 +8,11 @@ from ingestion import extract_text_from_pdf, chunk_text
|
|
| 8 |
from vectorstore import build_faiss_index
|
| 9 |
from qa import retrieve_chunks, generate_answer, cache_embeddings, embed_chunks, genai_generate
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# ==========================================================
|
| 14 |
-
# π§ SIDEBAR β
|
| 15 |
# ==========================================================
|
| 16 |
with st.sidebar:
|
| 17 |
st.markdown("### π§ Response Mode")
|
|
@@ -36,7 +37,7 @@ with st.sidebar:
|
|
| 36 |
st.caption("β¨ Built by Shubham Sharma")
|
| 37 |
|
| 38 |
# ==========================================================
|
| 39 |
-
# π§ SESSION STATE
|
| 40 |
# ==========================================================
|
| 41 |
for key, val in {
|
| 42 |
"user_query_input": "",
|
|
@@ -54,100 +55,127 @@ def set_user_query(q, idx):
|
|
| 54 |
st.experimental_rerun()
|
| 55 |
|
| 56 |
# ==========================================================
|
| 57 |
-
# π
|
| 58 |
# ==========================================================
|
| 59 |
st.markdown(
|
| 60 |
"""
|
| 61 |
-
<
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
""",
|
| 66 |
unsafe_allow_html=True,
|
| 67 |
)
|
| 68 |
|
| 69 |
# ==========================================================
|
| 70 |
-
#
|
| 71 |
# ==========================================================
|
| 72 |
-
st.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
st.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
st.success(
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
"
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ==========================================================
|
| 2 |
+
# streamlit_app.py β Restored Centered Layout + Green Status UI
|
| 3 |
# ==========================================================
|
| 4 |
import os
|
| 5 |
import re
|
|
|
|
| 8 |
from vectorstore import build_faiss_index
|
| 9 |
from qa import retrieve_chunks, generate_answer, cache_embeddings, embed_chunks, genai_generate
|
| 10 |
|
| 11 |
+
# β
Centered layout again (this caused the stretch difference)
|
| 12 |
+
st.set_page_config(page_title="Enterprise Knowledge Assistant", layout="centered")
|
| 13 |
|
| 14 |
# ==========================================================
|
| 15 |
+
# π§ SIDEBAR β As before
|
| 16 |
# ==========================================================
|
| 17 |
with st.sidebar:
|
| 18 |
st.markdown("### π§ Response Mode")
|
|
|
|
| 37 |
st.caption("β¨ Built by Shubham Sharma")
|
| 38 |
|
| 39 |
# ==========================================================
|
| 40 |
+
# π§ SESSION STATE
|
| 41 |
# ==========================================================
|
| 42 |
for key, val in {
|
| 43 |
"user_query_input": "",
|
|
|
|
| 55 |
st.experimental_rerun()
|
| 56 |
|
| 57 |
# ==========================================================
|
| 58 |
+
# π PAGE HEADER (Centered)
|
| 59 |
# ==========================================================
|
| 60 |
st.markdown(
|
| 61 |
"""
|
| 62 |
+
<div style="text-align:center; margin-top:-10px;">
|
| 63 |
+
<h1>π Enterprise Knowledge Assistant</h1>
|
| 64 |
+
<p style="color:gray; font-size:15px;">
|
| 65 |
+
Query SAP documentation and enterprise PDFs β powered by reasoning and retrieval.
|
| 66 |
+
</p>
|
| 67 |
+
</div>
|
| 68 |
""",
|
| 69 |
unsafe_allow_html=True,
|
| 70 |
)
|
| 71 |
|
| 72 |
# ==========================================================
|
| 73 |
+
# π MAIN CONTAINER β Centered
|
| 74 |
# ==========================================================
|
| 75 |
+
col_spacer_left, col_main, col_spacer_right = st.columns([0.15, 0.7, 0.15])
|
| 76 |
+
with col_main:
|
| 77 |
+
|
| 78 |
+
# ----------------------------------------------------------
|
| 79 |
+
# π Document Upload / Selection
|
| 80 |
+
# ----------------------------------------------------------
|
| 81 |
+
st.markdown("### Select a document:")
|
| 82 |
+
doc_choice = st.radio("", ("-- Select --", "Sample PDF", "Upload Custom PDF"))
|
| 83 |
+
|
| 84 |
+
temp_path = None
|
| 85 |
+
if doc_choice == "Sample PDF":
|
| 86 |
+
temp_path = os.path.join(os.path.dirname(__file__), "sample.pdf")
|
| 87 |
+
st.success("π Sample PDF loaded successfully. Ask questions below.")
|
| 88 |
+
st.success("β
Document ready β you can now ask questions below.")
|
| 89 |
+
elif doc_choice == "Upload Custom PDF":
|
| 90 |
+
uploaded_file = st.file_uploader("Upload your PDF", type="pdf")
|
| 91 |
+
if uploaded_file:
|
| 92 |
+
temp_path = os.path.join("/tmp", uploaded_file.name)
|
| 93 |
+
with open(temp_path, "wb") as f:
|
| 94 |
+
f.write(uploaded_file.getbuffer())
|
| 95 |
+
st.success(f"β
'{uploaded_file.name}' uploaded successfully β ready to query below.")
|
| 96 |
+
st.success("β
Document ready β you can now ask questions below.")
|
| 97 |
+
else:
|
| 98 |
+
st.info("β¬
οΈ Please select or upload a document to begin.")
|
| 99 |
+
|
| 100 |
+
# ----------------------------------------------------------
|
| 101 |
+
# π§ Process document when loaded
|
| 102 |
+
# ----------------------------------------------------------
|
| 103 |
+
if temp_path:
|
| 104 |
+
text, toc = extract_text_from_pdf(temp_path)
|
| 105 |
+
chunks = chunk_text(text, chunk_size=chunk_size, overlap=overlap)
|
| 106 |
+
embeddings = cache_embeddings(os.path.basename(temp_path), chunks, embed_chunks)
|
| 107 |
+
index = build_faiss_index(embeddings)
|
| 108 |
+
|
| 109 |
+
# ----------------------------------------------------------
|
| 110 |
+
# π‘ Suggested Questions
|
| 111 |
+
# ----------------------------------------------------------
|
| 112 |
+
if not st.session_state["query_suggestions_fixed"]:
|
| 113 |
+
st.session_state["query_suggestions_fixed"] = [
|
| 114 |
+
"What is the purpose of this document?",
|
| 115 |
+
"How can integration be set up in SAP Cloud?",
|
| 116 |
+
"What are the prerequisites mentioned?",
|
| 117 |
+
"What steps are involved in configuration?",
|
| 118 |
+
"How to troubleshoot integration issues?",
|
| 119 |
+
"What is the key functionality covered?"
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
st.markdown("### Ask the Assistant")
|
| 123 |
+
|
| 124 |
+
visible_qs = (
|
| 125 |
+
st.session_state["query_suggestions_fixed"][:3]
|
| 126 |
+
if not st.session_state["show_more"]
|
| 127 |
+
else st.session_state["query_suggestions_fixed"]
|
| 128 |
+
)
|
| 129 |
+
cols = st.columns(3)
|
| 130 |
+
for i, q in enumerate(visible_qs):
|
| 131 |
+
if cols[i % 3].button(f"π¬ {q}", key=f"suggest_{i}"):
|
| 132 |
+
set_user_query(q, i)
|
| 133 |
+
|
| 134 |
+
if st.button("Show more βΌ" if not st.session_state["show_more"] else "Show less β²"):
|
| 135 |
+
st.session_state["show_more"] = not st.session_state["show_more"]
|
| 136 |
+
|
| 137 |
+
# ----------------------------------------------------------
|
| 138 |
+
# π§© Query Input
|
| 139 |
+
# ----------------------------------------------------------
|
| 140 |
+
user_query = st.text_input(
|
| 141 |
+
"Type your question or click one above:",
|
| 142 |
+
value=st.session_state["user_query_input"],
|
| 143 |
+
key="user_query_input"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# ----------------------------------------------------------
|
| 147 |
+
# π€ Generate Answer
|
| 148 |
+
# ----------------------------------------------------------
|
| 149 |
+
if user_query:
|
| 150 |
+
reasoning_mode = mode == "Extended (Document + general)"
|
| 151 |
+
st.markdown(f"**Mode:** {'π§ Extended' if reasoning_mode else 'π Strict Document'}")
|
| 152 |
+
|
| 153 |
+
with st.spinner("π Generating answer..."):
|
| 154 |
+
retrieved = retrieve_chunks(
|
| 155 |
+
user_query, index, chunks, top_k=top_k, embeddings=embeddings
|
| 156 |
+
)
|
| 157 |
+
answer = generate_answer(
|
| 158 |
+
user_query, retrieved, reasoning_mode=reasoning_mode
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
st.markdown("### Assistantβs Answer")
|
| 162 |
+
st.markdown(
|
| 163 |
+
f"""
|
| 164 |
+
<div style='background:#0f172a;border-left:4px solid #22c55e;
|
| 165 |
+
padding:12px;border-radius:8px;color:#f1f5f9'>{answer}</div>
|
| 166 |
+
""",
|
| 167 |
+
unsafe_allow_html=True,
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
with st.expander("π Supporting Context", expanded=False):
|
| 171 |
+
for i, chunk in enumerate(retrieved, 1):
|
| 172 |
+
st.markdown(f"**Chunk {i}:** {chunk.strip()}")
|
| 173 |
+
|
| 174 |
+
# ----------------------------------------------------------
|
| 175 |
+
# π Document Preview
|
| 176 |
+
# ----------------------------------------------------------
|
| 177 |
+
if temp_path:
|
| 178 |
+
st.markdown("---")
|
| 179 |
+
with st.expander("π Document Preview", expanded=False):
|
| 180 |
+
st.text_area("Extracted text (first 1000 chars):", text[:1000], height=180)
|
| 181 |
+
st.caption(f"π¦ {len(chunks)} chunks processed.")
|