Ricercar commited on
Commit
0532088
โ€ข
1 Parent(s): 06ca453

custom radio style

Browse files
Files changed (5) hide show
  1. Home.py +4 -3
  2. pages/Gallery.py +6 -22
  3. pages/Ranking.py +87 -49
  4. pages/Summary.py +12 -2
  5. pages/style.css +27 -0
Home.py CHANGED
@@ -30,7 +30,7 @@ def save_user_id(user_id):
30
  if not user_id:
31
  user_id = 'anonymous' + str(random.randint(0, 100000))
32
  st.session_state.user_id = [user_id, datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
33
- st.session_state.assigned_rank_mode = random.choice(['sort', 'battle'])
34
 
35
 
36
  def logout():
@@ -55,11 +55,11 @@ def info():
55
  # )
56
 
57
  st.write(
58
- "This is a web application for individual users to quickly dig out the most suitable text-to-image generation model from civitai. Our research aims to understand personal preference to images synthesized by generative models fine-tuned on stable diffusion and you can contribute by playing with this tool and giving us your feedback! "
59
  )
60
 
61
  st.write(
62
- "After picking images you liked from Gallery and a battle-mode Ranking Contest, a summary dashboard will be presented indicating your preferred models with download links ready to be deployed in Webui !"
63
  )
64
 
65
 
@@ -80,6 +80,7 @@ if __name__ == '__main__':
80
  login()
81
  else:
82
  st.write(f"You have already logged in as `{st.session_state.user_id[0]}`")
 
83
  st.button('Log out', on_click=logout)
84
 
85
  st.write('---')
 
30
  if not user_id:
31
  user_id = 'anonymous' + str(random.randint(0, 100000))
32
  st.session_state.user_id = [user_id, datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
33
+ st.session_state.assigned_rank_mode = random.choice(['Drag and Sort', 'Battle'])
34
 
35
 
36
  def logout():
 
55
  # )
56
 
57
  st.write(
58
+ "**This is a web application for individual users to quickly dig out the most suitable text-to-image generation model from [civitai](https://civitai.com).** Our research aims to understand personal preference to images synthesized by generative models fine-tuned on stable diffusion and you can contribute by playing with this tool and giving us your feedback! "
59
  )
60
 
61
  st.write(
62
+ "After picking images you liked from Gallery and a battle-mode Ranking Contest, a summary dashboard will be presented **indicating your preferred models with download links ready to be deployed in [Webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)** !"
63
  )
64
 
65
 
 
80
  login()
81
  else:
82
  st.write(f"You have already logged in as `{st.session_state.user_id[0]}`")
83
+ st.write(f"Assigned ranking mode: `{st.session_state.assigned_rank_mode}`")
84
  st.button('Log out', on_click=logout)
85
 
86
  st.write('---')
pages/Gallery.py CHANGED
@@ -276,7 +276,7 @@ class GalleryApp:
276
  # return prompt_tags, tag, prompt_id, items
277
 
278
  def app(self):
279
- # st.title('Model Visualization and Retrieval')
280
  # st.write('This is a gallery of images generated by the models')
281
 
282
  # build the tabular view
@@ -288,7 +288,7 @@ class GalleryApp:
288
  # tag = stx.tab_bar(chosen_data, key='tag', default='food')
289
 
290
  # save tag to session state on change
291
- tag = st.radio('Select a tag', prompt_tags, index=5, horizontal=True, key='tag')
292
 
293
  # tabs = st.tabs(prompt_tags)
294
  # for i in range(len(prompt_tags)):
@@ -301,11 +301,6 @@ class GalleryApp:
301
  st.caption('Select a prompt')
302
  subset_selector = st.columns([3, 1])
303
  with subset_selector[0]:
304
- # remember last prompt
305
- # if 'prompt_idx_last_time' not in st.session_state:
306
- # st.session_state.prompt_idx_last_time = 0
307
-
308
- # selected_prompt = st.selectbox('Select prompt', prompts, index=3)
309
  selected_prompt = selectbox('Select prompt', prompts, key=f'prompt_{tag}', no_selection_label='---', label_visibility='collapsed', index=0)
310
  # st.session_state.prompt_idx_last_time = prompts.index(selected_prompt) if selected_prompt else 0
311
 
@@ -347,17 +342,8 @@ class GalleryApp:
347
 
348
  if st.session_state.gallery_state[prompt_id] == 'graph':
349
  if safety_check:
350
- # if subset == 'Selected Only' and 'selected_dict' in st.session_state:
351
- # # try:
352
- # items = items[items['modelVersion_id'].isin(st.session_state.selected_dict[prompt_id])].reset_index(drop=True)
353
- # self.gallery_mode(prompt_id, items)
354
- # # except:
355
- # # st.warning('No selected images found')
356
- # else:
357
  self.graph_mode(prompt_id, items)
358
  with subset_selector[-1]:
359
- # if st.session_state.gallery_state[prompt_id] == 'graph':
360
- # subset = st.selectbox('Select a subset', ['All', 'Selected Only'], index=0, key=f'subset_{tag}')
361
  has_selection = False
362
  try:
363
  if len(st.session_state.selected_dict.get(prompt_id, [])) > 0:
@@ -366,7 +352,7 @@ class GalleryApp:
366
  pass
367
 
368
  if has_selection:
369
- checkout = st.button('๐Ÿ›’ Check out selections', use_container_width=True, type='primary')
370
  if checkout:
371
  print('checkout')
372
 
@@ -394,11 +380,6 @@ class GalleryApp:
394
  if forward:
395
  switch_page('ranking')
396
 
397
-
398
-
399
- # else:
400
- # st.button('Proceed', use_container_width=True)
401
-
402
  try:
403
  self.sidebar(items, prompt_id, note)
404
  except:
@@ -712,6 +693,9 @@ def load_tsne_coordinates(items):
712
  if __name__ == "__main__":
713
  st.set_page_config(page_title="Model Coffer Gallery", page_icon="๐Ÿ–ผ๏ธ", layout="wide")
714
 
 
 
 
715
  if 'user_id' not in st.session_state:
716
  st.warning('Please log in first.')
717
  home_btn = st.button('Go to Home Page')
 
276
  # return prompt_tags, tag, prompt_id, items
277
 
278
  def app(self):
279
+ st.title('Model Visualization and Retrieval')
280
  # st.write('This is a gallery of images generated by the models')
281
 
282
  # build the tabular view
 
288
  # tag = stx.tab_bar(chosen_data, key='tag', default='food')
289
 
290
  # save tag to session state on change
291
+ tag = st.radio('Select a tag', prompt_tags, index=5, horizontal=True, key='tag', label_visibility='collapsed')
292
 
293
  # tabs = st.tabs(prompt_tags)
294
  # for i in range(len(prompt_tags)):
 
301
  st.caption('Select a prompt')
302
  subset_selector = st.columns([3, 1])
303
  with subset_selector[0]:
 
 
 
 
 
304
  selected_prompt = selectbox('Select prompt', prompts, key=f'prompt_{tag}', no_selection_label='---', label_visibility='collapsed', index=0)
305
  # st.session_state.prompt_idx_last_time = prompts.index(selected_prompt) if selected_prompt else 0
306
 
 
342
 
343
  if st.session_state.gallery_state[prompt_id] == 'graph':
344
  if safety_check:
 
 
 
 
 
 
 
345
  self.graph_mode(prompt_id, items)
346
  with subset_selector[-1]:
 
 
347
  has_selection = False
348
  try:
349
  if len(st.session_state.selected_dict.get(prompt_id, [])) > 0:
 
352
  pass
353
 
354
  if has_selection:
355
+ checkout = st.button('Check out selections', use_container_width=True, type='primary')
356
  if checkout:
357
  print('checkout')
358
 
 
380
  if forward:
381
  switch_page('ranking')
382
 
 
 
 
 
 
383
  try:
384
  self.sidebar(items, prompt_id, note)
385
  except:
 
693
  if __name__ == "__main__":
694
  st.set_page_config(page_title="Model Coffer Gallery", page_icon="๐Ÿ–ผ๏ธ", layout="wide")
695
 
696
+ with open('./pages/style.css') as f:
697
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
698
+
699
  if 'user_id' not in st.session_state:
700
  st.warning('Please log in first.')
701
  home_btn = st.button('Go to Home Page')
pages/Ranking.py CHANGED
@@ -24,28 +24,31 @@ class RankingApp:
24
  if 'counter' not in st.session_state:
25
  st.session_state.counter = {}
26
 
27
- def sidebar(self):
28
  with st.sidebar:
29
- prompt_tags = self.promptBook['tag'].unique()
30
- prompt_tags = np.sort(prompt_tags).tolist()
31
-
32
- print(st.session_state.gallery_focus)
33
- tag_idx = prompt_tags.index(st.session_state.gallery_focus['tag']) if st.session_state.gallery_focus['tag'] in prompt_tags else 0
34
- print(tag_idx)
35
-
36
- tag = st.selectbox('Select a prompt tag', prompt_tags, index=tag_idx)
37
- items = self.promptBook[self.promptBook['tag'] == tag].reset_index(drop=True)
38
- prompts = np.sort(items['prompt'].unique())[::-1].tolist()
39
-
40
- prompt_idx = prompts.index(st.session_state.gallery_focus['prompt']) if st.session_state.gallery_focus['prompt'] in prompts else 0
41
- print(prompt_idx)
42
-
43
- selected_prompt = st.selectbox('Select a prompt', prompts, index=prompt_idx)
44
-
45
- mode = st.radio('Select a mode', ['Drag and Sort', 'Battle'], index=1)
46
-
47
- items = items[items['prompt'] == selected_prompt].reset_index(drop=True)
48
- prompt_id = items['prompt_id'].unique()[0]
 
 
 
49
 
50
  with st.form(key='prompt_form'):
51
  # input image metadata
@@ -53,7 +56,7 @@ class RankingApp:
53
  negative_prompt = st.text_area('Negative Prompt', items['negativePrompt'].unique()[0], height=150, key='negative_prompt', disabled=True)
54
  st.form_submit_button('Generate Images [Coming Soon]', type='primary', use_container_width=True, disabled=True)
55
 
56
- return prompt_tags, tag, prompt_id, items, mode
57
 
58
  def draggable_images(self, items, prompt_id, layout='portrait'):
59
  # init ranking by the order of items
@@ -261,9 +264,8 @@ class RankingApp:
261
  self.battle_images(items, prompt_id)
262
 
263
  def app(self):
264
- st.title('Personal Image Ranking')
265
- st.write('Here you can test out your selected images with any prompt you like. ')
266
- st.caption("We might pair some other images that you haven't selected based on our evaluation matrix.")
267
  # st.write(self.promptBook)
268
 
269
  # save the current progress to session state
@@ -271,7 +273,32 @@ class RankingApp:
271
  st.session_state.progress = {}
272
  print('current progress: ', st.session_state.progress)
273
 
274
- prompt_tags, tag, prompt_id, items, mode = self.sidebar()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  batch_num = len(items) // self.batch_size
276
  batch_num += 1 if len(items) % self.batch_size != 0 else 0
277
 
@@ -284,35 +311,43 @@ class RankingApp:
284
  st.session_state.progress[prompt_id] = 'ranking'
285
 
286
  if st.session_state.progress[prompt_id] == 'ranking':
 
287
  if mode == 'Drag and Sort':
288
  self.dragsort_mode(items, prompt_id, batch_num)
289
  elif mode == 'Battle':
290
  self.battle_mode(items, prompt_id)
291
 
292
  elif st.session_state.progress[prompt_id] == 'finished':
293
- st.write('## You have ranked all models for this tag!')
294
- st.write('Thank you for your participation! Feel free to do the following things:')
295
- # st.write('* Rank for other tags and prompts.')
296
- # st.write('* Back to the gallery page to see more images.')
297
- # st.write('* Rank again for this tag and prompt.')
298
- # st.write('* Check the summary to see what model you like most.')
299
- # st.write('*More functions are coming soon... Please stay tuned*')
300
- st.button('๐Ÿ‘ˆ Rank for other tags and prompts')
301
- restart_btn = st.button('๐ŸŽ–๏ธ Rank this prompt again')
302
- if restart_btn:
303
- st.session_state.progress[prompt_id] = 'ranking'
304
- st.session_state.counter[prompt_id] = 0
305
- st.session_state.pointer[prompt_id] = {'left': 0, 'right': 1}
306
- st.experimental_rerun()
307
-
308
- gallery_btn = st.button('๐Ÿ–ผ๏ธ Back to Gallery')
309
- if gallery_btn:
310
- switch_page('gallery')
311
-
312
- summary_btn = st.button('๐Ÿ“Š See Summary')
313
- if summary_btn:
314
- switch_page('summary')
315
-
 
 
 
 
 
 
 
316
 
317
  def connect_to_db():
318
  conn = pymysql.connect(
@@ -331,6 +366,9 @@ def connect_to_db():
331
  if __name__ == "__main__":
332
  st.set_page_config(page_title="Personal Image Ranking", page_icon="๐ŸŽ–๏ธ๏ธ", layout="wide")
333
 
 
 
 
334
  if 'user_id' not in st.session_state:
335
  st.warning('Please log in first.')
336
  home_btn = st.button('Go to Home Page')
 
24
  if 'counter' not in st.session_state:
25
  st.session_state.counter = {}
26
 
27
+ def sidebar(self, selected_prompt, items):
28
  with st.sidebar:
29
+ st.title('Personal Image Ranking')
30
+ st.write('Here you can test out your selected images with any prompt you like. ')
31
+ # prompt_tags = self.promptBook['tag'].unique()
32
+ # prompt_tags = np.sort(prompt_tags).tolist()
33
+ #
34
+ # print(st.session_state.gallery_focus)
35
+ # tag_idx = prompt_tags.index(st.session_state.gallery_focus['tag']) if st.session_state.gallery_focus['tag'] in prompt_tags else 0
36
+ # print(tag_idx)
37
+ #
38
+ # tag = st.selectbox('Select a prompt tag', prompt_tags, index=tag_idx)
39
+ # items = self.promptBook[self.promptBook['tag'] == tag].reset_index(drop=True)
40
+ # prompts = np.sort(items['prompt'].unique())[::-1].tolist()
41
+ #
42
+ # prompt_idx = prompts.index(st.session_state.gallery_focus['prompt']) if st.session_state.gallery_focus['prompt'] in prompts else 0
43
+ # print(prompt_idx)
44
+ #
45
+ # selected_prompt = st.selectbox('Select a prompt', prompts, index=prompt_idx)
46
+ #
47
+ # # mode = st.radio('Select a mode', ['Drag and Sort', 'Battle'], index=1)
48
+ # mode = st.session_state.assigned_rank_mode
49
+ #
50
+ # items = items[items['prompt'] == selected_prompt].reset_index(drop=True)
51
+ # prompt_id = items['prompt_id'].unique()[0]
52
 
53
  with st.form(key='prompt_form'):
54
  # input image metadata
 
56
  negative_prompt = st.text_area('Negative Prompt', items['negativePrompt'].unique()[0], height=150, key='negative_prompt', disabled=True)
57
  st.form_submit_button('Generate Images [Coming Soon]', type='primary', use_container_width=True, disabled=True)
58
 
59
+ return None
60
 
61
  def draggable_images(self, items, prompt_id, layout='portrait'):
62
  # init ranking by the order of items
 
264
  self.battle_images(items, prompt_id)
265
 
266
  def app(self):
267
+ # st.title('Personal Image Ranking')
268
+ # st.write('Here you can test out your selected images with any prompt you like. ')
 
269
  # st.write(self.promptBook)
270
 
271
  # save the current progress to session state
 
273
  st.session_state.progress = {}
274
  print('current progress: ', st.session_state.progress)
275
 
276
+ # select tag and prompt
277
+ prompt_tags = self.promptBook['tag'].unique()
278
+ prompt_tags = np.sort(prompt_tags).tolist()
279
+
280
+ print(st.session_state.gallery_focus)
281
+ tag_idx = prompt_tags.index(st.session_state.gallery_focus['tag']) if st.session_state.gallery_focus[
282
+ 'tag'] in prompt_tags else 0
283
+ print(tag_idx)
284
+
285
+ tag = st.radio('Select a tag', prompt_tags, index=tag_idx, horizontal=True)
286
+ items = self.promptBook[self.promptBook['tag'] == tag].reset_index(drop=True)
287
+ prompts = np.sort(items['prompt'].unique())[::-1].tolist()
288
+
289
+ prompt_idx = prompts.index(st.session_state.gallery_focus['prompt']) if st.session_state.gallery_focus[
290
+ 'prompt'] in prompts else 0
291
+ print(prompt_idx)
292
+
293
+ selected_prompt = st.selectbox('Select a prompt', prompts, index=prompt_idx)
294
+
295
+ # mode = st.radio('Select a mode', ['Drag and Sort', 'Battle'], index=1)
296
+ mode = st.session_state.assigned_rank_mode
297
+
298
+ items = items[items['prompt'] == selected_prompt].reset_index(drop=True)
299
+ prompt_id = items['prompt_id'].unique()[0]
300
+
301
+ self.sidebar(selected_prompt, items)
302
  batch_num = len(items) // self.batch_size
303
  batch_num += 1 if len(items) % self.batch_size != 0 else 0
304
 
 
311
  st.session_state.progress[prompt_id] = 'ranking'
312
 
313
  if st.session_state.progress[prompt_id] == 'ranking':
314
+ st.caption("We might pair some other images that you haven't selected based on our evaluation matrix.")
315
  if mode == 'Drag and Sort':
316
  self.dragsort_mode(items, prompt_id, batch_num)
317
  elif mode == 'Battle':
318
  self.battle_mode(items, prompt_id)
319
 
320
  elif st.session_state.progress[prompt_id] == 'finished':
321
+ # st.toast('**Summary is available now!**')
322
+ # st.write('---')
323
+ with st.form(key='ranking_finished'):
324
+ st.info('**๐ŸŽ‰ You have ranked all models for this prompt!**')
325
+ st.write('Feel free to do the following things:')
326
+
327
+ # st.write('* Back to the gallery page to see more images.')
328
+ # st.write('* Rank again for this tag and prompt.')
329
+ options_panel = st.columns(4)
330
+
331
+ with options_panel[0]:
332
+ summary_btn = st.form_submit_button('๐Ÿ“Š See Summary', use_container_width=True, type='primary')
333
+ if summary_btn:
334
+ switch_page('summary')
335
+
336
+ with options_panel[1]:
337
+ gallery_btn = st.form_submit_button('๐Ÿ–ผ๏ธ Back to Gallery', use_container_width=True)
338
+ if gallery_btn:
339
+ switch_page('gallery')
340
+
341
+ with options_panel[2]:
342
+ st.form_submit_button('๐Ÿ‘† Rank other prompts', use_container_width=True)
343
+
344
+ with options_panel[3]:
345
+ restart_btn = st.form_submit_button('๐ŸŽ–๏ธ Re-rank this prompt', use_container_width=True)
346
+ if restart_btn:
347
+ st.session_state.progress[prompt_id] = 'ranking'
348
+ st.session_state.counter[prompt_id] = 0
349
+ st.session_state.pointer[prompt_id] = {'left': 0, 'right': 1}
350
+ st.experimental_rerun()
351
 
352
  def connect_to_db():
353
  conn = pymysql.connect(
 
366
  if __name__ == "__main__":
367
  st.set_page_config(page_title="Personal Image Ranking", page_icon="๐ŸŽ–๏ธ๏ธ", layout="wide")
368
 
369
+ with open('./pages/style.css') as f:
370
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
371
+
372
  if 'user_id' not in st.session_state:
373
  st.warning('Please log in first.')
374
  home_btn = st.button('Go to Home Page')
pages/Summary.py CHANGED
@@ -24,6 +24,15 @@ class DashboardApp:
24
  def sidebar(self, tags, mode):
25
  with st.sidebar:
26
  tag = st.selectbox('Select a tag', tags, key='tag')
 
 
 
 
 
 
 
 
 
27
 
28
  return tag
29
 
@@ -155,9 +164,10 @@ class DashboardApp:
155
  def app(self):
156
  st.title('Your Preferred Models', help="Scores are calculated based on your ranking results.")
157
 
158
- mode = st.sidebar.radio('Ranking mode', ['Sort', 'Battle'], horizontal=True, index=1)
 
159
  # get tags from database of the current user
160
- db_table = 'sort_results' if mode == 'Sort' else 'battle_results'
161
 
162
  tags = ['all']
163
  curser = RANKING_CONN.cursor()
 
24
  def sidebar(self, tags, mode):
25
  with st.sidebar:
26
  tag = st.selectbox('Select a tag', tags, key='tag')
27
+ st.write('---')
28
+ st.write('## Want a more comprehensive summary?')
29
+ st.write('Jump back to gallery and select more images to rank!')
30
+ back_to_gallery = st.button('๐Ÿ–ผ๏ธ Go to Gallery', key='summary_sidebar_gallery')
31
+ if back_to_gallery:
32
+ switch_page('gallery')
33
+ back_to_ranking = st.button('๐ŸŽ–๏ธ Go to Ranking', key='summary_sidebar_ranking')
34
+ if back_to_ranking:
35
+ switch_page('ranking')
36
 
37
  return tag
38
 
 
164
  def app(self):
165
  st.title('Your Preferred Models', help="Scores are calculated based on your ranking results.")
166
 
167
+ # mode = st.sidebar.radio('Ranking mode', ['Drag and Sort', 'Battle'], horizontal=True, index=1)
168
+ mode = st.session_state.assigned_rank_mode
169
  # get tags from database of the current user
170
+ db_table = 'sort_results' if mode == 'Drag and Sort' else 'battle_results'
171
 
172
  tags = ['all']
173
  curser = RANKING_CONN.cursor()
pages/style.css ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ div.row-widget.stRadio > div {
2
+ flex-direction: row;
3
+ align-items: stretch;
4
+ }
5
+
6
+ div.row-widget.stRadio > div[role="radiogroup"] > label[data-baseweb="radio"] {
7
+ /*background-color: rgb(240, 242, 246);*/
8
+ padding-right: 10px;
9
+ /*padding-left: 4px;*/
10
+ padding-bottom: 3px;
11
+ margin: 4px 0px;
12
+ border-radius: 0;
13
+ border-bottom: 2px solid whitesmoke;
14
+ transition: border-bottom-color 0.2s ease 0s;
15
+ }
16
+
17
+ /*change the background color of the parent label of the selected radio button*/
18
+ div.row-widget.stRadio > div[role="radiogroup"] > label[data-baseweb="radio"]:has( > input[type="radio"]:checked) {
19
+ /*background-color: blanchedalmond;*/
20
+ border-bottom: 2px solid red;
21
+ transition: border-bottom-color 0.2s ease 0s;
22
+ }
23
+
24
+ /*hide the circle of the radio button*/
25
+ div.row-widget.stRadio > div[role="radiogroup"] > label[data-baseweb="radio"] > div:first-child {
26
+ display: none;
27
+ }