ankanghosh commited on
Commit
5d2245e
·
verified ·
1 Parent(s): c440b0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -58
app.py CHANGED
@@ -22,58 +22,45 @@ st.markdown("""
22
  border-radius: 8px !important;
23
  }
24
  /* Set default green border for all input fields */
25
- div[data-baseweb="input"] {
26
  border: 2px solid #4CAF50 !important;
27
  border-radius: 8px !important;
 
28
  }
29
  </style>
30
 
31
  <script>
 
32
  document.addEventListener('DOMContentLoaded', function() {
33
- function setupInputStyles() {
 
 
34
  const inputs = document.querySelectorAll('.stTextInput input');
35
 
36
  inputs.forEach(function(input) {
37
- // Remove any existing listeners to prevent duplicates
38
- input.removeEventListener('focus', onFocus);
39
- input.removeEventListener('blur', onBlur);
 
 
40
 
41
- // Get the parent which holds the visible border
42
- const parent = input.closest('div[data-baseweb="input"]');
43
- if (parent) {
44
- // Force the initial green style
45
- parent.style.border = '2px solid #4CAF50';
46
- parent.style.borderRadius = '8px';
47
-
48
- // Add new listeners
49
- input.addEventListener('focus', onFocus);
50
- input.addEventListener('blur', onBlur);
51
- }
52
  });
53
  }
54
 
55
- function onFocus(event) {
56
- const parent = event.target.closest('div[data-baseweb="input"]');
57
- if (parent) {
58
- parent.style.border = '2px solid #FF5722';
59
- }
60
- }
61
-
62
- function onBlur(event) {
63
- const parent = event.target.closest('div[data-baseweb="input"]');
64
- if (parent) {
65
- parent.style.border = '2px solid #4CAF50';
66
- }
67
- }
68
 
69
- // Initial setup
70
- setupInputStyles();
71
-
72
- // Setup observer to handle dynamically added elements
73
- const observer = new MutationObserver(function(mutations) {
74
- setupInputStyles();
75
  });
76
 
 
77
  observer.observe(document.body, {
78
  childList: true,
79
  subtree: true
@@ -91,19 +78,10 @@ if 'last_query' not in st.session_state:
91
  st.session_state.last_query = ""
92
  if 'submit_clicked' not in st.session_state:
93
  st.session_state.submit_clicked = False
94
- if 'init_time' not in st.session_state:
95
- st.session_state.init_time = None
96
- if 'success_shown' not in st.session_state:
97
- st.session_state.success_shown = False
98
-
99
- # Setup all authentication
100
- if not st.session_state.initialized:
101
- try:
102
- setup_all_auth()
103
- except Exception as e:
104
- st.error(f"Authentication error: {str(e)}")
105
 
106
- # Create a placeholder for initialization messages
107
  init_message = st.empty()
108
 
109
  # Handle initialization
@@ -112,24 +90,28 @@ if not st.session_state.initialized:
112
  # Show the loading message
113
  init_message.info("Hang in there! We are setting the system up for you. 😊")
114
 
115
- # Load the model
 
 
 
 
 
 
116
  load_model()
117
 
118
- # Mark as initialized and record the time
119
  st.session_state.initialized = True
120
- st.session_state.init_time = time.time()
121
 
122
- # Show success message (will be removed later)
123
  init_message.success("System initialized successfully!")
124
- st.session_state.success_shown = True
125
  except Exception as e:
126
  st.error(f"Error initializing: {str(e)}")
127
- else:
128
- # Check if we need to clear the success message
129
- if st.session_state.success_shown and st.session_state.init_time is not None:
130
- if time.time() - st.session_state.init_time > 2:
131
- init_message.empty()
132
- st.session_state.success_shown = False
133
 
134
  # Function to process when enter is pressed or button is clicked
135
  def process_input():
 
22
  border-radius: 8px !important;
23
  }
24
  /* Set default green border for all input fields */
25
+ .stTextInput>div>div>input {
26
  border: 2px solid #4CAF50 !important;
27
  border-radius: 8px !important;
28
+ box-shadow: none !important;
29
  }
30
  </style>
31
 
32
  <script>
33
+ // Run once DOM is fully loaded
34
  document.addEventListener('DOMContentLoaded', function() {
35
+ // Function to add events to input elements
36
+ function addInputEvents() {
37
+ // Find all input elements in text inputs
38
  const inputs = document.querySelectorAll('.stTextInput input');
39
 
40
  inputs.forEach(function(input) {
41
+ // Focus event - red border
42
+ input.addEventListener('focus', function() {
43
+ this.style.border = '2px solid #FF5722 !important';
44
+ this.style.boxShadow = 'none !important';
45
+ });
46
 
47
+ // Blur event - green border
48
+ input.addEventListener('blur', function() {
49
+ this.style.border = '2px solid #4CAF50 !important';
50
+ this.style.boxShadow = 'none !important';
51
+ });
 
 
 
 
 
 
52
  });
53
  }
54
 
55
+ // Call immediately
56
+ addInputEvents();
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ // Watch for changes to the DOM (when Streamlit adds new elements)
59
+ const observer = new MutationObserver(function() {
60
+ addInputEvents();
 
 
 
61
  });
62
 
63
+ // Start observing
64
  observer.observe(document.body, {
65
  childList: true,
66
  subtree: true
 
78
  st.session_state.last_query = ""
79
  if 'submit_clicked' not in st.session_state:
80
  st.session_state.submit_clicked = False
81
+ if 'init_start_time' not in st.session_state:
82
+ st.session_state.init_start_time = None
 
 
 
 
 
 
 
 
 
83
 
84
+ # Create placeholder for initialization message
85
  init_message = st.empty()
86
 
87
  # Handle initialization
 
90
  # Show the loading message
91
  init_message.info("Hang in there! We are setting the system up for you. 😊")
92
 
93
+ # Record start time
94
+ st.session_state.init_start_time = time.time()
95
+
96
+ # Setup authentication
97
+ setup_all_auth()
98
+
99
+ # Force model loading at startup to avoid session state issues
100
  load_model()
101
 
102
+ # Mark as initialized
103
  st.session_state.initialized = True
 
104
 
105
+ # Show success message
106
  init_message.success("System initialized successfully!")
 
107
  except Exception as e:
108
  st.error(f"Error initializing: {str(e)}")
109
+ st.session_state.init_start_time = None
110
+ # Check if we need to clear the success message (if it's been more than 2 seconds since initialization)
111
+ elif st.session_state.init_start_time is not None:
112
+ if time.time() - st.session_state.init_start_time > 2:
113
+ init_message.empty()
114
+ st.session_state.init_start_time = None
115
 
116
  # Function to process when enter is pressed or button is clicked
117
  def process_input():