cryogenic22 commited on
Commit
5883a13
·
verified ·
1 Parent(s): 61e7e62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py CHANGED
@@ -142,6 +142,36 @@ def display_top_bar():
142
  if st.session_state.current_collection:
143
  st.info(f"📁 Active Collection: {st.session_state.current_collection['name']}")
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  def display_collection_sidebar():
147
  """Display the document management sidebar."""
 
142
  if st.session_state.current_collection:
143
  st.info(f"📁 Active Collection: {st.session_state.current_collection['name']}")
144
 
145
+ def initialize_chat_from_existing():
146
+ """Initialize chat system from existing documents in database."""
147
+ if not st.session_state.get('chat_ready'):
148
+ try:
149
+ documents = get_all_documents(st.session_state.db_conn)
150
+ if documents:
151
+ # Initialize vector store and QA system with existing documents
152
+ embeddings = get_embeddings_model()
153
+ chunks = []
154
+ for doc in documents:
155
+ doc_chunks = text_splitter.split_text(doc['content'])
156
+ for chunk in doc_chunks:
157
+ chunks.append({
158
+ 'content': chunk,
159
+ 'metadata': {'source': doc['name'], 'document_id': doc['id']}
160
+ })
161
+
162
+ vector_store = FAISS.from_texts(
163
+ [chunk['content'] for chunk in chunks],
164
+ embeddings,
165
+ [chunk['metadata'] for chunk in chunks]
166
+ )
167
+
168
+ st.session_state.vector_store = vector_store
169
+ st.session_state.qa_system = initialize_qa_system(vector_store)
170
+ st.session_state.chat_ready = True
171
+ return True
172
+ except Exception as e:
173
+ st.error(f"Error initializing chat: {e}")
174
+ return False
175
 
176
  def display_collection_sidebar():
177
  """Display the document management sidebar."""