m7mdal7aj commited on
Commit
d40826b
1 Parent(s): 356a130

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -34
app.py CHANGED
@@ -29,29 +29,16 @@ sample_images = ["Files/sample1.jpg", "Files/sample2.jpg", "Files/sample3.jpg",
29
  "Files/sample4.jpg", "Files/sample5.jpg", "Files/sample6.jpg",
30
  "Files/sample7.jpg"]
31
 
32
- def run_inference():
33
- st.title("Run Inference")
34
-
35
- # Initialize session state for the model
36
- if 'kbvqa' not in st.session_state:
37
- st.session_state['kbvqa'] = None
38
-
39
- # Button to load KBVQA models
40
- if st.button('Load KBVQA Model'):
41
- if st.session_state['kbvqa'] is not None:
42
- st.write("Model already loaded.")
43
- else:
44
- # Call the function to load models and show progress
45
- st.session_state['kbvqa'] = prepare_kbvqa_model('yolov5') # Replace with your model
46
 
47
- if st.session_state['kbvqa']:
48
- st.write("Model is ready for inference.")
49
- else:
50
- st.write("Please load the model first")
51
-
52
- if st.session_state['kbvqa']:
53
- image_qa_app(st.session_state['kbvqa'])
54
 
 
 
 
 
 
 
 
 
55
 
56
  def image_qa_app(kbvqa):
57
  # Initialize session state for storing the current image and its Q&A history
@@ -59,43 +46,71 @@ def image_qa_app(kbvqa):
59
  st.session_state['current_image'] = None
60
  if 'qa_history' not in st.session_state:
61
  st.session_state['qa_history'] = []
 
 
62
 
63
  # Display sample images as clickable thumbnails
64
  st.write("Choose from sample images:")
65
  cols = st.columns(len(sample_images))
66
  for idx, sample_image_path in enumerate(sample_images):
67
  with cols[idx]:
68
- # Display each sample image with a button
69
  image = Image.open(sample_image_path)
70
  st.image(image, use_column_width=True)
71
  if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
72
  st.session_state['current_image'] = image
73
- st.session_state['processed_image'] = copy.deepcopy(image)
74
  st.session_state['qa_history'] = []
 
75
 
76
  # Image uploader
77
  uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
78
  if uploaded_image is not None:
79
  image = Image.open(uploaded_image)
80
  st.session_state['current_image'] = image
81
- st.session_state['processed_image'] = copy.deepcopy(image)
82
  st.session_state['qa_history'] = []
 
 
 
 
 
 
 
 
83
 
84
  # Display the current image (unaltered)
85
- if st.session_state.get('current_image') is not None:
86
  st.image(st.session_state['current_image'], caption='Uploaded Image.', use_column_width=True)
87
 
88
- # Question input and processing
 
89
  question = st.text_input("Ask a question about this image:")
90
  if st.button('Get Answer'):
91
- processed_image = st.session_state.get('processed_image')
92
- if processed_image:
93
- answer = answer_question(processed_image, question, model=kbvqa)
94
- st.session_state['qa_history'].append((question, answer))
95
-
96
- # Display all Q&A
97
- for q, a in st.session_state['qa_history']:
98
- st.text(f"Q: {q}\nA: {a}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
 
101
  # Main function
 
29
  "Files/sample4.jpg", "Files/sample5.jpg", "Files/sample6.jpg",
30
  "Files/sample7.jpg"]
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
 
 
 
 
 
 
33
 
34
+ def analyze_image(image, model):
35
+ # Placeholder for your analysis function
36
+ # This function should prepare captions, detect objects, etc.
37
+ # For example:
38
+ # caption = model.get_caption(image)
39
+ # detected_objects = model.detect_objects(image)
40
+ # return caption, detected_objects
41
+ pass
42
 
43
  def image_qa_app(kbvqa):
44
  # Initialize session state for storing the current image and its Q&A history
 
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
 
52
  # Display sample images as clickable thumbnails
53
  st.write("Choose from sample images:")
54
  cols = st.columns(len(sample_images))
55
  for idx, sample_image_path in enumerate(sample_images):
56
  with cols[idx]:
 
57
  image = Image.open(sample_image_path)
58
  st.image(image, use_column_width=True)
59
  if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
60
  st.session_state['current_image'] = image
 
61
  st.session_state['qa_history'] = []
62
+ st.session_state['analysis_done'] = False
63
 
64
  # Image uploader
65
  uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
66
  if uploaded_image is not None:
67
  image = Image.open(uploaded_image)
68
  st.session_state['current_image'] = image
 
69
  st.session_state['qa_history'] = []
70
+ st.session_state['analysis_done'] = False
71
+
72
+ # Analyze Image button
73
+ if st.session_state.get('current_image') and not st.session_state['analysis_done']:
74
+ if st.button('Analyze Image'):
75
+ # Perform analysis on the image
76
+ analyze_image(st.session_state['current_image'], kbvqa)
77
+ st.session_state['analysis_done'] = True
78
 
79
  # Display the current image (unaltered)
80
+ if st.session_state.get('current_image'):
81
  st.image(st.session_state['current_image'], caption='Uploaded Image.', use_column_width=True)
82
 
83
+ # Get Answer button
84
+ if st.session_state['analysis_done']:
85
  question = st.text_input("Ask a question about this image:")
86
  if st.button('Get Answer'):
87
+ answer = answer_question(st.session_state['current_image'], question, model=kbvqa)
88
+ st.session_state['qa_history'].append((question, answer))
89
+
90
+ # Display all Q&A
91
+ for q, a in st.session_state['qa_history']:
92
+ st.text(f"Q: {q}\nA: {a}\n")
93
+
94
+ def run_inference():
95
+ st.title("Run Inference")
96
+
97
+ # Initialize session state for the model
98
+ if 'kbvqa' not in st.session_state:
99
+ st.session_state['kbvqa'] = None
100
+
101
+ # Button to load KBVQA models
102
+ if st.button('Load KBVQA Model'):
103
+ if st.session_state['kbvqa'] is not None:
104
+ st.write("Model already loaded.")
105
+ else:
106
+ # Call the function to load models and show progress
107
+ st.session_state['kbvqa'] = prepare_kbvqa_model('yolov5') # Replace with your model
108
+
109
+ if st.session_state['kbvqa']:
110
+ st.write("Model is ready for inference.")
111
+
112
+ if st.session_state['kbvqa']:
113
+ image_qa_app(st.session_state['kbvqa'])
114
 
115
 
116
  # Main function