Wajahat698 commited on
Commit
bf115bd
·
verified ·
1 Parent(s): 5aacebd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -419,22 +419,29 @@ def get_trust_tip_and_suggestion():
419
 
420
 
421
  def load_main_data_source():
 
 
 
422
  try:
423
  data_path = "./data_source/time_to_rethink_trust_book.md"
424
  if not os.path.exists(data_path):
425
- st.error("Data source file not found.")
426
  return []
427
 
428
  with open(data_path, "r") as f:
429
  main_content = f.read()
430
 
431
- # Split main content into chunks
432
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
433
  main_texts = text_splitter.split_text(main_content)
434
- st.write(f"Split into {len(main_texts)} chunks.")
435
 
436
  # Create Document objects
437
  main_documents = [Document(page_content=text) for text in main_texts]
 
 
 
 
 
438
  return main_documents
439
  except Exception as e:
440
  st.error(f"Error loading main data source: {e}")
@@ -442,19 +449,26 @@ def load_main_data_source():
442
 
443
 
444
 
 
445
  def refresh_faiss_index(documents=None):
446
  """
447
  Refresh or initialize the FAISS index with updated documents.
448
  """
449
  try:
 
 
 
 
450
  embeddings = OpenAIEmbeddings()
451
 
452
  # Initialize FAISS if it doesn't exist
453
  if "faiss_db" not in st.session_state:
454
  st.session_state["faiss_db"] = FAISS.from_documents(documents, embeddings)
455
- elif documents:
 
456
  # Add/Update specific documents in the index
457
  st.session_state["faiss_db"].add_documents(documents, embeddings)
 
458
  except Exception as e:
459
  st.error(f"Error refreshing FAISS index: {e}")
460
 
 
419
 
420
 
421
  def load_main_data_source():
422
+ """
423
+ Load the main data source and return a list of Document objects.
424
+ """
425
  try:
426
  data_path = "./data_source/time_to_rethink_trust_book.md"
427
  if not os.path.exists(data_path):
428
+ st.error(f"Data source file not found at: {data_path}")
429
  return []
430
 
431
  with open(data_path, "r") as f:
432
  main_content = f.read()
433
 
434
+ # Split the content into chunks
435
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
436
  main_texts = text_splitter.split_text(main_content)
 
437
 
438
  # Create Document objects
439
  main_documents = [Document(page_content=text) for text in main_texts]
440
+ if not main_documents:
441
+ st.error("No documents created. Data source may be empty.")
442
+ return []
443
+
444
+ st.write(f"Loaded {len(main_documents)} documents from the data source.")
445
  return main_documents
446
  except Exception as e:
447
  st.error(f"Error loading main data source: {e}")
 
449
 
450
 
451
 
452
+
453
  def refresh_faiss_index(documents=None):
454
  """
455
  Refresh or initialize the FAISS index with updated documents.
456
  """
457
  try:
458
+ if documents is None or not isinstance(documents, list):
459
+ st.error("No valid documents provided to refresh the FAISS index.")
460
+ return
461
+
462
  embeddings = OpenAIEmbeddings()
463
 
464
  # Initialize FAISS if it doesn't exist
465
  if "faiss_db" not in st.session_state:
466
  st.session_state["faiss_db"] = FAISS.from_documents(documents, embeddings)
467
+ st.success("FAISS database initialized.")
468
+ else:
469
  # Add/Update specific documents in the index
470
  st.session_state["faiss_db"].add_documents(documents, embeddings)
471
+ st.success("Documents added to FAISS database.")
472
  except Exception as e:
473
  st.error(f"Error refreshing FAISS index: {e}")
474