internkk commited on
Commit
57b0b17
Β·
1 Parent(s): 416859a

Add ability to add more documents to existing knowledge base

Browse files
Files changed (1) hide show
  1. app.py +43 -13
app.py CHANGED
@@ -125,13 +125,36 @@ if "chat_history" not in st.session_state:
125
  # ---------- File Upload ----------
126
  st.header("1️⃣ Upload Documents")
127
 
 
 
 
 
128
  uploaded_files = st.file_uploader(
129
  "Drop your files here",
130
  type=["pdf", "docx", "xlsx", "csv", "txt"],
131
  accept_multiple_files=True
132
  )
133
 
134
- if uploaded_files and st.button("πŸ”„ Process Documents", type="primary"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  # Load embedding model (cached after first load)
136
  with st.spinner("Loading embedding model..."):
137
  embedding_model = load_embedding_model()
@@ -198,20 +221,27 @@ if uploaded_files and st.button("πŸ”„ Process Documents", type="primary"):
198
  else:
199
  chunked_documents.append(doc)
200
 
201
- # Build vector store
202
- status.write("πŸ“¦ Building vector store...")
203
  embedding_fn = BGEEmbeddings(embedding_model)
204
- vector_store = Chroma.from_documents(
205
- documents=chunked_documents,
206
- embedding=embedding_fn,
207
- collection_name="rag_docs"
208
- )
209
 
210
- st.session_state.documents = all_documents
211
- st.session_state.vector_store = vector_store
212
-
213
- status.update(label="βœ… Processing complete!", state="complete")
214
- st.success(f"Processed {len(all_documents)} document(s) into {len(chunked_documents)} chunks.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
 
217
  # ---------- Document Preview ----------
 
125
  # ---------- File Upload ----------
126
  st.header("1️⃣ Upload Documents")
127
 
128
+ # Show current document count
129
+ if st.session_state.documents:
130
+ st.info(f"πŸ“š Current knowledge base: {len(st.session_state.documents)} document(s)")
131
+
132
  uploaded_files = st.file_uploader(
133
  "Drop your files here",
134
  type=["pdf", "docx", "xlsx", "csv", "txt"],
135
  accept_multiple_files=True
136
  )
137
 
138
+ # Show two buttons if we already have documents
139
+ col1, col2 = st.columns(2)
140
+
141
+ with col1:
142
+ process_btn = st.button("πŸ”„ Process Documents", type="primary", disabled=not uploaded_files)
143
+
144
+ with col2:
145
+ if st.session_state.documents:
146
+ add_btn = st.button("βž• Add to Existing", disabled=not uploaded_files)
147
+ else:
148
+ add_btn = False
149
+
150
+ # Determine mode: new or add
151
+ process_mode = None
152
+ if process_btn:
153
+ process_mode = "new"
154
+ elif add_btn:
155
+ process_mode = "add"
156
+
157
+ if uploaded_files and process_mode:
158
  # Load embedding model (cached after first load)
159
  with st.spinner("Loading embedding model..."):
160
  embedding_model = load_embedding_model()
 
221
  else:
222
  chunked_documents.append(doc)
223
 
 
 
224
  embedding_fn = BGEEmbeddings(embedding_model)
 
 
 
 
 
225
 
226
+ if process_mode == "add" and st.session_state.vector_store:
227
+ # Add to existing vector store
228
+ status.write("πŸ“¦ Adding to existing knowledge base...")
229
+ st.session_state.vector_store.add_documents(chunked_documents)
230
+ st.session_state.documents.extend(all_documents)
231
+ status.update(label="βœ… Documents added!", state="complete")
232
+ st.success(f"Added {len(all_documents)} document(s) ({len(chunked_documents)} chunks) to existing knowledge base.")
233
+ else:
234
+ # Build new vector store
235
+ status.write("πŸ“¦ Building vector store...")
236
+ vector_store = Chroma.from_documents(
237
+ documents=chunked_documents,
238
+ embedding=embedding_fn,
239
+ collection_name="rag_docs"
240
+ )
241
+ st.session_state.documents = all_documents
242
+ st.session_state.vector_store = vector_store
243
+ status.update(label="βœ… Processing complete!", state="complete")
244
+ st.success(f"Processed {len(all_documents)} document(s) into {len(chunked_documents)} chunks.")
245
 
246
 
247
  # ---------- Document Preview ----------