ankanghosh commited on
Commit
d3f3a6c
Β·
verified Β·
1 Parent(s): 18f7f0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import time
 
3
 
4
  # FIRST: Set page config before ANY other Streamlit command
5
  st.set_page_config(page_title="Spirituality Q&A", page_icon="πŸ•‰οΈ")
@@ -36,13 +37,31 @@ if 'page_loaded' not in st.session_state:
36
  st.session_state.last_answer = None
37
 
38
  # THEN: Import your modules
39
- # from rag_engine import process_query, load_model, cached_load_data_files
40
- # from utils import setup_all_auth
41
 
42
  # Function to toggle acknowledgment visibility
43
  def toggle_acknowledgment():
44
  st.session_state.show_acknowledgment = not st.session_state.show_acknowledgment
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  # Custom styling (pure CSS)
47
  st.markdown("""
48
  <style>
@@ -181,6 +200,7 @@ div.stInfo {
181
  /* Source link styling */
182
  .source-link {
183
  color: #3f51b5;
 
184
  text-decoration: underline;
185
  cursor: pointer;
186
  }
@@ -211,9 +231,9 @@ if not st.session_state.initialized:
211
  init_message.info("Hang in there! We are setting the system up for you. 😊")
212
  try:
213
  # Setup authentication and preload heavy resources
214
- # setup_all_auth()
215
- # load_model() # This uses cached_load_model via alias
216
- # cached_load_data_files() # Preload FAISS index, text chunks, and metadata
217
  st.session_state.initialized = True
218
  st.session_state.init_time = time.time()
219
  init_message.success("System initialized successfully!")
@@ -246,13 +266,10 @@ if st.session_state.show_acknowledgment:
246
  This application is merely a humble vessel for the ocean of wisdom they have shared with the world. We claim no ownership of these teachings - only profound gratitude for the opportunity to help make them more accessible.
247
  """)
248
 
249
- # Create a source link that works with Streamlit navigation
250
- sources_link = """
251
- <div class="more-info-link">
252
- For detailed information about our sources, please visit the <span class="source-link" onclick="parent.window.location.href='Sources'">Sources</span> page in the navigation menu.
253
- </div>
254
- """
255
- st.markdown(sources_link, unsafe_allow_html=True)
256
  st.markdown('</div>', unsafe_allow_html=True)
257
 
258
  # Function to handle query selection from the common questions buttons
@@ -345,7 +362,7 @@ if st.session_state.submit_clicked and st.session_state.last_query:
345
  st.session_state.submit_clicked = False
346
  with st.spinner("Processing your question..."):
347
  try:
348
- # result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
349
  st.session_state.last_answer = result # Store result in session state
350
  except Exception as e:
351
  st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
@@ -364,14 +381,13 @@ if st.session_state.last_answer is not None:
364
  # Add helpful information
365
  st.markdown("---")
366
 
367
- # About section with working sources link
368
- about_text = """
369
  ### About this app
370
  This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about spirituality based on insights from Indian spiritual texts.
371
  It searches through a database of texts to find relevant passages and generates answers based on those passages.
372
- For more information about the texts, see <span class="source-link" onclick="parent.window.location.href='Sources'">sources</span>.
373
- """
374
- st.markdown(about_text, unsafe_allow_html=True)
375
 
376
  # Citation note at the bottom - improved with support message
377
  st.markdown('<div class="citation-note">', unsafe_allow_html=True)
 
1
  import streamlit as st
2
  import time
3
+ import streamlit.components.v1 as components
4
 
5
  # FIRST: Set page config before ANY other Streamlit command
6
  st.set_page_config(page_title="Spirituality Q&A", page_icon="πŸ•‰οΈ")
 
37
  st.session_state.last_answer = None
38
 
39
  # THEN: Import your modules
40
+ from rag_engine import process_query, load_model, cached_load_data_files
41
+ from utils import setup_all_auth
42
 
43
  # Function to toggle acknowledgment visibility
44
  def toggle_acknowledgment():
45
  st.session_state.show_acknowledgment = not st.session_state.show_acknowledgment
46
 
47
+ # Custom HTML/JS to navigate to sources page
48
+ def navigate_to_sources():
49
+ components.html(
50
+ """
51
+ <script>
52
+ // Wait for the page to fully load
53
+ document.addEventListener('DOMContentLoaded', (event) => {
54
+ // This select the nav item for the Sources page in the sidebar
55
+ const sourcesLink = Array.from(document.querySelectorAll('a.css-z5fcl4')).find(el => el.innerText === 'Sources');
56
+ if (sourcesLink) {
57
+ sourcesLink.click();
58
+ }
59
+ });
60
+ </script>
61
+ """,
62
+ height=0,
63
+ )
64
+
65
  # Custom styling (pure CSS)
66
  st.markdown("""
67
  <style>
 
200
  /* Source link styling */
201
  .source-link {
202
  color: #3f51b5;
203
+ font-weight: bold;
204
  text-decoration: underline;
205
  cursor: pointer;
206
  }
 
231
  init_message.info("Hang in there! We are setting the system up for you. 😊")
232
  try:
233
  # Setup authentication and preload heavy resources
234
+ setup_all_auth()
235
+ load_model() # This uses cached_load_model via alias
236
+ cached_load_data_files() # Preload FAISS index, text chunks, and metadata
237
  st.session_state.initialized = True
238
  st.session_state.init_time = time.time()
239
  init_message.success("System initialized successfully!")
 
266
  This application is merely a humble vessel for the ocean of wisdom they have shared with the world. We claim no ownership of these teachings - only profound gratitude for the opportunity to help make them more accessible.
267
  """)
268
 
269
+ # Link to Sources using Streamlit's built-in way
270
+ st.markdown('<div class="more-info-link">', unsafe_allow_html=True)
271
+ st.write("For detailed information about our sources, please visit the *Sources* page in the navigation menu.")
272
+ st.markdown('</div>', unsafe_allow_html=True)
 
 
 
273
  st.markdown('</div>', unsafe_allow_html=True)
274
 
275
  # Function to handle query selection from the common questions buttons
 
362
  st.session_state.submit_clicked = False
363
  with st.spinner("Processing your question..."):
364
  try:
365
+ result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
366
  st.session_state.last_answer = result # Store result in session state
367
  except Exception as e:
368
  st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
 
381
  # Add helpful information
382
  st.markdown("---")
383
 
384
+ # About section with simpler text mention of sources
385
+ st.markdown("""
386
  ### About this app
387
  This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about spirituality based on insights from Indian spiritual texts.
388
  It searches through a database of texts to find relevant passages and generates answers based on those passages.
389
+ For more information about the texts, see *Sources* in the navigation menu.
390
+ """)
 
391
 
392
  # Citation note at the bottom - improved with support message
393
  st.markdown('<div class="citation-note">', unsafe_allow_html=True)