KeenWoo commited on
Commit
b8a3cda
Β·
verified Β·
1 Parent(s): 6094eec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -24
app.py CHANGED
@@ -781,6 +781,38 @@ def check_test_file():
781
  if path.exists(): return f"βœ… Success! Found test file. Contents: '{path.read_text()}'"
782
  return f"❌ Failure. Test file not found at: {path}"
783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
784
  # --- UI Definition ---
785
  CSS = """
786
  .gradio-container { font-size: 14px; }
@@ -983,17 +1015,17 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS) as demo:
983
 
984
  # NEW .click() event handler
985
  run_comprehensive_btn.click(
986
- fn=lambda: run_comprehensive_evaluation(
987
- vs_general=ensure_index("All"),
988
- vs_personal=personal_vectorstore,
989
- nlu_vectorstore=nlu_vectorstore,
990
- vs_playbook=playbook_vectorstore, # <-- Pass the playbook to the evaluation function
991
- config=CONFIG,
992
- storage_path=STORAGE_ROOT # Ensure storage_path is passed as well
993
- ),
994
- outputs=[batch_summary_md, comprehensive_results_df, comprehensive_results_df]
995
- )
996
-
997
  demo.load(auto_setup_on_load, inputs=[active_theme], outputs=[settings_state, files_box, mgmt_status])
998
  demo.load(load_test_fixtures)
999
  test_save_btn.click(fn=test_save_file, inputs=None, outputs=[test_status])
@@ -1001,45 +1033,86 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CSS) as demo:
1001
 
1002
  # --- Startup Logic ---
1003
  # --- Function 3: The Startup Orchestrator ---
1004
- # Replace your pre_load_indexes function with this revised version
 
1005
  def pre_load_indexes():
1006
  """Loads all data sources and indexes at startup."""
1007
  global personal_vectorstore, nlu_vectorstore, playbook_vectorstore
1008
  print("Pre-loading all indexes at startup...")
1009
 
1010
- # --- START OF MODIFICATION ---
1011
- # Get the absolute path to the directory where app.py is located
1012
  script_dir = Path(__file__).parent
1013
- # --- END OF MODIFICATION ---
1014
 
1015
  # Load NLU examples using the absolute path
1016
  print(" - Loading NLU examples index...")
1017
- nlu_path = script_dir / "nlu_training_examples.jsonl" # <-- CHANGE
1018
- nlu_vectorstore = bootstrap_nlu_vectorstore(str(nlu_path), NLU_EXAMPLES_INDEX_PATH) # <-- CHANGE
1019
 
1020
- # Load General Knowledge Themes (existing code is fine)
1021
  for theme in CONFIG["themes"]:
1022
  print(f" - Loading general index for theme: '{theme}'")
1023
  ensure_index(theme)
1024
 
1025
- # Load Personal Knowledge (existing code is fine)
1026
  print(" - Loading personal knowledge index...")
1027
  personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
1028
 
1029
- # Load the Caregiving Playbook using the absolute path
1030
  print(" - Loading Caregiving Playbook index...")
1031
- playbook_path = script_dir / "caregiving_playbook.jsonl" # <-- CHANGE
1032
- playbook_docs = texts_from_jsonl(str(playbook_path)) # <-- CHANGE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1033
 
1034
  if playbook_docs:
1035
  playbook_vectorstore = build_or_load_vectorstore(playbook_docs, PLAYBOOK_INDEX_PATH)
1036
  print(f" ...Successfully loaded {len(playbook_docs)} expert strategies into playbook.")
1037
  else:
1038
- # This warning will now only appear if the file is truly missing or empty
1039
  print(f" ...WARNING: {playbook_path} not found or empty. Caregiving advice will be generic.")
1040
  playbook_vectorstore = build_or_load_vectorstore([], PLAYBOOK_INDEX_PATH)
1041
 
1042
- print("All indexes loaded. Application is ready.")
 
 
1043
 
1044
  if __name__ == "__main__":
1045
  seed_files_into_theme('All')
 
781
  if path.exists(): return f"βœ… Success! Found test file. Contents: '{path.read_text()}'"
782
  return f"❌ Failure. Test file not found at: {path}"
783
 
784
+
785
+ # --- START: NEW HELPER FUNCTION ---
786
+ # Add this function anywhere before the Gradio UI block
787
+ def get_current_vectorstore_and_evaluate(vs_general, nlu_vs, playbook_vs, config, storage_path):
788
+ """
789
+ This helper function ensures we get the LATEST version of the global
790
+ personal_vectorstore right before running the evaluation.
791
+ """
792
+ global personal_vectorstore
793
+
794
+ # This is the same debug block from before, now inside the helper
795
+ print("\n" + "="*20 + " PRE-EVALUATION CHECK (from app.py) " + "="*20)
796
+ print(f"Type of personal_vectorstore: {type(personal_vectorstore)}")
797
+ if personal_vectorstore and hasattr(personal_vectorstore.docstore, '_dict'):
798
+ print(f"Number of documents in personal_vectorstore BEFORE evaluation: {len(personal_vectorstore.docstore._dict)}")
799
+ # You can add the content check here too if desired
800
+ else:
801
+ print("personal_vectorstore is None or has no docstore.")
802
+ print("="*78 + "\n")
803
+
804
+ # Now call the evaluation function with the CURRENT vector store
805
+ return run_comprehensive_evaluation(
806
+ vs_general=vs_general,
807
+ vs_personal=personal_vectorstore,
808
+ nlu_vectorstore=nlu_vs,
809
+ vs_playbook=playbook_vs,
810
+ config=config,
811
+ storage_path=storage_path
812
+ )
813
+ # --- END: NEW HELPER FUNCTION ---
814
+
815
+
816
  # --- UI Definition ---
817
  CSS = """
818
  .gradio-container { font-size: 14px; }
 
1015
 
1016
  # NEW .click() event handler
1017
  run_comprehensive_btn.click(
1018
+ fn=get_current_vectorstore_and_evaluate,
1019
+ inputs=[
1020
+ gr.State(ensure_index("All")),
1021
+ gr.State(nlu_vectorstore),
1022
+ gr.State(playbook_vectorstore),
1023
+ gr.State(CONFIG),
1024
+ gr.State(STORAGE_ROOT)
1025
+ ],
1026
+ outputs=[batch_summary_md, comprehensive_results_df, comprehensive_results_df]
1027
+ )
1028
+
1029
  demo.load(auto_setup_on_load, inputs=[active_theme], outputs=[settings_state, files_box, mgmt_status])
1030
  demo.load(load_test_fixtures)
1031
  test_save_btn.click(fn=test_save_file, inputs=None, outputs=[test_status])
 
1033
 
1034
  # --- Startup Logic ---
1035
  # --- Function 3: The Startup Orchestrator ---
1036
+ # In app.py, replace the entire pre_load_indexes function with this one.
1037
+
1038
  def pre_load_indexes():
1039
  """Loads all data sources and indexes at startup."""
1040
  global personal_vectorstore, nlu_vectorstore, playbook_vectorstore
1041
  print("Pre-loading all indexes at startup...")
1042
 
 
 
1043
  script_dir = Path(__file__).parent
 
1044
 
1045
  # Load NLU examples using the absolute path
1046
  print(" - Loading NLU examples index...")
1047
+ nlu_path = script_dir / "nlu_training_examples.jsonl"
1048
+ nlu_vectorstore = bootstrap_nlu_vectorstore(str(nlu_path), NLU_EXAMPLES_INDEX_PATH)
1049
 
1050
+ # Load General Knowledge Themes
1051
  for theme in CONFIG["themes"]:
1052
  print(f" - Loading general index for theme: '{theme}'")
1053
  ensure_index(theme)
1054
 
1055
+ # Load Personal Knowledge
1056
  print(" - Loading personal knowledge index...")
1057
  personal_vectorstore = build_or_load_vectorstore([], PERSONAL_INDEX_PATH, is_personal=True)
1058
 
1059
+ # Load the Caregiving Playbook
1060
  print(" - Loading Caregiving Playbook index...")
1061
+ playbook_path = script_dir / "caregiving_playbook.jsonl"
1062
+
1063
+ # --- START OF NEW DIAGNOSTIC CODE ---
1064
+ print("\n" + "="*20 + " FILE DIAGNOSTICS " + "="*20)
1065
+ print(f"DEBUG: Script directory is: {script_dir}")
1066
+ print(f"DEBUG: Attempting to load playbook from: {playbook_path}")
1067
+
1068
+ try:
1069
+ # 1. Check if the file exists at the path.
1070
+ if os.path.exists(playbook_path):
1071
+ print("DEBUG: βœ… SUCCESS: File exists at the specified path.")
1072
+
1073
+ # 2. Check if the file has content (is not empty).
1074
+ file_size = os.path.getsize(playbook_path)
1075
+ print(f"DEBUG: File size is: {file_size} bytes.")
1076
+ if file_size == 0:
1077
+ print("DEBUG: ❌ ERROR: The file exists, but it is empty.")
1078
+
1079
+ # 3. Check for read permissions.
1080
+ if os.access(playbook_path, os.R_OK):
1081
+ print("DEBUG: βœ… SUCCESS: Application has permission to read the file.")
1082
+ else:
1083
+ print("DEBUG: ❌ ERROR: Application does NOT have permission to read the file.")
1084
+
1085
+ else:
1086
+ print("DEBUG: ❌ ERROR: File does NOT exist at the specified path.")
1087
+ # Also check the directory contents to help debug.
1088
+ print(f"DEBUG: Listing contents of the script directory ({script_dir}):")
1089
+ try:
1090
+ dir_contents = os.listdir(script_dir)
1091
+ if dir_contents:
1092
+ for name in dir_contents:
1093
+ print(f" - Found: {name}")
1094
+ else:
1095
+ print(" - Directory is empty.")
1096
+ except Exception as e:
1097
+ print(f" - Could not list directory contents. Reason: {e}")
1098
+
1099
+ except Exception as e:
1100
+ print(f"DEBUG: ❌ An unexpected error occurred during diagnostics: {e}")
1101
+ print("="*58 + "\n")
1102
+ # --- END OF NEW DIAGNOSTIC CODE ---
1103
+
1104
+ playbook_docs = texts_from_jsonl(str(playbook_path))
1105
 
1106
  if playbook_docs:
1107
  playbook_vectorstore = build_or_load_vectorstore(playbook_docs, PLAYBOOK_INDEX_PATH)
1108
  print(f" ...Successfully loaded {len(playbook_docs)} expert strategies into playbook.")
1109
  else:
 
1110
  print(f" ...WARNING: {playbook_path} not found or empty. Caregiving advice will be generic.")
1111
  playbook_vectorstore = build_or_load_vectorstore([], PLAYBOOK_INDEX_PATH)
1112
 
1113
+ print("All indexes loaded. Application is ready.")
1114
+
1115
+
1116
 
1117
  if __name__ == "__main__":
1118
  seed_files_into_theme('All')