Update my_model/state_manager.py
Browse files- my_model/state_manager.py +12 -10
my_model/state_manager.py
CHANGED
@@ -22,8 +22,6 @@ class StateManager:
|
|
22 |
st.session_state['kbvqa'] = None
|
23 |
if "button_label" not in st.session_state:
|
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:
|
@@ -38,6 +36,8 @@ class StateManager:
|
|
38 |
st.session_state['settings_changed'] = self.settings_changed
|
39 |
if 'model_loaded' not in st.session_state:
|
40 |
st.session_state['model_loaded'] = self.is_model_loaded
|
|
|
|
|
41 |
|
42 |
|
43 |
|
@@ -120,7 +120,10 @@ class StateManager:
|
|
120 |
df = pd.DataFrame(data).reset_index(drop=True)
|
121 |
st.write(df)
|
122 |
|
123 |
-
|
|
|
|
|
|
|
124 |
def load_model(self):
|
125 |
"""
|
126 |
Loads the KBVQA model based on the chosen method and settings.
|
@@ -137,7 +140,7 @@ class StateManager:
|
|
137 |
st.session_state['kbvqa'] = prepare_kbvqa_model()
|
138 |
st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
|
139 |
# Update the previous state with current session state values
|
140 |
-
|
141 |
st.session_state['model_loaded'] = True
|
142 |
st.session_state['button_label'] = "Reload Model"
|
143 |
free_gpu_resources()
|
@@ -152,7 +155,8 @@ class StateManager:
|
|
152 |
st.session_state['kbvqa'] = prepare_kbvqa_model(force_reload=True)
|
153 |
st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
|
154 |
# Update the previous state with current session state values
|
155 |
-
|
|
|
156 |
st.session_state['model_loaded'] = True
|
157 |
free_gpu_resources()
|
158 |
except Exception as e:
|
@@ -184,6 +188,8 @@ class StateManager:
|
|
184 |
bool: True if any change is found, False otherwise.
|
185 |
"""
|
186 |
for key in st.session_state['previous_state']:
|
|
|
|
|
187 |
if st.session_state[key] != st.session_state['previous_state'][key]:
|
188 |
return True # Found a change
|
189 |
else: return False # No changes found
|
@@ -222,14 +228,10 @@ class StateManager:
|
|
222 |
try:
|
223 |
free_gpu_resources()
|
224 |
if self.is_model_loaded:
|
225 |
-
|
226 |
-
st.write(st.session_state['detection_model'] == st.session_state['previous_state']['detection_model'])
|
227 |
-
st.write(st.session_state['method'] == st.session_state['previous_state']['method'])
|
228 |
-
|
229 |
prepare_kbvqa_model(only_reload_detection_model=True)
|
230 |
st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
|
231 |
self.col1.success("Model reloaded with updated settings and ready for inference.")
|
232 |
-
|
233 |
st.session_state['button_label'] = "Reload Model"
|
234 |
|
235 |
free_gpu_resources()
|
|
|
22 |
st.session_state['kbvqa'] = None
|
23 |
if "button_label" not in st.session_state:
|
24 |
st.session_state['button_label'] = "Load Model"
|
|
|
|
|
25 |
if 'loading_in_progress' not in st.session_state:
|
26 |
st.session_state['loading_in_progress'] = False
|
27 |
if 'load_button_clicked' 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 |
+
if "previous_state" not in st.session_state:
|
40 |
+
st.session_state['previous_state'] = {'method': None, 'detection_model': None, 'confidence_level': None}
|
41 |
|
42 |
|
43 |
|
|
|
120 |
df = pd.DataFrame(data).reset_index(drop=True)
|
121 |
st.write(df)
|
122 |
|
123 |
+
def update_prev_state(self):
|
124 |
+
for key in st.session_state['previous_state']:
|
125 |
+
st.session_state['previous_state'][key] = st.session_state[key]
|
126 |
+
|
127 |
def load_model(self):
|
128 |
"""
|
129 |
Loads the KBVQA model based on the chosen method and settings.
|
|
|
140 |
st.session_state['kbvqa'] = prepare_kbvqa_model()
|
141 |
st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
|
142 |
# Update the previous state with current session state values
|
143 |
+
self.update_prev_state()
|
144 |
st.session_state['model_loaded'] = True
|
145 |
st.session_state['button_label'] = "Reload Model"
|
146 |
free_gpu_resources()
|
|
|
155 |
st.session_state['kbvqa'] = prepare_kbvqa_model(force_reload=True)
|
156 |
st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
|
157 |
# Update the previous state with current session state values
|
158 |
+
self.update_prev_state()
|
159 |
+
|
160 |
st.session_state['model_loaded'] = True
|
161 |
free_gpu_resources()
|
162 |
except Exception as e:
|
|
|
188 |
bool: True if any change is found, False otherwise.
|
189 |
"""
|
190 |
for key in st.session_state['previous_state']:
|
191 |
+
if key == 'confidence_level':
|
192 |
+
continue # confidence_level tracker is separate
|
193 |
if st.session_state[key] != st.session_state['previous_state'][key]:
|
194 |
return True # Found a change
|
195 |
else: return False # No changes found
|
|
|
228 |
try:
|
229 |
free_gpu_resources()
|
230 |
if self.is_model_loaded:
|
|
|
|
|
|
|
|
|
231 |
prepare_kbvqa_model(only_reload_detection_model=True)
|
232 |
st.session_state['kbvqa'].detection_confidence = st.session_state.confidence_level
|
233 |
self.col1.success("Model reloaded with updated settings and ready for inference.")
|
234 |
+
self.update_prev_state
|
235 |
st.session_state['button_label'] = "Reload Model"
|
236 |
|
237 |
free_gpu_resources()
|