Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,9 +35,6 @@ if 'page_loaded' not in st.session_state:
|
|
35 |
# Reset query state when returning to home page
|
36 |
st.session_state.last_query = ""
|
37 |
st.session_state.last_answer = None
|
38 |
-
# Add placeholder initialization tracking
|
39 |
-
if 'placeholders_initialized' not in st.session_state:
|
40 |
-
st.session_state.placeholders_initialized = False
|
41 |
|
42 |
# THEN: Import your modules
|
43 |
from rag_engine import process_query, load_model, cached_load_data_files
|
@@ -54,7 +51,7 @@ def navigate_to_sources():
|
|
54 |
<script>
|
55 |
// Wait for the page to fully load
|
56 |
document.addEventListener('DOMContentLoaded', (event) => {
|
57 |
-
// This
|
58 |
const sourcesLink = Array.from(document.querySelectorAll('a.css-z5fcl4')).find(el => el.innerText === 'Sources');
|
59 |
if (sourcesLink) {
|
60 |
sourcesLink.click();
|
@@ -210,40 +207,13 @@ div.stInfo {
|
|
210 |
.source-link:hover {
|
211 |
color: #6a1b9a;
|
212 |
}
|
213 |
-
/* Prevent layout shifts during loading */
|
214 |
-
.stApp {
|
215 |
-
min-height: 100vh;
|
216 |
-
}
|
217 |
-
/* Add smooth transitions to reduce perceived jerkiness */
|
218 |
-
* {
|
219 |
-
transition: opacity 0.15s ease-in-out;
|
220 |
-
}
|
221 |
-
/* Fix heights for containers that might change during loading */
|
222 |
-
div.stSpinner {
|
223 |
-
min-height: 50px;
|
224 |
-
display: flex;
|
225 |
-
align-items: center;
|
226 |
-
justify-content: center;
|
227 |
-
}
|
228 |
-
/* Ensure question and answer containers have consistent height */
|
229 |
-
div.stInfo, .answer-container {
|
230 |
-
min-height: 30px;
|
231 |
-
}
|
232 |
</style>
|
233 |
<div class="main-title">Spirituality Q&A</div>
|
234 |
""", unsafe_allow_html=True)
|
235 |
|
236 |
-
# Initialize placeholders at the start
|
237 |
-
if not st.session_state.placeholders_initialized:
|
238 |
-
st.session_state.question_placeholder = st.empty()
|
239 |
-
st.session_state.slider_placeholder = st.empty()
|
240 |
-
st.session_state.answer_placeholder = st.empty()
|
241 |
-
st.session_state.sources_placeholder = st.empty()
|
242 |
-
st.session_state.placeholders_initialized = True
|
243 |
-
|
244 |
# Centered button layout without columns
|
245 |
-
_, center_col, _ = st.columns([1, 2, 1])
|
246 |
-
with center_col:
|
247 |
if st.session_state.show_acknowledgment:
|
248 |
st.markdown('<div class="thank-you-button">', unsafe_allow_html=True)
|
249 |
if st.button("Hide Thank You Note", on_click=toggle_acknowledgment, disabled=st.session_state.is_processing, use_container_width=True):
|
@@ -260,9 +230,8 @@ init_message = st.empty()
|
|
260 |
if not st.session_state.initialized:
|
261 |
init_message.info("Hang in there! We are setting the system up for you. 😊")
|
262 |
try:
|
263 |
-
# Setup authentication and preload heavy resources
|
264 |
setup_all_auth()
|
265 |
-
load_model() #
|
266 |
cached_load_data_files() # Preload FAISS index, text chunks, and metadata
|
267 |
st.session_state.initialized = True
|
268 |
st.session_state.init_time = time.time()
|
@@ -295,8 +264,6 @@ if st.session_state.show_acknowledgment:
|
|
295 |
|
296 |
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.
|
297 |
""")
|
298 |
-
|
299 |
-
# Link to Sources using Streamlit's built-in way
|
300 |
st.markdown('<div class="more-info-link">', unsafe_allow_html=True)
|
301 |
st.write("For detailed information about our sources, please visit the *Sources* page in the navigation menu.")
|
302 |
st.markdown('</div>', unsafe_allow_html=True)
|
@@ -304,13 +271,12 @@ if st.session_state.show_acknowledgment:
|
|
304 |
|
305 |
# Function to handle query selection from the common questions buttons
|
306 |
def set_query(query):
|
307 |
-
# If already processing, ignore further input
|
308 |
if st.session_state.is_processing:
|
309 |
return
|
310 |
st.session_state.last_query = query
|
311 |
st.session_state.submit_clicked = True
|
312 |
st.session_state.is_processing = True
|
313 |
-
|
314 |
|
315 |
# Function to group questions into rows based on length
|
316 |
def group_buttons(questions, max_chars_per_row=100):
|
@@ -318,7 +284,6 @@ def group_buttons(questions, max_chars_per_row=100):
|
|
318 |
current_row = []
|
319 |
current_length = 0
|
320 |
for q in questions:
|
321 |
-
# Add some buffer for button padding/margins
|
322 |
q_length = len(q) + 5
|
323 |
if current_length + q_length > max_chars_per_row and current_row:
|
324 |
rows.append(current_row)
|
@@ -331,7 +296,6 @@ def group_buttons(questions, max_chars_per_row=100):
|
|
331 |
rows.append(current_row)
|
332 |
return rows
|
333 |
|
334 |
-
# All common questions in a single list
|
335 |
common_questions = [
|
336 |
"What is the Atman or the soul?",
|
337 |
"Are there rebirths?",
|
@@ -345,10 +309,7 @@ common_questions = [
|
|
345 |
"How can you attain self-realization?"
|
346 |
]
|
347 |
|
348 |
-
# Display heading for common questions
|
349 |
st.markdown("### Few questions to try:")
|
350 |
-
|
351 |
-
# Group questions into rows and create buttons (disabled if processing)
|
352 |
question_rows = group_buttons(common_questions, max_chars_per_row=80)
|
353 |
for row_idx, row in enumerate(question_rows):
|
354 |
cols = st.columns(len(row))
|
@@ -359,74 +320,47 @@ for row_idx, row in enumerate(question_rows):
|
|
359 |
|
360 |
# Function to handle form submission
|
361 |
def handle_form_submit():
|
362 |
-
# If already processing, ignore further input
|
363 |
if st.session_state.is_processing:
|
364 |
return
|
365 |
if st.session_state.query_input and st.session_state.query_input.strip():
|
366 |
st.session_state.last_query = st.session_state.query_input.strip()
|
367 |
st.session_state.submit_clicked = True
|
368 |
st.session_state.is_processing = True
|
369 |
-
# Increment the form key to force a reset
|
370 |
st.session_state.form_key += 1
|
371 |
-
st.rerun()
|
372 |
|
373 |
-
# Create a form with a dynamic key (to allow resetting)
|
374 |
with st.form(key=f"query_form_{st.session_state.form_key}"):
|
375 |
query = st.text_input("Ask your question:", key="query_input",
|
376 |
placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
|
377 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
|
378 |
|
379 |
-
# Display the current question if available using placeholders
|
380 |
if st.session_state.last_query:
|
381 |
-
|
382 |
-
|
383 |
-
st.info(st.session_state.last_query)
|
384 |
-
else:
|
385 |
-
st.session_state.question_placeholder.empty()
|
386 |
|
387 |
-
|
388 |
-
with
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
with col2:
|
393 |
-
word_limit = st.slider("Word limit:", 50, 500, 200)
|
394 |
|
395 |
-
# Process the query only if it has been explicitly submitted
|
396 |
if st.session_state.submit_clicked and st.session_state.last_query:
|
397 |
st.session_state.submit_clicked = False
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
except Exception as e:
|
406 |
-
st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
|
407 |
-
|
408 |
-
# Reset processing flag
|
409 |
-
st.session_state.is_processing = False
|
410 |
-
st.rerun() # Still using rerun, but the layout is more stable now
|
411 |
|
412 |
-
# Display the answer if available using placeholders
|
413 |
if st.session_state.last_answer is not None:
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
st.subheader("Sources:")
|
420 |
-
for citation in st.session_state.last_answer["citations"].split("\n"):
|
421 |
-
st.write(citation)
|
422 |
-
else:
|
423 |
-
st.session_state.answer_placeholder.empty()
|
424 |
-
st.session_state.sources_placeholder.empty()
|
425 |
|
426 |
-
# Add helpful information
|
427 |
st.markdown("---")
|
428 |
-
|
429 |
-
# About section with enhanced explanations
|
430 |
st.markdown("""
|
431 |
### About this app
|
432 |
This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about spirituality based on insights from Indian spiritual texts. It searches through a database of texts to find relevant passages and generates answers based on those passages.
|
@@ -442,7 +376,6 @@ We value your feedback to enhance this application. Please visit the *Contacts*
|
|
442 |
For more information about the source texts used, see *Sources* in the navigation menu.
|
443 |
""")
|
444 |
|
445 |
-
# Citation note at the bottom - improved with support message
|
446 |
st.markdown('<div class="citation-note">', unsafe_allow_html=True)
|
447 |
st.markdown("""
|
448 |
The answers presented in this application are re-presented summaries of relevant passages from the listed citations.
|
|
|
35 |
# Reset query state when returning to home page
|
36 |
st.session_state.last_query = ""
|
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
|
|
|
51 |
<script>
|
52 |
// Wait for the page to fully load
|
53 |
document.addEventListener('DOMContentLoaded', (event) => {
|
54 |
+
// This selects 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();
|
|
|
207 |
.source-link:hover {
|
208 |
color: #6a1b9a;
|
209 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
</style>
|
211 |
<div class="main-title">Spirituality Q&A</div>
|
212 |
""", unsafe_allow_html=True)
|
213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
# Centered button layout without columns
|
215 |
+
_, center_col, _ = st.columns([1, 2, 1])
|
216 |
+
with center_col:
|
217 |
if st.session_state.show_acknowledgment:
|
218 |
st.markdown('<div class="thank-you-button">', unsafe_allow_html=True)
|
219 |
if st.button("Hide Thank You Note", on_click=toggle_acknowledgment, disabled=st.session_state.is_processing, use_container_width=True):
|
|
|
230 |
if not st.session_state.initialized:
|
231 |
init_message.info("Hang in there! We are setting the system up for you. 😊")
|
232 |
try:
|
|
|
233 |
setup_all_auth()
|
234 |
+
load_model() # Uses cached_load_model via alias
|
235 |
cached_load_data_files() # Preload FAISS index, text chunks, and metadata
|
236 |
st.session_state.initialized = True
|
237 |
st.session_state.init_time = time.time()
|
|
|
264 |
|
265 |
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.
|
266 |
""")
|
|
|
|
|
267 |
st.markdown('<div class="more-info-link">', unsafe_allow_html=True)
|
268 |
st.write("For detailed information about our sources, please visit the *Sources* page in the navigation menu.")
|
269 |
st.markdown('</div>', unsafe_allow_html=True)
|
|
|
271 |
|
272 |
# Function to handle query selection from the common questions buttons
|
273 |
def set_query(query):
|
|
|
274 |
if st.session_state.is_processing:
|
275 |
return
|
276 |
st.session_state.last_query = query
|
277 |
st.session_state.submit_clicked = True
|
278 |
st.session_state.is_processing = True
|
279 |
+
# No explicit rerun here; widget interaction already triggers a re-run
|
280 |
|
281 |
# Function to group questions into rows based on length
|
282 |
def group_buttons(questions, max_chars_per_row=100):
|
|
|
284 |
current_row = []
|
285 |
current_length = 0
|
286 |
for q in questions:
|
|
|
287 |
q_length = len(q) + 5
|
288 |
if current_length + q_length > max_chars_per_row and current_row:
|
289 |
rows.append(current_row)
|
|
|
296 |
rows.append(current_row)
|
297 |
return rows
|
298 |
|
|
|
299 |
common_questions = [
|
300 |
"What is the Atman or the soul?",
|
301 |
"Are there rebirths?",
|
|
|
309 |
"How can you attain self-realization?"
|
310 |
]
|
311 |
|
|
|
312 |
st.markdown("### Few questions to try:")
|
|
|
|
|
313 |
question_rows = group_buttons(common_questions, max_chars_per_row=80)
|
314 |
for row_idx, row in enumerate(question_rows):
|
315 |
cols = st.columns(len(row))
|
|
|
320 |
|
321 |
# Function to handle form submission
|
322 |
def handle_form_submit():
|
|
|
323 |
if st.session_state.is_processing:
|
324 |
return
|
325 |
if st.session_state.query_input and st.session_state.query_input.strip():
|
326 |
st.session_state.last_query = st.session_state.query_input.strip()
|
327 |
st.session_state.submit_clicked = True
|
328 |
st.session_state.is_processing = True
|
|
|
329 |
st.session_state.form_key += 1
|
|
|
330 |
|
|
|
331 |
with st.form(key=f"query_form_{st.session_state.form_key}"):
|
332 |
query = st.text_input("Ask your question:", key="query_input",
|
333 |
placeholder="Press enter to submit your question", disabled=st.session_state.is_processing)
|
334 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit, disabled=st.session_state.is_processing)
|
335 |
|
|
|
336 |
if st.session_state.last_query:
|
337 |
+
st.markdown("### Current Question:")
|
338 |
+
st.info(st.session_state.last_query)
|
|
|
|
|
|
|
339 |
|
340 |
+
col1, col2 = st.columns(2)
|
341 |
+
with col1:
|
342 |
+
top_k = st.slider("Number of sources:", 3, 10, 5)
|
343 |
+
with col2:
|
344 |
+
word_limit = st.slider("Word limit:", 50, 500, 200)
|
|
|
|
|
345 |
|
|
|
346 |
if st.session_state.submit_clicked and st.session_state.last_query:
|
347 |
st.session_state.submit_clicked = False
|
348 |
+
with st.spinner("Processing your question..."):
|
349 |
+
try:
|
350 |
+
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
351 |
+
st.session_state.last_answer = result
|
352 |
+
except Exception as e:
|
353 |
+
st.session_state.last_answer = {"answer_with_rag": f"Error processing query: {str(e)}", "citations": ""}
|
354 |
+
st.session_state.is_processing = False # Reset processing flag
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
|
|
|
356 |
if st.session_state.last_answer is not None:
|
357 |
+
st.subheader("Answer:")
|
358 |
+
st.write(st.session_state.last_answer["answer_with_rag"])
|
359 |
+
st.subheader("Sources:")
|
360 |
+
for citation in st.session_state.last_answer["citations"].split("\n"):
|
361 |
+
st.write(citation)
|
|
|
|
|
|
|
|
|
|
|
|
|
362 |
|
|
|
363 |
st.markdown("---")
|
|
|
|
|
364 |
st.markdown("""
|
365 |
### About this app
|
366 |
This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about spirituality based on insights from Indian spiritual texts. It searches through a database of texts to find relevant passages and generates answers based on those passages.
|
|
|
376 |
For more information about the source texts used, see *Sources* in the navigation menu.
|
377 |
""")
|
378 |
|
|
|
379 |
st.markdown('<div class="citation-note">', unsafe_allow_html=True)
|
380 |
st.markdown("""
|
381 |
The answers presented in this application are re-presented summaries of relevant passages from the listed citations.
|