cryogenic22 commited on
Commit
a4f3b74
Β·
verified Β·
1 Parent(s): 3c5dd83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -132
app.py CHANGED
@@ -84,25 +84,7 @@ def initialize_chat_system(collection_id=None) -> bool:
84
  st.error("No documents found.")
85
  return False
86
 
87
- # Generate a unique identifier for this set of documents
88
- document_ids = sorted([doc['id'] for doc in documents])
89
- vector_store_id = f"vs_{'_'.join(map(str, document_ids))}"
90
- vector_store_path = st.session_state.vector_store_path / vector_store_id
91
-
92
- # Check for existing vector store on disk
93
- if vector_store_path.exists():
94
- try:
95
- # Load existing vector store
96
- embeddings = get_embeddings_model()
97
- vector_store = FAISS.load_local(str(vector_store_path), embeddings)
98
- st.session_state.vector_store = vector_store
99
- st.session_state.qa_system = initialize_qa_system(vector_store)
100
- st.session_state.chat_ready = True
101
- return True
102
- except Exception as e:
103
- st.warning(f"Could not load existing vector store: {e}")
104
-
105
- # Initialize new vector store if needed
106
  with st.spinner("Processing documents..."):
107
  embeddings = get_embeddings_model()
108
  text_splitter = RecursiveCharacterTextSplitter(
@@ -118,24 +100,23 @@ def initialize_chat_system(collection_id=None) -> bool:
118
  'content': chunk,
119
  'metadata': {
120
  'source': doc['name'],
121
- 'document_id': doc['id']
 
122
  }
123
  } for chunk in doc_chunks])
124
 
125
- # Create new vector store
126
  vector_store = FAISS.from_texts(
127
  [chunk['content'] for chunk in chunks],
128
  embeddings,
129
  [chunk['metadata'] for chunk in chunks]
130
  )
131
 
132
- # Save vector store to disk
133
- vector_store.save_local(str(vector_store_path))
134
-
135
  st.session_state.vector_store = vector_store
136
  st.session_state.qa_system = initialize_qa_system(vector_store)
137
  st.session_state.chat_ready = True
138
  return True
 
139
  except Exception as e:
140
  st.error(f"Error initializing chat system: {e}")
141
  return False
@@ -157,28 +138,34 @@ def display_header():
157
  st.markdown("##### Synaptyx RFP Analyzer Agent")
158
 
159
  with col3:
160
- cols = st.columns(4)
161
- with cols[0]:
162
- if st.button("🏠 Home", use_container_width=True):
163
- st.session_state.chat_ready = False
164
- st.session_state.messages = []
165
- st.session_state.current_chat_id = None
166
- st.session_state.show_explorer = False
167
- st.rerun()
168
- with cols[1]:
169
- if st.button("πŸ“š Explorer", use_container_width=True):
170
- st.session_state.show_explorer = True
171
- st.session_state.chat_ready = False
172
- st.rerun()
173
- with cols[2]:
174
- if st.session_state.chat_ready and st.button("πŸ”„ New Chat", use_container_width=True):
175
- st.session_state.messages = []
176
- st.session_state.current_chat_id = None
177
- st.rerun()
178
- with cols[3]:
179
- if st.button("πŸ“ Upload", use_container_width=True):
180
- st.session_state.show_collection_dialog = True
181
- st.rerun()
 
 
 
 
 
 
182
  st.divider()
183
 
184
 
@@ -189,10 +176,11 @@ def display_welcome_screen():
189
  with col1:
190
  st.header("Quick Start")
191
 
192
- # Upload new documents
193
  st.markdown("### Upload Documents")
194
  collection_id = None
195
  collections = get_collections(st.session_state.db_conn)
 
196
  if collections:
197
  selected_collection = st.selectbox(
198
  "Select Collection (Optional)",
@@ -200,6 +188,12 @@ def display_welcome_screen():
200
  format_func=lambda x: x[0]
201
  )
202
  collection_id = selected_collection[1] if selected_collection[0] != "None" else None
 
 
 
 
 
 
203
  uploaded_files = st.file_uploader(
204
  "Upload Documents",
205
  type=['pdf'],
@@ -220,7 +214,7 @@ def display_welcome_screen():
220
  for collection in collections:
221
  with st.expander(f"πŸ“ {collection['name']} ({collection['doc_count']} documents)"):
222
  st.write(collection.get('description', ''))
223
- if st.button("Start Chat", key=f"chat_{collection['id']}"):
224
  st.session_state.selected_collection = collection
225
  if initialize_chat_system(collection['id']):
226
  st.rerun()
@@ -233,14 +227,14 @@ def display_welcome_screen():
233
  st.caption(f"Upload date: {doc['upload_date']}")
234
  if doc['collections']:
235
  st.caption(f"Collections: {', '.join(doc['collections'])}")
236
- if st.button("Start Chat", key=f"doc_{doc['id']}"):
237
  if initialize_chat_system():
238
  st.rerun()
239
 
240
 
241
  def display_chat_interface():
242
  """Display the main chat interface with persistent storage."""
243
- st.header("πŸ’¬ Chat Interface")
244
 
245
  # Create new chat if needed
246
  if not st.session_state.current_chat_id:
@@ -257,7 +251,7 @@ def display_chat_interface():
257
  if prompt := st.chat_input("Ask a question about your documents..."):
258
  st.session_state.messages.append(HumanMessage(content=prompt))
259
 
260
- with st.spinner("Thinking..."):
261
  response = st.session_state.qa_system.invoke({
262
  "input": prompt,
263
  "chat_history": st.session_state.messages
@@ -281,86 +275,7 @@ def display_chat_interface():
281
 
282
  st.rerun()
283
 
284
-
285
- def display_document_chunks():
286
- """Display document chunks with search and filtering capabilities."""
287
- st.subheader("Document Chunk Explorer")
288
-
289
- # Get all documents
290
- documents = get_all_documents(st.session_state.db_conn)
291
- if not documents:
292
- st.info("No documents available.")
293
- return
294
-
295
- # Document selection
296
- selected_doc = st.selectbox(
297
- "Select Document",
298
- options=documents,
299
- format_func=lambda x: x['name']
300
- )
301
- if not selected_doc:
302
- return
303
-
304
- # Load vector store for the document
305
- doc_id = selected_doc['id']
306
- vector_store_path = st.session_state.vector_store_path / f"vs_{doc_id}"
307
-
308
- if not vector_store_path.exists():
309
- st.warning("Vector store not found for this document. Process the document first.")
310
- return
311
-
312
- try:
313
- # Load vector store
314
- embeddings = get_embeddings_model()
315
- vector_store = FAISS.load_local(str(vector_store_path), embeddings)
316
-
317
- # Search functionality
318
- search_query = st.text_input("πŸ” Search within chunks")
319
-
320
- # Get chunks
321
- if search_query:
322
- chunks = vector_store.similarity_search(search_query, k=5)
323
- else:
324
- chunks = vector_store.similarity_search("", k=100) # Get all chunks
325
-
326
- # Display chunks with metadata
327
- st.markdown("### Document Chunks")
328
-
329
- # Filtering options
330
- col1, col2 = st.columns(2)
331
- with col1:
332
- chunk_size = st.slider("Preview Size", 100, 1000, 500)
333
- with col2:
334
- sort_by = st.selectbox("Sort By", ["Relevance", "Position"])
335
-
336
- # Display chunks in an organized way
337
- for i, chunk in enumerate(chunks):
338
- with st.expander(f"Chunk {i+1} | Source: {chunk.metadata.get('source', 'Unknown')}"):
339
- # Content preview
340
- st.markdown("**Content:**")
341
- st.text(chunk.page_content[:chunk_size] + "..." if len(chunk.page_content) > chunk_size else chunk.page_content)
342
-
343
- # Metadata
344
- st.markdown("**Metadata:**")
345
- for key, value in chunk.metadata.items():
346
- st.text(f"{key}: {value}")
347
-
348
- # Actions
349
- col1, col2 = st.columns(2)
350
- with col1:
351
- if st.button("Copy", key=f"copy_{i}"):
352
- st.write("Content copied to clipboard!")
353
- with col2:
354
- if st.button("Start Chat", key=f"chat_{i}"):
355
- initialize_chat_system()
356
- st.session_state.messages.append(
357
- HumanMessage(content=f"Tell me about: {chunk.page_content[:100]}...")
358
- )
359
- st.rerun()
360
-
361
- except Exception as e:
362
- st.error(f"Error loading document chunks: {e}")
363
-
364
 
365
  def main():
366
  """Main application function with improved state management."""
@@ -391,7 +306,7 @@ def main():
391
  name = st.text_input("Collection Name")
392
  description = st.text_area("Description")
393
 
394
- if st.form_submit_button("Create"):
395
  if name:
396
  if create_collection(st.session_state.db_conn, name, description):
397
  st.success(f"Collection '{name}' created successfully!")
 
84
  st.error("No documents found.")
85
  return False
86
 
87
+ # Initialize new vector store
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  with st.spinner("Processing documents..."):
89
  embeddings = get_embeddings_model()
90
  text_splitter = RecursiveCharacterTextSplitter(
 
100
  'content': chunk,
101
  'metadata': {
102
  'source': doc['name'],
103
+ 'document_id': doc['id'],
104
+ 'collection_id': collection_id
105
  }
106
  } for chunk in doc_chunks])
107
 
108
+ # Create new vector store with allow_dangerous_deserialization
109
  vector_store = FAISS.from_texts(
110
  [chunk['content'] for chunk in chunks],
111
  embeddings,
112
  [chunk['metadata'] for chunk in chunks]
113
  )
114
 
 
 
 
115
  st.session_state.vector_store = vector_store
116
  st.session_state.qa_system = initialize_qa_system(vector_store)
117
  st.session_state.chat_ready = True
118
  return True
119
+
120
  except Exception as e:
121
  st.error(f"Error initializing chat system: {e}")
122
  return False
 
138
  st.markdown("##### Synaptyx RFP Analyzer Agent")
139
 
140
  with col3:
141
+ # Use full width buttons with proper spacing
142
+ if st.button("🏠 Home", use_container_width=True, key="home_btn"):
143
+ st.session_state.chat_ready = False
144
+ st.session_state.messages = []
145
+ st.session_state.current_chat_id = None
146
+ st.session_state.show_explorer = False
147
+ st.rerun()
148
+
149
+ st.markdown("#") # Add spacing
150
+
151
+ if st.button("πŸ“š Document Explorer", use_container_width=True, key="explorer_btn"):
152
+ st.session_state.show_explorer = True
153
+ st.session_state.chat_ready = False
154
+ st.rerun()
155
+
156
+ st.markdown("#") # Add spacing
157
+
158
+ if st.session_state.chat_ready and st.button("πŸ”„ Start New Chat", use_container_width=True, key="chat_btn"):
159
+ st.session_state.messages = []
160
+ st.session_state.current_chat_id = None
161
+ st.rerun()
162
+
163
+ st.markdown("#") # Add spacing
164
+
165
+ if st.button("πŸ“ Upload Documents", use_container_width=True, key="upload_btn"):
166
+ st.session_state.show_collection_dialog = True
167
+ st.rerun()
168
+
169
  st.divider()
170
 
171
 
 
176
  with col1:
177
  st.header("Quick Start")
178
 
179
+ # Upload new documents with collection linking
180
  st.markdown("### Upload Documents")
181
  collection_id = None
182
  collections = get_collections(st.session_state.db_conn)
183
+
184
  if collections:
185
  selected_collection = st.selectbox(
186
  "Select Collection (Optional)",
 
188
  format_func=lambda x: x[0]
189
  )
190
  collection_id = selected_collection[1] if selected_collection[0] != "None" else None
191
+
192
+ # Add new collection button
193
+ if st.button("Create New Collection", use_container_width=True):
194
+ st.session_state.show_collection_dialog = True
195
+ st.rerun()
196
+
197
  uploaded_files = st.file_uploader(
198
  "Upload Documents",
199
  type=['pdf'],
 
214
  for collection in collections:
215
  with st.expander(f"πŸ“ {collection['name']} ({collection['doc_count']} documents)"):
216
  st.write(collection.get('description', ''))
217
+ if st.button("Start Chat", key=f"chat_{collection['id']}", use_container_width=True):
218
  st.session_state.selected_collection = collection
219
  if initialize_chat_system(collection['id']):
220
  st.rerun()
 
227
  st.caption(f"Upload date: {doc['upload_date']}")
228
  if doc['collections']:
229
  st.caption(f"Collections: {', '.join(doc['collections'])}")
230
+ if st.button("Start Chat", key=f"doc_{doc['id']}", use_container_width=True):
231
  if initialize_chat_system():
232
  st.rerun()
233
 
234
 
235
  def display_chat_interface():
236
  """Display the main chat interface with persistent storage."""
237
+ st.header("πŸ’¬ Ask your documents")
238
 
239
  # Create new chat if needed
240
  if not st.session_state.current_chat_id:
 
251
  if prompt := st.chat_input("Ask a question about your documents..."):
252
  st.session_state.messages.append(HumanMessage(content=prompt))
253
 
254
+ with st.spinner("Analyzing your documents..."):
255
  response = st.session_state.qa_system.invoke({
256
  "input": prompt,
257
  "chat_history": st.session_state.messages
 
275
 
276
  st.rerun()
277
 
278
+ # Rest of the code remains the same...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
  def main():
281
  """Main application function with improved state management."""
 
306
  name = st.text_input("Collection Name")
307
  description = st.text_area("Description")
308
 
309
+ if st.form_submit_button("Create", use_container_width=True):
310
  if name:
311
  if create_collection(st.session_state.db_conn, name, description):
312
  st.success(f"Collection '{name}' created successfully!")