Spaces:
Sleeping
Sleeping
Update my_model/tabs/run_inference.py
Browse files- my_model/tabs/run_inference.py +82 -92
my_model/tabs/run_inference.py
CHANGED
|
@@ -10,99 +10,89 @@ import pandas as pd
|
|
| 10 |
from my_model.object_detection import detect_and_draw_objects
|
| 11 |
from my_model.captioner.image_captioning import get_caption
|
| 12 |
from my_model.gen_utilities import free_gpu_resources
|
| 13 |
-
from my_model.KBVQA import KBVQA, prepare_kbvqa_model
|
| 14 |
from my_model.state_manager import StateManager
|
| 15 |
|
| 16 |
state_manager = StateManager()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
if
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
st.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
if state_manager.is_model_loaded() and st.session_state.kbvqa.all_models_loaded:
|
| 102 |
-
image_qa_app(state_manager.get_model())
|
| 103 |
-
|
| 104 |
-
else:
|
| 105 |
-
st.write(f'Model using {st.session_state.method} is not deplyed yet, will be ready later.')
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 10 |
from my_model.object_detection import detect_and_draw_objects
|
| 11 |
from my_model.captioner.image_captioning import get_caption
|
| 12 |
from my_model.gen_utilities import free_gpu_resources
|
| 13 |
+
#from my_model.KBVQA import KBVQA, prepare_kbvqa_model
|
| 14 |
from my_model.state_manager import StateManager
|
| 15 |
|
| 16 |
state_manager = StateManager()
|
| 17 |
|
| 18 |
+
class InferenceRunner(StateManager):
|
| 19 |
+
def __init__(self):
|
| 20 |
+
super().__init__()
|
| 21 |
+
self.sample_images = [
|
| 22 |
+
"Files/sample1.jpg", "Files/sample2.jpg", "Files/sample3.jpg",
|
| 23 |
+
"Files/sample4.jpg", "Files/sample5.jpg", "Files/sample6.jpg",
|
| 24 |
+
"Files/sample7.jpg"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
def answer_question(self, caption, detected_objects_str, question, model):
|
| 28 |
+
free_gpu_resources()
|
| 29 |
+
answer = model.generate_answer(question, caption, detected_objects_str)
|
| 30 |
+
free_gpu_resources()
|
| 31 |
+
return answer
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def image_qa_app(self, kbvqa):
|
| 35 |
+
# Display sample images as clickable thumbnails
|
| 36 |
+
st.write("Choose from sample images:")
|
| 37 |
+
cols = st.columns(len(self.sample_images))
|
| 38 |
+
for idx, sample_image_path in enumerate(self.sample_images):
|
| 39 |
+
with cols[idx]:
|
| 40 |
+
image = Image.open(sample_image_path)
|
| 41 |
+
st.image(image, use_column_width=True)
|
| 42 |
+
if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
|
| 43 |
+
self.process_new_image(sample_image_path, image, kbvqa)
|
| 44 |
+
|
| 45 |
+
# Image uploader
|
| 46 |
+
uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
| 47 |
+
if uploaded_image is not None:
|
| 48 |
+
self.process_new_image(uploaded_image.name, Image.open(uploaded_image), kbvqa)
|
| 49 |
+
|
| 50 |
+
# Display and interact with each uploaded/selected image
|
| 51 |
+
for image_key, image_data in self.get_images_data().items():
|
| 52 |
+
st.image(image_data['image'], caption=f'Uploaded Image: {image_key[-11:]}', use_column_width=True)
|
| 53 |
+
if not image_data['analysis_done']:
|
| 54 |
+
st.text("Cool image, please click 'Analyze Image'..")
|
| 55 |
+
if st.button('Analyze Image', key=f'analyze_{image_key}'):
|
| 56 |
+
caption, detected_objects_str, image_with_boxes = self.analyze_image(image_data['image'], kbvqa)
|
| 57 |
+
self.update_image_data(image_key, caption, detected_objects_str, True)
|
| 58 |
+
|
| 59 |
+
# Initialize qa_history for each image
|
| 60 |
+
qa_history = image_data.get('qa_history', [])
|
| 61 |
+
|
| 62 |
+
if image_data['analysis_done']:
|
| 63 |
+
question = st.text_input(f"Ask a question about this image ({image_key[-11:]}):", key=f'question_{image_key}')
|
| 64 |
+
if st.button('Get Answer', key=f'answer_{image_key}'):
|
| 65 |
+
if question not in [q for q, _ in qa_history]:
|
| 66 |
+
answer = self.answer_question(image_data['caption'], image_data['detected_objects_str'], question, kbvqa)
|
| 67 |
+
self.add_to_qa_history(image_key, question, answer)
|
| 68 |
+
|
| 69 |
+
# Display Q&A history for each image
|
| 70 |
+
for q, a in qa_history:
|
| 71 |
+
st.text(f"Q: {q}\nA: {a}\n")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def run_inference(self):
|
| 76 |
+
st.title("Run Inference")
|
| 77 |
+
self.initialize_state()
|
| 78 |
+
self.set_up_widgets()
|
| 79 |
+
st.session_state['settings_changed'] = self.has_state_changed()
|
| 80 |
+
if st.session_state['settings_changed']:
|
| 81 |
+
st.warning("Model settings have changed, please reload the model, this will take a second .. ")
|
| 82 |
+
|
| 83 |
+
st.session_state.button_label = "Reload Model" if self.is_model_loaded() and self.settings_changed else "Load Model"
|
| 84 |
+
|
| 85 |
+
if st.session_state.method == "Fine-Tuned Model":
|
| 86 |
+
if st.button(st.session_state.button_label):
|
| 87 |
+
if st.session_state.button_label == "Load Model":
|
| 88 |
+
if self.is_model_loaded():
|
| 89 |
+
st.text("Model already loaded and no settings were changed:)")
|
| 90 |
+
else:
|
| 91 |
+
self.load_model()
|
| 92 |
+
else:
|
| 93 |
+
self.reload_detection_model()
|
| 94 |
+
|
| 95 |
+
if self.is_model_loaded() and st.session_state.kbvqa.all_models_loaded:
|
| 96 |
+
self.image_qa_app(self.get_model())
|
| 97 |
+
else:
|
| 98 |
+
st.write(f'Model using {st.session_state.method} is not deployed yet, will be ready later.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|