gabrielaltay commited on
Commit
c75f2fc
Β·
1 Parent(s): c45b00b
src/legisqa_local/app.py CHANGED
@@ -31,13 +31,16 @@ def main():
31
  setup_environment()
32
  logger.info("βœ… Environment setup complete")
33
 
34
- # Setup ChromaDB (download if needed)
35
- logger.info("πŸ’Ύ Inspecting ChromaDB...")
36
- inspect_chromadb()
37
- logger.info("βœ… ChromaDB inspection complete")
 
 
38
 
39
  # Initialize vectorstore (load once and cache in session state)
40
- initialize_vectorstore()
 
41
 
42
  # Main content
43
  st.title(":classical_building: LegisQA :classical_building:")
 
31
  setup_environment()
32
  logger.info("βœ… Environment setup complete")
33
 
34
+ # Setup ChromaDB (download if needed) - run once per session
35
+ if "chromadb_inspected" not in st.session_state:
36
+ logger.info("πŸ’Ύ Inspecting ChromaDB...")
37
+ inspect_chromadb()
38
+ logger.info("βœ… ChromaDB inspection complete")
39
+ st.session_state.chromadb_inspected = True
40
 
41
  # Initialize vectorstore (load once and cache in session state)
42
+ if "vectorstore" not in st.session_state:
43
+ initialize_vectorstore()
44
 
45
  # Main content
46
  st.title(":classical_building: LegisQA :classical_building:")
src/legisqa_local/components/sidebar.py CHANGED
@@ -12,17 +12,17 @@ def render_chromadb_status():
12
 
13
  vectorstore = get_vectorstore()
14
  if vectorstore is not None:
15
- try:
16
- # Test the vectorstore to get document count
17
- count = vectorstore._collection.count()
18
  st.success("βœ… ChromaDB Ready")
19
  st.caption(f"πŸ“Š {count:,} documents loaded")
20
 
21
  config = get_chroma_config()
22
  st.caption(f"πŸ“ Collection: {config['collection_name']}")
23
- except Exception as e:
24
  st.warning("⚠️ ChromaDB Loaded (verification failed)")
25
- st.caption(f"Error: {str(e)[:50]}...")
26
  else:
27
  st.info("⏳ ChromaDB Loading...")
28
  st.caption("Vectorstore is being initialized")
 
12
 
13
  vectorstore = get_vectorstore()
14
  if vectorstore is not None:
15
+ # Use cached count from session state to avoid network calls on every rerun
16
+ count = st.session_state.get("vectorstore_count")
17
+ if count is not None:
18
  st.success("βœ… ChromaDB Ready")
19
  st.caption(f"πŸ“Š {count:,} documents loaded")
20
 
21
  config = get_chroma_config()
22
  st.caption(f"πŸ“ Collection: {config['collection_name']}")
23
+ else:
24
  st.warning("⚠️ ChromaDB Loaded (verification failed)")
25
+ st.caption("Could not retrieve document count")
26
  else:
27
  st.info("⏳ ChromaDB Loading...")
28
  st.caption("Vectorstore is being initialized")
src/legisqa_local/core/vectorstore.py CHANGED
@@ -36,13 +36,15 @@ def initialize_vectorstore():
36
  vectorstore = load_vectorstore()
37
  st.session_state.vectorstore = vectorstore
38
 
39
- # Test the vectorstore to verify it's working
40
  collection = vectorstore._collection
41
  count = collection.count()
 
42
  logger.info(f"βœ… Vectorstore initialized and cached - {count} documents available")
43
 
44
  except Exception as e:
45
  logger.error(f"❌ Error initializing vectorstore: {e}")
 
46
  # Don't raise - let the app continue and show loading message to users
47
 
48
 
 
36
  vectorstore = load_vectorstore()
37
  st.session_state.vectorstore = vectorstore
38
 
39
+ # Test the vectorstore to verify it's working and cache the count
40
  collection = vectorstore._collection
41
  count = collection.count()
42
+ st.session_state.vectorstore_count = count # Cache count to avoid repeated network calls
43
  logger.info(f"βœ… Vectorstore initialized and cached - {count} documents available")
44
 
45
  except Exception as e:
46
  logger.error(f"❌ Error initializing vectorstore: {e}")
47
+ st.session_state.vectorstore_count = None # Mark as failed
48
  # Don't raise - let the app continue and show loading message to users
49
 
50