Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,109 +15,84 @@ 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 '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
|
22 |
|
23 |
# THEN: Import your modules
|
24 |
from rag_engine import process_query, load_model
|
25 |
from utils import setup_all_auth
|
26 |
|
27 |
-
# Create a placeholder for
|
28 |
init_message = st.empty()
|
29 |
|
30 |
-
# β
Custom CSS for
|
31 |
st.markdown("""
|
32 |
<style>
|
33 |
-
/* β
Green by default */
|
34 |
-
|
35 |
border: 2px solid #4CAF50 !important;
|
36 |
border-radius: 8px !important;
|
37 |
transition: border 0.3s ease-in-out;
|
38 |
}
|
39 |
|
40 |
/* π΄ Change to red while typing */
|
41 |
-
|
42 |
border: 2px solid #FF5722 !important;
|
43 |
}
|
44 |
|
45 |
-
/* β
|
46 |
-
.answered
|
47 |
border: 2px solid #4CAF50 !important;
|
48 |
}
|
49 |
|
50 |
-
/* β
|
51 |
.stButton>button {
|
52 |
-
background-color: #
|
53 |
color: white !important;
|
54 |
-
border:
|
55 |
border-radius: 8px !important;
|
|
|
|
|
|
|
|
|
56 |
}
|
57 |
</style>
|
58 |
""", unsafe_allow_html=True)
|
59 |
|
60 |
-
# Handle initialization
|
61 |
if not st.session_state.initialized:
|
62 |
init_message.info("Hang in there! We are setting the system up for you. π")
|
63 |
|
64 |
try:
|
65 |
-
# Setup authentication
|
66 |
setup_all_auth()
|
67 |
-
|
68 |
-
# Load the model
|
69 |
load_model()
|
70 |
-
|
71 |
-
# Mark as initialized and record time
|
72 |
st.session_state.initialized = True
|
73 |
-
st.session_state.
|
74 |
-
|
75 |
-
# Show success message
|
76 |
init_message.success("System initialized successfully!")
|
77 |
-
|
78 |
-
|
79 |
-
time.sleep(0.1) # Small delay to ensure message appears
|
80 |
-
st.rerun()
|
81 |
-
|
82 |
except Exception as e:
|
83 |
init_message.error(f"Error initializing: {str(e)}")
|
84 |
|
85 |
-
# Check if we need to hide the success message (after initialization)
|
86 |
-
elif st.session_state.init_time is not None:
|
87 |
-
elapsed_time = time.time() - st.session_state.init_time
|
88 |
-
if elapsed_time >= 2.0:
|
89 |
-
init_message.empty()
|
90 |
-
st.session_state.init_time = None # Reset timer after clearing
|
91 |
-
|
92 |
# Function to handle form submission
|
93 |
def handle_form_submit():
|
94 |
if st.session_state.query_input:
|
95 |
st.session_state.last_query = st.session_state.query_input
|
96 |
st.session_state.submit_clicked = True
|
97 |
-
st.session_state.query_answered = True # β
Ensure
|
98 |
-
st.session_state.query_input = "" # Clear input
|
99 |
-
st.
|
100 |
|
101 |
-
# β
Wrap the text input inside a div
|
102 |
css_class = "answered" if st.session_state.query_answered else ""
|
103 |
st.markdown(f'<div class="{css_class}">', unsafe_allow_html=True)
|
104 |
|
105 |
-
# β
Create a form for
|
106 |
with st.form(key="query_form"):
|
107 |
-
|
108 |
-
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
109 |
-
query = st.text_input(
|
110 |
-
"Ask your question:",
|
111 |
-
key="query_input",
|
112 |
-
value="",
|
113 |
-
placeholder="Type here..."
|
114 |
-
)
|
115 |
-
st.markdown('</div>', unsafe_allow_html=True) # β
Close input-container div
|
116 |
-
|
117 |
-
# Submit button
|
118 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
119 |
|
120 |
-
st.markdown("</div>", unsafe_allow_html=True) # β
Close
|
121 |
|
122 |
# β
Display the last question after submission
|
123 |
if st.session_state.last_query:
|
@@ -147,7 +122,7 @@ if st.session_state.submit_clicked and st.session_state.last_query:
|
|
147 |
st.write(citation)
|
148 |
|
149 |
st.session_state.query_answered = True # β
Mark query as answered
|
150 |
-
st.
|
151 |
|
152 |
except Exception as e:
|
153 |
st.error(f"Error processing query: {str(e)}")
|
|
|
15 |
st.session_state.last_query = ""
|
16 |
if 'submit_clicked' not in st.session_state:
|
17 |
st.session_state.submit_clicked = False
|
|
|
|
|
18 |
if 'query_answered' not in st.session_state:
|
19 |
+
st.session_state.query_answered = False # Tracks if query was answered
|
20 |
|
21 |
# THEN: Import your modules
|
22 |
from rag_engine import process_query, load_model
|
23 |
from utils import setup_all_auth
|
24 |
|
25 |
+
# Create a placeholder for initialization message
|
26 |
init_message = st.empty()
|
27 |
|
28 |
+
# β
Custom CSS for border & button fixes
|
29 |
st.markdown("""
|
30 |
<style>
|
31 |
+
/* β
Green border by default */
|
32 |
+
div[data-baseweb="input"] {
|
33 |
border: 2px solid #4CAF50 !important;
|
34 |
border-radius: 8px !important;
|
35 |
transition: border 0.3s ease-in-out;
|
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: none !important;
|
53 |
border-radius: 8px !important;
|
54 |
+
padding: 8px 16px;
|
55 |
+
font-size: 16px;
|
56 |
+
font-weight: bold;
|
57 |
+
cursor: pointer;
|
58 |
}
|
59 |
</style>
|
60 |
""", unsafe_allow_html=True)
|
61 |
|
62 |
+
# Handle initialization
|
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 |
+
st.session_state.query_answered = False # Reset query state
|
|
|
|
|
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 |
# Function to handle form submission
|
78 |
def handle_form_submit():
|
79 |
if st.session_state.query_input:
|
80 |
st.session_state.last_query = st.session_state.query_input
|
81 |
st.session_state.submit_clicked = True
|
82 |
+
st.session_state.query_answered = True # β
Ensure it resets to green
|
83 |
+
st.session_state.query_input = "" # β
Clear input
|
84 |
+
st.experimental_rerun() # β
Refresh UI properly
|
85 |
|
86 |
+
# β
Wrap the text input inside a div for styling
|
87 |
css_class = "answered" if st.session_state.query_answered else ""
|
88 |
st.markdown(f'<div class="{css_class}">', unsafe_allow_html=True)
|
89 |
|
90 |
+
# β
Create a form for user input
|
91 |
with st.form(key="query_form"):
|
92 |
+
query = st.text_input("Ask your question:", key="query_input", placeholder="Type here...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
|
94 |
|
95 |
+
st.markdown("</div>", unsafe_allow_html=True) # β
Close styling div
|
96 |
|
97 |
# β
Display the last question after submission
|
98 |
if st.session_state.last_query:
|
|
|
122 |
st.write(citation)
|
123 |
|
124 |
st.session_state.query_answered = True # β
Mark query as answered
|
125 |
+
st.experimental_rerun() # β
Proper rerun to refresh UI
|
126 |
|
127 |
except Exception as e:
|
128 |
st.error(f"Error processing query: {str(e)}")
|