bhacquin commited on
Commit
4e8ae89
·
verified ·
1 Parent(s): 6a26852

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +18 -41
demo.py CHANGED
@@ -1,6 +1,4 @@
1
  import datetime
2
- from functools import partial
3
- import json
4
  from pathlib import Path
5
  import random
6
  import gradio as gr
@@ -42,7 +40,6 @@ except Exception as e:
42
  # Data Layer
43
  ##################################################################
44
 
45
-
46
  class Experiment(dict):
47
  """
48
  Represents an experiment consisting of two videos and the user's selection.
@@ -55,7 +52,6 @@ class Experiment(dict):
55
  selected_video=selected_video,
56
  )
57
 
58
-
59
  def experiment_to_dict(experiment, skip=False):
60
  """
61
  Converts the Experiment object to a dictionary for Firebase storage.
@@ -96,7 +92,6 @@ def experiment_to_dict(experiment, skip=False):
96
 
97
  return info
98
 
99
-
100
  def generate_new_experiment() -> Experiment:
101
  """
102
  Generates a new experiment by randomly selecting a sample number and fetching corresponding videos from both folders.
@@ -121,14 +116,12 @@ def generate_new_experiment() -> Experiment:
121
  video_left, video_right = video_omni_path, video_ours_path
122
 
123
  print(f"Generated new experiment with left video: {video_left}, right video: {video_right}")
124
-
125
  return Experiment(
126
  DATASET,
127
  str(video_left),
128
  str(video_right),
129
  )
130
 
131
-
132
  def load_initial():
133
  """
134
  Initializes the first experiment on app load.
@@ -146,14 +139,11 @@ def load_initial():
146
  print(f"Error loading initial experiment: {e}")
147
  return [None, gr.update(value=""), gr.update(value=""), "❌ Failed to load initial videos ❌"]
148
 
149
-
150
  def select_and_load(selected_label, experiment, message_component):
151
  """
152
  Handles the selection of a video by the user.
153
  Saves the selection and loads the next experiment.
154
  """
155
- print(f"User selected: {selected_label}")
156
-
157
  # Update the experiment's selected video
158
  if selected_label.lower() == "left":
159
  experiment["selected_video"] = "left"
@@ -166,7 +156,10 @@ def select_and_load(selected_label, experiment, message_component):
166
  try:
167
  dict_to_save = experiment_to_dict(experiment, skip=False)
168
  firebase_data_ref.push(dict_to_save)
169
- print("Saved selection to Firebase:", dict_to_save)
 
 
 
170
 
171
  # Update the message component with a success message
172
  message_component.value = "✅ Your choice has been saved to Firebase ✅"
@@ -198,18 +191,11 @@ def select_and_load(selected_label, experiment, message_component):
198
  message_component.value
199
  ]
200
 
201
-
202
- def skip_experiment(mode, experiment, message_component):
203
  """
204
  Handles the skipping of an experiment.
205
  Saves the skip and loads the next experiment.
206
  """
207
- if mode != "skip":
208
- print("Skip mode not activated.")
209
- return [experiment, gr.update(), gr.update(), ""]
210
-
211
- print("User chose to skip the current experiment.")
212
-
213
  # Set selected_video to "None" to indicate skip
214
  experiment["selected_video"] = "None"
215
 
@@ -217,7 +203,10 @@ def skip_experiment(mode, experiment, message_component):
217
  try:
218
  dict_to_save = experiment_to_dict(experiment, skip=True)
219
  firebase_data_ref.push(dict_to_save)
220
- print("Saved skip to Firebase:", dict_to_save)
 
 
 
221
 
222
  # Update the message component with a success message
223
  message_component.value = "✅ Your skip has been recorded ✅"
@@ -253,18 +242,7 @@ def skip_experiment(mode, experiment, message_component):
253
  # UI Layer
254
  ##################################################################
255
 
256
-
257
  css = """
258
- #unsel {
259
- border: solid 5px transparent !important;
260
- border-radius: 15px !important;
261
- position: relative;
262
- }
263
- #sel {
264
- border: solid 5px #00c0ff !important;
265
- border-radius: 15px !important;
266
- position: relative;
267
- }
268
  #padded {
269
  padding-left: 2%;
270
  padding-right: 2%;
@@ -296,7 +274,6 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
296
  with gr.Column(scale=3, elem_id="padded"):
297
  gr.Markdown("<div style='width: 100%'><h1 style='text-align: center;'>Choose Your Preferred Video</h1></div>")
298
  gr.Markdown("<div style='width: 100%'><h3 style='text-align: center;'>Select the video you prefer.<br/>⚠️Consider fidelity and quality⚠️</h3></div>")
299
- # Removed the submit button as per requirement
300
  btn_skip = gr.Button("I have no preference")
301
  message_component = gr.Markdown("") # For displaying messages
302
 
@@ -331,27 +308,27 @@ with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
331
  with gr.Row():
332
  # Configure the skip button
333
  btn_skip.click(
334
- fn=partial(skip_experiment, mode="skip"),
335
  inputs=[experiment, message_component],
336
  outputs=[experiment, video_left_component, video_right_component, message_component],
337
- queue=False,
338
- show_progress=False
339
  )
340
 
341
  # Configure the select buttons to handle selection and load next experiment
342
  select_left.click(
343
- fn=partial(select_and_load, selected_label="left"),
344
  inputs=[experiment, message_component],
345
  outputs=[experiment, video_left_component, video_right_component, message_component],
346
- queue=False,
347
- show_progress=False
348
  )
349
  select_right.click(
350
- fn=partial(select_and_load, selected_label="right"),
351
  inputs=[experiment, message_component],
352
  outputs=[experiment, video_left_component, video_right_component, message_component],
353
- queue=False,
354
- show_progress=False
355
  )
356
 
357
  # Load the first experiment on app startup
 
1
  import datetime
 
 
2
  from pathlib import Path
3
  import random
4
  import gradio as gr
 
40
  # Data Layer
41
  ##################################################################
42
 
 
43
  class Experiment(dict):
44
  """
45
  Represents an experiment consisting of two videos and the user's selection.
 
52
  selected_video=selected_video,
53
  )
54
 
 
55
  def experiment_to_dict(experiment, skip=False):
56
  """
57
  Converts the Experiment object to a dictionary for Firebase storage.
 
92
 
93
  return info
94
 
 
95
  def generate_new_experiment() -> Experiment:
96
  """
97
  Generates a new experiment by randomly selecting a sample number and fetching corresponding videos from both folders.
 
116
  video_left, video_right = video_omni_path, video_ours_path
117
 
118
  print(f"Generated new experiment with left video: {video_left}, right video: {video_right}")
 
119
  return Experiment(
120
  DATASET,
121
  str(video_left),
122
  str(video_right),
123
  )
124
 
 
125
  def load_initial():
126
  """
127
  Initializes the first experiment on app load.
 
139
  print(f"Error loading initial experiment: {e}")
140
  return [None, gr.update(value=""), gr.update(value=""), "❌ Failed to load initial videos ❌"]
141
 
 
142
  def select_and_load(selected_label, experiment, message_component):
143
  """
144
  Handles the selection of a video by the user.
145
  Saves the selection and loads the next experiment.
146
  """
 
 
147
  # Update the experiment's selected video
148
  if selected_label.lower() == "left":
149
  experiment["selected_video"] = "left"
 
156
  try:
157
  dict_to_save = experiment_to_dict(experiment, skip=False)
158
  firebase_data_ref.push(dict_to_save)
159
+
160
+ print("=====================")
161
+ print(dict_to_save)
162
+ print("=====================")
163
 
164
  # Update the message component with a success message
165
  message_component.value = "✅ Your choice has been saved to Firebase ✅"
 
191
  message_component.value
192
  ]
193
 
194
+ def skip_experiment(experiment, message_component):
 
195
  """
196
  Handles the skipping of an experiment.
197
  Saves the skip and loads the next experiment.
198
  """
 
 
 
 
 
 
199
  # Set selected_video to "None" to indicate skip
200
  experiment["selected_video"] = "None"
201
 
 
203
  try:
204
  dict_to_save = experiment_to_dict(experiment, skip=True)
205
  firebase_data_ref.push(dict_to_save)
206
+
207
+ print("=====================")
208
+ print(dict_to_save)
209
+ print("=====================")
210
 
211
  # Update the message component with a success message
212
  message_component.value = "✅ Your skip has been recorded ✅"
 
242
  # UI Layer
243
  ##################################################################
244
 
 
245
  css = """
 
 
 
 
 
 
 
 
 
 
246
  #padded {
247
  padding-left: 2%;
248
  padding-right: 2%;
 
274
  with gr.Column(scale=3, elem_id="padded"):
275
  gr.Markdown("<div style='width: 100%'><h1 style='text-align: center;'>Choose Your Preferred Video</h1></div>")
276
  gr.Markdown("<div style='width: 100%'><h3 style='text-align: center;'>Select the video you prefer.<br/>⚠️Consider fidelity and quality⚠️</h3></div>")
 
277
  btn_skip = gr.Button("I have no preference")
278
  message_component = gr.Markdown("") # For displaying messages
279
 
 
308
  with gr.Row():
309
  # Configure the skip button
310
  btn_skip.click(
311
+ fn=skip_experiment,
312
  inputs=[experiment, message_component],
313
  outputs=[experiment, video_left_component, video_right_component, message_component],
314
+ queue=True,
315
+ show_progress=True
316
  )
317
 
318
  # Configure the select buttons to handle selection and load next experiment
319
  select_left.click(
320
+ fn=lambda exp, msg: select_and_load("left", exp, msg),
321
  inputs=[experiment, message_component],
322
  outputs=[experiment, video_left_component, video_right_component, message_component],
323
+ queue=True,
324
+ show_progress=True
325
  )
326
  select_right.click(
327
+ fn=lambda exp, msg: select_and_load("right", exp, msg),
328
  inputs=[experiment, message_component],
329
  outputs=[experiment, video_left_component, video_right_component, message_component],
330
+ queue=True,
331
+ show_progress=True
332
  )
333
 
334
  # Load the first experiment on app startup