Spaces:
Sleeping
Sleeping
Rename my_model/utilities/st_utils.py to my_model/utilities/ui_manager.py
Browse files
my_model/utilities/{st_utils.py → ui_manager.py}
RENAMED
@@ -67,90 +67,3 @@ class UIManager:
|
|
67 |
|
68 |
|
69 |
|
70 |
-
|
71 |
-
class StateManager:
|
72 |
-
def __init__(self):
|
73 |
-
self.initialize_state()
|
74 |
-
|
75 |
-
def initialize_state(self):
|
76 |
-
if 'images_data' not in st.session_state:
|
77 |
-
st.session_state['images_data'] = {}
|
78 |
-
if 'model_settings' not in st.session_state:
|
79 |
-
st.session_state['model_settings'] = {'detection_model': None, 'confidence_level': None}
|
80 |
-
if 'kbvqa' not in st.session_state:
|
81 |
-
st.session_state['kbvqa'] = None
|
82 |
-
if 'selected_method' not in st.session_state:
|
83 |
-
st.session_state['selected_method'] = None
|
84 |
-
|
85 |
-
def update_model_settings(self, detection_model=None, confidence_level=None, selected_method=None):
|
86 |
-
if detection_model is not None:
|
87 |
-
st.session_state['model_settings']['detection_model'] = detection_model
|
88 |
-
if confidence_level is not None:
|
89 |
-
st.session_state['model_settings']['confidence_level'] = confidence_level
|
90 |
-
if selected_method is not None:
|
91 |
-
st.session_state['selected_method'] = selected_method
|
92 |
-
|
93 |
-
def check_settings_changed(self, current_selected_method, current_detection_model, current_confidence_level):
|
94 |
-
return (st.session_state['model_settings']['detection_model'] != current_detection_model or
|
95 |
-
st.session_state['model_settings']['confidence_level'] != current_confidence_level or
|
96 |
-
st.session_state['selected_method'] != current_selected_method)
|
97 |
-
|
98 |
-
def display_model_settings(self):
|
99 |
-
st.write("### Current Model Settings:")
|
100 |
-
st.table(pd.DataFrame(st.session_state['model_settings'], index=[0]))
|
101 |
-
|
102 |
-
def display_session_state(self):
|
103 |
-
st.write("### Current Session State:")
|
104 |
-
data = [{'Key': key, 'Value': str(value)} for key, value in st.session_state.items()]
|
105 |
-
df = pd.DataFrame(data)
|
106 |
-
st.table(df)
|
107 |
-
|
108 |
-
def get_model(self):
|
109 |
-
"""Retrieve the KBVQA model from the session state."""
|
110 |
-
return st.session_state.get('kbvqa', None)
|
111 |
-
|
112 |
-
def is_model_loaded(self):
|
113 |
-
return 'kbvqa' in st.session_state and st.session_state['kbvqa'] is not None
|
114 |
-
|
115 |
-
def reload_detection_model(self, detection_model, confidence_level):
|
116 |
-
try:
|
117 |
-
free_gpu_resources()
|
118 |
-
if self.is_model_loaded():
|
119 |
-
prepare_kbvqa_model(detection_model, only_reload_detection_model=True)
|
120 |
-
st.session_state['kbvqa'].detection_confidence = confidence_level
|
121 |
-
self.update_model_settings(detection_model, confidence_level)
|
122 |
-
free_gpu_resources()
|
123 |
-
except Exception as e:
|
124 |
-
st.error(f"Error reloading detection model: {e}")
|
125 |
-
|
126 |
-
# New methods to be added
|
127 |
-
def process_new_image(self, image_key, image, kbvqa):
|
128 |
-
if image_key not in st.session_state['images_data']:
|
129 |
-
st.session_state['images_data'][image_key] = {
|
130 |
-
'image': image,
|
131 |
-
'caption': '',
|
132 |
-
'detected_objects_str': '',
|
133 |
-
'qa_history': [],
|
134 |
-
'analysis_done': False
|
135 |
-
}
|
136 |
-
|
137 |
-
def analyze_image(self, image, kbvqa):
|
138 |
-
img = copy.deepcopy(image)
|
139 |
-
caption = kbvqa.get_caption(img)
|
140 |
-
image_with_boxes, detected_objects_str = kbvqa.detect_objects(img)
|
141 |
-
return caption, detected_objects_str, image_with_boxes
|
142 |
-
|
143 |
-
def add_to_qa_history(self, image_key, question, answer):
|
144 |
-
if image_key in st.session_state['images_data']:
|
145 |
-
st.session_state['images_data'][image_key]['qa_history'].append((question, answer))
|
146 |
-
|
147 |
-
def get_images_data(self):
|
148 |
-
return st.session_state['images_data']
|
149 |
-
|
150 |
-
def update_image_data(self, image_key, caption, detected_objects_str, analysis_done):
|
151 |
-
if image_key in st.session_state['images_data']:
|
152 |
-
st.session_state['images_data'][image_key].update({
|
153 |
-
'caption': caption,
|
154 |
-
'detected_objects_str': detected_objects_str,
|
155 |
-
'analysis_done': analysis_done
|
156 |
-
})
|
|
|
67 |
|
68 |
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|