waghib commited on
Commit
c50df20
·
verified ·
1 Parent(s): 3196994

Updated app.py to add the cache code

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -80,7 +80,11 @@ def build_vectorstore():
80
  vectorstore = FAISS.from_documents(splits, embeddings)
81
  return vectorstore
82
 
83
- vectorstore = build_vectorstore()
 
 
 
 
84
 
85
  def run_rag_chat(query, vectorstore):
86
  """Run the Retrieval-Augmented Generation (RAG) for clinical questions"""
@@ -152,6 +156,16 @@ def main():
152
  layout="wide",
153
  initial_sidebar_state="expanded"
154
  )
 
 
 
 
 
 
 
 
 
 
155
 
156
  # Custom CSS for modern look
157
  st.markdown("""
@@ -287,19 +301,25 @@ def main():
287
 
288
  # Process query
289
  if submit_button and user_input:
290
- with st.spinner("Analyzing clinical data..."):
291
- # Add a small delay for UX
292
- time.sleep(0.5)
293
-
294
- # Run RAG
295
- response = run_rag_chat(user_input, vectorstore)
296
- response["retriever"] = vectorstore.as_retriever()
297
-
298
- # Add to chat history
299
- st.session_state.chat_history.append((user_input, response))
300
-
301
- # Rerun to update UI
302
- st.experimental_rerun()
 
 
 
 
 
 
303
 
304
  # About page
305
  elif st.session_state.page == 'about':
 
80
  vectorstore = FAISS.from_documents(splits, embeddings)
81
  return vectorstore
82
 
83
+ # Initialize vectorstore in session state to load only once
84
+ @st.cache_resource
85
+ def get_vectorstore():
86
+ """Get or create the vectorstore (cached)"""
87
+ return build_vectorstore()
88
 
89
  def run_rag_chat(query, vectorstore):
90
  """Run the Retrieval-Augmented Generation (RAG) for clinical questions"""
 
156
  layout="wide",
157
  initial_sidebar_state="expanded"
158
  )
159
+
160
+ # Load vectorstore only once using session state
161
+ if 'vectorstore' not in st.session_state:
162
+ with st.spinner("Loading clinical knowledge base... This may take a minute."):
163
+ try:
164
+ st.session_state.vectorstore = get_vectorstore()
165
+ st.success("Clinical knowledge base loaded successfully!")
166
+ except Exception as e:
167
+ st.error(f"Error loading knowledge base: {str(e)}")
168
+ st.session_state.vectorstore = None
169
 
170
  # Custom CSS for modern look
171
  st.markdown("""
 
301
 
302
  # Process query
303
  if submit_button and user_input:
304
+ if st.session_state.vectorstore is None:
305
+ st.error("Knowledge base not loaded. Please refresh the page and try again.")
306
+ else:
307
+ with st.spinner("Analyzing clinical data..."):
308
+ try:
309
+ # Add a small delay for UX
310
+ time.sleep(0.5)
311
+
312
+ # Run RAG
313
+ response = run_rag_chat(user_input, st.session_state.vectorstore)
314
+ response["retriever"] = st.session_state.vectorstore.as_retriever()
315
+
316
+ # Add to chat history
317
+ st.session_state.chat_history.append((user_input, response))
318
+
319
+ # Rerun to update UI
320
+ st.experimental_rerun()
321
+ except Exception as e:
322
+ st.error(f"Error processing query: {str(e)}")
323
 
324
  # About page
325
  elif st.session_state.page == 'about':