Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -29,34 +29,125 @@ st.markdown("""
|
|
29 |
<style>
|
30 |
.main-title {
|
31 |
font-size: 2.5rem;
|
32 |
-
color: #
|
33 |
text-align: center;
|
34 |
margin-bottom: 1rem;
|
35 |
}
|
|
|
|
|
36 |
.stButton>button {
|
37 |
-
background-color: #
|
38 |
-
color:
|
39 |
-
border:
|
40 |
-
border-radius:
|
41 |
padding: 8px 16px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
}
|
43 |
.stButton>button:hover {
|
44 |
-
background-color: #
|
|
|
45 |
}
|
46 |
-
|
|
|
47 |
div[data-baseweb="input"] {
|
48 |
-
border:
|
49 |
border-radius: 8px !important;
|
|
|
50 |
}
|
51 |
-
/* While editing - Red */
|
52 |
div[data-baseweb="input"]:focus-within {
|
53 |
-
border:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
border-radius: 8px !important;
|
55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
</style>
|
57 |
<div class="main-title">Spirituality Q&A</div>
|
58 |
""", unsafe_allow_html=True)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# Handle initialization and success message timing
|
61 |
init_message = st.empty()
|
62 |
|
@@ -82,17 +173,47 @@ elif st.session_state.init_time is not None:
|
|
82 |
init_message.empty()
|
83 |
st.session_state.init_time = None
|
84 |
|
85 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
def handle_form_submit():
|
87 |
if st.session_state.query_input and st.session_state.query_input.strip():
|
88 |
st.session_state.last_query = st.session_state.query_input.strip()
|
89 |
st.session_state.submit_clicked = True
|
90 |
-
#
|
91 |
-
st.session_state.
|
|
|
92 |
|
93 |
# Create a form with dynamic key for resetting
|
94 |
with st.form(key=f"query_form_{st.session_state.form_key}"):
|
95 |
-
query = st.text_input("Ask your question:", key="query_input",
|
|
|
96 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
97 |
|
98 |
# Display the current question if there is one
|
@@ -114,6 +235,11 @@ if st.session_state.submit_clicked and st.session_state.last_query:
|
|
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 |
st.subheader("Answer:")
|
118 |
st.write(result["answer_with_rag"])
|
119 |
st.subheader("Sources:")
|
|
|
29 |
<style>
|
30 |
.main-title {
|
31 |
font-size: 2.5rem;
|
32 |
+
color: #c0392b;
|
33 |
text-align: center;
|
34 |
margin-bottom: 1rem;
|
35 |
}
|
36 |
+
|
37 |
+
/* Button styling */
|
38 |
.stButton>button {
|
39 |
+
background-color: #fff0f0 !important;
|
40 |
+
color: #3f51b5 !important;
|
41 |
+
border: 1px solid #e1e4f2 !important;
|
42 |
+
border-radius: 20px !important;
|
43 |
padding: 8px 16px !important;
|
44 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.03) !important;
|
45 |
+
white-space: nowrap !important;
|
46 |
+
overflow: hidden !important;
|
47 |
+
text-overflow: ellipsis !important;
|
48 |
+
}
|
49 |
+
|
50 |
+
/* Form submit button specific styling */
|
51 |
+
button[type="submit"],
|
52 |
+
.stFormSubmit>button,
|
53 |
+
[data-testid="stFormSubmitButton"]>button {
|
54 |
+
background-color: #fff0f0 !important;
|
55 |
+
color: #3f51b5 !important;
|
56 |
}
|
57 |
.stButton>button:hover {
|
58 |
+
background-color: #fafbff !important;
|
59 |
+
border-color: #c5cae9 !important;
|
60 |
}
|
61 |
+
|
62 |
+
/* Input field styling */
|
63 |
div[data-baseweb="input"] {
|
64 |
+
border: 1px solid #fff0f0 !important;
|
65 |
border-radius: 8px !important;
|
66 |
+
background-color: #ffffff !important;
|
67 |
}
|
|
|
68 |
div[data-baseweb="input"]:focus-within {
|
69 |
+
border: 1px solid #3f51b5 !important;
|
70 |
+
}
|
71 |
+
div[data-baseweb="input"]:active {
|
72 |
+
border: 1px solid #fff0f0 !important;
|
73 |
+
}
|
74 |
+
|
75 |
+
/* Style the st.info boxes */
|
76 |
+
div.stInfo {
|
77 |
+
background-color: #f8faff !important;
|
78 |
+
color: #3f51b5 !important;
|
79 |
+
border: 1px solid #e1e4f2 !important;
|
80 |
border-radius: 8px !important;
|
81 |
}
|
82 |
+
|
83 |
+
/* COMBINED SCROLL CONTAINER */
|
84 |
+
.questions-scroll-container {
|
85 |
+
width: 100%;
|
86 |
+
overflow-x: auto;
|
87 |
+
scrollbar-width: none; /* Firefox */
|
88 |
+
-ms-overflow-style: none; /* IE and Edge */
|
89 |
+
}
|
90 |
+
|
91 |
+
/* Hide scrollbar for Chrome, Safari and Opera */
|
92 |
+
.questions-scroll-container::-webkit-scrollbar {
|
93 |
+
display: none;
|
94 |
+
}
|
95 |
+
|
96 |
+
/* Inner content that holds both rows */
|
97 |
+
.questions-content {
|
98 |
+
display: inline-flex;
|
99 |
+
flex-direction: column;
|
100 |
+
min-width: max-content;
|
101 |
+
gap: 10px;
|
102 |
+
padding: 5px 0;
|
103 |
+
}
|
104 |
+
|
105 |
+
/* Individual rows */
|
106 |
+
.questions-row {
|
107 |
+
display: flex;
|
108 |
+
flex-direction: row;
|
109 |
+
gap: 10px;
|
110 |
+
}
|
111 |
+
|
112 |
+
/* Placeholder for buttons */
|
113 |
+
.button-placeholder {
|
114 |
+
min-height: 38px;
|
115 |
+
min-width: 120px;
|
116 |
+
margin: 0 5px;
|
117 |
+
}
|
118 |
</style>
|
119 |
<div class="main-title">Spirituality Q&A</div>
|
120 |
""", unsafe_allow_html=True)
|
121 |
|
122 |
+
# Function to handle query selection
|
123 |
+
def set_query(query):
|
124 |
+
st.session_state.last_query = query
|
125 |
+
st.session_state.submit_clicked = True
|
126 |
+
st.rerun()
|
127 |
+
|
128 |
+
# Function to group questions into rows based on length
|
129 |
+
def group_buttons(questions, max_chars_per_row=100):
|
130 |
+
rows = []
|
131 |
+
current_row = []
|
132 |
+
current_length = 0
|
133 |
+
|
134 |
+
for q in questions:
|
135 |
+
# Add some buffer for button padding/margins
|
136 |
+
q_length = len(q) + 5
|
137 |
+
|
138 |
+
if current_length + q_length > max_chars_per_row and current_row:
|
139 |
+
rows.append(current_row)
|
140 |
+
current_row = [q]
|
141 |
+
current_length = q_length
|
142 |
+
else:
|
143 |
+
current_row.append(q)
|
144 |
+
current_length += q_length
|
145 |
+
|
146 |
+
if current_row:
|
147 |
+
rows.append(current_row)
|
148 |
+
|
149 |
+
return rows
|
150 |
+
|
151 |
# Handle initialization and success message timing
|
152 |
init_message = st.empty()
|
153 |
|
|
|
173 |
init_message.empty()
|
174 |
st.session_state.init_time = None
|
175 |
|
176 |
+
# All common questions in a single list
|
177 |
+
common_questions = [
|
178 |
+
"What is the Atman or the soul?",
|
179 |
+
"Are there rebirths?",
|
180 |
+
"What is Karma?",
|
181 |
+
"What is the ultimate truth?",
|
182 |
+
"What is Swami Vivekananda's opinion about the SELF?",
|
183 |
+
"Explain moksha or salvation. is that for real?",
|
184 |
+
"Destiny or free will?",
|
185 |
+
"What is the ultimate goal of human life?",
|
186 |
+
"Do we really die?",
|
187 |
+
"How can you attain self-realization?"
|
188 |
+
]
|
189 |
+
|
190 |
+
# Display heading for common questions
|
191 |
+
st.markdown("### Common questions to try:")
|
192 |
+
|
193 |
+
# Group questions into rows
|
194 |
+
question_rows = group_buttons(common_questions, max_chars_per_row=80)
|
195 |
+
|
196 |
+
# Create buttons for each row
|
197 |
+
for row_idx, row in enumerate(question_rows):
|
198 |
+
cols = st.columns(len(row))
|
199 |
+
for i, (col, q) in enumerate(zip(cols, row)):
|
200 |
+
with col:
|
201 |
+
if st.button(q, key=f"r{row_idx}_q{i}", use_container_width=True):
|
202 |
+
set_query(q)
|
203 |
+
|
204 |
+
# Function to handle form submission with enter key handling
|
205 |
def handle_form_submit():
|
206 |
if st.session_state.query_input and st.session_state.query_input.strip():
|
207 |
st.session_state.last_query = st.session_state.query_input.strip()
|
208 |
st.session_state.submit_clicked = True
|
209 |
+
# Clear input and focus out to reset border color
|
210 |
+
st.session_state.query_input = ""
|
211 |
+
st.rerun()
|
212 |
|
213 |
# Create a form with dynamic key for resetting
|
214 |
with st.form(key=f"query_form_{st.session_state.form_key}"):
|
215 |
+
query = st.text_input("Ask your question:", key="query_input",
|
216 |
+
placeholder="Press enter to submit your question")
|
217 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
218 |
|
219 |
# Display the current question if there is one
|
|
|
235 |
with st.spinner("Processing your question..."):
|
236 |
try:
|
237 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
238 |
+
# Mock answer for testing
|
239 |
+
result = {
|
240 |
+
"answer_with_rag": "🔹 This is a placeholder answer for testing UI.",
|
241 |
+
"citations": "🔹 Sample Source 1\n🔹 Sample Source 2"
|
242 |
+
}
|
243 |
st.subheader("Answer:")
|
244 |
st.write(result["answer_with_rag"])
|
245 |
st.subheader("Sources:")
|