Spaces:
Sleeping
Sleeping
Create tabs/run_inference.py
Browse files- my_model/tabs/run_inference.py +0 -188
my_model/tabs/run_inference.py
CHANGED
|
@@ -1,188 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
import bitsandbytes
|
| 4 |
-
import accelerate
|
| 5 |
-
import scipy
|
| 6 |
-
import copy
|
| 7 |
-
from PIL import Image
|
| 8 |
-
import torch.nn as nn
|
| 9 |
-
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.utilities.st_utils import UIManager, StateManager
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def answer_question(caption, detected_objects_str, question, model):
|
| 19 |
-
|
| 20 |
-
answer = model.generate_answer(question, caption, detected_objects_str)
|
| 21 |
-
return answer
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Sample images (assuming these are paths to your sample images)
|
| 25 |
-
sample_images = ["Files/sample1.jpg", "Files/sample2.jpg", "Files/sample3.jpg",
|
| 26 |
-
"Files/sample4.jpg", "Files/sample5.jpg", "Files/sample6.jpg",
|
| 27 |
-
"Files/sample7.jpg"]
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def analyze_image(image, model):
|
| 32 |
-
|
| 33 |
-
img = copy.deepcopy(image) # we dont wanna apply changes to the original image
|
| 34 |
-
caption = model.get_caption(img)
|
| 35 |
-
image_with_boxes, detected_objects_str = model.detect_objects(img)
|
| 36 |
-
st.text("I am ready, let's talk!")
|
| 37 |
-
free_gpu_resources()
|
| 38 |
-
|
| 39 |
-
return caption, detected_objects_str, image_with_boxes
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def image_qa_app(kbvqa):
|
| 43 |
-
if 'images_data' not in st.session_state:
|
| 44 |
-
st.session_state['images_data'] = {}
|
| 45 |
-
|
| 46 |
-
# Display sample images as clickable thumbnails
|
| 47 |
-
st.write("Choose from sample images:")
|
| 48 |
-
cols = st.columns(len(sample_images))
|
| 49 |
-
for idx, sample_image_path in enumerate(sample_images):
|
| 50 |
-
with cols[idx]:
|
| 51 |
-
image = Image.open(sample_image_path)
|
| 52 |
-
st.image(image, use_column_width=True)
|
| 53 |
-
if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
|
| 54 |
-
process_new_image(sample_image_path, image, kbvqa)
|
| 55 |
-
|
| 56 |
-
# Image uploader
|
| 57 |
-
uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
| 58 |
-
if uploaded_image is not None:
|
| 59 |
-
process_new_image(uploaded_image.name, Image.open(uploaded_image), kbvqa)
|
| 60 |
-
|
| 61 |
-
# Display and interact with each uploaded/selected image
|
| 62 |
-
for image_key, image_data in st.session_state['images_data'].items():
|
| 63 |
-
st.image(image_data['image'], caption=f'Uploaded Image: {image_key[-11:]}', use_column_width=True)
|
| 64 |
-
if not image_data['analysis_done']:
|
| 65 |
-
st.text("Cool image, please click 'Analyze Image'..")
|
| 66 |
-
if st.button('Analyze Image', key=f'analyze_{image_key}'):
|
| 67 |
-
caption, detected_objects_str, image_with_boxes = analyze_image(image_data['image'], kbvqa) # we can use the image_with_boxes later if we want to show it.
|
| 68 |
-
image_data['caption'] = caption
|
| 69 |
-
image_data['detected_objects_str'] = detected_objects_str
|
| 70 |
-
image_data['analysis_done'] = True
|
| 71 |
-
|
| 72 |
-
# Initialize qa_history for each image
|
| 73 |
-
qa_history = image_data.get('qa_history', [])
|
| 74 |
-
|
| 75 |
-
if image_data['analysis_done']:
|
| 76 |
-
question = st.text_input(f"Ask a question about this image ({image_key[-11:]}):", key=f'question_{image_key}')
|
| 77 |
-
if st.button('Get Answer', key=f'answer_{image_key}'):
|
| 78 |
-
if question not in [q for q, _ in qa_history]:
|
| 79 |
-
answer = answer_question(image_data['caption'], image_data['detected_objects_str'], question, kbvqa)
|
| 80 |
-
qa_history.append((question, answer))
|
| 81 |
-
image_data['qa_history'] = qa_history
|
| 82 |
-
else:
|
| 83 |
-
st.info("This question has already been asked.")
|
| 84 |
-
|
| 85 |
-
# Display Q&A history for each image
|
| 86 |
-
for q, a in qa_history:
|
| 87 |
-
st.text(f"Q: {q}\nA: {a}\n")
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
def process_new_image(image_key, image, kbvqa):
|
| 91 |
-
"""Process a new image and update the session state."""
|
| 92 |
-
if image_key not in st.session_state['images_data']:
|
| 93 |
-
st.session_state['images_data'][image_key] = {
|
| 94 |
-
'image': image,
|
| 95 |
-
'caption': '',
|
| 96 |
-
'detected_objects_str': '',
|
| 97 |
-
'qa_history': [],
|
| 98 |
-
'analysis_done': False
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
def run_inference():
|
| 102 |
-
st.title("Run Inference")
|
| 103 |
-
st.write("Please note that this is not a general purpose model, it is specifically trained on OK-VQA dataset and is designed to give direct and short answers to the given questions.")
|
| 104 |
-
|
| 105 |
-
method = st.selectbox(
|
| 106 |
-
"Choose a method:",
|
| 107 |
-
["Fine-Tuned Model", "In-Context Learning (n-shots)"],
|
| 108 |
-
index=0
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
detection_model = st.selectbox(
|
| 112 |
-
"Choose a model for objects detection:",
|
| 113 |
-
["yolov5", "detic"],
|
| 114 |
-
index=1 # "detic" is selected by default
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
default_confidence = 0.2 if detection_model == "yolov5" else 0.4
|
| 118 |
-
confidence_level = st.slider(
|
| 119 |
-
"Select minimum detection confidence level",
|
| 120 |
-
min_value=0.1,
|
| 121 |
-
max_value=0.9,
|
| 122 |
-
value=default_confidence,
|
| 123 |
-
step=0.1
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
if 'model_settings' not in st.session_state:
|
| 127 |
-
st.session_state['model_settings'] = {'detection_model': detection_model, 'confidence_level': confidence_level}
|
| 128 |
-
|
| 129 |
-
settings_changed = (st.session_state['model_settings']['detection_model'] != detection_model or
|
| 130 |
-
st.session_state['model_settings']['confidence_level'] != confidence_level)
|
| 131 |
-
|
| 132 |
-
need_model_reload = settings_changed and 'kbvqa' in st.session_state and st.session_state['kbvqa'] is not None
|
| 133 |
-
|
| 134 |
-
if need_model_reload:
|
| 135 |
-
st.text("Model Settings have changed, please reload the model, this will take no time :)")
|
| 136 |
-
|
| 137 |
-
button_label = "Reload Model" if need_model_reload else "Load Model"
|
| 138 |
-
|
| 139 |
-
if method == "Fine-Tuned Model":
|
| 140 |
-
if 'kbvqa' not in st.session_state:
|
| 141 |
-
st.session_state['kbvqa'] = None
|
| 142 |
-
|
| 143 |
-
if st.button(button_label):
|
| 144 |
-
|
| 145 |
-
free_gpu_resources()
|
| 146 |
-
if st.session_state['kbvqa'] is not None:
|
| 147 |
-
if not settings_changed:
|
| 148 |
-
st.write("Model already loaded.")
|
| 149 |
-
else:
|
| 150 |
-
free_gpu_resources()
|
| 151 |
-
detection_model = st.session_state['model_settings']['detection_model']
|
| 152 |
-
confidence_level = st.session_state['model_settings']['confidence_level']
|
| 153 |
-
prepare_kbvqa_model(detection_model, only_reload_detection_model=True) # only reload detection model with new settings
|
| 154 |
-
st.session_state['kbvqa'].detection_confidence = confidence_level
|
| 155 |
-
free_gpu_resources()
|
| 156 |
-
else:
|
| 157 |
-
st.text("Loading the model will take no more than a few minutes . .")
|
| 158 |
-
st.session_state['kbvqa'] = prepare_kbvqa_model(detection_model)
|
| 159 |
-
st.session_state['kbvqa'].detection_confidence = confidence_level
|
| 160 |
-
st.session_state['model_settings'] = {'detection_model': detection_model, 'confidence_level': confidence_level}
|
| 161 |
-
st.write("Model is ready for inference.")
|
| 162 |
-
free_gpu_resources()
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
if st.session_state['kbvqa']:
|
| 167 |
-
display_model_settings()
|
| 168 |
-
display_session_state()
|
| 169 |
-
image_qa_app(st.session_state['kbvqa'])
|
| 170 |
-
|
| 171 |
-
else:
|
| 172 |
-
st.write('Model is not ready yet, will be updated later.')
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
def display_model_settings():
|
| 176 |
-
st.write("### Current Model Settings:")
|
| 177 |
-
st.table(pd.DataFrame(st.session_state['model_settings'], index=[0]))
|
| 178 |
-
|
| 179 |
-
def display_session_state():
|
| 180 |
-
st.write("### Current Session State:")
|
| 181 |
-
# Convert session state to a list of dictionaries, each representing a row
|
| 182 |
-
data = [{'Key': key, 'Value': str(value)} for key, value in st.session_state.items()]
|
| 183 |
-
# Create a DataFrame from the list
|
| 184 |
-
df = pd.DataFrame(data)
|
| 185 |
-
st.table(df)
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|