Dinesh310 commited on
Commit
9a7975c
Β·
verified Β·
1 Parent(s): 39b78ff

Create streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +151 -0
streamlit_app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Streamlit UI for Agentic RAG System - Simplified Version"""
2
+
3
+ import streamlit as st
4
+ from pathlib import Path
5
+ import sys
6
+ import time
7
+
8
+ # Add src to path
9
+ sys.path.append(str(Path(__file__).parent))
10
+
11
+ from src.config.config import Config
12
+ from src.document_ingestion.document_processor import DocumentProcessor
13
+ from src.vectorstore.vectorstore import VectorStore
14
+ from src.graph_builder.graph_builder import GraphBuilder
15
+
16
+ # Page configuration
17
+ st.set_page_config(
18
+ page_title="πŸ€– RAG Search",
19
+ page_icon="πŸ”",
20
+ layout="centered"
21
+ )
22
+
23
+ # Simple CSS
24
+ st.markdown("""
25
+ <style>
26
+ .stButton > button {
27
+ width: 100%;
28
+ background-color: #4CAF50;
29
+ color: white;
30
+ font-weight: bold;
31
+ }
32
+ </style>
33
+ """, unsafe_allow_html=True)
34
+
35
+ def init_session_state():
36
+ """Initialize session state variables"""
37
+ if 'rag_system' not in st.session_state:
38
+ st.session_state.rag_system = None
39
+ if 'initialized' not in st.session_state:
40
+ st.session_state.initialized = False
41
+ if 'history' not in st.session_state:
42
+ st.session_state.history = []
43
+
44
+ @st.cache_resource
45
+ def initialize_rag():
46
+ """Initialize the RAG system (cached)"""
47
+ try:
48
+ # Initialize components
49
+ llm = Config.get_llm()
50
+ doc_processor = DocumentProcessor(
51
+ chunk_size=Config.CHUNK_SIZE,
52
+ chunk_overlap=Config.CHUNK_OVERLAP
53
+ )
54
+ vector_store = VectorStore()
55
+
56
+ # Use default URLs
57
+ urls = Config.DEFAULT_URLS
58
+
59
+ # Process documents
60
+ documents = doc_processor.process_urls(urls)
61
+
62
+ # Create vector store
63
+ vector_store.create_vectorstore(documents)
64
+
65
+ # Build graph
66
+ graph_builder = GraphBuilder(
67
+ retriever=vector_store.get_retriever(),
68
+ llm=llm
69
+ )
70
+ graph_builder.build()
71
+
72
+ return graph_builder, len(documents)
73
+ except Exception as e:
74
+ st.error(f"Failed to initialize: {str(e)}")
75
+ return None, 0
76
+
77
+ def main():
78
+ """Main application"""
79
+ init_session_state()
80
+
81
+ # Title
82
+ st.title("πŸ” RAG Document Search")
83
+ st.markdown("Ask questions about the loaded documents")
84
+
85
+ # Initialize system
86
+ if not st.session_state.initialized:
87
+ with st.spinner("Loading system..."):
88
+ rag_system, num_chunks = initialize_rag()
89
+ if rag_system:
90
+ st.session_state.rag_system = rag_system
91
+ st.session_state.initialized = True
92
+ st.success(f"βœ… System ready! ({num_chunks} document chunks loaded)")
93
+
94
+ st.markdown("---")
95
+
96
+ # Search interface
97
+ with st.form("search_form"):
98
+ question = st.text_input(
99
+ "Enter your question:",
100
+ placeholder="What would you like to know?"
101
+ )
102
+ submit = st.form_submit_button("πŸ” Search")
103
+
104
+ # Process search
105
+ if submit and question:
106
+ if st.session_state.rag_system:
107
+ with st.spinner("Searching..."):
108
+ start_time = time.time()
109
+
110
+ # Get answer
111
+ result = st.session_state.rag_system.run(question)
112
+
113
+ elapsed_time = time.time() - start_time
114
+
115
+ # Add to history
116
+ st.session_state.history.append({
117
+ 'question': question,
118
+ 'answer': result['answer'],
119
+ 'time': elapsed_time
120
+ })
121
+
122
+ # Display answer
123
+ st.markdown("### πŸ’‘ Answer")
124
+ st.success(result['answer'])
125
+
126
+ # Show retrieved docs in expander
127
+ with st.expander("πŸ“„ Source Documents"):
128
+ for i, doc in enumerate(result['retrieved_docs'], 1):
129
+ st.text_area(
130
+ f"Document {i}",
131
+ doc.page_content[:300] + "...",
132
+ height=100,
133
+ disabled=True
134
+ )
135
+
136
+ st.caption(f"⏱️ Response time: {elapsed_time:.2f} seconds")
137
+
138
+ # Show history
139
+ if st.session_state.history:
140
+ st.markdown("---")
141
+ st.markdown("### πŸ“œ Recent Searches")
142
+
143
+ for item in reversed(st.session_state.history[-3:]): # Show last 3
144
+ with st.container():
145
+ st.markdown(f"**Q:** {item['question']}")
146
+ st.markdown(f"**A:** {item['answer'][:200]}...")
147
+ st.caption(f"Time: {item['time']:.2f}s")
148
+ st.markdown("")
149
+
150
+ if __name__ == "__main__":
151
+ main()