Madhuri commited on
Commit
6fda2a0
1 Parent(s): 78c15e2

Add gallery to the page for selecting image easily.

Browse files
Files changed (4) hide show
  1. audiobot.py +52 -10
  2. chatbot.py +47 -5
  3. images/image10.jpg +0 -0
  4. requirements.txt +1 -0
audiobot.py CHANGED
@@ -3,10 +3,31 @@ import streamlit as st
3
  from PIL import Image
4
  from bokeh.models.widgets import Button
5
  from bokeh.models import CustomJS
 
6
  from streamlit_bokeh_events import streamlit_bokeh_events
7
  from bokeh.models.widgets.buttons import Button
8
  import time
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def show():
12
  st.session_state.audio_answer = ''
@@ -19,16 +40,35 @@ def show():
19
  </i></h4>
20
  ''', unsafe_allow_html=True)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  weights = [5, 2]
23
  image_col, audio_col = st.columns(weights)
24
  with image_col:
25
- upload_pic = st.file_uploader('Choose an image...', type=[
26
- 'jpg', 'png', 'jpeg'], accept_multiple_files=False)
27
- if upload_pic is not None:
28
- st.session_state.image = Image.open(upload_pic)
29
- st.image(upload_pic, use_column_width='auto')
 
 
30
  else:
31
  st.session_state.image = None
 
 
32
 
33
  with audio_col:
34
  welcome_text = 'Hello and Welcome. I have been trained as visual question answering model. You are welcome to look at any image and ask me any questions about it. I will do my best to provide the most accurate information possible based on my expertise. Select an image of interest by pressing the browse files button. Now use the Ask question button to ask a question. Please feel free to ask me any questions about this image. Now. to get my answer. press the Get answer button.'
@@ -75,11 +115,13 @@ def show():
75
 
76
  if result:
77
  if 'GET_TEXT' in result:
78
- with st.spinner('Preparing answer...'):
79
- while 'predictor' not in st.session_state:
80
- time.sleep(2)
81
- st.session_state.audio_answer = st.session_state.predictor.predict_answer_from_text(
82
- st.session_state.image, result.get('GET_TEXT'))
 
 
83
 
84
  tts_button = Button(label='Get Answer', width=100)
85
  tts_button.js_on_event('button_click', CustomJS(code=f"""
3
  from PIL import Image
4
  from bokeh.models.widgets import Button
5
  from bokeh.models import CustomJS
6
+ from st_clickable_images import clickable_images
7
  from streamlit_bokeh_events import streamlit_bokeh_events
8
  from bokeh.models.widgets.buttons import Button
9
  import time
10
+ from os.path import *
11
+ from os import listdir
12
+ import base64
13
 
14
+ def update_gallery_images():
15
+ if 'gallery' not in st.session_state:
16
+ st.session_state.gallery = []
17
+ st.session_state.gallery_images = []
18
+ image_path = join(dirname(abspath(__file__)), 'images')
19
+ for f in listdir(image_path):
20
+ if f.startswith('image'):
21
+ with open(join(image_path, f), "rb") as image:
22
+ encoded = base64.b64encode(image.read()).decode()
23
+ st.session_state.gallery.append(
24
+ f"data:image/jpeg;base64,{encoded}")
25
+ st.session_state.gallery_images.append(join(image_path, f))
26
+
27
+
28
+ def upload_image_callback():
29
+ st.session_state.uploaded_image = st.session_state.uploader
30
+ st.session_state.input = ''
31
 
32
  def show():
33
  st.session_state.audio_answer = ''
40
  </i></h4>
41
  ''', unsafe_allow_html=True)
42
 
43
+ update_gallery_images()
44
+ if 'gallery' in st.session_state:
45
+ clicked = clickable_images(
46
+ st.session_state.gallery,
47
+ titles=[f"Image #{str(i)}" for i in range(2)],
48
+ div_style={"display": "flex",
49
+ "justify-content": "center", "flex-wrap": "nowrap"},
50
+ img_style={"margin": "5px", "height": "100px"},
51
+ )
52
+
53
+ if 'clicked' not in st.session_state or st.session_state.clicked != clicked:
54
+ st.session_state.uploaded_image = st.session_state.gallery_images[clicked]
55
+ st.session_state.clicked = clicked
56
+ st.session_state.input = ''
57
+
58
  weights = [5, 2]
59
  image_col, audio_col = st.columns(weights)
60
  with image_col:
61
+ st.file_uploader('Select an image...', type=[
62
+ 'jpg', 'jpeg'], accept_multiple_files=False,
63
+ on_change=upload_image_callback, key='uploader')
64
+
65
+ if st.session_state.uploaded_image is not None:
66
+ st.session_state.image = Image.open(st.session_state.uploaded_image)
67
+ st.image(st.session_state.uploaded_image, use_column_width='always')
68
  else:
69
  st.session_state.image = None
70
+ st.session_state.input = ''
71
+ st.session_state.audio_answer = ''
72
 
73
  with audio_col:
74
  welcome_text = 'Hello and Welcome. I have been trained as visual question answering model. You are welcome to look at any image and ask me any questions about it. I will do my best to provide the most accurate information possible based on my expertise. Select an image of interest by pressing the browse files button. Now use the Ask question button to ask a question. Please feel free to ask me any questions about this image. Now. to get my answer. press the Get answer button.'
115
 
116
  if result:
117
  if 'GET_TEXT' in result:
118
+ if 'question' not in st.session_state or st.session_state.question != result.get('GET_TEXT'):
119
+ st.session_state['question'] = result.get('GET_TEXT')
120
+ with st.spinner('Preparing answer...'):
121
+ while 'predictor' not in st.session_state:
122
+ time.sleep(2)
123
+ st.session_state.audio_answer = st.session_state.predictor.predict_answer_from_text(
124
+ st.session_state.image, result.get('GET_TEXT'))
125
 
126
  tts_button = Button(label='Get Answer', width=100)
127
  tts_button.js_on_event('button_click', CustomJS(code=f"""
chatbot.py CHANGED
@@ -1,7 +1,11 @@
1
  import streamlit as st
2
  from streamlit_chat import message
 
3
  from PIL import Image
4
  import time
 
 
 
5
 
6
 
7
  def init_chat_history():
@@ -38,6 +42,24 @@ def predict(image, input):
38
  st.session_state.question.pop(0)
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def show():
42
  init_chat_history()
43
 
@@ -49,17 +71,37 @@ def show():
49
  </i></h4>
50
  ''', unsafe_allow_html=True)
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  image_col, text_col = st.columns(2)
53
  with image_col:
54
- upload_pic = st.file_uploader('Select an image...', type=[
55
- 'jpg', 'png', 'jpeg'], accept_multiple_files=False)
56
- if upload_pic is not None:
57
- image = Image.open(upload_pic)
58
- st.image(upload_pic, use_column_width='auto')
 
 
59
  else:
60
  st.session_state.question = []
61
  st.session_state.answer = []
62
  st.session_state.input = ''
 
63
  with text_col:
64
  input = st.text_input('Enter question: ', '', key='input')
65
  if input:
1
  import streamlit as st
2
  from streamlit_chat import message
3
+ from st_clickable_images import clickable_images
4
  from PIL import Image
5
  import time
6
+ from os.path import *
7
+ from os import listdir
8
+ import base64
9
 
10
 
11
  def init_chat_history():
42
  st.session_state.question.pop(0)
43
 
44
 
45
+ def update_gallery_images():
46
+ if 'gallery' not in st.session_state:
47
+ st.session_state.gallery = []
48
+ st.session_state.gallery_images = []
49
+ image_path = join(dirname(abspath(__file__)), 'images')
50
+ for f in listdir(image_path):
51
+ if f.startswith('image'):
52
+ with open(join(image_path, f), "rb") as image:
53
+ encoded = base64.b64encode(image.read()).decode()
54
+ st.session_state.gallery.append(
55
+ f"data:image/jpeg;base64,{encoded}")
56
+ st.session_state.gallery_images.append(join(image_path, f))
57
+
58
+
59
+ def upload_image_callback():
60
+ st.session_state.uploaded_image = st.session_state.uploader
61
+
62
+
63
  def show():
64
  init_chat_history()
65
 
71
  </i></h4>
72
  ''', unsafe_allow_html=True)
73
 
74
+ update_gallery_images()
75
+ if 'gallery' in st.session_state:
76
+ clicked = clickable_images(
77
+ st.session_state.gallery,
78
+ titles=[f"Image #{str(i)}" for i in range(2)],
79
+ div_style={"display": "flex",
80
+ "justify-content": "center", "flex-wrap": "nowrap"},
81
+ img_style={"margin": "5px", "height": "100px"},
82
+ )
83
+
84
+ if 'clicked' not in st.session_state or st.session_state.clicked != clicked:
85
+ st.session_state.uploaded_image = st.session_state.gallery_images[clicked]
86
+ st.session_state.clicked = clicked
87
+ st.session_state.question = []
88
+ st.session_state.answer = []
89
+ st.session_state.input = ''
90
+
91
  image_col, text_col = st.columns(2)
92
  with image_col:
93
+ st.file_uploader('Select an image...', type=[
94
+ 'jpg', 'jpeg'], accept_multiple_files=False,
95
+ on_change=upload_image_callback, key='uploader')
96
+
97
+ if st.session_state.uploaded_image is not None:
98
+ image = Image.open(st.session_state.uploaded_image)
99
+ st.image(st.session_state.uploaded_image, use_column_width='always')
100
  else:
101
  st.session_state.question = []
102
  st.session_state.answer = []
103
  st.session_state.input = ''
104
+
105
  with text_col:
106
  input = st.text_input('Enter question: ', '', key='input')
107
  if input:
images/image10.jpg CHANGED
requirements.txt CHANGED
@@ -107,6 +107,7 @@ six==1.12.0
107
  sklearn==0.0
108
  smmap==5.0.0
109
  soupsieve==2.3.2.post1
 
110
  stack-data==0.3.0
111
  streamlit==1.10.0
112
  streamlit-bokeh-events==0.1.2
107
  sklearn==0.0
108
  smmap==5.0.0
109
  soupsieve==2.3.2.post1
110
+ st-clickable-images==0.0.3
111
  stack-data==0.3.0
112
  streamlit==1.10.0
113
  streamlit-bokeh-events==0.1.2