Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -17,6 +17,8 @@ if 'submit_clicked' not in st.session_state:
|
|
17 |
st.session_state.submit_clicked = False
|
18 |
if 'init_time' not in st.session_state:
|
19 |
st.session_state.init_time = None
|
|
|
|
|
20 |
|
21 |
# THEN: Import your modules
|
22 |
from rag_engine import process_query, load_model
|
@@ -25,37 +27,38 @@ from utils import setup_all_auth
|
|
25 |
# Create a placeholder for our initialization message
|
26 |
init_message = st.empty()
|
27 |
|
28 |
-
# Custom styling (
|
29 |
st.markdown("""
|
30 |
<style>
|
|
|
31 |
.main-title {
|
32 |
font-size: 2.5rem;
|
33 |
color: #FF5722;
|
34 |
text-align: center;
|
35 |
margin-bottom: 1rem;
|
36 |
}
|
|
|
|
|
37 |
.stButton>button {
|
38 |
border: 2px solid #FF5722 !important;
|
39 |
border-radius: 8px !important;
|
40 |
}
|
41 |
-
|
|
|
42 |
div[data-baseweb="input"] {
|
43 |
border: 2px solid #4CAF50 !important;
|
44 |
border-radius: 8px !important;
|
|
|
45 |
}
|
46 |
-
|
|
|
47 |
div[data-baseweb="input"]:focus-within {
|
48 |
border: 2px solid #FF5722 !important;
|
49 |
-
border-radius: 8px !important;
|
50 |
-
}
|
51 |
-
/* Remove default shadow */
|
52 |
-
div[data-baseweb="base-input"] {
|
53 |
-
box-shadow: none !important;
|
54 |
}
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
}
|
60 |
</style>
|
61 |
<div class="main-title">Indian Spiritual Texts Q&A</div>
|
@@ -63,29 +66,29 @@ div[data-baseweb="input"] > div {
|
|
63 |
|
64 |
# Handle initialization and success message timing
|
65 |
if not st.session_state.initialized:
|
66 |
-
# Show loading message
|
67 |
init_message.info("Hang in there! We are setting the system up for you. π")
|
68 |
-
|
69 |
try:
|
70 |
# Setup authentication
|
71 |
setup_all_auth()
|
72 |
-
|
73 |
# Load the model
|
74 |
load_model()
|
75 |
-
|
76 |
# Mark as initialized and record time
|
77 |
st.session_state.initialized = True
|
78 |
st.session_state.init_time = time.time()
|
79 |
-
|
80 |
# Show success message
|
81 |
init_message.success("System initialized successfully!")
|
82 |
-
|
83 |
# Force rerun to start the timer for removing the message
|
84 |
time.sleep(0.1) # Small delay to ensure message appears
|
85 |
st.rerun()
|
86 |
-
|
87 |
except Exception as e:
|
88 |
init_message.error(f"Error initializing: {str(e)}")
|
|
|
89 |
# Check if we need to hide the success message (after initialization)
|
90 |
elif st.session_state.init_time is not None:
|
91 |
elapsed_time = time.time() - st.session_state.init_time
|
@@ -93,7 +96,7 @@ elif st.session_state.init_time is not None:
|
|
93 |
init_message.empty()
|
94 |
st.session_state.init_time = None # Reset timer after clearing
|
95 |
|
96 |
-
# Handle form submission -
|
97 |
if 'form_submitted' not in st.session_state:
|
98 |
st.session_state.form_submitted = False
|
99 |
|
@@ -103,12 +106,16 @@ def handle_form_submit():
|
|
103 |
st.session_state.last_query = st.session_state.query_input
|
104 |
st.session_state.submit_clicked = True
|
105 |
st.session_state.form_submitted = True
|
|
|
106 |
|
107 |
# Create a form for handling the user input
|
108 |
with st.form(key="query_form"):
|
109 |
-
#
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
112 |
# Submit button within the form
|
113 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
114 |
|
@@ -133,16 +140,22 @@ with col2:
|
|
133 |
if st.session_state.submit_clicked and st.session_state.last_query:
|
134 |
# Reset the submit flag
|
135 |
st.session_state.submit_clicked = False
|
136 |
-
|
137 |
# Process the query
|
138 |
with st.spinner("Processing your question..."):
|
139 |
try:
|
140 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
|
|
141 |
st.subheader("Answer:")
|
142 |
st.write(result["answer_with_rag"])
|
|
|
143 |
st.subheader("Sources:")
|
144 |
for citation in result["citations"].split("\n"):
|
145 |
st.write(citation)
|
|
|
|
|
|
|
|
|
146 |
except Exception as e:
|
147 |
st.error(f"Error processing query: {str(e)}")
|
148 |
|
|
|
17 |
st.session_state.submit_clicked = False
|
18 |
if 'init_time' not in st.session_state:
|
19 |
st.session_state.init_time = None
|
20 |
+
if 'query_answered' not in st.session_state:
|
21 |
+
st.session_state.query_answered = False # Tracks if the query has been answered
|
22 |
|
23 |
# THEN: Import your modules
|
24 |
from rag_engine import process_query, load_model
|
|
|
27 |
# Create a placeholder for our initialization message
|
28 |
init_message = st.empty()
|
29 |
|
30 |
+
# Custom styling (CSS)
|
31 |
st.markdown("""
|
32 |
<style>
|
33 |
+
/* Styling for the main title */
|
34 |
.main-title {
|
35 |
font-size: 2.5rem;
|
36 |
color: #FF5722;
|
37 |
text-align: center;
|
38 |
margin-bottom: 1rem;
|
39 |
}
|
40 |
+
|
41 |
+
/* Styling for the submit button */
|
42 |
.stButton>button {
|
43 |
border: 2px solid #FF5722 !important;
|
44 |
border-radius: 8px !important;
|
45 |
}
|
46 |
+
|
47 |
+
/* β
Initially green border */
|
48 |
div[data-baseweb="input"] {
|
49 |
border: 2px solid #4CAF50 !important;
|
50 |
border-radius: 8px !important;
|
51 |
+
transition: border 0.3s ease-in-out;
|
52 |
}
|
53 |
+
|
54 |
+
/* π΄ Change to red while typing */
|
55 |
div[data-baseweb="input"]:focus-within {
|
56 |
border: 2px solid #FF5722 !important;
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
58 |
+
|
59 |
+
/* β
Revert to green after submission */
|
60 |
+
div.answered-query {
|
61 |
+
border: 2px solid #4CAF50 !important;
|
62 |
}
|
63 |
</style>
|
64 |
<div class="main-title">Indian Spiritual Texts Q&A</div>
|
|
|
66 |
|
67 |
# Handle initialization and success message timing
|
68 |
if not st.session_state.initialized:
|
|
|
69 |
init_message.info("Hang in there! We are setting the system up for you. π")
|
70 |
+
|
71 |
try:
|
72 |
# Setup authentication
|
73 |
setup_all_auth()
|
74 |
+
|
75 |
# Load the model
|
76 |
load_model()
|
77 |
+
|
78 |
# Mark as initialized and record time
|
79 |
st.session_state.initialized = True
|
80 |
st.session_state.init_time = time.time()
|
81 |
+
|
82 |
# Show success message
|
83 |
init_message.success("System initialized successfully!")
|
84 |
+
|
85 |
# Force rerun to start the timer for removing the message
|
86 |
time.sleep(0.1) # Small delay to ensure message appears
|
87 |
st.rerun()
|
88 |
+
|
89 |
except Exception as e:
|
90 |
init_message.error(f"Error initializing: {str(e)}")
|
91 |
+
|
92 |
# Check if we need to hide the success message (after initialization)
|
93 |
elif st.session_state.init_time is not None:
|
94 |
elapsed_time = time.time() - st.session_state.init_time
|
|
|
96 |
init_message.empty()
|
97 |
st.session_state.init_time = None # Reset timer after clearing
|
98 |
|
99 |
+
# Handle form submission - ensures input is cleared and focus is removed
|
100 |
if 'form_submitted' not in st.session_state:
|
101 |
st.session_state.form_submitted = False
|
102 |
|
|
|
106 |
st.session_state.last_query = st.session_state.query_input
|
107 |
st.session_state.submit_clicked = True
|
108 |
st.session_state.form_submitted = True
|
109 |
+
st.session_state.query_answered = False # Mark as not yet answered
|
110 |
|
111 |
# Create a form for handling the user input
|
112 |
with st.form(key="query_form"):
|
113 |
+
# Apply CSS class dynamically based on submission status
|
114 |
+
css_class = "answered-query" if st.session_state.query_answered else ""
|
115 |
+
query = st.text_input(
|
116 |
+
"Ask your question:", key="query_input", value="", placeholder="Type here...",
|
117 |
+
)
|
118 |
+
|
119 |
# Submit button within the form
|
120 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
121 |
|
|
|
140 |
if st.session_state.submit_clicked and st.session_state.last_query:
|
141 |
# Reset the submit flag
|
142 |
st.session_state.submit_clicked = False
|
143 |
+
|
144 |
# Process the query
|
145 |
with st.spinner("Processing your question..."):
|
146 |
try:
|
147 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
148 |
+
|
149 |
st.subheader("Answer:")
|
150 |
st.write(result["answer_with_rag"])
|
151 |
+
|
152 |
st.subheader("Sources:")
|
153 |
for citation in result["citations"].split("\n"):
|
154 |
st.write(citation)
|
155 |
+
|
156 |
+
# Mark the query as answered
|
157 |
+
st.session_state.query_answered = True
|
158 |
+
|
159 |
except Exception as e:
|
160 |
st.error(f"Error processing query: {str(e)}")
|
161 |
|