Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
import time
|
|
|
|
|
3 |
|
4 |
-
# FIRST: Set page config
|
5 |
-
st.set_page_config(page_title="
|
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:
|
@@ -17,111 +19,87 @@ 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 |
-
#
|
22 |
-
from rag_engine import process_query, load_model
|
23 |
-
from utils import setup_all_auth
|
24 |
-
|
25 |
-
# Create a placeholder for our initialization message
|
26 |
-
init_message = st.empty()
|
27 |
-
|
28 |
-
# Custom styling (pure CSS)
|
29 |
st.markdown("""
|
30 |
<style>
|
31 |
-
/* Main title */
|
32 |
.main-title {
|
33 |
font-size: 2.5rem;
|
34 |
color: #FF5722;
|
35 |
text-align: center;
|
36 |
margin-bottom: 1rem;
|
37 |
}
|
38 |
-
|
39 |
-
/* Input field: Green by default, Red when editing */
|
40 |
-
div[data-baseweb="input"] {
|
41 |
-
border: 2px solid #81C784 !important;
|
42 |
-
border-radius: 8px !important;
|
43 |
-
}
|
44 |
-
div[data-baseweb="input"]:focus-within {
|
45 |
-
border: 2px solid #FF5722 !important;
|
46 |
-
border-radius: 8px !important;
|
47 |
-
}
|
48 |
-
|
49 |
-
/* Custom Button: Green with Hover */
|
50 |
.stButton>button {
|
51 |
-
background-color: #
|
52 |
color: white !important;
|
|
|
53 |
border-radius: 8px !important;
|
54 |
-
|
55 |
-
padding: 0.6rem 1.2rem !important;
|
56 |
-
font-size: 1rem !important;
|
57 |
-
transition: 0.3s ease-in-out;
|
58 |
}
|
59 |
.stButton>button:hover {
|
60 |
background-color: #66BB6A !important;
|
|
|
61 |
}
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
67 |
}
|
68 |
-
|
69 |
</style>
|
70 |
-
<div class="main-title">
|
71 |
""", unsafe_allow_html=True)
|
72 |
|
73 |
-
#
|
74 |
if not st.session_state.initialized:
|
75 |
-
init_message.info("Hang in there!
|
76 |
-
|
77 |
try:
|
78 |
setup_all_auth()
|
79 |
load_model()
|
80 |
-
|
81 |
st.session_state.initialized = True
|
82 |
st.session_state.init_time = time.time()
|
83 |
-
|
84 |
init_message.success("System initialized successfully!")
|
85 |
-
time.sleep(0.1) #
|
86 |
st.rerun()
|
87 |
except Exception as e:
|
88 |
init_message.error(f"Error initializing: {str(e)}")
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
init_message.empty()
|
94 |
-
st.session_state.init_time = None
|
95 |
|
96 |
-
# Function to handle form submission
|
97 |
def handle_form_submit():
|
98 |
if st.session_state.query_input:
|
99 |
st.session_state.last_query = st.session_state.query_input
|
100 |
st.session_state.submit_clicked = True
|
101 |
-
st.session_state.
|
|
|
102 |
st.rerun()
|
103 |
|
104 |
-
#
|
105 |
with st.form(key="query_form"):
|
106 |
-
query = st.text_input("
|
107 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
108 |
|
109 |
-
# Display last
|
110 |
if st.session_state.last_query:
|
111 |
st.markdown("### Current Question:")
|
112 |
st.info(st.session_state.last_query)
|
113 |
|
114 |
-
#
|
115 |
col1, col2 = st.columns(2)
|
116 |
with col1:
|
117 |
top_k = st.slider("Number of sources:", 3, 10, 5)
|
118 |
with col2:
|
119 |
word_limit = st.slider("Word limit:", 50, 500, 200)
|
120 |
|
121 |
-
# Process query
|
122 |
if st.session_state.submit_clicked and st.session_state.last_query:
|
123 |
st.session_state.submit_clicked = False
|
124 |
-
|
125 |
with st.spinner("Processing your question..."):
|
126 |
try:
|
127 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
@@ -133,7 +111,7 @@ if st.session_state.submit_clicked and st.session_state.last_query:
|
|
133 |
except Exception as e:
|
134 |
st.error(f"Error processing query: {str(e)}")
|
135 |
|
136 |
-
# About
|
137 |
st.markdown("---")
|
138 |
st.markdown("""
|
139 |
### About this app
|
|
|
1 |
import streamlit as st
|
2 |
import time
|
3 |
+
from rag_engine import process_query, load_model
|
4 |
+
from utils import setup_all_auth
|
5 |
|
6 |
+
# FIRST: Set page config
|
7 |
+
st.set_page_config(page_title="Spirituality Q&A")
|
8 |
|
9 |
+
# Initialize session state variables
|
10 |
if 'initialized' not in st.session_state:
|
11 |
st.session_state.initialized = False
|
12 |
if 'model' not in st.session_state:
|
|
|
19 |
st.session_state.submit_clicked = False
|
20 |
if 'init_time' not in st.session_state:
|
21 |
st.session_state.init_time = None
|
22 |
+
if 'border_color' not in st.session_state:
|
23 |
+
st.session_state.border_color = "#4CAF50" # Green by default
|
24 |
|
25 |
+
# CSS Styling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
st.markdown("""
|
27 |
<style>
|
|
|
28 |
.main-title {
|
29 |
font-size: 2.5rem;
|
30 |
color: #FF5722;
|
31 |
text-align: center;
|
32 |
margin-bottom: 1rem;
|
33 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
.stButton>button {
|
35 |
+
background-color: #4CAF50 !important;
|
36 |
color: white !important;
|
37 |
+
border: 2px solid #388E3C !important;
|
38 |
border-radius: 8px !important;
|
39 |
+
box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.2);
|
|
|
|
|
|
|
40 |
}
|
41 |
.stButton>button:hover {
|
42 |
background-color: #66BB6A !important;
|
43 |
+
border-color: #388E3C !important;
|
44 |
}
|
45 |
+
div[data-baseweb="input"] {
|
46 |
+
border: 2px solid """ + st.session_state.border_color + """ !important;
|
47 |
+
border-radius: 8px !important;
|
48 |
+
}
|
49 |
+
div[data-baseweb="input"]:focus-within {
|
50 |
+
border: 2px solid #FF5722 !important;
|
51 |
+
border-radius: 8px !important;
|
52 |
}
|
|
|
53 |
</style>
|
54 |
+
<div class="main-title">Spirituality Q&A</div>
|
55 |
""", unsafe_allow_html=True)
|
56 |
|
57 |
+
# Initialization logic
|
58 |
if not st.session_state.initialized:
|
59 |
+
init_message = st.info("Hang in there! Setting up the system for you. 😊")
|
|
|
60 |
try:
|
61 |
setup_all_auth()
|
62 |
load_model()
|
|
|
63 |
st.session_state.initialized = True
|
64 |
st.session_state.init_time = time.time()
|
|
|
65 |
init_message.success("System initialized successfully!")
|
66 |
+
time.sleep(0.1) # Small delay
|
67 |
st.rerun()
|
68 |
except Exception as e:
|
69 |
init_message.error(f"Error initializing: {str(e)}")
|
70 |
|
71 |
+
# Handle form submission
|
72 |
+
if 'form_submitted' not in st.session_state:
|
73 |
+
st.session_state.form_submitted = False
|
|
|
|
|
74 |
|
|
|
75 |
def handle_form_submit():
|
76 |
if st.session_state.query_input:
|
77 |
st.session_state.last_query = st.session_state.query_input
|
78 |
st.session_state.submit_clicked = True
|
79 |
+
st.session_state.form_submitted = True
|
80 |
+
st.session_state.border_color = "#4CAF50" # Reset to green
|
81 |
st.rerun()
|
82 |
|
83 |
+
# Create form for user input
|
84 |
with st.form(key="query_form"):
|
85 |
+
query = st.text_input("", key="query_input", placeholder="Press enter to submit question")
|
86 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
87 |
|
88 |
+
# Display last question
|
89 |
if st.session_state.last_query:
|
90 |
st.markdown("### Current Question:")
|
91 |
st.info(st.session_state.last_query)
|
92 |
|
93 |
+
# Customization sliders
|
94 |
col1, col2 = st.columns(2)
|
95 |
with col1:
|
96 |
top_k = st.slider("Number of sources:", 3, 10, 5)
|
97 |
with col2:
|
98 |
word_limit = st.slider("Word limit:", 50, 500, 200)
|
99 |
|
100 |
+
# Process query
|
101 |
if st.session_state.submit_clicked and st.session_state.last_query:
|
102 |
st.session_state.submit_clicked = False
|
|
|
103 |
with st.spinner("Processing your question..."):
|
104 |
try:
|
105 |
result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
|
|
|
111 |
except Exception as e:
|
112 |
st.error(f"Error processing query: {str(e)}")
|
113 |
|
114 |
+
# About section
|
115 |
st.markdown("---")
|
116 |
st.markdown("""
|
117 |
### About this app
|