m7mdal7aj commited on
Commit
9becb2c
1 Parent(s): 45517a5

Update my_model/state_manager.py

Browse files
Files changed (1) hide show
  1. my_model/state_manager.py +43 -29
my_model/state_manager.py CHANGED
@@ -24,10 +24,6 @@ class StateManager:
24
  st.session_state['button_label'] = "Load Model"
25
  if "previous_state" not in st.session_state:
26
  st.session_state['previous_state'] = {}
27
- if "settings_changed" not in st.session_state:
28
- st.session_state['settings_changed'] = self.settings_changed
29
- if 'model_loaded' not in st.session_state:
30
- st.session_state['model_loaded'] = False
31
  if 'loading_in_progress' not in st.session_state:
32
  st.session_state['loading_in_progress'] = False
33
  if 'load_button_clicked' not in st.session_state:
@@ -36,8 +32,12 @@ class StateManager:
36
  st.session_state['force_reload_button_clicked'] = False
37
  if 'time_taken_to_load_model' not in st.session_state:
38
  st.session_state['time_taken_to_load_model'] = 0
 
 
 
 
39
 
40
-
41
 
42
 
43
  def set_up_widgets(self):
@@ -78,6 +78,7 @@ class StateManager:
78
  else:
79
  return col.slider(text, min_value, max_value, value, step, key=slider_key_name, disabled=self.is_widget_disabled)
80
 
 
81
  @property
82
  def is_widget_disabled(self):
83
  return st.session_state['loading_in_progress']
@@ -95,17 +96,16 @@ class StateManager:
95
  """
96
  return self.has_state_changed()
97
 
98
-
99
  def display_model_settings(self):
100
  """
101
  Displays a table of current model settings in the third column.
102
 
103
- Uses formatted HTML to style the table for better readability.
104
  """
105
  self.col3.write("##### Current Model Settings:")
106
  data = [{'Setting': key, 'Value': str(value)} for key, value in st.session_state.items() if key in ["confidence_level", 'detection_model', 'method', 'kbvqa', 'previous_state', 'settings_changed', 'loading_in_progress', 'model_loaded', 'time_taken_to_load_model' ]]
107
  df = pd.DataFrame(data)
108
- styled_df = df.style.set_properties(**{'background-color': 'white', 'color': 'black', 'border-color': 'black'}).set_table_styles([{'selector': 'th','props': [('background-color', 'gray'), ('font-weight', 'bold')]}])
109
  self.col3.write(df)
110
 
111
 
@@ -165,7 +165,7 @@ class StateManager:
165
 
166
  free_gpu_resources()
167
 
168
- if self.is_model_loaded():
169
  try:
170
  del st.session_state['kbvqa']
171
  free_gpu_resources()
@@ -173,7 +173,6 @@ class StateManager:
173
  free_gpu_resources()
174
  pass
175
 
176
-
177
 
178
  # Function to check if any session state values have changed
179
  def has_state_changed(self):
@@ -197,7 +196,7 @@ class StateManager:
197
  """
198
  return st.session_state.get('kbvqa', None)
199
 
200
-
201
  def is_model_loaded(self):
202
  """
203
  Checks if the KBVQA model is loaded in the session state.
@@ -221,11 +220,13 @@ class StateManager:
221
 
222
  try:
223
  free_gpu_resources()
224
- if self.is_model_loaded():
225
 
226
  prepare_kbvqa_model(only_reload_detection_model=True)
227
  st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
228
  self.col1.success("Model reloaded with updated settings and ready for inference.")
 
 
229
 
230
  free_gpu_resources()
231
  except Exception as e:
@@ -303,7 +304,26 @@ class StateManager:
303
  dict: The dictionary storing information about processed images.
304
  """
305
  return st.session_state['images_data']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
 
307
  def resize_image(self, image_input, new_width=None, new_height=None):
308
  """
309
  Resize an image. If only new_width is provided, the height is adjusted to maintain aspect ratio.
@@ -346,20 +366,14 @@ class StateManager:
346
  return resized_image
347
 
348
 
349
-
350
- def update_image_data(self, image_key, caption, detected_objects_str, analysis_done):
351
- """
352
- Updates the information stored for a specific image in the `images_data` dictionary in the application session state.
353
-
354
- Args:
355
- image_key (str): Unique key for the image.
356
- caption (str): The generated caption for the image.
357
- detected_objects_str (str): String representation of detected objects.
358
- analysis_done (bool): Flag indicating if analysis of the image is complete.
359
- """
360
- if image_key in st.session_state['images_data']:
361
- st.session_state['images_data'][image_key].update({
362
- 'caption': caption,
363
- 'detected_objects_str': detected_objects_str,
364
- 'analysis_done': analysis_done
365
- })
 
24
  st.session_state['button_label'] = "Load Model"
25
  if "previous_state" not in st.session_state:
26
  st.session_state['previous_state'] = {}
 
 
 
 
27
  if 'loading_in_progress' not in st.session_state:
28
  st.session_state['loading_in_progress'] = False
29
  if 'load_button_clicked' not in st.session_state:
 
32
  st.session_state['force_reload_button_clicked'] = False
33
  if 'time_taken_to_load_model' not in st.session_state:
34
  st.session_state['time_taken_to_load_model'] = 0
35
+ if "settings_changed" not in st.session_state:
36
+ st.session_state['settings_changed'] = self.settings_changed
37
+ if 'model_loaded' not in st.session_state:
38
+ st.session_state['model_loaded'] = self.is_model_loaded
39
 
40
+
41
 
42
 
43
  def set_up_widgets(self):
 
78
  else:
79
  return col.slider(text, min_value, max_value, value, step, key=slider_key_name, disabled=self.is_widget_disabled)
80
 
81
+
82
  @property
83
  def is_widget_disabled(self):
84
  return st.session_state['loading_in_progress']
 
96
  """
97
  return self.has_state_changed()
98
 
99
+ @staticmethod
100
  def display_model_settings(self):
101
  """
102
  Displays a table of current model settings in the third column.
103
 
 
104
  """
105
  self.col3.write("##### Current Model Settings:")
106
  data = [{'Setting': key, 'Value': str(value)} for key, value in st.session_state.items() if key in ["confidence_level", 'detection_model', 'method', 'kbvqa', 'previous_state', 'settings_changed', 'loading_in_progress', 'model_loaded', 'time_taken_to_load_model' ]]
107
  df = pd.DataFrame(data)
108
+ #styled_df = df.style.set_properties(**{'background-color': 'white', 'color': 'black', 'border-color': 'black'}).set_table_styles([{'selector': 'th','props': [('background-color', 'gray'), ('font-weight', 'bold')]}])
109
  self.col3.write(df)
110
 
111
 
 
165
 
166
  free_gpu_resources()
167
 
168
+ if self.is_model_loaded:
169
  try:
170
  del st.session_state['kbvqa']
171
  free_gpu_resources()
 
173
  free_gpu_resources()
174
  pass
175
 
 
176
 
177
  # Function to check if any session state values have changed
178
  def has_state_changed(self):
 
196
  """
197
  return st.session_state.get('kbvqa', None)
198
 
199
+ @property
200
  def is_model_loaded(self):
201
  """
202
  Checks if the KBVQA model is loaded in the session state.
 
220
 
221
  try:
222
  free_gpu_resources()
223
+ if self.is_model_loaded:
224
 
225
  prepare_kbvqa_model(only_reload_detection_model=True)
226
  st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
227
  self.col1.success("Model reloaded with updated settings and ready for inference.")
228
+ st.session_state['previous_state'] = {'method': st.session_state.method, 'detection_model': st.session_state.detection_model, 'confidence_level': st.session_state.confidence_level}
229
+ st.session_state['button_label'] = "Reload Model"
230
 
231
  free_gpu_resources()
232
  except Exception as e:
 
304
  dict: The dictionary storing information about processed images.
305
  """
306
  return st.session_state['images_data']
307
+
308
+
309
+ def update_image_data(self, image_key, caption, detected_objects_str, analysis_done):
310
+ """
311
+ Updates the information stored for a specific image in the `images_data` dictionary in the application session state.
312
+
313
+ Args:
314
+ image_key (str): Unique key for the image.
315
+ caption (str): The generated caption for the image.
316
+ detected_objects_str (str): String representation of detected objects.
317
+ analysis_done (bool): Flag indicating if analysis of the image is complete.
318
+ """
319
+ if image_key in st.session_state['images_data']:
320
+ st.session_state['images_data'][image_key].update({
321
+ 'caption': caption,
322
+ 'detected_objects_str': detected_objects_str,
323
+ 'analysis_done': analysis_done
324
+ })
325
 
326
+ @staticmethod
327
  def resize_image(self, image_input, new_width=None, new_height=None):
328
  """
329
  Resize an image. If only new_width is provided, the height is adjusted to maintain aspect ratio.
 
366
  return resized_image
367
 
368
 
369
+ @staticmethod
370
+ def display_message(self, message, message_type):
371
+ if message_type == "warning":
372
+ st.warning(message)
373
+ elif message_type == "text":
374
+ st.text(message)
375
+ elif message_type == "success":
376
+ st.success(messae)
377
+ elif message_type == "write":
378
+ st.write(message)
379
+ else: st.error("Message type unknown")