kdexd commited on
Commit
15921a8
1 Parent(s): 8d0e872

Try markdown in right pane.

Browse files
Files changed (1) hide show
  1. app.py +31 -49
app.py CHANGED
@@ -32,32 +32,20 @@ def gen_show_caption(sub_prompt=None, cap_prompt=""):
32
  unsafe_allow_html=True,
33
  )
34
 
 
35
 
36
  _, center, _ = st.columns([1, 8, 1])
37
 
38
- with center:
39
- st.title("Image Captioning Demo from RedCaps")
40
- st.sidebar.markdown(
41
- """
42
- ### Image Captioning Model from VirTex trained on RedCaps
43
-
44
- Use this page to caption your own images or try out some of our samples.
45
- You can also generate captions as if they are from specific subreddits,
46
- as if they start with a particular prompt, or even both.
47
-
48
- Share your results on twitter with #redcaps or with a friend*.
49
- """
50
- )
51
- # st.markdown(footer,unsafe_allow_html=True)
52
-
53
  with st.spinner("Loading Model"):
54
  virtexModel, imageLoader, sample_images, valid_subs = create_objects()
55
 
56
 
 
 
 
57
  select_idx = None
58
 
59
- st.sidebar.title("Select a sample image")
60
-
61
  if st.sidebar.button("Random Sample Image"):
62
  select_idx = get_rand_idx(sample_images)
63
 
@@ -65,27 +53,12 @@ sample_image = sample_images[0 if select_idx is None else select_idx]
65
 
66
 
67
  uploaded_image = None
68
- # with st.sidebar.form("file-uploader-form", clear_on_submit=True):
69
  uploaded_file = st.sidebar.file_uploader("Choose a file")
70
- # submitted = st.form_submit_button("Submit")
71
- if uploaded_file is not None: # and submitted:
72
- uploaded_image = Image.open(io.BytesIO(uploaded_file.getvalue()))
73
- select_idx = None # set this to help rewrite the cache
74
-
75
- # class OnChange():
76
- # def __init__(self, idx):
77
- # self.idx = idx
78
 
79
- # def __call__(self):
80
- # st.write(f"the idx is: {self.idx}")
81
- # st.write(f"the sample_image is {sample_image}")
82
 
83
- # sample_image = st.sidebar.selectbox(
84
- # "",
85
- # sample_images,
86
- # index = 0 if select_idx is None else select_idx,
87
- # on_change=OnChange(0 if select_idx is None else select_idx)
88
- # )
89
 
90
  st.sidebar.title("Select a Subreddit")
91
  sub = st.sidebar.selectbox(
@@ -99,17 +72,28 @@ cap_prompt = st.sidebar.text_input("Write the start of your caption below", valu
99
  _ = st.sidebar.button("Regenerate Caption")
100
 
101
 
102
- st.sidebar.write("Advanced Options:")
103
  num_captions = st.sidebar.select_slider(
104
  "Number of Captions to Predict", options=[1, 2, 3, 4, 5], value=1
105
  )
106
  nuc_size = st.sidebar.slider(
107
- "Nucelus Size:\nLarger values lead to more diverse captions",
108
  min_value=0.0,
109
  max_value=1.0,
110
  value=0.8,
111
  step=0.05,
112
  )
 
 
 
 
 
 
 
 
 
 
 
113
  virtexModel.model.decoder.nucleus_size = nuc_size
114
 
115
  image_file = sample_image
@@ -131,21 +115,19 @@ image_dict = imageLoader.transform(image)
131
 
132
  show_image = imageLoader.show_resize(image)
133
 
 
134
  with center:
135
- show = st.image(show_image)
136
- show.image(show_image)
 
 
 
 
 
 
 
137
 
138
  if sub is None and imageLoader.text_transform(cap_prompt) is not "":
139
  st.write("Without a specified subreddit we default to /r/pics")
140
  for i in range(num_captions):
141
  gen_show_caption(sub, imageLoader.text_transform(cap_prompt))
142
-
143
- st.sidebar.markdown(
144
- """
145
- *Please note that this model was explicitly not trained on images of people, and as a result is not designed to caption images with humans.
146
-
147
- This demo accompanies our paper RedCaps.
148
-
149
- Created by Karan Desai, Gaurav Kaul, Zubin Aysola, Justin Johnson
150
- """
151
- )
 
32
  unsafe_allow_html=True,
33
  )
34
 
35
+ # st.markdown(footer,unsafe_allow_html=True)
36
 
37
  _, center, _ = st.columns([1, 8, 1])
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  with st.spinner("Loading Model"):
40
  virtexModel, imageLoader, sample_images, valid_subs = create_objects()
41
 
42
 
43
+ # ----------------------------------------------------------------------------
44
+ # Populate sidebar.
45
+ # ----------------------------------------------------------------------------
46
  select_idx = None
47
 
48
+ st.sidebar.title("Select or upload an image")
 
49
  if st.sidebar.button("Random Sample Image"):
50
  select_idx = get_rand_idx(sample_images)
51
 
 
53
 
54
 
55
  uploaded_image = None
 
56
  uploaded_file = st.sidebar.file_uploader("Choose a file")
 
 
 
 
 
 
 
 
57
 
58
+ if uploaded_file is not None:
59
+ uploaded_image = Image.open(io.BytesIO(uploaded_file.getvalue()))
60
+ select_idx = None # Set this to help rewrite the cache
61
 
 
 
 
 
 
 
62
 
63
  st.sidebar.title("Select a Subreddit")
64
  sub = st.sidebar.selectbox(
 
72
  _ = st.sidebar.button("Regenerate Caption")
73
 
74
 
75
+ st.sidebar.title("Advanced Options:")
76
  num_captions = st.sidebar.select_slider(
77
  "Number of Captions to Predict", options=[1, 2, 3, 4, 5], value=1
78
  )
79
  nuc_size = st.sidebar.slider(
80
+ "Nucleus Size:\nLarger values lead to more diverse captions",
81
  min_value=0.0,
82
  max_value=1.0,
83
  value=0.8,
84
  step=0.05,
85
  )
86
+ st.sidebar.markdown(
87
+ """
88
+ *Please note that this model was explicitly not trained on images of people, and as a result is not designed to caption images with humans.
89
+
90
+ This demo accompanies our paper RedCaps.
91
+
92
+ Created by Karan Desai, Gaurav Kaul, Zubin Aysola, Justin Johnson
93
+ """
94
+ )
95
+ # ----------------------------------------------------------------------------
96
+
97
  virtexModel.model.decoder.nucleus_size = nuc_size
98
 
99
  image_file = sample_image
 
115
 
116
  show_image = imageLoader.show_resize(image)
117
 
118
+
119
  with center:
120
+ st.title("Image Captioning with VirTex model trained on RedCaps")
121
+ st.markdown("""
122
+ Caption your own images or try out some of our sample images.
123
+ You can also generate captions as if they are from specific subreddits,
124
+ as if they start with a particular prompt, or even both.
125
+
126
+ Tweet your results with `#redcaps`!
127
+ """)
128
+ st.image(show_image)
129
 
130
  if sub is None and imageLoader.text_transform(cap_prompt) is not "":
131
  st.write("Without a specified subreddit we default to /r/pics")
132
  for i in range(num_captions):
133
  gen_show_caption(sub, imageLoader.text_transform(cap_prompt))