m7mdal7aj commited on
Commit
9d4c7bc
1 Parent(s): c6252cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -48
app.py CHANGED
@@ -44,61 +44,46 @@ def image_qa_and_object_detection():
44
  object_detection_app()
45
 
46
  def image_qa_app():
47
- # Initialize session state for storing images and their Q&A histories
48
- if 'images_qa_history' not in st.session_state:
49
- st.session_state['images_qa_history'] = []
50
-
51
- # Button to clear all data
52
- if st.button('Clear All'):
53
- st.session_state['images_qa_history'] = []
54
- st.experimental_rerun()
55
-
56
- # Image uploader
57
- uploaded_image = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
58
-
59
- # Display sample images
60
- st.write("Or choose from sample images:")
61
  for idx, sample_image_path in enumerate(sample_images):
62
- if st.button(f"Use Sample Image {idx+1}", key=f"sample_{idx}"):
63
- uploaded_image = Image.open(sample_image_path)
64
- process_uploaded_image(uploaded_image)
 
 
65
 
 
 
66
  if uploaded_image is not None:
67
- image = Image.open(uploaded_image)
68
- process_uploaded_image(image)
69
-
70
- def process_uploaded_image(image):
71
- current_image_key = image.filename # Use image filename as a unique key
72
- # Check if the image is already in the history
73
- if not any(info['image_key'] == current_image_key for info in st.session_state['images_qa_history']):
74
- st.session_state['images_qa_history'].append({
75
- 'image_key': current_image_key,
76
- 'image': image,
77
- 'qa_history': []
78
- })
79
-
80
- # Display all images and their Q&A histories
81
- for image_info in st.session_state['images_qa_history']:
82
- st.image(image_info['image'], caption='Uploaded Image.', use_column_width=True)
83
- for q, a in image_info['qa_history']:
84
- st.text(f"Q: {q}\nA: {a}\n")
85
 
86
- # If the current image is being processed
87
- if image_info['image_key'] == current_image_key:
88
- # Unique keys for each widget
89
- question_key = f"question_{current_image_key}"
90
- button_key = f"button_{current_image_key}"
91
 
92
- # Question input for the current image
93
- question = st.text_input("Ask a question about this image:", key=question_key)
 
 
 
94
 
95
- # Get Answer button for the current image
96
- if st.button('Get Answer', key=button_key):
97
- # Process the image and question
98
- answer = answer_question(image_info['image'], question, None, None) # Implement this function
99
- image_info['qa_history'].append((question, answer))
100
- st.experimental_rerun() # Rerun to update the display
101
 
 
102
  # Object Detection App
103
  def object_detection_app():
104
  # ... Implement your code for object detection ...
 
44
  object_detection_app()
45
 
46
  def image_qa_app():
47
+ # Initialize session state for storing the current image and its Q&A history
48
+ if 'current_image' not in st.session_state:
49
+ st.session_state['current_image'] = None
50
+ if 'qa_history' not in st.session_state:
51
+ st.session_state['qa_history'] = []
52
+
53
+ # Display sample images as clickable thumbnails
54
+ st.write("Choose from sample images:")
55
+ cols = st.columns(len(sample_images))
 
 
 
 
 
56
  for idx, sample_image_path in enumerate(sample_images):
57
+ with cols[idx]:
58
+ image = Image.open(sample_image_path)
59
+ if st.image(image, use_column_width=True):
60
+ st.session_state['current_image'] = image
61
+ st.session_state['qa_history'] = []
62
 
63
+ # Image uploader
64
+ uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
65
  if uploaded_image is not None:
66
+ st.session_state['current_image'] = Image.open(uploaded_image)
67
+ st.session_state['qa_history'] = []
68
+
69
+ # Display the current image
70
+ if st.session_state['current_image'] is not None:
71
+ st.image(st.session_state['current_image'], caption='Uploaded Image.', use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ # Question input
74
+ question = st.text_input("Ask a question about this image:")
 
 
 
75
 
76
+ # Get Answer button
77
+ if st.button('Get Answer'):
78
+ # Process the question
79
+ answer = answer_question(st.session_state['current_image'], question)
80
+ st.session_state['qa_history'].append((question, answer))
81
 
82
+ # Display all Q&A
83
+ for q, a in st.session_state['qa_history']:
84
+ st.text(f"Q: {q}\nA: {a}\n")
 
 
 
85
 
86
+
87
  # Object Detection App
88
  def object_detection_app():
89
  # ... Implement your code for object detection ...