m7mdal7aj commited on
Commit
ef4d7f5
1 Parent(s): f05f048

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -48
app.py CHANGED
@@ -41,15 +41,8 @@ def analyze_image(image, model):
41
 
42
 
43
  def image_qa_app(kbvqa):
44
- # Initialize session state for storing the current image and its Q&A history.
45
- if 'current_image' not in st.session_state:
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
- st.session_state['current_image'] = image
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
- image = Image.open(uploaded_image)
71
- st.session_state['current_image'] = image
72
- st.session_state['qa_history'] = []
73
- st.session_state['analysis_done'] = False
74
- st.session_state['answer_in_progress'] = False
75
-
76
-
77
-
78
- # Display the current image (unaltered)
79
- if st.session_state.get('current_image'):
80
- st.image(st.session_state['current_image'], caption='Uploaded Image.', use_column_width=True)
81
-
82
- # Analyze Image button
83
- if st.session_state.get('current_image') and not st.session_state['analysis_done']:
84
- if st.button('Analyze Image'):
85
- # Perform analysis on the image
86
- caption, detected_objects_str = analyze_image(st.session_state['current_image'], kbvqa)
87
- st.session_state['analysis_done'] = True
88
- st.session_state['processed_image'] = copy.deepcopy(st.session_state['current_image'])
89
-
90
- # Get Answer button
91
- if st.session_state['analysis_done'] and not st.session_state['answer_in_progress']:
92
- question = st.text_input("Ask a question about this image:")
93
- if st.button('Get Answer'):
94
- st.session_state['answer_in_progress'] = True
95
- answer = answer_question(caption, detected_objects_str, question, model=kbvqa)
96
- st.session_state['qa_history'].append((question, answer))
97
-
98
- # Display all Q&A
99
- for q, a in st.session_state['qa_history']:
100
- st.text(f"Q: {q}\nA: {a}\n")
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