Wajahat698 commited on
Commit
0cd761c
·
verified ·
1 Parent(s): 69a0d95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -411,29 +411,41 @@ def load_main_data_source():
411
  st.error(f"Error loading main data source: {e}")
412
  return []
413
 
414
- def combine_data_sources():
415
- main_data_source = load_main_data_source()
416
-
417
- return main_data_source
418
 
419
 
420
- def refresh_faiss_index():
421
- combined_sources = combine_data_sources()
422
- if combined_sources:
 
 
 
 
423
  embeddings = OpenAIEmbeddings()
424
- db_faiss = FAISS.from_documents(combined_sources, embeddings)
425
- st.session_state["faiss_db"] = db_faiss
 
 
 
 
 
 
426
 
427
  def load_user_data_source(user_id):
428
- """
429
- Fetches user-uploaded data sources from Firebase.
430
- """
431
- docs = fetch_documents(user_id)
432
- user_documents = []
 
 
 
 
 
433
 
434
-
435
-
436
- return user_documents
437
 
438
 
439
 
 
411
  st.error(f"Error loading main data source: {e}")
412
  return []
413
 
414
+ def combine_data_sources(user_id, include_main=True):
415
+ main_data_source = load_main_data_source() if include_main else []
416
+ user_documents = load_user_data_source(user_id)
417
+ return main_data_source + user_documents
418
 
419
 
420
+ def refresh_faiss_index(documents=None):
421
+ """
422
+ Refresh the FAISS index with updated documents.
423
+ If documents are provided, only update those; otherwise, rebuild the index.
424
+ """
425
+ if documents:
426
+ # Add/Update specific documents in the index
427
  embeddings = OpenAIEmbeddings()
428
+ st.session_state["faiss_db"].add_documents(documents, embeddings)
429
+ else:
430
+ # Rebuild the entire index
431
+ combined_sources = combine_data_sources()
432
+ if combined_sources:
433
+ embeddings = OpenAIEmbeddings()
434
+ st.session_state["faiss_db"] = FAISS.from_documents(combined_sources, embeddings)
435
+
436
 
437
  def load_user_data_source(user_id):
438
+ try:
439
+ docs = db.child("users").child(user_id).child("KnowledgeBase").get().val()
440
+ if not docs:
441
+ return []
442
+ user_documents = [Document(page_content=doc["content"]) for doc in docs.values()]
443
+ return user_documents
444
+ except Exception as e:
445
+ st.error(f"Error loading user data source: {e}")
446
+ return []
447
+
448
 
 
 
 
449
 
450
 
451