ankanghosh commited on
Commit
b971de8
·
verified ·
1 Parent(s): 5362520

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -1,12 +1,10 @@
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,10 +17,12 @@ if 'submit_clicked' 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 {
@@ -34,18 +34,19 @@ st.markdown("""
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;
@@ -54,52 +55,57 @@ div[data-baseweb="input"]:focus-within {
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,7 +117,7 @@ if st.session_state.submit_clicked and st.session_state.last_query:
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
 
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")
6
 
7
+ # Initialize ALL session state variables right at the beginning
8
  if 'initialized' not in st.session_state:
9
  st.session_state.initialized = False
10
  if 'model' 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
23
+ from utils import setup_all_auth
24
+
25
+ # Custom styling (pure CSS)
26
  st.markdown("""
27
  <style>
28
  .main-title {
 
34
  .stButton>button {
35
  background-color: #4CAF50 !important;
36
  color: white !important;
37
+ border: none !important;
38
  border-radius: 8px !important;
39
+ padding: 8px 16px !important;
40
  }
41
  .stButton>button:hover {
42
+ background-color: #45A049 !important;
 
43
  }
44
+ /* Default input box - Green */
45
  div[data-baseweb="input"] {
46
+ border: 2px solid #4CAF50 !important;
47
  border-radius: 8px !important;
48
  }
49
+ /* While editing - Red */
50
  div[data-baseweb="input"]:focus-within {
51
  border: 2px solid #FF5722 !important;
52
  border-radius: 8px !important;
 
55
  <div class="main-title">Spirituality Q&A</div>
56
  """, unsafe_allow_html=True)
57
 
58
+ # Handle initialization and success message timing
59
+ init_message = st.empty()
60
+
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_all_auth()
66
  load_model()
67
  st.session_state.initialized = True
68
  st.session_state.init_time = time.time()
69
  init_message.success("System initialized successfully!")
70
+ time.sleep(0.1)
71
+ st.experimental_rerun()
72
  except Exception as e:
73
  init_message.error(f"Error initializing: {str(e)}")
74
 
75
+ elif st.session_state.init_time is not None:
76
+ elapsed_time = time.time() - st.session_state.init_time
77
+ if elapsed_time >= 2.0:
78
+ init_message.empty()
79
+ st.session_state.init_time = None
80
 
81
+ # Handle form submission
82
  def handle_form_submit():
83
  if st.session_state.query_input:
84
  st.session_state.last_query = st.session_state.query_input
85
  st.session_state.submit_clicked = True
86
+ st.session_state.query_input = ""
 
 
87
 
88
+ # Create a form for handling the user input
89
  with st.form(key="query_form"):
90
+ query = st.text_input("Ask your question:", key="query_input", placeholder="Press enter to submit question")
91
  submit_button = st.form_submit_button("Get Answer", on_click=handle_form_submit)
92
 
93
+ # Display the current question if there is one
94
  if st.session_state.last_query:
95
  st.markdown("### Current Question:")
96
  st.info(st.session_state.last_query)
97
 
98
+ # Sliders for customization
99
  col1, col2 = st.columns(2)
100
  with col1:
101
  top_k = st.slider("Number of sources:", 3, 10, 5)
102
  with col2:
103
  word_limit = st.slider("Word limit:", 50, 500, 200)
104
 
105
+ # Only process the query if explicitly submitted
106
  if st.session_state.submit_clicked and st.session_state.last_query:
107
  st.session_state.submit_clicked = False
108
+
109
  with st.spinner("Processing your question..."):
110
  try:
111
  result = process_query(st.session_state.last_query, top_k=top_k, word_limit=word_limit)
 
117
  except Exception as e:
118
  st.error(f"Error processing query: {str(e)}")
119
 
120
+ # Add helpful information
121
  st.markdown("---")
122
  st.markdown("""
123
  ### About this app