ankanghosh commited on
Commit
9ac9057
Β·
verified Β·
1 Parent(s): 538e101

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -52
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 the query has been answered
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 our initialization message
28
  init_message = st.empty()
29
 
30
- # βœ… Custom CSS for dynamic behavior
31
  st.markdown("""
32
  <style>
33
- /* βœ… Green by default */
34
- .input-container input[type="text"] {
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
- .input-container input[type="text"]:focus {
42
  border: 2px solid #FF5722 !important;
43
  }
44
 
45
- /* βœ… Change back to green AFTER submission */
46
- .answered .input-container input[type="text"] {
47
  border: 2px solid #4CAF50 !important;
48
  }
49
 
50
- /* βœ… Make button a MUCH lighter green */
51
  .stButton>button {
52
- background-color: #81C784 !important;
53
  color: white !important;
54
- border: 2px solid #66BB6A !important;
55
  border-radius: 8px !important;
 
 
 
 
56
  }
57
  </style>
58
  """, unsafe_allow_html=True)
59
 
60
- # Handle initialization and success message timing
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.init_time = time.time()
74
-
75
- # Show success message
76
  init_message.success("System initialized successfully!")
77
-
78
- # Force rerun to start the timer for removing the message
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 box goes back to green
98
- st.session_state.query_input = "" # Clear input box after submission
99
- st.rerun() # βœ… Force UI refresh so color resets
100
 
101
- # βœ… Wrap the text input inside a div to allow dynamic styling
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 handling user input
106
  with st.form(key="query_form"):
107
- # Input field with dynamic border behavior
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 the div for styling
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.rerun() # βœ… Refresh UI so that text box turns green again
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)}")