Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
-
import uuid
|
4 |
-
from datetime import datetime
|
5 |
|
6 |
# FIRST: Set page config before ANY other Streamlit command
|
7 |
st.set_page_config(page_title="Spirituality Q&A")
|
@@ -21,12 +19,6 @@ if 'init_time' not in st.session_state:
|
|
21 |
st.session_state.init_time = None
|
22 |
if 'form_key' not in st.session_state:
|
23 |
st.session_state.form_key = 0 # This will help us reset the form
|
24 |
-
if 'button_disabled' not in st.session_state:
|
25 |
-
st.session_state.button_disabled = {}
|
26 |
-
if 'last_click_id' not in st.session_state:
|
27 |
-
st.session_state.last_click_id = None
|
28 |
-
if 'answer_displayed' not in st.session_state:
|
29 |
-
st.session_state.answer_displayed = False
|
30 |
|
31 |
# THEN: Import your modules
|
32 |
from rag_engine import process_query, load_model
|
@@ -121,18 +113,10 @@ div.stInfo {
|
|
121 |
""", unsafe_allow_html=True)
|
122 |
|
123 |
# Function to handle query selection
|
124 |
-
def set_query(query
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
st.session_state.button_disabled[k] = True
|
129 |
-
st.session_state.button_disabled[button_id] = True
|
130 |
-
st.session_state.last_click_id = button_id
|
131 |
-
|
132 |
-
st.session_state.last_query = query
|
133 |
-
st.session_state.submit_clicked = True
|
134 |
-
st.session_state.answer_displayed = False
|
135 |
-
st.rerun()
|
136 |
|
137 |
# Function to group questions into rows based on length
|
138 |
def group_buttons(questions, max_chars_per_row=100):
|
@@ -207,31 +191,14 @@ for row_idx, row in enumerate(question_rows):
|
|
207 |
cols = st.columns(len(row))
|
208 |
for i, (col, q) in enumerate(zip(cols, row)):
|
209 |
with col:
|
210 |
-
|
211 |
-
|
212 |
-
if button_id not in st.session_state.button_disabled:
|
213 |
-
st.session_state.button_disabled[button_id] = False
|
214 |
-
|
215 |
-
if st.button(q, key=button_id, use_container_width=True,
|
216 |
-
disabled=st.session_state.button_disabled.get(button_id, False)):
|
217 |
-
set_query(q, button_id)
|
218 |
|
219 |
# Function to handle form submission
|
220 |
def handle_form_submit():
|
221 |
-
|
222 |
-
if (st.session_state.query_input and st.session_state.query_input.strip() and
|
223 |
-
(form_id not in st.session_state.button_disabled or
|
224 |
-
not st.session_state.button_disabled[form_id])):
|
225 |
-
|
226 |
-
# Disable all buttons
|
227 |
-
for k in st.session_state.button_disabled:
|
228 |
-
st.session_state.button_disabled[k] = True
|
229 |
-
st.session_state.button_disabled[form_id] = True
|
230 |
-
st.session_state.last_click_id = form_id
|
231 |
-
|
232 |
st.session_state.last_query = st.session_state.query_input.strip()
|
233 |
st.session_state.submit_clicked = True
|
234 |
-
st.session_state.answer_displayed = False
|
235 |
# Increment the form key to force a reset
|
236 |
st.session_state.form_key += 1
|
237 |
|
@@ -239,13 +206,7 @@ def handle_form_submit():
|
|
239 |
with st.form(key=f"query_form_{st.session_state.form_key}"):
|
240 |
query = st.text_input("Ask your question:", key="query_input",
|
241 |
placeholder="Press enter to submit your question")
|
242 |
-
|
243 |
-
form_id = "form_submit"
|
244 |
-
if form_id not in st.session_state.button_disabled:
|
245 |
-
st.session_state.button_disabled[form_id] = False
|
246 |
-
|
247 |
-
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit,
|
248 |
-
disabled=st.session_state.button_disabled.get(form_id, False))
|
249 |
|
250 |
# Display the current question if there is one
|
251 |
if st.session_state.last_query:
|
@@ -266,21 +227,12 @@ if st.session_state.submit_clicked and st.session_state.last_query:
|
|
266 |
with st.spinner("Processing your question..."):
|
267 |
try:
|
268 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
269 |
-
st.session_state.answer_displayed = True
|
270 |
-
|
271 |
-
# Re-enable all buttons after answer is displayed
|
272 |
-
for k in st.session_state.button_disabled:
|
273 |
-
st.session_state.button_disabled[k] = False
|
274 |
-
|
275 |
st.subheader("Answer:")
|
276 |
st.write(result["answer_with_rag"])
|
277 |
st.subheader("Sources:")
|
278 |
for citation in result["citations"].split("\n"):
|
279 |
st.write(citation)
|
280 |
except Exception as e:
|
281 |
-
# Re-enable all buttons on error too
|
282 |
-
for k in st.session_state.button_disabled:
|
283 |
-
st.session_state.button_disabled[k] = False
|
284 |
st.error(f"Error processing query: {str(e)}")
|
285 |
|
286 |
# Add helpful information
|
|
|
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="Spirituality Q&A")
|
|
|
19 |
st.session_state.init_time = None
|
20 |
if 'form_key' not in st.session_state:
|
21 |
st.session_state.form_key = 0 # This will help us reset the form
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# THEN: Import your modules
|
24 |
from rag_engine import process_query, load_model
|
|
|
113 |
""", unsafe_allow_html=True)
|
114 |
|
115 |
# Function to handle query selection
|
116 |
+
def set_query(query):
|
117 |
+
st.session_state.last_query = query
|
118 |
+
st.session_state.submit_clicked = True
|
119 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
# Function to group questions into rows based on length
|
122 |
def group_buttons(questions, max_chars_per_row=100):
|
|
|
191 |
cols = st.columns(len(row))
|
192 |
for i, (col, q) in enumerate(zip(cols, row)):
|
193 |
with col:
|
194 |
+
if st.button(q, key=f"r{row_idx}_q{i}", use_container_width=True):
|
195 |
+
set_query(q)
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
# Function to handle form submission
|
198 |
def handle_form_submit():
|
199 |
+
if st.session_state.query_input and st.session_state.query_input.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
st.session_state.last_query = st.session_state.query_input.strip()
|
201 |
st.session_state.submit_clicked = True
|
|
|
202 |
# Increment the form key to force a reset
|
203 |
st.session_state.form_key += 1
|
204 |
|
|
|
206 |
with st.form(key=f"query_form_{st.session_state.form_key}"):
|
207 |
query = st.text_input("Ask your question:", key="query_input",
|
208 |
placeholder="Press enter to submit your question")
|
209 |
+
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
|
211 |
# Display the current question if there is one
|
212 |
if st.session_state.last_query:
|
|
|
227 |
with st.spinner("Processing your question..."):
|
228 |
try:
|
229 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
st.subheader("Answer:")
|
231 |
st.write(result["answer_with_rag"])
|
232 |
st.subheader("Sources:")
|
233 |
for citation in result["citations"].split("\n"):
|
234 |
st.write(citation)
|
235 |
except Exception as e:
|
|
|
|
|
|
|
236 |
st.error(f"Error processing query: {str(e)}")
|
237 |
|
238 |
# Add helpful information
|