Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,80 @@
|
|
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="Indian Spiritual RAG")
|
6 |
|
7 |
-
# Initialize
|
8 |
if 'initialized' not in st.session_state:
|
9 |
st.session_state.initialized = False
|
10 |
-
if 'model' not in st.session_state:
|
11 |
-
st.session_state.model = None
|
12 |
-
if 'tokenizer' not in st.session_state:
|
13 |
-
st.session_state.tokenizer = None
|
14 |
if 'last_query' not in st.session_state:
|
15 |
st.session_state.last_query = ""
|
16 |
if 'submit_clicked' not in st.session_state:
|
17 |
st.session_state.submit_clicked = False
|
18 |
-
if '
|
19 |
-
st.session_state.
|
20 |
|
21 |
-
#
|
22 |
from rag_engine import process_query, load_model
|
23 |
from utils import setup_all_auth
|
24 |
|
25 |
-
#
|
26 |
-
init_message = st.empty()
|
27 |
-
|
28 |
-
# β
Custom CSS for border & button fixes
|
29 |
st.markdown("""
|
30 |
<style>
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
}
|
37 |
-
|
38 |
-
/* π΄ Change to red while typing */
|
39 |
-
div[data-baseweb="input"]:focus-within {
|
40 |
-
border: 2px solid #FF5722 !important;
|
41 |
}
|
42 |
-
|
43 |
-
/* β
Ensure it turns green again AFTER submission */
|
44 |
-
.answered div[data-baseweb="input"] {
|
45 |
-
border: 2px solid #4CAF50 !important;
|
46 |
-
}
|
47 |
-
|
48 |
-
/* β
Fully green button */
|
49 |
.stButton>button {
|
50 |
background-color: #4CAF50 !important;
|
51 |
color: white !important;
|
52 |
-
border: 2px solid #4CAF50 !important;
|
53 |
border-radius: 8px !important;
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
}
|
59 |
</style>
|
|
|
60 |
""", unsafe_allow_html=True)
|
61 |
|
62 |
-
#
|
63 |
if not st.session_state.initialized:
|
|
|
64 |
init_message.info("Hang in there! We are setting the system up for you. π")
|
65 |
-
|
66 |
try:
|
67 |
setup_all_auth()
|
68 |
load_model()
|
69 |
st.session_state.initialized = True
|
70 |
-
|
71 |
init_message.success("System initialized successfully!")
|
72 |
time.sleep(1)
|
73 |
init_message.empty()
|
74 |
except Exception as e:
|
75 |
init_message.error(f"Error initializing: {str(e)}")
|
76 |
|
77 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
with st.form(key="query_form"):
|
79 |
query = st.text_input(
|
80 |
-
"Ask your question:",
|
81 |
-
key="query_input",
|
82 |
-
|
83 |
)
|
84 |
-
submit_button = st.form_submit_button("Get Answer")
|
85 |
-
|
86 |
-
# β
Handle submission **without st.rerun()**
|
87 |
-
if submit_button:
|
88 |
-
if query.strip():
|
89 |
-
st.session_state.last_query = query.strip()
|
90 |
-
st.session_state.submit_clicked = True
|
91 |
-
st.session_state.query_answered = True # β
Ensure text box resets to green
|
92 |
-
|
93 |
-
# β
Wrap the text input inside a div for styling
|
94 |
-
css_class = "answered" if st.session_state.query_answered else ""
|
95 |
-
st.markdown(f'<div class="{css_class}">', unsafe_allow_html=True)
|
96 |
-
st.markdown("</div>", unsafe_allow_html=True) # β
Close styling div
|
97 |
|
98 |
-
#
|
99 |
if st.session_state.last_query:
|
100 |
st.markdown("### Current Question:")
|
101 |
st.info(st.session_state.last_query)
|
@@ -107,25 +86,21 @@ with col1:
|
|
107 |
with col2:
|
108 |
word_limit = st.slider("Word limit:", 50, 500, 200)
|
109 |
|
110 |
-
#
|
111 |
-
if st.session_state.submit_clicked:
|
112 |
-
st.session_state.submit_clicked = False
|
113 |
-
|
114 |
with st.spinner("Processing your question..."):
|
115 |
try:
|
116 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
117 |
-
|
118 |
st.subheader("Answer:")
|
119 |
st.write(result["answer_with_rag"])
|
120 |
-
|
121 |
st.subheader("Sources:")
|
122 |
for citation in result["citations"].split("\n"):
|
123 |
st.write(citation)
|
124 |
-
|
125 |
except Exception as e:
|
126 |
st.error(f"Error processing query: {str(e)}")
|
127 |
|
128 |
-
#
|
129 |
st.markdown("---")
|
130 |
st.markdown("""
|
131 |
### About this app
|
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
|
|
|
4 |
st.set_page_config(page_title="Indian Spiritual RAG")
|
5 |
|
6 |
+
# Initialize session state variables
|
7 |
if 'initialized' not in st.session_state:
|
8 |
st.session_state.initialized = False
|
|
|
|
|
|
|
|
|
9 |
if 'last_query' not in st.session_state:
|
10 |
st.session_state.last_query = ""
|
11 |
if 'submit_clicked' not in st.session_state:
|
12 |
st.session_state.submit_clicked = False
|
13 |
+
if 'query_input' not in st.session_state:
|
14 |
+
st.session_state.query_input = ""
|
15 |
|
16 |
+
# Import other modules
|
17 |
from rag_engine import process_query, load_model
|
18 |
from utils import setup_all_auth
|
19 |
|
20 |
+
# Custom CSS for styling
|
|
|
|
|
|
|
21 |
st.markdown("""
|
22 |
<style>
|
23 |
+
.main-title {
|
24 |
+
font-size: 2.5rem;
|
25 |
+
color: #FF5722;
|
26 |
+
text-align: center;
|
27 |
+
margin-bottom: 1rem;
|
|
|
|
|
|
|
|
|
|
|
28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
.stButton>button {
|
30 |
background-color: #4CAF50 !important;
|
31 |
color: white !important;
|
|
|
32 |
border-radius: 8px !important;
|
33 |
+
font-weight: bold !important;
|
34 |
+
}
|
35 |
+
div[data-baseweb="input"] {
|
36 |
+
border: 3px solid #4CAF50 !important;
|
37 |
+
border-radius: 8px !important;
|
38 |
+
transition: border-color 0.3s ease-in-out;
|
39 |
+
}
|
40 |
+
div[data-baseweb="input"]:focus-within {
|
41 |
+
border: 3px solid #FF5722 !important;
|
42 |
}
|
43 |
</style>
|
44 |
+
<div class="main-title">Indian Spiritual Texts Q&A</div>
|
45 |
""", unsafe_allow_html=True)
|
46 |
|
47 |
+
# Initialization logic
|
48 |
if not st.session_state.initialized:
|
49 |
+
init_message = st.empty()
|
50 |
init_message.info("Hang in there! We are setting the system up for you. π")
|
|
|
51 |
try:
|
52 |
setup_all_auth()
|
53 |
load_model()
|
54 |
st.session_state.initialized = True
|
55 |
+
time.sleep(0.5)
|
56 |
init_message.success("System initialized successfully!")
|
57 |
time.sleep(1)
|
58 |
init_message.empty()
|
59 |
except Exception as e:
|
60 |
init_message.error(f"Error initializing: {str(e)}")
|
61 |
|
62 |
+
# Function to handle form submission
|
63 |
+
def handle_form_submit():
|
64 |
+
st.session_state.last_query = st.session_state.query_input
|
65 |
+
st.session_state.submit_clicked = True
|
66 |
+
st.session_state.query_input = "" # β
Clears input field on submission
|
67 |
+
|
68 |
+
# Form for user input
|
69 |
with st.form(key="query_form"):
|
70 |
query = st.text_input(
|
71 |
+
"Ask your question:",
|
72 |
+
key="query_input",
|
73 |
+
value=st.session_state.query_input
|
74 |
)
|
75 |
+
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# Display last question
|
78 |
if st.session_state.last_query:
|
79 |
st.markdown("### Current Question:")
|
80 |
st.info(st.session_state.last_query)
|
|
|
86 |
with col2:
|
87 |
word_limit = st.slider("Word limit:", 50, 500, 200)
|
88 |
|
89 |
+
# Process query when submitted
|
90 |
+
if st.session_state.submit_clicked and st.session_state.last_query:
|
91 |
+
st.session_state.submit_clicked = False
|
|
|
92 |
with st.spinner("Processing your question..."):
|
93 |
try:
|
94 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
|
|
95 |
st.subheader("Answer:")
|
96 |
st.write(result["answer_with_rag"])
|
|
|
97 |
st.subheader("Sources:")
|
98 |
for citation in result["citations"].split("\n"):
|
99 |
st.write(citation)
|
|
|
100 |
except Exception as e:
|
101 |
st.error(f"Error processing query: {str(e)}")
|
102 |
|
103 |
+
# Footer
|
104 |
st.markdown("---")
|
105 |
st.markdown("""
|
106 |
### About this app
|