Update my_model/state_manager.py
Browse files- my_model/state_manager.py +86 -46
my_model/state_manager.py
CHANGED
@@ -2,6 +2,7 @@ import pandas as pd
|
|
2 |
import copy
|
3 |
import time
|
4 |
from PIL import Image
|
|
|
5 |
import streamlit as st
|
6 |
from my_model.utilities.gen_utilities import free_gpu_resources
|
7 |
from my_model.KBVQA import KBVQA, prepare_kbvqa_model
|
@@ -10,20 +11,35 @@ from my_model.KBVQA import KBVQA, prepare_kbvqa_model
|
|
10 |
|
11 |
|
12 |
class StateManager:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def __init__(self):
|
|
|
|
|
|
|
|
|
15 |
# Create three columns with different widths
|
16 |
self.col1, self.col2, self.col3 = st.columns([0.2, 0.6, 0.2])
|
17 |
|
18 |
def initialize_state(self):
|
|
|
|
|
|
|
19 |
if "previous_state" not in st.session_state:
|
20 |
st.session_state['previous_state'] = {'method': None, 'detection_model': None, 'confidence_level': None}
|
21 |
-
#if 'method' not in st.session_state:
|
22 |
-
# st.session_state['method'] = "Fine-Tuned Model"
|
23 |
-
#if 'confidence_level' not in st.session_state:
|
24 |
-
# st.session_state['confidence_level'] = 0.4
|
25 |
-
#if 'detection_model' not in st.session_state:
|
26 |
-
# st.session_state['detection_model'] = 'detic'
|
27 |
if 'images_data' not in st.session_state:
|
28 |
st.session_state['images_data'] = {}
|
29 |
if 'kbvqa' not in st.session_state:
|
@@ -38,18 +54,12 @@ class StateManager:
|
|
38 |
st.session_state['force_reload_button_clicked'] = False
|
39 |
if 'time_taken_to_load_model' not in st.session_state:
|
40 |
st.session_state['time_taken_to_load_model'] = None
|
41 |
-
|
42 |
if "settings_changed" not in st.session_state:
|
43 |
st.session_state['settings_changed'] = self.settings_changed
|
44 |
if 'model_loaded' not in st.session_state:
|
45 |
st.session_state['model_loaded'] = self.is_model_loaded
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
def set_up_widgets(self):
|
53 |
"""
|
54 |
Sets up user interface widgets for selecting models, settings, and displaying model settings conditionally.
|
55 |
"""
|
@@ -68,7 +78,7 @@ class StateManager:
|
|
68 |
|
69 |
|
70 |
|
71 |
-
def set_slider_value(self, text, min_value, max_value, value, step, slider_key_name, col=None):
|
72 |
"""
|
73 |
Creates a slider widget with the specified parameters, optionally placing it in a specific column.
|
74 |
|
@@ -87,13 +97,18 @@ class StateManager:
|
|
87 |
else:
|
88 |
return col.slider(text, min_value, max_value, value, step, key=slider_key_name, disabled=self.is_widget_disabled)
|
89 |
|
90 |
-
|
91 |
@property
|
92 |
def is_widget_disabled(self):
|
93 |
return st.session_state['loading_in_progress']
|
94 |
|
95 |
def disable_widgets(self):
|
|
|
|
|
|
|
|
|
96 |
st.session_state['loading_in_progress'] = True
|
|
|
97 |
|
98 |
@property
|
99 |
def settings_changed(self):
|
@@ -104,38 +119,30 @@ class StateManager:
|
|
104 |
bool: True if any setting has changed, False otherwise.
|
105 |
"""
|
106 |
return self.has_state_changed()
|
|
|
107 |
|
108 |
@property
|
109 |
def confidance_change(self):
|
110 |
-
return st.session_state["confidence_level"] != st.session_state["previous_state"]["confidence_level"]
|
111 |
-
|
112 |
-
@property
|
113 |
-
def display_model_settings(self):
|
114 |
"""
|
115 |
-
|
116 |
-
|
|
|
|
|
117 |
"""
|
118 |
-
|
119 |
-
|
120 |
-
df = pd.DataFrame(data).reset_index(drop=True)
|
121 |
-
return self.col3.write(df)
|
122 |
|
123 |
-
|
124 |
-
def
|
125 |
"""
|
126 |
-
|
127 |
"""
|
128 |
-
|
129 |
-
st.write("Current Model:")
|
130 |
-
data = [{'Key': key, 'Value': str(value)} for key, value in st.session_state.items()]
|
131 |
-
df = pd.DataFrame(data).reset_index(drop=True)
|
132 |
-
st.write(df)
|
133 |
|
134 |
-
def update_prev_state(self):
|
135 |
for key in st.session_state['previous_state']:
|
136 |
st.session_state['previous_state'][key] = st.session_state[key]
|
|
|
137 |
|
138 |
-
def load_model(self):
|
139 |
"""
|
140 |
Loads the KBVQA model based on the chosen method and settings.
|
141 |
|
@@ -159,7 +166,17 @@ class StateManager:
|
|
159 |
except Exception as e:
|
160 |
st.error(f"Error loading model: {e}")
|
161 |
|
162 |
-
def force_reload_model(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
try:
|
164 |
self.delete_model()
|
165 |
free_gpu_resources()
|
@@ -174,9 +191,9 @@ class StateManager:
|
|
174 |
st.error(f"Error reloading model: {e}")
|
175 |
free_gpu_resources()
|
176 |
|
177 |
-
def delete_model(self):
|
178 |
"""
|
179 |
-
|
180 |
"""
|
181 |
|
182 |
free_gpu_resources()
|
@@ -191,7 +208,7 @@ class StateManager:
|
|
191 |
|
192 |
|
193 |
# Function to check if any session state values have changed
|
194 |
-
def has_state_changed(self):
|
195 |
"""
|
196 |
Compares current session state with the previous state to identify changes.
|
197 |
|
@@ -207,7 +224,7 @@ class StateManager:
|
|
207 |
else: return False # No changes found
|
208 |
|
209 |
|
210 |
-
def get_model(self):
|
211 |
"""
|
212 |
Retrieve the KBVQA model from the session state.
|
213 |
|
@@ -216,7 +233,7 @@ class StateManager:
|
|
216 |
return st.session_state.get('kbvqa', None)
|
217 |
|
218 |
@property
|
219 |
-
def is_model_loaded(self):
|
220 |
"""
|
221 |
Checks if the KBVQA model is loaded in the session state.
|
222 |
|
@@ -226,7 +243,7 @@ class StateManager:
|
|
226 |
return 'kbvqa' in st.session_state and st.session_state['kbvqa'] is not None and st.session_state.kbvqa.all_models_loaded
|
227 |
|
228 |
|
229 |
-
def reload_detection_model(self):
|
230 |
"""
|
231 |
Reloads only the detection model of the KBVQA model with updated settings.
|
232 |
|
@@ -251,7 +268,7 @@ class StateManager:
|
|
251 |
st.error(f"Error reloading detection model: {e}")
|
252 |
|
253 |
|
254 |
-
def process_new_image(self, image_key, image):
|
255 |
"""
|
256 |
Processes a new uploaded image by creating an entry in the `images_data` dictionary in the application session state.
|
257 |
|
@@ -278,7 +295,7 @@ class StateManager:
|
|
278 |
}
|
279 |
|
280 |
|
281 |
-
def analyze_image(self, image):
|
282 |
"""
|
283 |
Analyzes the image using the KBVQA model.
|
284 |
|
@@ -302,7 +319,7 @@ class StateManager:
|
|
302 |
return caption, detected_objects_str, image_with_boxes
|
303 |
|
304 |
|
305 |
-
def add_to_qa_history(self, image_key, question, answer, prompt_length):
|
306 |
"""
|
307 |
Adds a question-answer pair to the QA history of a specific image, to be used as hitory tracker.
|
308 |
|
@@ -395,4 +412,27 @@ class StateManager:
|
|
395 |
st.success(messae)
|
396 |
elif message_type == "write":
|
397 |
st.write(message)
|
398 |
-
else: st.error("Message type unknown")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import copy
|
3 |
import time
|
4 |
from PIL import Image
|
5 |
+
from typing import Tuple, Dict
|
6 |
import streamlit as st
|
7 |
from my_model.utilities.gen_utilities import free_gpu_resources
|
8 |
from my_model.KBVQA import KBVQA, prepare_kbvqa_model
|
|
|
11 |
|
12 |
|
13 |
class StateManager:
|
14 |
+
|
15 |
+
# Hints for methods
|
16 |
+
# initialize_state: Initializes default values for session state.
|
17 |
+
# set_up_widgets: Creates UI elements for model selection and settings.
|
18 |
+
# set_slider_value: Generates a slider widget for numerical input.
|
19 |
+
# is_widget_disabled: Returns True if UI elements should be disabled.
|
20 |
+
# disable_widgets: Disables interactive UI elements during processing.
|
21 |
+
# settings_changed: Checks if any model settings have changed.
|
22 |
+
# confidance_change: Determines if the confidence level setting has changed.
|
23 |
+
# display_model_settings: Shows current model settings in the UI.
|
24 |
+
# display_session_state: Displays the current state of the application.
|
25 |
+
# update_prev_state: Updates the record of the previous application state.
|
26 |
+
# force_reload_model: Reloads the model, clearing and resetting necessary states.
|
27 |
+
|
28 |
|
29 |
def __init__(self):
|
30 |
+
"""
|
31 |
+
Initializes the StateManager instance, setting up the Streamlit columns for the user interface.
|
32 |
+
"""
|
33 |
+
|
34 |
# Create three columns with different widths
|
35 |
self.col1, self.col2, self.col3 = st.columns([0.2, 0.6, 0.2])
|
36 |
|
37 |
def initialize_state(self):
|
38 |
+
"""
|
39 |
+
Initializes the Streamlit session state with default values for various keys.
|
40 |
+
"""
|
41 |
if "previous_state" not in st.session_state:
|
42 |
st.session_state['previous_state'] = {'method': None, 'detection_model': None, 'confidence_level': None}
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
if 'images_data' not in st.session_state:
|
44 |
st.session_state['images_data'] = {}
|
45 |
if 'kbvqa' not in st.session_state:
|
|
|
54 |
st.session_state['force_reload_button_clicked'] = False
|
55 |
if 'time_taken_to_load_model' not in st.session_state:
|
56 |
st.session_state['time_taken_to_load_model'] = None
|
|
|
57 |
if "settings_changed" not in st.session_state:
|
58 |
st.session_state['settings_changed'] = self.settings_changed
|
59 |
if 'model_loaded' not in st.session_state:
|
60 |
st.session_state['model_loaded'] = self.is_model_loaded
|
61 |
|
62 |
+
def set_up_widgets(self) -> None:
|
|
|
|
|
|
|
|
|
|
|
63 |
"""
|
64 |
Sets up user interface widgets for selecting models, settings, and displaying model settings conditionally.
|
65 |
"""
|
|
|
78 |
|
79 |
|
80 |
|
81 |
+
def set_slider_value(self, text: str, min_value: float, max_value: float, value: float, step: float, slider_key_name: str, col=None) -> None:
|
82 |
"""
|
83 |
Creates a slider widget with the specified parameters, optionally placing it in a specific column.
|
84 |
|
|
|
97 |
else:
|
98 |
return col.slider(text, min_value, max_value, value, step, key=slider_key_name, disabled=self.is_widget_disabled)
|
99 |
|
100 |
+
|
101 |
@property
|
102 |
def is_widget_disabled(self):
|
103 |
return st.session_state['loading_in_progress']
|
104 |
|
105 |
def disable_widgets(self):
|
106 |
+
"""
|
107 |
+
Disables widgets by setting the 'loading_in_progress' state to True.
|
108 |
+
"""
|
109 |
+
|
110 |
st.session_state['loading_in_progress'] = True
|
111 |
+
|
112 |
|
113 |
@property
|
114 |
def settings_changed(self):
|
|
|
119 |
bool: True if any setting has changed, False otherwise.
|
120 |
"""
|
121 |
return self.has_state_changed()
|
122 |
+
|
123 |
|
124 |
@property
|
125 |
def confidance_change(self):
|
|
|
|
|
|
|
|
|
126 |
"""
|
127 |
+
Checks if the confidence level setting has changed compared to the previous state.
|
128 |
+
|
129 |
+
Returns:
|
130 |
+
bool: True if the confidence level has changed, False otherwise.
|
131 |
"""
|
132 |
+
|
133 |
+
return st.session_state["confidence_level"] != st.session_state["previous_state"]["confidence_level"]
|
|
|
|
|
134 |
|
135 |
+
|
136 |
+
def update_prev_state(self):
|
137 |
"""
|
138 |
+
Updates the 'previous_state' in the session state with the current state values.
|
139 |
"""
|
|
|
|
|
|
|
|
|
|
|
140 |
|
|
|
141 |
for key in st.session_state['previous_state']:
|
142 |
st.session_state['previous_state'][key] = st.session_state[key]
|
143 |
+
|
144 |
|
145 |
+
def load_model(self) -> None:
|
146 |
"""
|
147 |
Loads the KBVQA model based on the chosen method and settings.
|
148 |
|
|
|
166 |
except Exception as e:
|
167 |
st.error(f"Error loading model: {e}")
|
168 |
|
169 |
+
def force_reload_model(self) -> None:
|
170 |
+
"""
|
171 |
+
Forces a reload of all models, freeing up GPU resources. This method deletes the current models and calls `free_gpu_resources`.
|
172 |
+
|
173 |
+
- Deletes the current KBVQA model from the session state.
|
174 |
+
- Calls `prepare_kbvqa_model` with `force_reload=True` to reload the model.
|
175 |
+
- Updates the detection confidence level on the model object.
|
176 |
+
- Displays a success message if the model is reloaded successfully.
|
177 |
+
"""
|
178 |
+
|
179 |
+
|
180 |
try:
|
181 |
self.delete_model()
|
182 |
free_gpu_resources()
|
|
|
191 |
st.error(f"Error reloading model: {e}")
|
192 |
free_gpu_resources()
|
193 |
|
194 |
+
def delete_model(self) -> None:
|
195 |
"""
|
196 |
+
This method deletes the current models and calls `free_gpu_resources`.
|
197 |
"""
|
198 |
|
199 |
free_gpu_resources()
|
|
|
208 |
|
209 |
|
210 |
# Function to check if any session state values have changed
|
211 |
+
def has_state_changed(self) -> bool:
|
212 |
"""
|
213 |
Compares current session state with the previous state to identify changes.
|
214 |
|
|
|
224 |
else: return False # No changes found
|
225 |
|
226 |
|
227 |
+
def get_model(self) -> KBVQA:
|
228 |
"""
|
229 |
Retrieve the KBVQA model from the session state.
|
230 |
|
|
|
233 |
return st.session_state.get('kbvqa', None)
|
234 |
|
235 |
@property
|
236 |
+
def is_model_loaded(self) -> bool:
|
237 |
"""
|
238 |
Checks if the KBVQA model is loaded in the session state.
|
239 |
|
|
|
243 |
return 'kbvqa' in st.session_state and st.session_state['kbvqa'] is not None and st.session_state.kbvqa.all_models_loaded
|
244 |
|
245 |
|
246 |
+
def reload_detection_model(self) -> None:
|
247 |
"""
|
248 |
Reloads only the detection model of the KBVQA model with updated settings.
|
249 |
|
|
|
268 |
st.error(f"Error reloading detection model: {e}")
|
269 |
|
270 |
|
271 |
+
def process_new_image(self, image_key: str, image) -> None:
|
272 |
"""
|
273 |
Processes a new uploaded image by creating an entry in the `images_data` dictionary in the application session state.
|
274 |
|
|
|
295 |
}
|
296 |
|
297 |
|
298 |
+
def analyze_image(self, image) -> Tuple[str, str, object]:
|
299 |
"""
|
300 |
Analyzes the image using the KBVQA model.
|
301 |
|
|
|
319 |
return caption, detected_objects_str, image_with_boxes
|
320 |
|
321 |
|
322 |
+
def add_to_qa_history(self, image_key: str, question: str, answer: str, prompt_length: int) -> None:
|
323 |
"""
|
324 |
Adds a question-answer pair to the QA history of a specific image, to be used as hitory tracker.
|
325 |
|
|
|
412 |
st.success(messae)
|
413 |
elif message_type == "write":
|
414 |
st.write(message)
|
415 |
+
else: st.error("Message type unknown")
|
416 |
+
@property
|
417 |
+
|
418 |
+
|
419 |
+
def display_model_settings(self):
|
420 |
+
"""
|
421 |
+
Displays a table of current model settings in the third column.
|
422 |
+
|
423 |
+
"""
|
424 |
+
self.col3.write("##### Current Model Settings:")
|
425 |
+
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' ]]
|
426 |
+
df = pd.DataFrame(data).reset_index(drop=True)
|
427 |
+
return self.col3.write(df)
|
428 |
+
|
429 |
+
|
430 |
+
def display_session_state(self):
|
431 |
+
"""
|
432 |
+
Displays a table of the complete application state..
|
433 |
+
"""
|
434 |
+
|
435 |
+
st.write("Current Model:")
|
436 |
+
data = [{'Key': key, 'Value': str(value)} for key, value in st.session_state.items()]
|
437 |
+
df = pd.DataFrame(data).reset_index(drop=True)
|
438 |
+
st.write(df)
|