Spaces:
Sleeping
Sleeping
Newbyl commited on
Commit ·
e5fe4dc
1
Parent(s): 6a42d89
modif app
Browse files- app.py +25 -7
- requirements.txt +11 -0
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import threading
|
|
| 4 |
from datetime import datetime
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Dict, List, Optional, Set, Tuple
|
|
|
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
import pandas as pd
|
|
@@ -288,10 +289,16 @@ def append_vote(method1: str, method2: str, video_name: str, winner: str):
|
|
| 288 |
"timestamp": ts,
|
| 289 |
}
|
| 290 |
with _write_lock:
|
| 291 |
-
#
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
# Update memory
|
| 296 |
PAIR_COUNTS[(method1, method2)] = PAIR_COUNTS.get((method1, method2), 0) + 1
|
| 297 |
TOTAL_VOTES += 1
|
|
@@ -362,6 +369,12 @@ def on_vote(choice: str, state: dict):
|
|
| 362 |
)
|
| 363 |
|
| 364 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
# ----------------------------
|
| 366 |
# App initialization
|
| 367 |
# ----------------------------
|
|
@@ -395,6 +408,7 @@ initialize()
|
|
| 395 |
|
| 396 |
with gr.Blocks(title="Scientific Video Comparison Study") as demo:
|
| 397 |
gr.Markdown("Compare the two videos and vote. Randomized positions prevent bias.")
|
|
|
|
| 398 |
|
| 399 |
with gr.Row():
|
| 400 |
left_video = gr.Video(label="Left Video", autoplay=True, height=360)
|
|
@@ -416,13 +430,17 @@ with gr.Blocks(title="Scientific Video Comparison Study") as demo:
|
|
| 416 |
btn_right = gr.Button("Right Video is Better", variant="primary")
|
| 417 |
btn_toggle = gr.Button("Play/Pause Both")
|
| 418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
# Wire events
|
| 420 |
demo.load(
|
| 421 |
fn=on_load,
|
| 422 |
inputs=None,
|
| 423 |
outputs=[left_video, right_video, progress, status, state, btn_left, btn_right],
|
| 424 |
)
|
| 425 |
-
|
| 426 |
btn_left.click(
|
| 427 |
fn=lambda s: on_vote("left", s),
|
| 428 |
inputs=[state],
|
|
@@ -433,8 +451,6 @@ with gr.Blocks(title="Scientific Video Comparison Study") as demo:
|
|
| 433 |
inputs=[state],
|
| 434 |
outputs=[left_video, right_video, progress, status, state, btn_left, btn_right],
|
| 435 |
)
|
| 436 |
-
|
| 437 |
-
# Client-side JS to toggle both <video> elements simultaneously
|
| 438 |
btn_toggle.click(
|
| 439 |
fn=None,
|
| 440 |
inputs=None,
|
|
@@ -452,6 +468,8 @@ with gr.Blocks(title="Scientific Video Comparison Study") as demo:
|
|
| 452 |
}
|
| 453 |
"""
|
| 454 |
)
|
|
|
|
|
|
|
| 455 |
|
| 456 |
if __name__ == "__main__":
|
| 457 |
# Launch locally; adjust server_name/port as needed
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Dict, List, Optional, Set, Tuple
|
| 7 |
+
import csv
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
import pandas as pd
|
|
|
|
| 289 |
"timestamp": ts,
|
| 290 |
}
|
| 291 |
with _write_lock:
|
| 292 |
+
# Robust append with immediate flush/fsync so the file is always up-to-date
|
| 293 |
+
VOTES_CSV.parent.mkdir(parents=True, exist_ok=True)
|
| 294 |
+
need_header = (not VOTES_CSV.exists()) or (VOTES_CSV.stat().st_size == 0)
|
| 295 |
+
with open(VOTES_CSV, "a", newline="", encoding="utf-8") as f:
|
| 296 |
+
writer = csv.DictWriter(f, fieldnames=["method1", "method2", "video_name", "winner", "timestamp"])
|
| 297 |
+
if need_header:
|
| 298 |
+
writer.writeheader()
|
| 299 |
+
writer.writerow(row)
|
| 300 |
+
f.flush()
|
| 301 |
+
os.fsync(f.fileno())
|
| 302 |
# Update memory
|
| 303 |
PAIR_COUNTS[(method1, method2)] = PAIR_COUNTS.get((method1, method2), 0) + 1
|
| 304 |
TOTAL_VOTES += 1
|
|
|
|
| 369 |
)
|
| 370 |
|
| 371 |
|
| 372 |
+
def export_votes():
|
| 373 |
+
"""Return the current CSV path for download."""
|
| 374 |
+
ensure_votes_csv()
|
| 375 |
+
# Return path; Gradio will serve it as a downloadable file
|
| 376 |
+
return str(VOTES_CSV)
|
| 377 |
+
|
| 378 |
# ----------------------------
|
| 379 |
# App initialization
|
| 380 |
# ----------------------------
|
|
|
|
| 408 |
|
| 409 |
with gr.Blocks(title="Scientific Video Comparison Study") as demo:
|
| 410 |
gr.Markdown("Compare the two videos and vote. Randomized positions prevent bias.")
|
| 411 |
+
gr.Markdown(f"Votes file: {VOTES_CSV}")
|
| 412 |
|
| 413 |
with gr.Row():
|
| 414 |
left_video = gr.Video(label="Left Video", autoplay=True, height=360)
|
|
|
|
| 430 |
btn_right = gr.Button("Right Video is Better", variant="primary")
|
| 431 |
btn_toggle = gr.Button("Play/Pause Both")
|
| 432 |
|
| 433 |
+
# Add export/download controls
|
| 434 |
+
with gr.Row():
|
| 435 |
+
btn_export = gr.Button("Refresh & Download votes.csv")
|
| 436 |
+
votes_file = gr.File(label="votes.csv", interactive=False)
|
| 437 |
+
|
| 438 |
# Wire events
|
| 439 |
demo.load(
|
| 440 |
fn=on_load,
|
| 441 |
inputs=None,
|
| 442 |
outputs=[left_video, right_video, progress, status, state, btn_left, btn_right],
|
| 443 |
)
|
|
|
|
| 444 |
btn_left.click(
|
| 445 |
fn=lambda s: on_vote("left", s),
|
| 446 |
inputs=[state],
|
|
|
|
| 451 |
inputs=[state],
|
| 452 |
outputs=[left_video, right_video, progress, status, state, btn_left, btn_right],
|
| 453 |
)
|
|
|
|
|
|
|
| 454 |
btn_toggle.click(
|
| 455 |
fn=None,
|
| 456 |
inputs=None,
|
|
|
|
| 468 |
}
|
| 469 |
"""
|
| 470 |
)
|
| 471 |
+
# Export votes.csv
|
| 472 |
+
btn_export.click(fn=export_votes, inputs=None, outputs=[votes_file])
|
| 473 |
|
| 474 |
if __name__ == "__main__":
|
| 475 |
# Launch locally; adjust server_name/port as needed
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
gradio[oauth,mcp]==5.43.1
|
| 3 |
+
huggingface-hub>=0.19
|
| 4 |
+
hf_xet>=1.0.0,<2.0.0
|
| 5 |
+
hf-transfer>=0.1.4
|
| 6 |
+
protobuf<4
|
| 7 |
+
click<8.1
|
| 8 |
+
pydantic~=1.0
|
| 9 |
+
uvicorn>=0.14.0
|
| 10 |
+
spaces
|
| 11 |
+
datasets
|