bhacquin commited on
Commit
558835b
·
verified ·
1 Parent(s): 929719e

Update demo.py

Browse files
Files changed (1) hide show
  1. demo.py +17 -162
demo.py CHANGED
@@ -145,16 +145,16 @@ def select_and_load(selected_label, experiment, message_component):
145
  Handles the selection of a video by the user.
146
  Saves the selection and loads the next experiment.
147
  """
148
- # Update the experiment's selected video
149
- if selected_label.lower() == "left":
150
- experiment["selected_video"] = "left"
151
- elif selected_label.lower() == "right":
152
- experiment["selected_video"] = "right"
153
- else:
154
- experiment["selected_video"] = None # For safety
155
-
156
- # Save the current selection to Firebase
157
  try:
 
 
 
 
 
 
 
 
 
158
  dict_to_save = experiment_to_dict(experiment, skip=False)
159
  firebase_data_ref.push(dict_to_save)
160
 
@@ -162,11 +162,11 @@ def select_and_load(selected_label, experiment, message_component):
162
  print(dict_to_save)
163
  print("=====================")
164
 
165
- # Update the message component with a success message
166
- message_component.value = "✅ Your choice has been saved to Firebase ✅"
167
  except Exception as e:
168
- # Update the message component with an error message
169
- message_component.value = f"❌ Failed to save your choice: {e} ❌"
170
  print(f"Error saving to Firebase: {e}")
171
 
172
  # Generate the next experiment
@@ -178,166 +178,21 @@ def select_and_load(selected_label, experiment, message_component):
178
  new_experiment,
179
  gr.update(value=new_experiment["video_left"]),
180
  gr.update(value=new_experiment["video_right"]),
181
- message_component.value
182
  ]
183
  except Exception as e:
184
  # If generating a new experiment fails, notify the user
185
- message_component.value = f"❌ Failed to load new videos: {e} ❌"
186
  print(f"Error generating new experiment: {e}")
187
  # Keep the current experiment and videos
188
  return [
189
  experiment,
190
  gr.update(value=experiment["video_left"]),
191
  gr.update(value=experiment["video_right"]),
192
- message_component.value
193
  ]
194
 
195
  def skip_experiment(experiment, message_component):
196
  """
197
  Handles the skipping of an experiment.
198
- Saves the skip and loads the next experiment.
199
- """
200
- # Set selected_video to "None" to indicate skip
201
- experiment["selected_video"] = "None"
202
-
203
- # Save the skip to Firebase
204
- try:
205
- dict_to_save = experiment_to_dict(experiment, skip=True)
206
- firebase_data_ref.push(dict_to_save)
207
-
208
- print("=====================")
209
- print(dict_to_save)
210
- print("=====================")
211
-
212
- # Update the message component with a success message
213
- message_component.value = "✅ Your skip has been recorded ✅"
214
- except Exception as e:
215
- # Update the message component with an error message
216
- message_component.value = f"❌ Failed to record your skip: {e} ❌"
217
- print(f"Error saving skip to Firebase: {e}")
218
-
219
- # Generate the next experiment
220
- try:
221
- new_experiment = generate_new_experiment()
222
-
223
- # Update the video components with new video paths
224
- return [
225
- new_experiment,
226
- gr.update(value=new_experiment["video_left"]),
227
- gr.update(value=new_experiment["video_right"]),
228
- message_component.value
229
- ]
230
- except Exception as e:
231
- # If generating a new experiment fails, notify the user
232
- message_component.value = f"❌ Failed to load new videos: {e} ❌"
233
- print(f"Error generating new experiment: {e}")
234
- # Keep the current experiment and videos
235
- return [
236
- experiment,
237
- gr.update(value=experiment["video_left"]),
238
- gr.update(value=experiment["video_right"]),
239
- message_component.value
240
- ]
241
-
242
- ##################################################################
243
- # UI Layer
244
- ##################################################################
245
-
246
- css = """
247
- #padded {
248
- padding-left: 2%;
249
- padding-right: 2%;
250
- }
251
- .select-button {
252
- margin-top: 10px;
253
- width: 100%;
254
- }
255
- .select-button:hover {
256
- background-color: #00c0ff;
257
- color: white;
258
- }
259
- .video-container {
260
- display: flex;
261
- justify-content: center;
262
- align-items: center;
263
- }
264
- video {
265
- width: 100%;
266
- height: auto;
267
- }
268
- """
269
-
270
- with gr.Blocks(title="Unsupervised Video Editing", css=css) as demo:
271
- # Initialize the state
272
- experiment = gr.State()
273
-
274
- with gr.Row(elem_id="padded"):
275
- with gr.Column(scale=3, elem_id="padded"):
276
- gr.Markdown("<div style='width: 100%'><h1 style='text-align: center;'>Choose Your Preferred Video</h1></div>")
277
- gr.Markdown("<div style='width: 100%'><h3 style='text-align: center;'>Select the video you prefer.<br/>⚠️Consider fidelity and quality⚠️</h3></div>")
278
- btn_skip = gr.Button("I have no preference")
279
- message_component = gr.Markdown("") # For displaying messages
280
-
281
- with gr.Row():
282
- # Define both video components first
283
- with gr.Column(scale=1):
284
- video_left_component = gr.Video(
285
- label="left",
286
- elem_id="video-left",
287
- show_label=False,
288
- show_download_button=False,
289
- show_share_button=False,
290
- interactive=True
291
- )
292
- with gr.Column(scale=1):
293
- video_right_component = gr.Video(
294
- label="right",
295
- elem_id="video-right",
296
- show_label=False,
297
- show_download_button=False,
298
- show_share_button=False,
299
- interactive=True
300
- )
301
-
302
- with gr.Row():
303
- # Add Select buttons below each video
304
- with gr.Column(scale=1):
305
- select_left = gr.Button("Select Left Video", elem_id="select-button-left", variant="primary")
306
- with gr.Column(scale=1):
307
- select_right = gr.Button("Select Right Video", elem_id="select-button-right", variant="primary")
308
-
309
- with gr.Row():
310
- # Configure the skip button
311
- btn_skip.click(
312
- fn=skip_experiment,
313
- inputs=[experiment, message_component],
314
- outputs=[experiment, video_left_component, video_right_component, message_component],
315
- queue=True,
316
- show_progress=True
317
- )
318
-
319
- # Configure the select buttons to handle selection and load next experiment
320
- select_left.click(
321
- fn=lambda exp, msg: select_and_load("left", exp, msg),
322
- inputs=[experiment, message_component],
323
- outputs=[experiment, video_left_component, video_right_component, message_component],
324
- queue=True,
325
- show_progress=True
326
- )
327
- select_right.click(
328
- fn=lambda exp, msg: select_and_load("right", exp, msg),
329
- inputs=[experiment, message_component],
330
- outputs=[experiment, video_left_component, video_right_component, message_component],
331
- queue=True,
332
- show_progress=True
333
- )
334
-
335
- # Load the first experiment on app startup
336
- demo.load(
337
- fn=load_initial,
338
- inputs=None,
339
- outputs=[experiment, video_left_component, video_right_component, message_component]
340
- )
341
-
342
- # Launch the app with share=True to create a public link
343
- demo.launch(share=True)
 
145
  Handles the selection of a video by the user.
146
  Saves the selection and loads the next experiment.
147
  """
 
 
 
 
 
 
 
 
 
148
  try:
149
+ # Update the experiment's selected video
150
+ if selected_label.lower() == "left":
151
+ experiment["selected_video"] = "left"
152
+ elif selected_label.lower() == "right":
153
+ experiment["selected_video"] = "right"
154
+ else:
155
+ experiment["selected_video"] = None # For safety
156
+
157
+ # Save the current selection to Firebase
158
  dict_to_save = experiment_to_dict(experiment, skip=False)
159
  firebase_data_ref.push(dict_to_save)
160
 
 
162
  print(dict_to_save)
163
  print("=====================")
164
 
165
+ # Set success message
166
+ new_message = "✅ Your choice has been saved to Firebase ✅"
167
  except Exception as e:
168
+ # Set error message
169
+ new_message = f"❌ Failed to save your choice: {e} ❌"
170
  print(f"Error saving to Firebase: {e}")
171
 
172
  # Generate the next experiment
 
178
  new_experiment,
179
  gr.update(value=new_experiment["video_left"]),
180
  gr.update(value=new_experiment["video_right"]),
181
+ new_message
182
  ]
183
  except Exception as e:
184
  # If generating a new experiment fails, notify the user
185
+ new_message = f"❌ Failed to load new videos: {e} ❌"
186
  print(f"Error generating new experiment: {e}")
187
  # Keep the current experiment and videos
188
  return [
189
  experiment,
190
  gr.update(value=experiment["video_left"]),
191
  gr.update(value=experiment["video_right"]),
192
+ new_message
193
  ]
194
 
195
  def skip_experiment(experiment, message_component):
196
  """
197
  Handles the skipping of an experiment.
198
+ Saves the skip an