Update streamlit_app.py
Browse files- streamlit_app.py +238 -151
streamlit_app.py
CHANGED
|
@@ -1,151 +1,238 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Streamlit UI for Agentic RAG System
|
| 3 |
+
- Default URL ingestion
|
| 4 |
+
- Sidebar PDF upload
|
| 5 |
+
- Incremental indexing
|
| 6 |
+
- Question answering with sources
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import streamlit as st
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import sys
|
| 12 |
+
import time
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
# -------------------------------------------------
|
| 16 |
+
# Path setup
|
| 17 |
+
# -------------------------------------------------
|
| 18 |
+
sys.path.append(str(Path(__file__).parent))
|
| 19 |
+
|
| 20 |
+
# -------------------------------------------------
|
| 21 |
+
# Project imports
|
| 22 |
+
# -------------------------------------------------
|
| 23 |
+
from src.config.config import Config
|
| 24 |
+
from src.document_ingestion.document_processor import DocumentProcessor
|
| 25 |
+
from src.vectorstore.vectorstore import VectorStore
|
| 26 |
+
from src.graph_builder.graph_builder import GraphBuilder
|
| 27 |
+
|
| 28 |
+
# -------------------------------------------------
|
| 29 |
+
# Page configuration
|
| 30 |
+
# -------------------------------------------------
|
| 31 |
+
st.set_page_config(
|
| 32 |
+
page_title="π€ Agentic RAG Search",
|
| 33 |
+
page_icon="π",
|
| 34 |
+
layout="centered"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# -------------------------------------------------
|
| 38 |
+
# Simple CSS
|
| 39 |
+
# -------------------------------------------------
|
| 40 |
+
st.markdown(
|
| 41 |
+
"""
|
| 42 |
+
<style>
|
| 43 |
+
.stButton > button {
|
| 44 |
+
width: 100%;
|
| 45 |
+
background-color: #4CAF50;
|
| 46 |
+
color: white;
|
| 47 |
+
font-weight: bold;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
| 50 |
+
""",
|
| 51 |
+
unsafe_allow_html=True
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# -------------------------------------------------
|
| 55 |
+
# Session state initialization
|
| 56 |
+
# -------------------------------------------------
|
| 57 |
+
def init_session_state():
|
| 58 |
+
if "rag_system" not in st.session_state:
|
| 59 |
+
st.session_state.rag_system = None
|
| 60 |
+
if "initialized" not in st.session_state:
|
| 61 |
+
st.session_state.initialized = False
|
| 62 |
+
if "history" not in st.session_state:
|
| 63 |
+
st.session_state.history = []
|
| 64 |
+
if "processed_files" not in st.session_state:
|
| 65 |
+
st.session_state.processed_files = []
|
| 66 |
+
|
| 67 |
+
# -------------------------------------------------
|
| 68 |
+
# RAG system initialization (cached)
|
| 69 |
+
# -------------------------------------------------
|
| 70 |
+
@st.cache_resource
|
| 71 |
+
def initialize_rag():
|
| 72 |
+
"""
|
| 73 |
+
Initializes RAG using default URLs.
|
| 74 |
+
This runs ONLY once due to caching.
|
| 75 |
+
"""
|
| 76 |
+
try:
|
| 77 |
+
llm = Config.get_llm()
|
| 78 |
+
|
| 79 |
+
doc_processor = DocumentProcessor(
|
| 80 |
+
chunk_size=Config.CHUNK_SIZE,
|
| 81 |
+
chunk_overlap=Config.CHUNK_OVERLAP
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
vector_store = VectorStore()
|
| 85 |
+
|
| 86 |
+
# Load default URLs
|
| 87 |
+
urls = Config.DEFAULT_URLS
|
| 88 |
+
documents = doc_processor.process_urls(urls)
|
| 89 |
+
|
| 90 |
+
# Create vector store
|
| 91 |
+
vector_store.create_vectorstore(documents)
|
| 92 |
+
|
| 93 |
+
# Build agentic graph
|
| 94 |
+
graph_builder = GraphBuilder(
|
| 95 |
+
retriever=vector_store.get_retriever(),
|
| 96 |
+
llm=llm
|
| 97 |
+
)
|
| 98 |
+
graph_builder.build()
|
| 99 |
+
|
| 100 |
+
return graph_builder, vector_store, doc_processor, len(documents)
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
st.error(f"Initialization failed: {str(e)}")
|
| 104 |
+
return None, None, None, 0
|
| 105 |
+
|
| 106 |
+
# -------------------------------------------------
|
| 107 |
+
# Main app
|
| 108 |
+
# -------------------------------------------------
|
| 109 |
+
def main():
|
| 110 |
+
init_session_state()
|
| 111 |
+
|
| 112 |
+
# -------------------------------
|
| 113 |
+
# Title
|
| 114 |
+
# -------------------------------
|
| 115 |
+
st.title("π Agentic RAG Document Search")
|
| 116 |
+
st.markdown("Ask questions over default docs or uploaded PDFs")
|
| 117 |
+
|
| 118 |
+
# -------------------------------
|
| 119 |
+
# Initialize RAG system
|
| 120 |
+
# -------------------------------
|
| 121 |
+
if not st.session_state.initialized:
|
| 122 |
+
with st.spinner("Loading RAG system..."):
|
| 123 |
+
rag_system, vector_store, doc_processor, num_chunks = initialize_rag()
|
| 124 |
+
|
| 125 |
+
if rag_system:
|
| 126 |
+
st.session_state.rag_system = rag_system
|
| 127 |
+
st.session_state.vector_store = vector_store
|
| 128 |
+
st.session_state.doc_processor = doc_processor
|
| 129 |
+
st.session_state.initialized = True
|
| 130 |
+
|
| 131 |
+
st.success(f"β
System ready! ({num_chunks} chunks indexed)")
|
| 132 |
+
|
| 133 |
+
# -------------------------------------------------
|
| 134 |
+
# Sidebar: PDF Upload
|
| 135 |
+
# -------------------------------------------------
|
| 136 |
+
st.sidebar.header("π Upload Project PDFs")
|
| 137 |
+
|
| 138 |
+
uploaded_files = st.sidebar.file_uploader(
|
| 139 |
+
"Upload PDF documents",
|
| 140 |
+
type="pdf",
|
| 141 |
+
accept_multiple_files=True
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if uploaded_files:
|
| 145 |
+
uploaded_names = {f.name for f in uploaded_files}
|
| 146 |
+
|
| 147 |
+
if (
|
| 148 |
+
not st.session_state.processed_files
|
| 149 |
+
or set(st.session_state.processed_files) != uploaded_names
|
| 150 |
+
):
|
| 151 |
+
with st.spinner("Analyzing uploaded PDFs..."):
|
| 152 |
+
temp_dir = "temp"
|
| 153 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 154 |
+
|
| 155 |
+
paths = []
|
| 156 |
+
for f in uploaded_files:
|
| 157 |
+
path = os.path.join(temp_dir, f.name)
|
| 158 |
+
with open(path, "wb") as out:
|
| 159 |
+
out.write(f.getbuffer())
|
| 160 |
+
paths.append(path)
|
| 161 |
+
|
| 162 |
+
# Process PDFs
|
| 163 |
+
documents = st.session_state.doc_processor.process_pdfs(paths)
|
| 164 |
+
|
| 165 |
+
# Add to existing vector store
|
| 166 |
+
st.session_state.vector_store.add_documents(documents)
|
| 167 |
+
|
| 168 |
+
# Update processed file list
|
| 169 |
+
st.session_state.processed_files = list(uploaded_names)
|
| 170 |
+
|
| 171 |
+
st.sidebar.success("π PDFs indexed successfully!")
|
| 172 |
+
|
| 173 |
+
st.markdown("---")
|
| 174 |
+
|
| 175 |
+
# -------------------------------------------------
|
| 176 |
+
# Query input
|
| 177 |
+
# -------------------------------------------------
|
| 178 |
+
with st.form("search_form"):
|
| 179 |
+
question = st.text_input(
|
| 180 |
+
"Enter your question:",
|
| 181 |
+
placeholder="Ask something about the documents..."
|
| 182 |
+
)
|
| 183 |
+
submit = st.form_submit_button("π Search")
|
| 184 |
+
|
| 185 |
+
# -------------------------------------------------
|
| 186 |
+
# Query processing
|
| 187 |
+
# -------------------------------------------------
|
| 188 |
+
if submit and question:
|
| 189 |
+
if st.session_state.rag_system:
|
| 190 |
+
with st.spinner("Searching..."):
|
| 191 |
+
start_time = time.time()
|
| 192 |
+
|
| 193 |
+
result = st.session_state.rag_system.run(question)
|
| 194 |
+
|
| 195 |
+
elapsed_time = time.time() - start_time
|
| 196 |
+
|
| 197 |
+
# Save history
|
| 198 |
+
st.session_state.history.append(
|
| 199 |
+
{
|
| 200 |
+
"question": question,
|
| 201 |
+
"answer": result["answer"],
|
| 202 |
+
"time": elapsed_time,
|
| 203 |
+
}
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Display answer
|
| 207 |
+
st.markdown("### π‘ Answer")
|
| 208 |
+
st.success(result["answer"])
|
| 209 |
+
|
| 210 |
+
# Show retrieved documents
|
| 211 |
+
with st.expander("π Source Documents"):
|
| 212 |
+
for i, doc in enumerate(result["retrieved_docs"], 1):
|
| 213 |
+
st.text_area(
|
| 214 |
+
f"Document {i}",
|
| 215 |
+
doc.page_content[:300] + "...",
|
| 216 |
+
height=100,
|
| 217 |
+
disabled=True,
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
st.caption(f"β±οΈ Response time: {elapsed_time:.2f} seconds")
|
| 221 |
+
|
| 222 |
+
# -------------------------------------------------
|
| 223 |
+
# Search history
|
| 224 |
+
# -------------------------------------------------
|
| 225 |
+
if st.session_state.history:
|
| 226 |
+
st.markdown("---")
|
| 227 |
+
st.markdown("### π Recent Searches")
|
| 228 |
+
|
| 229 |
+
for item in reversed(st.session_state.history[-3:]):
|
| 230 |
+
st.markdown(f"**Q:** {item['question']}")
|
| 231 |
+
st.markdown(f"**A:** {item['answer'][:200]}...")
|
| 232 |
+
st.caption(f"Time: {item['time']:.2f}s")
|
| 233 |
+
|
| 234 |
+
# -------------------------------------------------
|
| 235 |
+
# Entry point
|
| 236 |
+
# -------------------------------------------------
|
| 237 |
+
if __name__ == "__main__":
|
| 238 |
+
main()
|