Spaces:
Running
Running
Create state_manager.py
Browse files
my_model/utilities/state_manager.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
|
5 |
+
class StateManager:
|
6 |
+
def __init__(self):
|
7 |
+
self.initialize_state()
|
8 |
+
|
9 |
+
def initialize_state(self):
|
10 |
+
if 'images_data' not in st.session_state:
|
11 |
+
st.session_state['images_data'] = {}
|
12 |
+
if 'model_settings' not in st.session_state:
|
13 |
+
st.session_state['model_settings'] = {'detection_model': None, 'confidence_level': None}
|
14 |
+
if 'kbvqa' not in st.session_state:
|
15 |
+
st.session_state['kbvqa'] = None
|
16 |
+
if 'selected_method' not in st.session_state:
|
17 |
+
st.session_state['selected_method'] = None
|
18 |
+
|
19 |
+
def update_model_settings(self, detection_model=None, confidence_level=None, selected_method=None):
|
20 |
+
if detection_model is not None:
|
21 |
+
st.session_state['model_settings']['detection_model'] = detection_model
|
22 |
+
if confidence_level is not None:
|
23 |
+
st.session_state['model_settings']['confidence_level'] = confidence_level
|
24 |
+
if selected_method is not None:
|
25 |
+
st.session_state['selected_method'] = selected_method
|
26 |
+
|
27 |
+
def check_settings_changed(self, current_selected_method, current_detection_model, current_confidence_level):
|
28 |
+
return (st.session_state['model_settings']['detection_model'] != current_detection_model or
|
29 |
+
st.session_state['model_settings']['confidence_level'] != current_confidence_level or
|
30 |
+
st.session_state['selected_method'] != current_selected_method)
|
31 |
+
|
32 |
+
def display_model_settings(self):
|
33 |
+
st.write("### Current Model Settings:")
|
34 |
+
st.table(pd.DataFrame(st.session_state['model_settings'], index=[0]))
|
35 |
+
|
36 |
+
def display_session_state(self):
|
37 |
+
st.write("### Current Session State:")
|
38 |
+
data = [{'Key': key, 'Value': str(value)} for key, value in st.session_state.items()]
|
39 |
+
df = pd.DataFrame(data)
|
40 |
+
st.table(df)
|
41 |
+
|
42 |
+
def get_model(self):
|
43 |
+
"""Retrieve the KBVQA model from the session state."""
|
44 |
+
return st.session_state.get('kbvqa', None)
|
45 |
+
|
46 |
+
def is_model_loaded(self):
|
47 |
+
return 'kbvqa' in st.session_state and st.session_state['kbvqa'] is not None
|
48 |
+
|
49 |
+
def reload_detection_model(self, detection_model, confidence_level):
|
50 |
+
try:
|
51 |
+
free_gpu_resources()
|
52 |
+
if self.is_model_loaded():
|
53 |
+
prepare_kbvqa_model(detection_model, only_reload_detection_model=True)
|
54 |
+
st.session_state['kbvqa'].detection_confidence = confidence_level
|
55 |
+
self.update_model_settings(detection_model, confidence_level)
|
56 |
+
free_gpu_resources()
|
57 |
+
except Exception as e:
|
58 |
+
st.error(f"Error reloading detection model: {e}")
|
59 |
+
|
60 |
+
# New methods to be added
|
61 |
+
def process_new_image(self, image_key, image, kbvqa):
|
62 |
+
if image_key not in st.session_state['images_data']:
|
63 |
+
st.session_state['images_data'][image_key] = {
|
64 |
+
'image': image,
|
65 |
+
'caption': '',
|
66 |
+
'detected_objects_str': '',
|
67 |
+
'qa_history': [],
|
68 |
+
'analysis_done': False
|
69 |
+
}
|
70 |
+
|
71 |
+
def analyze_image(self, image, kbvqa):
|
72 |
+
img = copy.deepcopy(image)
|
73 |
+
caption = kbvqa.get_caption(img)
|
74 |
+
image_with_boxes, detected_objects_str = kbvqa.detect_objects(img)
|
75 |
+
return caption, detected_objects_str, image_with_boxes
|
76 |
+
|
77 |
+
def add_to_qa_history(self, image_key, question, answer):
|
78 |
+
if image_key in st.session_state['images_data']:
|
79 |
+
st.session_state['images_data'][image_key]['qa_history'].append((question, answer))
|
80 |
+
|
81 |
+
def get_images_data(self):
|
82 |
+
return st.session_state['images_data']
|
83 |
+
|
84 |
+
def update_image_data(self, image_key, caption, detected_objects_str, analysis_done):
|
85 |
+
if image_key in st.session_state['images_data']:
|
86 |
+
st.session_state['images_data'][image_key].update({
|
87 |
+
'caption': caption,
|
88 |
+
'detected_objects_str': detected_objects_str,
|
89 |
+
'analysis_done': analysis_done
|
90 |
+
})
|