Spaces:
Sleeping
Sleeping
Update demo.py
Browse files
demo.py
CHANGED
|
@@ -195,4 +195,149 @@ def select_and_load(selected_label, experiment, message_component):
|
|
| 195 |
def skip_experiment(experiment, message_component):
|
| 196 |
"""
|
| 197 |
Handles the skipping of an experiment.
|
| 198 |
-
Saves the skip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
try:
|
| 201 |
+
# Set selected_video to "None" to indicate skip
|
| 202 |
+
experiment["selected_video"] = "None"
|
| 203 |
+
|
| 204 |
+
# Save the skip to Firebase
|
| 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 |
+
# Set success message
|
| 213 |
+
new_message = "✅ Your skip has been recorded ✅"
|
| 214 |
+
except Exception as e:
|
| 215 |
+
# Set error message
|
| 216 |
+
new_message = 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 |
+
new_message
|
| 229 |
+
]
|
| 230 |
+
except Exception as e:
|
| 231 |
+
# If generating a new experiment fails, notify the user
|
| 232 |
+
new_message = 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 |
+
new_message
|
| 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)
|