Update my_model/tabs/run_inference.py
Browse files- my_model/tabs/run_inference.py +11 -11
my_model/tabs/run_inference.py
CHANGED
@@ -32,8 +32,8 @@ class InferenceRunner(StateManager):
|
|
32 |
|
33 |
def image_qa_app(self, kbvqa):
|
34 |
# Display sample images as clickable thumbnails
|
35 |
-
|
36 |
-
cols =
|
37 |
for idx, sample_image_path in enumerate(self.sample_images):
|
38 |
with cols[idx]:
|
39 |
image = Image.open(sample_image_path)
|
@@ -42,16 +42,16 @@ class InferenceRunner(StateManager):
|
|
42 |
self.process_new_image(sample_image_path, image, kbvqa)
|
43 |
|
44 |
# Image uploader
|
45 |
-
uploaded_image =
|
46 |
if uploaded_image is not None:
|
47 |
self.process_new_image(uploaded_image.name, Image.open(uploaded_image), kbvqa)
|
48 |
|
49 |
# Display and interact with each uploaded/selected image
|
50 |
for image_key, image_data in self.get_images_data().items():
|
51 |
-
|
52 |
if not image_data['analysis_done']:
|
53 |
-
|
54 |
-
if
|
55 |
caption, detected_objects_str, image_with_boxes = self.analyze_image(image_data['image'], kbvqa)
|
56 |
self.update_image_data(image_key, caption, detected_objects_str, True)
|
57 |
|
@@ -59,8 +59,8 @@ class InferenceRunner(StateManager):
|
|
59 |
qa_history = image_data.get('qa_history', [])
|
60 |
|
61 |
if image_data['analysis_done']:
|
62 |
-
question =
|
63 |
-
if
|
64 |
if question not in [q for q, _ in qa_history]:
|
65 |
answer = self.answer_question(image_data['caption'], image_data['detected_objects_str'], question, kbvqa)
|
66 |
self.add_to_qa_history(image_key, question, answer)
|
@@ -77,15 +77,15 @@ class InferenceRunner(StateManager):
|
|
77 |
self.set_up_widgets()
|
78 |
st.session_state['settings_changed'] = self.has_state_changed()
|
79 |
if st.session_state['settings_changed']:
|
80 |
-
|
81 |
|
82 |
st.session_state.button_label = "Reload Model" if self.is_model_loaded() and self.settings_changed else "Load Model"
|
83 |
|
84 |
if st.session_state.method == "Fine-Tuned Model":
|
85 |
-
if
|
86 |
if st.session_state.button_label == "Load Model":
|
87 |
if self.is_model_loaded():
|
88 |
-
|
89 |
else:
|
90 |
self.load_model()
|
91 |
else:
|
|
|
32 |
|
33 |
def image_qa_app(self, kbvqa):
|
34 |
# Display sample images as clickable thumbnails
|
35 |
+
self.col1.write("Choose from sample images:")
|
36 |
+
cols = self.col1.columns(len(self.sample_images))
|
37 |
for idx, sample_image_path in enumerate(self.sample_images):
|
38 |
with cols[idx]:
|
39 |
image = Image.open(sample_image_path)
|
|
|
42 |
self.process_new_image(sample_image_path, image, kbvqa)
|
43 |
|
44 |
# Image uploader
|
45 |
+
uploaded_image = self.col1.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
46 |
if uploaded_image is not None:
|
47 |
self.process_new_image(uploaded_image.name, Image.open(uploaded_image), kbvqa)
|
48 |
|
49 |
# Display and interact with each uploaded/selected image
|
50 |
for image_key, image_data in self.get_images_data().items():
|
51 |
+
col2.image(image_data['image'], caption=f'Uploaded Image: {image_key[-11:]}', use_column_width=True)
|
52 |
if not image_data['analysis_done']:
|
53 |
+
self.col2.text("Cool image, please click 'Analyze Image'..")
|
54 |
+
if col2.button('Analyze Image', key=f'analyze_{image_key}'):
|
55 |
caption, detected_objects_str, image_with_boxes = self.analyze_image(image_data['image'], kbvqa)
|
56 |
self.update_image_data(image_key, caption, detected_objects_str, True)
|
57 |
|
|
|
59 |
qa_history = image_data.get('qa_history', [])
|
60 |
|
61 |
if image_data['analysis_done']:
|
62 |
+
question = col2.text_input(f"Ask a question about this image ({image_key[-11:]}):", key=f'question_{image_key}')
|
63 |
+
if col2.button('Get Answer', key=f'answer_{image_key}'):
|
64 |
if question not in [q for q, _ in qa_history]:
|
65 |
answer = self.answer_question(image_data['caption'], image_data['detected_objects_str'], question, kbvqa)
|
66 |
self.add_to_qa_history(image_key, question, answer)
|
|
|
77 |
self.set_up_widgets()
|
78 |
st.session_state['settings_changed'] = self.has_state_changed()
|
79 |
if st.session_state['settings_changed']:
|
80 |
+
col1.warning("Model settings have changed, please reload the model, this will take a second .. ")
|
81 |
|
82 |
st.session_state.button_label = "Reload Model" if self.is_model_loaded() and self.settings_changed else "Load Model"
|
83 |
|
84 |
if st.session_state.method == "Fine-Tuned Model":
|
85 |
+
if col1.button(st.session_state.button_label):
|
86 |
if st.session_state.button_label == "Load Model":
|
87 |
if self.is_model_loaded():
|
88 |
+
col1.text("Model already loaded and no settings were changed:)")
|
89 |
else:
|
90 |
self.load_model()
|
91 |
else:
|