Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,47 +11,49 @@ from my_model.captioner.image_captioning import get_caption
|
|
11 |
from my_model.utilities import free_gpu_resources
|
12 |
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
def answer_question(image, question, model, processor):
|
17 |
-
|
18 |
-
|
19 |
-
image = Image.open(image)
|
20 |
-
|
21 |
-
inputs = processor(image, question, return_tensors="pt").to("cuda", torch.float16)
|
22 |
-
|
23 |
-
if isinstance(model, torch.nn.DataParallel):
|
24 |
-
# Use the 'module' attribute to access the original model
|
25 |
-
out = model.module.generate(**inputs, max_length=100, min_length=20)
|
26 |
-
else:
|
27 |
-
|
28 |
-
out = model.generate(**inputs, max_length=100, min_length=20)
|
29 |
-
|
30 |
-
answer = processor.decode(out[0], skip_special_tokens=True).strip()
|
31 |
-
return answer
|
32 |
-
|
33 |
-
|
34 |
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
st.write("Home page content goes here...")
|
49 |
-
# You can include more content for the home page here
|
50 |
|
51 |
-
|
52 |
st.title("Dissertation Report")
|
53 |
st.write("Click the link below to view the PDF.")
|
54 |
-
# Example to display a link to a PDF
|
55 |
st.download_button(
|
56 |
label="Download PDF",
|
57 |
data=open("Files/Dissertation Report.pdf", "rb"),
|
@@ -59,133 +61,74 @@ elif selection == "Dissertation Report":
|
|
59 |
mime="application/octet-stream"
|
60 |
)
|
61 |
|
62 |
-
|
63 |
st.title("Evaluation Results")
|
64 |
st.write("This is a Place Holder until the contents are uploaded.")
|
65 |
|
66 |
-
|
67 |
st.title("OK-VQA Dataset Analysis")
|
68 |
st.write("This is a Place Holder until the contents are uploaded.")
|
69 |
|
70 |
-
|
71 |
-
st.title("
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
#
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
# Dropdown to select the model
|
134 |
-
detect_model = st.sidebar.selectbox("Choose a model for object detection:", ["detic", "yolov5"])
|
135 |
-
# Slider for threshold with default values based on the model
|
136 |
-
threshold = st.sidebar.slider("Select Detection Threshold", 0.1, 0.9, 0.2 if detect_model == "yolov5" else 0.4)
|
137 |
-
# Button to trigger object detection
|
138 |
-
detect_button = st.sidebar.button("Detect Objects")
|
139 |
-
|
140 |
-
|
141 |
-
def perform_object_detection(image, model_name, threshold):
|
142 |
-
"""
|
143 |
-
Perform object detection on the given image using the specified model and threshold.
|
144 |
-
|
145 |
-
Args:
|
146 |
-
image (PIL.Image): The image on which to perform object detection.
|
147 |
-
model_name (str): The name of the object detection model to use.
|
148 |
-
threshold (float): The threshold for object detection.
|
149 |
-
|
150 |
-
Returns:
|
151 |
-
PIL.Image, str: The image with drawn bounding boxes and a string of detected objects.
|
152 |
-
"""
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
# Perform object detection and draw bounding boxes
|
157 |
-
|
158 |
-
processed_image, detected_objects = detect_and_draw_objects(image, model_name, threshold)
|
159 |
-
|
160 |
-
return processed_image, detected_objects
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
# Check if the 'Detect Objects' button was clicked
|
165 |
-
if detect_button:
|
166 |
-
if image is not None:
|
167 |
-
# Open the uploaded image
|
168 |
-
try:
|
169 |
-
image = Image.open(image)
|
170 |
-
|
171 |
-
# Display the original image
|
172 |
-
st.image(image, use_column_width=True, caption="Original Image")
|
173 |
-
|
174 |
-
# Perform object detection
|
175 |
-
processed_image, detected_objects = perform_object_detection(image, detect_model, threshold)
|
176 |
-
|
177 |
-
|
178 |
-
# Display the image with detected objects
|
179 |
-
st.image(processed_image, use_column_width=True, caption="Image with Detected Objects")
|
180 |
-
|
181 |
-
# Display the detected objects as text
|
182 |
-
st.write(detected_objects)
|
183 |
-
|
184 |
-
|
185 |
-
except Exception as e:
|
186 |
-
st.error(f"Error loading image: {e}")
|
187 |
-
|
188 |
-
else:
|
189 |
-
st.write("Please upload an image for object detection.")
|
190 |
-
|
191 |
-
|
|
|
11 |
from my_model.utilities import free_gpu_resources
|
12 |
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Placeholder for undefined functions
|
16 |
+
def load_caption_model():
|
17 |
+
st.write("Placeholder for load_caption_model function")
|
18 |
+
return None, None
|
19 |
|
20 |
+
def answer_question(image, question, model, processor):
|
21 |
+
return "Placeholder answer for the question"
|
22 |
+
|
23 |
+
def detect_and_draw_objects(image, model_name, threshold):
|
24 |
+
return image, "Detected objects"
|
25 |
+
|
26 |
+
def get_caption(image):
|
27 |
+
return "Generated caption for the image"
|
28 |
+
|
29 |
+
def free_gpu_resources():
|
30 |
+
pass
|
31 |
+
|
32 |
+
# Main function
|
33 |
+
def main():
|
34 |
+
st.sidebar.title("Navigation")
|
35 |
+
selection = st.sidebar.radio("Go to", ["Home", "Dataset Analysis", "Evaluation Results", "Run Inference", "Dissertation Report", "Object Detection"])
|
36 |
+
|
37 |
+
if selection == "Home":
|
38 |
+
display_home()
|
39 |
+
elif selection == "Dissertation Report":
|
40 |
+
display_dissertation_report()
|
41 |
+
elif selection == "Evaluation Results":
|
42 |
+
display_evaluation_results()
|
43 |
+
elif selection == "Dataset Analysis":
|
44 |
+
display_dataset_analysis()
|
45 |
+
elif selection == "Run Inference":
|
46 |
+
run_inference()
|
47 |
+
elif selection == "Object Detection":
|
48 |
+
run_object_detection()
|
49 |
+
|
50 |
+
def display_home():
|
51 |
+
st.title("MultiModal Learning for Knowledge-Based Visual Question Answering")
|
52 |
st.write("Home page content goes here...")
|
|
|
53 |
|
54 |
+
def display_dissertation_report():
|
55 |
st.title("Dissertation Report")
|
56 |
st.write("Click the link below to view the PDF.")
|
|
|
57 |
st.download_button(
|
58 |
label="Download PDF",
|
59 |
data=open("Files/Dissertation Report.pdf", "rb"),
|
|
|
61 |
mime="application/octet-stream"
|
62 |
)
|
63 |
|
64 |
+
def display_evaluation_results():
|
65 |
st.title("Evaluation Results")
|
66 |
st.write("This is a Place Holder until the contents are uploaded.")
|
67 |
|
68 |
+
def display_dataset_analysis():
|
69 |
st.title("OK-VQA Dataset Analysis")
|
70 |
st.write("This is a Place Holder until the contents are uploaded.")
|
71 |
|
72 |
+
def run_inference():
|
73 |
+
st.title("Image-based Q&A App")
|
74 |
+
# Image-based Q&A functionality
|
75 |
+
image_qa_app()
|
76 |
+
|
77 |
+
def run_object_detection():
|
78 |
+
st.title("Object Detection")
|
79 |
+
# Object Detection functionality
|
80 |
+
# ... Implement your code for this section ...
|
81 |
+
|
82 |
+
def image_qa_app():
|
83 |
+
# Initialize session state for storing images and their Q&A histories
|
84 |
+
if 'images_qa_history' not in st.session_state:
|
85 |
+
st.session_state['images_qa_history'] = []
|
86 |
+
|
87 |
+
# Button to clear all data
|
88 |
+
if st.button('Clear All'):
|
89 |
+
st.session_state['images_qa_history'] = []
|
90 |
+
st.experimental_rerun()
|
91 |
+
|
92 |
+
# Image uploader
|
93 |
+
uploaded_image = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
|
94 |
+
|
95 |
+
if uploaded_image is not None:
|
96 |
+
image = Image.open(uploaded_image)
|
97 |
+
current_image_key = uploaded_image.name # Use image name as a unique key
|
98 |
+
|
99 |
+
# Check if the image is already in the history
|
100 |
+
if not any(info['image_key'] == current_image_key for info in st.session_state['images_qa_history']):
|
101 |
+
st.session_state['images_qa_history'].append({
|
102 |
+
'image_key': current_image_key,
|
103 |
+
'image': image,
|
104 |
+
'qa_history': []
|
105 |
+
})
|
106 |
+
|
107 |
+
# Display all images and their Q&A histories
|
108 |
+
for image_info in st.session_state['images_qa_history']:
|
109 |
+
st.image(image_info['image'], caption='Uploaded Image.', use_column_width=True)
|
110 |
+
for q, a in image_info['qa_history']:
|
111 |
+
st.text(f"Q: {q}\nA: {a}\n")
|
112 |
+
|
113 |
+
# If the current image is being processed
|
114 |
+
if image_info['image_key'] == current_image_key:
|
115 |
+
# Unique keys for each widget
|
116 |
+
question_key = f"question_{current_image_key}"
|
117 |
+
button_key = f"button_{current_image_key}"
|
118 |
+
|
119 |
+
# Question input for the current image
|
120 |
+
question = st.text_input("Ask a question about this image:", key=question_key)
|
121 |
+
|
122 |
+
# Get Answer button for the current image
|
123 |
+
if st.button('Get Answer', key=button_key):
|
124 |
+
# Process the image and question
|
125 |
+
answer = get_answer(image_info['image'], question) # Implement this function
|
126 |
+
image_info['qa_history'].append((question, answer))
|
127 |
+
st.experimental_rerun() # Rerun to update the display
|
128 |
+
|
129 |
+
def get_answer(image, question):
|
130 |
+
# Implement the logic to process the image and question, and return the answer
|
131 |
+
return "Sample answer based on the image and question."
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|