Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,26 +7,78 @@ st.set_page_config(page_title="Indian Spiritual RAG")
|
|
7 |
from rag_engine import process_query, load_model
|
8 |
from utils import setup_all_auth
|
9 |
|
10 |
-
# Display title
|
11 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Setup all authentication
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
# Preload the model to avoid session state issues
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
# Query input
|
29 |
-
query = st.text_input(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Sliders for customization
|
32 |
col1, col2 = st.columns(2)
|
@@ -36,11 +88,15 @@ with col2:
|
|
36 |
word_limit = st.slider("Word limit:", 50, 500, 200)
|
37 |
|
38 |
# Process button
|
39 |
-
if st.button("Get Answer"):
|
40 |
-
if
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
try:
|
43 |
-
result = process_query(
|
44 |
|
45 |
st.subheader("Answer:")
|
46 |
st.write(result["answer_with_rag"])
|
@@ -57,6 +113,7 @@ if st.button("Get Answer"):
|
|
57 |
st.markdown("---")
|
58 |
st.markdown("""
|
59 |
### About this app
|
60 |
-
This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about Indian spiritual texts.
|
|
|
61 |
It searches through a database of texts to find relevant passages and generates answers based on those passages.
|
62 |
""")
|
|
|
7 |
from rag_engine import process_query, load_model
|
8 |
from utils import setup_all_auth
|
9 |
|
10 |
+
# Display title with custom styling
|
11 |
+
st.markdown("""
|
12 |
+
<style>
|
13 |
+
.main-title {
|
14 |
+
font-size: 2.5rem;
|
15 |
+
color: #FF5722;
|
16 |
+
text-align: center;
|
17 |
+
margin-bottom: 1rem;
|
18 |
+
}
|
19 |
+
.button-style {
|
20 |
+
border: 2px solid #FF5722;
|
21 |
+
border-radius: 8px;
|
22 |
+
}
|
23 |
+
.input-style {
|
24 |
+
border: 2px solid #4CAF50;
|
25 |
+
border-radius: 8px;
|
26 |
+
}
|
27 |
+
.stButton>button {
|
28 |
+
border: 2px solid #FF5722 !important;
|
29 |
+
border-radius: 8px !important;
|
30 |
+
}
|
31 |
+
.stTextInput>div>div>input {
|
32 |
+
border: 2px solid #4CAF50 !important;
|
33 |
+
border-radius: 8px !important;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
<div class="main-title">Indian Spiritual Texts Q&A</div>
|
37 |
+
""", unsafe_allow_html=True)
|
38 |
+
|
39 |
+
# Initialize session state
|
40 |
+
if 'initialized' not in st.session_state:
|
41 |
+
st.session_state.initialized = False
|
42 |
+
if 'last_query' not in st.session_state:
|
43 |
+
st.session_state.last_query = ""
|
44 |
+
if 'submit_clicked' not in st.session_state:
|
45 |
+
st.session_state.submit_clicked = False
|
46 |
|
47 |
# Setup all authentication
|
48 |
+
if not st.session_state.initialized:
|
49 |
+
try:
|
50 |
+
setup_all_auth()
|
51 |
+
except Exception as e:
|
52 |
+
st.error(f"Authentication error: {str(e)}")
|
53 |
|
54 |
# Preload the model to avoid session state issues
|
55 |
+
if not st.session_state.initialized:
|
56 |
+
try:
|
57 |
+
with st.spinner("Hang in there! We are setting the system up for you. 😊"):
|
58 |
+
# Force model loading at startup to avoid session state issues
|
59 |
+
load_model()
|
60 |
+
st.session_state.initialized = True
|
61 |
+
st.success("System initialized successfully!")
|
62 |
+
except Exception as e:
|
63 |
+
st.error(f"Error initializing: {str(e)}")
|
64 |
+
|
65 |
+
# Function to process when enter is pressed or button is clicked
|
66 |
+
def process_input():
|
67 |
+
st.session_state.last_query = st.session_state.query
|
68 |
+
st.session_state.query = ""
|
69 |
+
st.session_state.submit_clicked = True
|
70 |
|
71 |
+
# Query input with callback for Enter key
|
72 |
+
query = st.text_input(
|
73 |
+
"Ask your question:",
|
74 |
+
key="query",
|
75 |
+
on_change=process_input if st.session_state.query and st.session_state.query.strip() else None
|
76 |
+
)
|
77 |
+
|
78 |
+
# Display the current question if there is one
|
79 |
+
if st.session_state.last_query:
|
80 |
+
st.markdown("### Current Question:")
|
81 |
+
st.info(st.session_state.last_query)
|
82 |
|
83 |
# Sliders for customization
|
84 |
col1, col2 = st.columns(2)
|
|
|
88 |
word_limit = st.slider("Word limit:", 50, 500, 200)
|
89 |
|
90 |
# Process button
|
91 |
+
if st.button("Get Answer") or st.session_state.submit_clicked:
|
92 |
+
if st.session_state.last_query:
|
93 |
+
# Reset the submit flag
|
94 |
+
st.session_state.submit_clicked = False
|
95 |
+
|
96 |
+
# Single processing spinner for the entire operation
|
97 |
+
with st.spinner("Processing your question..."):
|
98 |
try:
|
99 |
+
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
100 |
|
101 |
st.subheader("Answer:")
|
102 |
st.write(result["answer_with_rag"])
|
|
|
113 |
st.markdown("---")
|
114 |
st.markdown("""
|
115 |
### About this app
|
116 |
+
This application uses a Retrieval-Augmented Generation (RAG) system to answer questions about Indian spiritual texts.
|
117 |
+
|
118 |
It searches through a database of texts to find relevant passages and generates answers based on those passages.
|
119 |
""")
|