zamborg commited on
Commit
7332d54
1 Parent(s): 66e3bd1
Files changed (1) hide show
  1. app.py +81 -75
app.py CHANGED
@@ -32,85 +32,91 @@ st.sidebar.markdown(
32
  with st.spinner("Loading Model"):
33
  virtexModel, imageLoader, sample_images, valid_subs = create_objects()
34
 
35
-
36
- select_idx = None
37
-
38
- st.sidebar.title("Select a sample image")
39
-
40
- if st.sidebar.button("Random Sample Image"):
41
- select_idx = get_rand_idx(sample_images)
42
-
43
- sample_image = sample_images[0 if select_idx is None else select_idx]
44
-
45
- # class OnChange():
46
- # def __init__(self, idx):
47
- # self.idx = idx
48
-
49
- # def __call__(self):
50
- # st.write(f"the idx is: {self.idx}")
51
- # st.write(f"the sample_image is {sample_image}")
52
-
53
- # sample_image = st.sidebar.selectbox(
54
- # "",
55
- # sample_images,
56
- # index = 0 if select_idx is None else select_idx,
57
- # on_change=OnChange(0 if select_idx is None else select_idx)
58
- # )
59
-
60
- st.sidebar.title("Select a Subreddit")
61
- sub = st.sidebar.selectbox(
62
- "Select None for a Predicted Subreddit",
63
- valid_subs
64
- )
65
 
66
- st.sidebar.title("Write a Custom Prompt")
67
- cap_prompt = st.sidebar.text_input(
68
- "Leave this blank for an unbiased caption",
69
- value=""
70
- )
71
-
72
-
73
- uploaded_image = None
74
- with st.sidebar.form("file-uploader-form", clear_on_submit=True):
75
- uploaded_file = st.file_uploader("Choose a file")
76
- submitted = st.form_submit_button("Submit")
77
- if uploaded_file is not None and submitted:
78
- uploaded_image = Image.open(io.BytesIO(uploaded_file.getvalue()))
79
- select_idx = None # set this to help rewrite the cache
80
-
81
- _ = st.sidebar.button("Regenerate Caption")
82
-
83
- if uploaded_image is None and submitted:
84
- st.write("Please select a file to upload")
85
 
 
 
86
  else:
87
- image_file = sample_image
88
 
89
- # LOAD AND CACHE THE IMAGE
90
- if uploaded_image is not None:
91
- image = uploaded_image
92
- elif select_idx is None and 'image' in st.session_state:
93
- image = st.session_state['image']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  else:
95
- image = Image.open(image_file)
96
-
97
- st.session_state['image'] = image
98
-
99
- image_dict = imageLoader.transform(image)
100
-
101
- show_image = imageLoader.show_resize(image)
102
-
103
- show = st.image(show_image)
104
- show.image(show_image, "Your Image")
105
-
106
- gen_show_caption(sub, imageLoader.text_transform(cap_prompt))
 
 
 
 
 
 
 
 
107
 
108
- # from model import *
109
- # sample_images = get_samples()
110
- # v, il = VirTexModel(), ImageLoader()
111
 
112
- # for s in sample_images:
113
- # subreddit, caption = v.predict(il.load(s))
114
- # print("=====================")
115
- # print(subreddit)
116
- # print(caption)
 
32
  with st.spinner("Loading Model"):
33
  virtexModel, imageLoader, sample_images, valid_subs = create_objects()
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ staggered = st.sidebar.checkbox("Iteratively Generate Captions")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ if staggered:
39
+ pass
40
  else:
 
41
 
42
+ select_idx = None
43
+
44
+ st.sidebar.title("Select a sample image")
45
+
46
+ if st.sidebar.button("Random Sample Image"):
47
+ select_idx = get_rand_idx(sample_images)
48
+
49
+ sample_image = sample_images[0 if select_idx is None else select_idx]
50
+
51
+ # class OnChange():
52
+ # def __init__(self, idx):
53
+ # self.idx = idx
54
+
55
+ # def __call__(self):
56
+ # st.write(f"the idx is: {self.idx}")
57
+ # st.write(f"the sample_image is {sample_image}")
58
+
59
+ # sample_image = st.sidebar.selectbox(
60
+ # "",
61
+ # sample_images,
62
+ # index = 0 if select_idx is None else select_idx,
63
+ # on_change=OnChange(0 if select_idx is None else select_idx)
64
+ # )
65
+
66
+ st.sidebar.title("Select a Subreddit")
67
+ sub = st.sidebar.selectbox(
68
+ "Select None for a Predicted Subreddit",
69
+ valid_subs
70
+ )
71
+
72
+ st.sidebar.title("Write a Custom Prompt")
73
+ cap_prompt = st.sidebar.text_input(
74
+ "Leave this blank for an unbiased caption",
75
+ value=""
76
+ )
77
+
78
+
79
+ uploaded_image = None
80
+ with st.sidebar.form("file-uploader-form", clear_on_submit=True):
81
+ uploaded_file = st.file_uploader("Choose a file")
82
+ submitted = st.form_submit_button("Submit")
83
+ if uploaded_file is not None and submitted:
84
+ uploaded_image = Image.open(io.BytesIO(uploaded_file.getvalue()))
85
+ select_idx = None # set this to help rewrite the cache
86
+
87
+ _ = st.sidebar.button("Regenerate Caption")
88
+
89
+ if uploaded_image is None and submitted:
90
+ st.write("Please select a file to upload")
91
+
92
  else:
93
+ image_file = sample_image
94
+
95
+ # LOAD AND CACHE THE IMAGE
96
+ if uploaded_image is not None:
97
+ image = uploaded_image
98
+ elif select_idx is None and 'image' in st.session_state:
99
+ image = st.session_state['image']
100
+ else:
101
+ image = Image.open(image_file)
102
+
103
+ st.session_state['image'] = image
104
+
105
+ image_dict = imageLoader.transform(image)
106
+
107
+ show_image = imageLoader.show_resize(image)
108
+
109
+ show = st.image(show_image)
110
+ show.image(show_image, "Your Image")
111
+
112
+ gen_show_caption(sub, imageLoader.text_transform(cap_prompt))
113
 
114
+ # from model import *
115
+ # sample_images = get_samples()
116
+ # v, il = VirTexModel(), ImageLoader()
117
 
118
+ # for s in sample_images:
119
+ # subreddit, caption = v.predict(il.load(s))
120
+ # print("=====================")
121
+ # print(subreddit)
122
+ # print(caption)