Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,15 +41,8 @@ def analyze_image(image, model):
|
|
41 |
|
42 |
|
43 |
def image_qa_app(kbvqa):
|
44 |
-
|
45 |
-
|
46 |
-
st.session_state['current_image'] = None
|
47 |
-
if 'qa_history' not in st.session_state:
|
48 |
-
st.session_state['qa_history'] = []
|
49 |
-
if 'analysis_done' not in st.session_state:
|
50 |
-
st.session_state['analysis_done'] = False
|
51 |
-
if 'answer_in_progress' not in st.session_state:
|
52 |
-
st.session_state['answer_in_progress'] = False
|
53 |
|
54 |
# Display sample images as clickable thumbnails
|
55 |
st.write("Choose from sample images:")
|
@@ -59,49 +52,42 @@ def image_qa_app(kbvqa):
|
|
59 |
image = Image.open(sample_image_path)
|
60 |
st.image(image, use_column_width=True)
|
61 |
if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
|
62 |
-
|
63 |
-
st.session_state['qa_history'] = []
|
64 |
-
st.session_state['analysis_done'] = False
|
65 |
-
st.session_state['answer_in_progress'] = False
|
66 |
|
67 |
# Image uploader
|
68 |
uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
69 |
if uploaded_image is not None:
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
st.
|
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 |
-
# Reset the answer_in_progress flag after displaying the answer
|
103 |
-
if st.session_state['answer_in_progress']:
|
104 |
-
st.session_state['answer_in_progress'] = False
|
105 |
|
106 |
def run_inference():
|
107 |
st.title("Run Inference")
|
@@ -159,6 +145,8 @@ def run_inference():
|
|
159 |
|
160 |
# Main function
|
161 |
def main():
|
|
|
|
|
162 |
st.sidebar.title("Navigation")
|
163 |
selection = st.sidebar.radio("Go to", ["Home", "Dataset Analysis", "Evaluation Results", "Run Inference", "Dissertation Report", "More Pages will follow .. "])
|
164 |
|
|
|
41 |
|
42 |
|
43 |
def image_qa_app(kbvqa):
|
44 |
+
if 'images_data' not in st.session_state:
|
45 |
+
st.session_state['images_data'] = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Display sample images as clickable thumbnails
|
48 |
st.write("Choose from sample images:")
|
|
|
52 |
image = Image.open(sample_image_path)
|
53 |
st.image(image, use_column_width=True)
|
54 |
if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
|
55 |
+
process_new_image(sample_image_path, image, kbvqa)
|
|
|
|
|
|
|
56 |
|
57 |
# Image uploader
|
58 |
uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
59 |
if uploaded_image is not None:
|
60 |
+
process_new_image(uploaded_image.name, Image.open(uploaded_image), kbvqa)
|
61 |
+
|
62 |
+
# Display and interact with each uploaded/selected image
|
63 |
+
for image_key, image_data in st.session_state['images_data'].items():
|
64 |
+
st.image(image_data['image'], caption=f'Uploaded Image: {image_key}', use_column_width=True)
|
65 |
+
if not image_data['analysis_done']:
|
66 |
+
if st.button('Analyze Image', key=f'analyze_{image_key}'):
|
67 |
+
caption, detected_objects_str = analyze_image(image_data['image'], kbvqa)
|
68 |
+
image_data['caption'] = caption
|
69 |
+
image_data['detected_objects_str'] = detected_objects_str
|
70 |
+
image_data['analysis_done'] = True
|
71 |
+
|
72 |
+
if image_data['analysis_done']:
|
73 |
+
question = st.text_input(f"Ask a question about this image ({image_key}):", key=f'question_{image_key}')
|
74 |
+
if st.button('Get Answer', key=f'answer_{image_key}'):
|
75 |
+
answer = answer_question(image_data['caption'], image_data['detected_objects_str'], question, kbvqa)
|
76 |
+
image_data['qa_history'].append((question, answer))
|
77 |
+
|
78 |
+
for q, a in image_data['qa_history']:
|
79 |
+
st.text(f"Q: {q}\nA: {a}\n")
|
80 |
+
|
81 |
+
def process_new_image(image_key, image, kbvqa):
|
82 |
+
"""Process a new image and update the session state."""
|
83 |
+
if image_key not in st.session_state['images_data']:
|
84 |
+
st.session_state['images_data'][image_key] = {
|
85 |
+
'image': image,
|
86 |
+
'caption': '',
|
87 |
+
'detected_objects_str': '',
|
88 |
+
'qa_history': [],
|
89 |
+
'analysis_done': False
|
90 |
+
}
|
|
|
|
|
|
|
|
|
91 |
|
92 |
def run_inference():
|
93 |
st.title("Run Inference")
|
|
|
145 |
|
146 |
# Main function
|
147 |
def main():
|
148 |
+
|
149 |
+
|
150 |
st.sidebar.title("Navigation")
|
151 |
selection = st.sidebar.radio("Go to", ["Home", "Dataset Analysis", "Evaluation Results", "Run Inference", "Dissertation Report", "More Pages will follow .. "])
|
152 |
|