Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
embeddings = OpenAIEmbeddings()
|
| 424 |
-
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
def load_user_data_source(user_id):
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|