m7mdal7aj commited on
Commit
f35e4aa
1 Parent(s): 739a2d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -159
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
- # Set up the sidebar navigation
42
- st.sidebar.title("Navigation")
43
- selection = st.sidebar.radio("Go to", ["Home", "Dataset Analysis", "Evaluation Results", "Run Inference", "Dissertation Report", ])
44
-
45
- # Set up the main page content based on navigation selection
46
- if selection == "Home":
47
- st.title("MultiModal Learning for Knowledg-Based Visual Question Answering")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  st.write("Home page content goes here...")
49
- # You can include more content for the home page here
50
 
51
- elif selection == "Dissertation Report":
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
- elif selection == "Evaluation Results":
63
  st.title("Evaluation Results")
64
  st.write("This is a Place Holder until the contents are uploaded.")
65
 
66
- elif selection == "Dataset Analysis":
67
  st.title("OK-VQA Dataset Analysis")
68
  st.write("This is a Place Holder until the contents are uploaded.")
69
 
70
- elif selection == "Run Inference":
71
- st.title("Run Inference")
72
- st.write("This page allows you to run the space for inference.")
73
- # You would include your inference code here
74
- # For example, if you have a form to collect user input for the model:
75
- user_input = st.text_input("Enter your text here...")
76
- if st.button("Run"):
77
- # Call your model inference function
78
- # result = run_inference(user_input)
79
- # st.write(result)
80
- pass # Replace pass with your inference code
81
-
82
- # Other pages and functionality would be added in a similar manner.
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
- st.title("Image Question Answering")
94
-
95
- # File uploader for the image
96
- image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
97
-
98
- # Text input for the question
99
- question = st.text_input("Enter your question about the image:")
100
-
101
- if st.button('Generate Caption'):
102
- free_gpu_resources()
103
- if image is not None:
104
- # Display the image
105
- st.image(image, use_column_width=True)
106
- caption = get_caption(image)
107
- st.write(caption)
108
- free_gpu_resources()
109
-
110
- else:
111
- st.write("Please upload an image and enter a question.")
112
-
113
- if st.button("Get Answer"):
114
- if image is not None and question:
115
- # Display the image
116
- st.image(image, use_column_width=True)
117
- # Get and display the answer
118
- model, processor = load_caption_model()
119
- answer = answer_question(image, question, model, processor)
120
- st.write(answer)
121
- else:
122
- st.write("Please upload an image and enter a question.")
123
-
124
-
125
-
126
-
127
-
128
-
129
- # Object Detection
130
-
131
- # Object Detection UI in the sidebar
132
- st.sidebar.title("Object Detection")
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()