patrickvonplaten commited on
Commit
a7e7106
β€’
1 Parent(s): 7a8dab7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -115
app.py CHANGED
@@ -31,17 +31,6 @@ MODEL_KEYS = "-".join(SUBMISSIONS.keys())
31
  SUBMISSION_ORG = f"results-{MODEL_KEYS}"
32
  PROMPT_FORMAT = "Pick the picture that best matches the prompt: **{}**"
33
 
34
-
35
- all_datasets = list_datasets(author=SUBMISSION_ORG)
36
- relevant_ids = [d.id for d in all_datasets]
37
-
38
- submitted_ids = []
39
- for _id in relevant_ids:
40
- ds = load_dataset(_id)["train"]
41
- submitted_ids += ds["id"]
42
-
43
- submitted_ids = Counter(submitted_ids)
44
-
45
  submission_names = list(SUBMISSIONS.keys())
46
  num_images = len(SUBMISSIONS[submission_names[0]])
47
 
@@ -65,10 +54,30 @@ def generate_random_hash(length=8):
65
 
66
  return random_hash
67
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- def start():
70
- ids = {id: 0 for id in range(num_images)}
 
 
 
 
71
 
 
 
 
 
 
72
  ids = {**ids, **submitted_ids}
73
 
74
  # sort by count
@@ -170,110 +179,113 @@ def change_view(row_number, dataframe):
170
  }
171
 
172
 
173
- if True:
174
- TITLE = "# Community Parti Prompts - Who is your open-source genAI model?"
175
- DESCRIPTION = """
176
- *This is an interactive game in which you click through pre-generated images from SD-v1-5, SD-v2.1, Karlo, and IF
177
- using [Parti Prompts](https://huggingface.co/datasets/nateraw/parti-prompts) prompts.* \n
178
- *You choices will go into the public community [genAI leaderboard](TODO).*
179
- """
180
- EXPLANATION = """\n\n
181
- ## How it works πŸ“– \n\n
182
-
183
- 1. Click on 'Start'
184
- 2. A prompt and 4 different images are displayed
185
- 3. Select your favorite image
186
- 4. Click on 'Select'
187
- 5. After 10 rounds your favorite diffusion model is displayed
188
- """
189
 
190
- GALLERY_COLUMN_NUM = len(SUBMISSIONS)
191
-
192
- with gr.Blocks(css="style.css") as demo:
193
- gr.Markdown(TITLE)
194
- gr.Markdown(DESCRIPTION)
195
-
196
- with gr.Column(visible=True) as intro_view:
197
- gr.Markdown(EXPLANATION)
198
- start_button = gr.Button("Start").style(full_width=False)
199
-
200
- headers = ["prompt", "result", "id", "Challenge", "Category", "Note"] + [
201
- f"choice_{i}" for i in range(len(SUBMISSIONS))
202
- ]
203
- datatype = ["str", "str", "number", "str", "str", "str"] + len(SUBMISSIONS) * [
204
- "number"
205
- ]
206
-
207
- with gr.Column(visible=False):
208
- row_number = gr.Number(
209
- label="Current row selection index",
210
- value=0,
211
- precision=0,
212
- interactive=False,
213
- )
214
-
215
- # Create Data Frame
216
- with gr.Column(visible=False) as result_view:
217
- result = gr.Markdown("")
218
- dataframe = gr.Dataframe(
219
- headers=headers,
220
- datatype=datatype,
221
- row_count=NUM_QUESTIONS,
222
- col_count=(6 + len(SUBMISSIONS), "fixed"),
223
- interactive=False,
224
- )
225
- gr.Markdown("Click on start to play again!")
226
-
227
- with gr.Column(visible=False) as gallery_view:
228
- counter = gr.Markdown(f" ### 1/{NUM_QUESTIONS}")
229
- prompt = gr.Markdown(PROMPT_FORMAT.format(""))
230
- gallery = gr.Gallery(
231
- label="All images", show_label=False, elem_id="gallery"
232
- ).style(columns=GALLERY_COLUMN_NUM, object_fit="contain")
233
- next_button = gr.Button("Select").style(full_width=False)
234
-
235
- with gr.Column(visible=False):
236
- selected_image = gr.Number(label="Selected index", value=-1, precision=0)
237
-
238
- start_button.click(
239
- fn=start,
240
- inputs=[],
241
- outputs=dataframe,
242
- show_progress=True
243
- ).then(
244
- fn=lambda x: 0 if x == NUM_QUESTIONS else x,
245
- inputs=[row_number],
246
- outputs=[row_number],
247
- ).then(
248
- fn=change_view,
249
- inputs=[row_number, dataframe],
250
- outputs=[intro_view, result_view, gallery_view, result]
251
- ).then(
252
- fn=process, inputs=[dataframe], outputs=[gallery, prompt, counter]
253
- )
254
 
255
- gallery.select(
256
- fn=get_index,
257
- outputs=selected_image,
258
- queue=False,
259
- )
260
 
261
- next_button.click(
262
- fn=write_result,
263
- inputs=[selected_image, row_number, dataframe],
264
- outputs=[row_number, dataframe],
265
- ).then(
266
- fn=change_view,
267
- inputs=[row_number, dataframe],
268
- outputs=[intro_view, result_view, gallery_view, result]
269
- ).then(
270
- fn=process,
271
- inputs=[dataframe, row_number],
272
- outputs=[gallery, prompt, counter]
273
- ).then(
274
- fn=lambda x: 0 if x == NUM_QUESTIONS else x,
275
- inputs=[row_number],
276
- outputs=[row_number],
 
277
  )
278
 
279
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  SUBMISSION_ORG = f"results-{MODEL_KEYS}"
32
  PROMPT_FORMAT = "Pick the picture that best matches the prompt: **{}**"
33
 
 
 
 
 
 
 
 
 
 
 
 
34
  submission_names = list(SUBMISSIONS.keys())
35
  num_images = len(SUBMISSIONS[submission_names[0]])
36
 
 
54
 
55
  return random_hash
56
 
57
+ def load_submissions():
58
+ all_datasets = list_datasets(author=SUBMISSION_ORG)
59
+ relevant_ids = [d.id for d in all_datasets]
60
+
61
+ submitted_ids = []
62
+ for _id in relevant_ids:
63
+ ds = load_dataset(_id)["train"]
64
+ submitted_ids += ds["id"]
65
+
66
+ submitted_ids = Counter(submitted_ids)
67
+ return submitted_ids
68
 
69
+ def refresh(row_number, dataframe):
70
+ if row_number == NUM_QUESTIONS:
71
+ submitted_ids = load_submissions()
72
+ return start(submitted_ids)
73
+ else:
74
+ return dataframe
75
 
76
+ def start(submitted_ids=None):
77
+ if submitted_ids is None:
78
+ submitted_ids = load_submissions()
79
+
80
+ ids = {id: 0 for id in range(num_images)}
81
  ids = {**ids, **submitted_ids}
82
 
83
  # sort by count
 
179
  }
180
 
181
 
182
+ TITLE = "# Community Parti Prompts - Who is your open-source genAI model?"
183
+ DESCRIPTION = """
184
+ *This is an interactive game in which you click through pre-generated images from SD-v1-5, SD-v2.1, Karlo, and IF
185
+ using [Parti Prompts](https://huggingface.co/datasets/nateraw/parti-prompts) prompts.* \n
186
+ *You choices will go into the public community [genAI leaderboard](TODO).*
187
+ """
188
+ EXPLANATION = """\n\n
189
+ ## How it works πŸ“– \n\n
 
 
 
 
 
 
 
 
190
 
191
+ 1. Click on 'Start'
192
+ 2. A prompt and 4 different images are displayed
193
+ 3. Select your favorite image
194
+ 4. Click on 'Select'
195
+ 5. After 10 rounds your favorite diffusion model is displayed
196
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
+ GALLERY_COLUMN_NUM = len(SUBMISSIONS)
199
+
200
+ with gr.Blocks() as demo:
201
+ gr.Markdown(TITLE)
202
+ gr.Markdown(DESCRIPTION)
203
 
204
+ with gr.Column(visible=True) as intro_view:
205
+ gr.Markdown(EXPLANATION)
206
+ start_button = gr.Button("Start").style(full_width=False)
207
+
208
+ headers = ["prompt", "result", "id", "Challenge", "Category", "Note"] + [
209
+ f"choice_{i}" for i in range(len(SUBMISSIONS))
210
+ ]
211
+ datatype = ["str", "str", "number", "str", "str", "str"] + len(SUBMISSIONS) * [
212
+ "number"
213
+ ]
214
+
215
+ with gr.Column(visible=False):
216
+ row_number = gr.Number(
217
+ label="Current row selection index",
218
+ value=0,
219
+ precision=0,
220
+ interactive=False,
221
  )
222
 
223
+ # Create Data Frame
224
+ with gr.Column(visible=False) as result_view:
225
+ result = gr.Markdown("")
226
+ dataframe = gr.Dataframe(
227
+ headers=headers,
228
+ datatype=datatype,
229
+ row_count=NUM_QUESTIONS,
230
+ col_count=(6 + len(SUBMISSIONS), "fixed"),
231
+ interactive=False,
232
+ )
233
+ gr.Markdown("Click on start to play again!")
234
+
235
+ with gr.Column(visible=False) as gallery_view:
236
+ counter = gr.Markdown(f" ### 1/{NUM_QUESTIONS}")
237
+ prompt = gr.Markdown(PROMPT_FORMAT.format(""))
238
+ gallery = gr.Gallery(
239
+ label="All images", show_label=False, elem_id="gallery"
240
+ ).style(columns=GALLERY_COLUMN_NUM, object_fit="contain")
241
+ next_button = gr.Button("Select").style(full_width=False)
242
+
243
+ with gr.Column(visible=False):
244
+ selected_image = gr.Number(label="Selected index", value=-1, precision=0)
245
+
246
+ start_button.click(
247
+ fn=start,
248
+ inputs=[],
249
+ outputs=dataframe,
250
+ show_progress=True
251
+ ).then(
252
+ fn=lambda x: 0 if x == NUM_QUESTIONS else x,
253
+ inputs=[row_number],
254
+ outputs=[row_number],
255
+ ).then(
256
+ fn=change_view,
257
+ inputs=[row_number, dataframe],
258
+ outputs=[intro_view, result_view, gallery_view, result]
259
+ ).then(
260
+ fn=process, inputs=[dataframe], outputs=[gallery, prompt, counter]
261
+ )
262
+
263
+ gallery.select(
264
+ fn=get_index,
265
+ outputs=selected_image,
266
+ queue=False,
267
+ )
268
+
269
+ next_button.click(
270
+ fn=write_result,
271
+ inputs=[selected_image, row_number, dataframe],
272
+ outputs=[row_number, dataframe],
273
+ ).then(
274
+ fn=change_view,
275
+ inputs=[row_number, dataframe],
276
+ outputs=[intro_view, result_view, gallery_view, result]
277
+ ).then(
278
+ fn=process,
279
+ inputs=[dataframe, row_number],
280
+ outputs=[gallery, prompt, counter]
281
+ ).then(
282
+ fn=lambda x: 0 if x == NUM_QUESTIONS else x,
283
+ inputs=[row_number],
284
+ outputs=[row_number],
285
+ ).then(
286
+ fn=refresh,
287
+ inputs=[row_number, dataframe],
288
+ outputs=[dataframe],
289
+ )
290
+
291
+ demo.launch()