Spaces:
Sleeping
Sleeping
import streamlit as st | |
import time | |
# FIRST: Set page config before ANY other Streamlit command | |
st.set_page_config(page_title="Indian Spiritual RAG") | |
# Initialize ALL session state variables right at the beginning | |
if 'initialized' not in st.session_state: | |
st.session_state.initialized = False | |
if 'model' not in st.session_state: | |
st.session_state.model = None | |
if 'tokenizer' not in st.session_state: | |
st.session_state.tokenizer = None | |
if 'last_query' not in st.session_state: | |
st.session_state.last_query = "" | |
if 'submit_clicked' not in st.session_state: | |
st.session_state.submit_clicked = False | |
if 'init_time' not in st.session_state: | |
st.session_state.init_time = None | |
if 'query_answered' not in st.session_state: | |
st.session_state.query_answered = False # Tracks if the query has been answered | |
# THEN: Import your modules | |
from rag_engine import process_query, load_model | |
from utils import setup_all_auth | |
# Create a placeholder for our initialization message | |
init_message = st.empty() | |
# Custom styling (CSS) | |
st.markdown(""" | |
<style> | |
/* Styling for the main title */ | |
.main-title { | |
font-size: 2.5rem; | |
color: #FF5722; | |
text-align: center; | |
margin-bottom: 1rem; | |
} | |
/* Styling for the submit button */ | |
.stButton>button { | |
border: 2px solid #FF5722 !important; | |
border-radius: 8px !important; | |
} | |
/* β Initially green border */ | |
div[data-baseweb="input"] { | |
border: 2px solid #4CAF50 !important; | |
border-radius: 8px !important; | |
transition: border 0.3s ease-in-out; | |
} | |
/* π΄ Change to red while typing */ | |
div[data-baseweb="input"]:focus-within { | |
border: 2px solid #FF5722 !important; | |
} | |
/* β Revert to green after submission */ | |
div.answered-query { | |
border: 2px solid #4CAF50 !important; | |
} | |
</style> | |
<div class="main-title">Indian Spiritual Texts Q&A</div> | |
""", unsafe_allow_html=True) | |
# Handle initialization and success message timing | |
if not st.session_state.initialized: | |
init_message.info("Hang in there! We are setting the system up for you. π") | |
try: | |
# Setup authentication | |
setup_all_auth() | |
# Load the model | |
load_model() | |
# Mark as initialized and record time | |
st.session_state.initialized = True | |
st.session_state.init_time = time.time() | |
# Show success message | |
init_message.success("System initialized successfully!") | |
# Force rerun to start the timer for removing the message | |
time.sleep(0.1) # Small delay to ensure message appears | |
st.rerun() | |
except Exception as e: | |
init_message.error(f"Error initializing: {str(e)}") | |
# Check if we need to hide the success message (after initialization) | |
elif st.session_state.init_time is not None: | |
elapsed_time = time.time() - st.session_state.init_time | |
if elapsed_time >= 2.0: | |
init_message.empty() | |
st.session_state.init_time = None # Reset timer after clearing | |
# Handle form submission - ensures input is cleared and focus is removed | |
if 'form_submitted' not in st.session_state: | |
st.session_state.form_submitted = False | |
# Function to handle form submission | |
def handle_form_submit(): | |
if st.session_state.query_input: | |
st.session_state.last_query = st.session_state.query_input | |
st.session_state.submit_clicked = True | |
st.session_state.form_submitted = True | |
st.session_state.query_answered = False # Mark as not yet answered | |
# Create a form for handling the user input | |
with st.form(key="query_form"): | |
# Apply CSS class dynamically based on submission status | |
css_class = "answered-query" if st.session_state.query_answered else "" | |
query = st.text_input( | |
"Ask your question:", key="query_input", value="", placeholder="Type here...", | |
) | |
# Submit button within the form | |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit) | |
# Force a rerun after form submission to reset the input field and focus | |
if st.session_state.form_submitted: | |
st.session_state.form_submitted = False | |
st.rerun() | |
# Display the current question if there is one | |
if st.session_state.last_query: | |
st.markdown("### Current Question:") | |
st.info(st.session_state.last_query) | |
# Sliders for customization | |
col1, col2 = st.columns(2) | |
with col1: | |
top_k = st.slider("Number of sources:", 3, 10, 5) | |
with col2: | |
word_limit = st.slider("Word limit:", 50, 500, 200) | |
# Only process the query if explicitly submitted | |
if st.session_state.submit_clicked and st.session_state.last_query: | |
# Reset the submit flag | |
st.session_state.submit_clicked = False | |
# Process the query | |
with st.spinner("Processing your question..."): | |
try: | |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit) | |
st.subheader("Answer:") | |
st.write(result["answer_with_rag"]) | |
st.subheader("Sources:") | |
for citation in result["citations"].split("\n"): | |
st.write(citation) | |
# Mark the query as answered | |
st.session_state.query_answered = True | |
except Exception as e: | |
st.error(f"Error processing query: {str(e)}") | |
# Add helpful information | |
st.markdown("---") | |
st.markdown(""" | |
### About this app | |
This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about Indian spiritual texts. | |
It searches through a database of texts to find relevant passages and generates answers based on those passages. | |
""") |