Spaces:
Sleeping
Sleeping
File size: 6,032 Bytes
0628cbb 226da61 0628cbb 40b71eb 0628cbb 40b71eb 8dfb503 8bfb7cc 24f8abc 8dfb503 5d2245e 1a4c2ba 5d2245e 24f8abc 8bfb7cc 1d4d07f 8dfb503 d2770d0 1d4d07f d2770d0 8dfb503 5d2245e d2770d0 8dfb503 5d2245e 1d4d07f e36ffb0 8dfb503 d2770d0 8dfb503 5d2245e 8dfb503 e36ffb0 d2770d0 1d4d07f 8bfb7cc 5d2245e 1a4c2ba 8dfb503 8bfb7cc 8dfb503 8bfb7cc 5d2245e 8dfb503 226da61 5d2245e d2770d0 8dfb503 5d2245e 226da61 8bfb7cc 8dfb503 8bfb7cc 226da61 8bfb7cc 17a737c 8bfb7cc 226da61 8bfb7cc 0628cbb 17a737c 8bfb7cc 17a737c 8bfb7cc 17a737c e36ffb0 17a737c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import streamlit as st
import time
# FIRST: Set page config before ANY other Streamlit command
st.set_page_config(page_title="Indian Spiritual RAG")
# THEN: Import your modules
from rag_engine import process_query, load_model
from utils import setup_all_auth
# Initialize session state variables (at the very top)
if 'initialized' not in st.session_state:
st.session_state.initialized = False
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 'show_success' not in st.session_state:
st.session_state.show_success = False
if 'success_time' not in st.session_state:
st.session_state.success_time = None
# Display title with custom styling
st.markdown("""
<style>
.main-title {
font-size: 2.5rem;
color: #FF5722;
text-align: center;
margin-bottom: 1rem;
}
.stButton>button {
border: 2px solid #FF5722 !important;
border-radius: 8px !important;
}
/* Remove any default styling for input fields */
div[data-baseweb="input"] {
border: none !important;
box-shadow: none !important;
}
div[data-baseweb="input"] > div {
background-color: transparent !important;
}
div[data-baseweb="input"] > div > div {
box-shadow: none !important;
}
.stTextInput>div>div>input {
border: 2px solid #4CAF50 !important;
border-radius: 8px !important;
box-shadow: none !important;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
function applyInputStyles() {
const inputs = document.querySelectorAll('.stTextInput input');
inputs.forEach(function(input) {
// Clean up old event listeners if any
const newInput = input.cloneNode(true);
input.parentNode.replaceChild(newInput, input);
// Set initial green border
newInput.style.border = '2px solid #4CAF50';
newInput.style.borderRadius = '8px';
newInput.style.boxShadow = 'none';
// Focus - change to red
newInput.addEventListener('focus', function() {
this.style.border = '2px solid #FF5722';
this.style.borderRadius = '8px';
this.style.boxShadow = 'none';
});
// Blur - back to green
newInput.addEventListener('blur', function() {
this.style.border = '2px solid #4CAF50';
this.style.borderRadius = '8px';
this.style.boxShadow = 'none';
});
});
}
// Run initially
applyInputStyles();
// Watch for changes
const observer = new MutationObserver(function() {
applyInputStyles();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
</script>
<div class="main-title">Indian Spiritual Texts Q&A</div>
""", unsafe_allow_html=True)
# Create placeholder for initialization message
init_message = st.empty()
# Handle initialization only once
if not st.session_state.initialized:
# Show the loading message
init_message.info("Hang in there! We are setting the system up for you. 😊")
try:
# Setup authentication
setup_all_auth()
# Force model loading
load_model()
# Mark as initialized
st.session_state.initialized = True
# Set flags to show success message temporarily
st.session_state.show_success = True
st.session_state.success_time = time.time()
# Show success message
init_message.success("System initialized successfully!")
except Exception as e:
init_message.error(f"Error initializing: {str(e)}")
st.stop() # Stop execution if initialization fails
else:
# Check if we need to clear the success message
current_time = time.time()
if st.session_state.show_success:
if current_time - st.session_state.success_time > 2:
init_message.empty()
st.session_state.show_success = False
# Function to process when enter is pressed or button is clicked
def process_input():
st.session_state.last_query = st.session_state.query_input
st.session_state.query_input = ""
st.session_state.submit_clicked = True
# Query input with callback for Enter key
query = st.text_input(
"Ask your question:",
key="query_input",
on_change=process_input
)
# 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)
# Process button
if st.button("Get Answer") or st.session_state.submit_clicked:
if st.session_state.last_query:
# Reset the submit flag
st.session_state.submit_clicked = False
# Single processing spinner for the entire operation
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)
except Exception as e:
st.error(f"Error processing query: {str(e)}")
else:
st.warning("Please enter a question first.")
# 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.
""") |