taesiri commited on
Commit
a7ff383
Β·
1 Parent(s): a167ff0
Files changed (1) hide show
  1. app.py +328 -174
app.py CHANGED
@@ -312,190 +312,348 @@ def update_button_styles(verdict):
312
  return a_better_style, b_better_style, neither_style, tie_style
313
 
314
 
315
- # Create Gradio interface
316
- with gr.Blocks() as demo:
 
 
 
 
 
317
 
318
- # Add instruction panel at the top
319
- gr.HTML(
320
- """
321
- <div style="padding: 0.8rem; margin-bottom: 0.8rem; border-radius: 0.5rem; color: white; text-align: center;">
322
- <div style="font-size: 1.2rem; margin-bottom: 0.5rem;">Read the user instruction, look at the source image, then evaluate which edit (A or B) best satisfies the request better.</div>
323
- <div style="font-size: 1rem;">
324
- <strong>🀝 Tie</strong> &nbsp;&nbsp;|&nbsp;&nbsp;
325
- <strong>πŸ‘ˆ A is better</strong> &nbsp;&nbsp;|&nbsp;&nbsp;
326
- <strong>πŸ‘‰ B is better</strong>
327
- </div>
328
- <div style="color: #ff4444; font-size: 0.9rem; margin-top: 0.5rem;">
329
- Please ignore any watermark on the image. Your rating should not be affected by any watermark on the image.
330
- </div>
331
- </div>
332
- """
333
- )
334
 
335
- with gr.Row():
336
- simplified_instruction = gr.Textbox(
337
- label="Simplified Instruction", show_label=True, visible=False
338
- )
339
- instruction = gr.Markdown(label="Original Instruction", show_label=True)
340
-
341
- with gr.Row():
342
- with gr.Column():
343
- source_image = gr.Image(label="Source Image", show_label=True, height=500)
344
- gr.HTML("<h2 style='text-align: center;'>Source Image</h2>")
345
- tie_btn = gr.Button("🀝 Tie", variant="secondary")
346
- with gr.Column():
347
- image_a = gr.Image(label="Image A", show_label=True, height=500)
348
- gr.HTML("<h2 style='text-align: center;'>Image A</h2>")
349
- a_better_btn = gr.Button("πŸ‘ˆ A is better", variant="secondary")
350
- with gr.Column():
351
- image_b = gr.Image(label="Image B", show_label=True, height=500)
352
- gr.HTML("<h2 style='text-align: center;'>Image B</h2>")
353
- b_better_btn = gr.Button("πŸ‘‰ B is better", variant="secondary")
354
-
355
- # Add confirmation button in new row
356
- with gr.Row():
357
- confirm_btn = gr.Button("Confirm Selection", variant="primary", visible=False)
358
- with gr.Row():
359
- neither_btn = gr.Button("πŸ‘Ž Both are bad", variant="secondary", visible=False)
360
-
361
- with gr.Accordion("DEBUG", open=False):
362
- with gr.Column():
363
- post_id_display = gr.Textbox(
364
- label="Post ID", show_label=True, interactive=False
 
 
 
365
  )
366
- model_info = gr.Textbox(label="Model Information", show_label=True)
367
- simplified_instruction_debug = gr.Textbox(
368
- label="Simplified Instruction", show_label=True, interactive=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  )
370
- state = gr.State()
371
- selected_verdict = gr.State()
372
-
373
- # Add states for button selection
374
- a_better_selected = gr.Checkbox(visible=False)
375
- b_better_selected = gr.Checkbox(visible=False)
376
- neither_selected = gr.Checkbox(visible=False)
377
- tie_selected = gr.Checkbox(visible=False)
378
-
379
- def update_confirm_visibility(a_better, b_better, neither, tie):
380
- # Update button text based on selection
381
- if a_better:
382
- return gr.update(visible=True, value="Confirm A is better")
383
- elif b_better:
384
- return gr.update(visible=True, value="Confirm B is better")
385
- elif neither:
386
- return gr.update(visible=True, value="Confirm Neither is good")
387
- elif tie:
388
- return gr.update(visible=True, value="Confirm Tie")
389
- return gr.update(visible=False)
390
-
391
- # Initialize the interface
392
- demo.load(
393
- initialize,
394
- outputs=[
395
- source_image,
396
- image_a,
397
- image_b,
398
- instruction,
399
- simplified_instruction,
400
- model_info,
401
- state,
402
- selected_verdict,
403
- a_better_selected,
404
- b_better_selected,
405
- neither_selected,
406
- tie_selected,
407
- post_id_display,
408
- simplified_instruction_debug,
409
- ],
410
- )
411
 
412
- # Handle first step button clicks
413
- a_better_btn.click(
414
- lambda state: select_verdict("A is better", state),
415
- inputs=[state],
416
- outputs=[
417
- selected_verdict,
418
- a_better_selected,
419
- b_better_selected,
420
- neither_selected,
421
- tie_selected,
422
- ],
423
- ).then(
424
- update_button_styles,
425
- inputs=[selected_verdict],
426
- outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
427
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
 
429
- b_better_btn.click(
430
- lambda state: select_verdict("B is better", state),
431
- inputs=[state],
432
- outputs=[
433
- selected_verdict,
434
- a_better_selected,
435
- b_better_selected,
436
- neither_selected,
437
- tie_selected,
438
- ],
439
- ).then(
440
- update_button_styles,
441
- inputs=[selected_verdict],
442
- outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
443
- )
 
444
 
445
- neither_btn.click(
446
- lambda state: select_verdict("Neither is good", state),
447
- inputs=[state],
448
- outputs=[
449
- selected_verdict,
450
- a_better_selected,
451
- b_better_selected,
452
- neither_selected,
453
- tie_selected,
454
- ],
455
- ).then(
456
- update_button_styles,
457
- inputs=[selected_verdict],
458
- outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
459
- )
460
 
461
- tie_btn.click(
462
- lambda state: select_verdict("Tie", state),
463
- inputs=[state],
464
- outputs=[
465
- selected_verdict,
466
- a_better_selected,
467
- b_better_selected,
468
- neither_selected,
469
- tie_selected,
470
- ],
471
- ).then(
472
- update_button_styles,
473
- inputs=[selected_verdict],
474
- outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
475
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
 
477
- # Update confirm button visibility when selection changes
478
- for checkbox in [
479
- a_better_selected,
480
- b_better_selected,
481
- neither_selected,
482
- tie_selected,
483
- ]:
484
- checkbox.change(
485
- update_confirm_visibility,
486
- inputs=[
487
  a_better_selected,
488
  b_better_selected,
489
  neither_selected,
490
  tie_selected,
491
- ],
492
- outputs=[confirm_btn],
493
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
 
495
- # Handle confirmation button click
496
- confirm_btn.click(
497
- lambda verdict, state: evaluate(verdict, state),
498
- inputs=[selected_verdict, state],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
  outputs=[
500
  source_image,
501
  image_a,
@@ -509,10 +667,6 @@ with gr.Blocks() as demo:
509
  b_better_selected,
510
  neither_selected,
511
  tie_selected,
512
- a_better_btn,
513
- b_better_btn,
514
- neither_btn,
515
- tie_btn,
516
  post_id_display,
517
  simplified_instruction_debug,
518
  ],
 
312
  return a_better_style, b_better_style, neither_style, tie_style
313
 
314
 
315
+ # Add at the top after imports
316
+ def create_instruction_page(html_content, image_path=None):
317
+ """Helper function to create consistent instruction pages"""
318
+ with gr.Column():
319
+ gr.HTML(html_content)
320
+ if image_path:
321
+ gr.Image(image_path, container=False)
322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
+ # Add before the main Gradio interface
325
+ def handle_username_submit(username, current_page):
326
+ """Handle username submission and advance to next page"""
327
+ if not username or len(username.strip()) < 2:
328
+ # Just stay on current page if validation fails
329
+ return current_page, gr.update(value=username)
330
+ return 2, gr.update(value="") # Advance to page 2 and clear the input
331
+
332
+
333
+ def advance_page(current_page):
334
+ """Handle next button clicks to advance pages"""
335
+ return current_page + 1
336
+
337
+
338
+ # Modify the main interface
339
+ with gr.Blocks() as demo:
340
+ # Add states for page management and user info
341
+ current_page = gr.State(1) # Start at page 1
342
+ username_state = gr.State(None)
343
+
344
+ # Create container for all pages
345
+ with gr.Column() as page_container:
346
+ # Page 1 - Username Collection
347
+ with gr.Column(visible=True) as page1:
348
+ create_instruction_page(
349
+ """
350
+ <div style="text-align: center; padding: 20px;">
351
+ <h1>Welcome to the Image Edit Evaluation</h1>
352
+ <p>Help us evaluate AI-generated image edits!</p>
353
+ <!-- Add more welcome content here -->
354
+ </div>
355
+ """,
356
+ image_path="./instructions/home.jpg",
357
  )
358
+ username_input = gr.Textbox(
359
+ label="Please enter your username",
360
+ placeholder="Username (min 2 characters)",
361
+ )
362
+ validation_text = gr.HTML(
363
+ visible=False,
364
+ value='<p style="color: red;">Please enter a valid username (min 2 characters)</p>',
365
+ )
366
+ start_btn = gr.Button("Start", variant="primary")
367
+
368
+ # Page 2 - First instruction page
369
+ with gr.Column(visible=False) as page2:
370
+ create_instruction_page(
371
+ """
372
+ <div style="text-align: center; padding: 20px;">
373
+ <h1>How to Evaluate Edits</h1>
374
+ <p>You'll be shown pairs of edited images...</p>
375
+ <!-- Add instruction content here -->
376
+ </div>
377
+ """,
378
+ image_path="./instructions/page2.jpg", # Replace with actual image path
379
+ )
380
+ next_btn1 = gr.Button("Next", variant="primary")
381
+
382
+ # Page 3 - Second instruction page
383
+ with gr.Column(visible=False) as page3:
384
+ create_instruction_page(
385
+ """
386
+ <div style="text-align: center; padding: 20px;">
387
+ <h1>Making Your Decision</h1>
388
+ <p>Compare the edits carefully...</p>
389
+ <!-- Add more instruction content here -->
390
+ </div>
391
+ """,
392
+ image_path="./instructions/page3.jpg", # Replace with actual image path
393
+ )
394
+ next_btn2 = gr.Button("Start Evaluation", variant="primary")
395
+
396
+ # Main Evaluation UI (existing code)
397
+ with gr.Column(visible=False) as main_ui:
398
+ # ... existing evaluation UI code ...
399
+ # Move all the existing UI elements here
400
+
401
+ # Add instruction panel at the top
402
+ gr.HTML(
403
+ """
404
+ <div style="padding: 0.8rem; margin-bottom: 0.8rem; border-radius: 0.5rem; color: white; text-align: center;">
405
+ <div style="font-size: 1.2rem; margin-bottom: 0.5rem;">Read the user instruction, look at the source image, then evaluate which edit (A or B) best satisfies the request better.</div>
406
+ <div style="font-size: 1rem;">
407
+ <strong>🀝 Tie</strong> &nbsp;&nbsp;|&nbsp;&nbsp;
408
+ <strong>πŸ‘ˆ A is better</strong> &nbsp;&nbsp;|&nbsp;&nbsp;
409
+ <strong>πŸ‘‰ B is better</strong>
410
+ </div>
411
+ <div style="color: #ff4444; font-size: 0.9rem; margin-top: 0.5rem;">
412
+ Please ignore any watermark on the image. Your rating should not be affected by any watermark on the image.
413
+ </div>
414
+ </div>
415
+ """
416
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
 
418
+ with gr.Row():
419
+ simplified_instruction = gr.Textbox(
420
+ label="Simplified Instruction", show_label=True, visible=False
421
+ )
422
+ instruction = gr.Markdown(label="Original Instruction", show_label=True)
423
+
424
+ with gr.Row():
425
+ with gr.Column():
426
+ source_image = gr.Image(
427
+ label="Source Image", show_label=True, height=500
428
+ )
429
+ gr.HTML("<h2 style='text-align: center;'>Source Image</h2>")
430
+ tie_btn = gr.Button("🀝 Tie", variant="secondary")
431
+ with gr.Column():
432
+ image_a = gr.Image(label="Image A", show_label=True, height=500)
433
+ gr.HTML("<h2 style='text-align: center;'>Image A</h2>")
434
+ a_better_btn = gr.Button("πŸ‘ˆ A is better", variant="secondary")
435
+ with gr.Column():
436
+ image_b = gr.Image(label="Image B", show_label=True, height=500)
437
+ gr.HTML("<h2 style='text-align: center;'>Image B</h2>")
438
+ b_better_btn = gr.Button("πŸ‘‰ B is better", variant="secondary")
439
+
440
+ # Add confirmation button in new row
441
+ with gr.Row():
442
+ confirm_btn = gr.Button(
443
+ "Confirm Selection", variant="primary", visible=False
444
+ )
445
+ with gr.Row():
446
+ neither_btn = gr.Button(
447
+ "πŸ‘Ž Both are bad", variant="secondary", visible=False
448
+ )
449
+
450
+ with gr.Accordion("DEBUG", open=False):
451
+ with gr.Column():
452
+ post_id_display = gr.Textbox(
453
+ label="Post ID", show_label=True, interactive=False
454
+ )
455
+ model_info = gr.Textbox(label="Model Information", show_label=True)
456
+ simplified_instruction_debug = gr.Textbox(
457
+ label="Simplified Instruction",
458
+ show_label=True,
459
+ interactive=False,
460
+ )
461
+ state = gr.State()
462
+ selected_verdict = gr.State()
463
+
464
+ # Add states for button selection
465
+ a_better_selected = gr.Checkbox(visible=False)
466
+ b_better_selected = gr.Checkbox(visible=False)
467
+ neither_selected = gr.Checkbox(visible=False)
468
+ tie_selected = gr.Checkbox(visible=False)
469
+
470
+ def update_confirm_visibility(a_better, b_better, neither, tie):
471
+ # Update button text based on selection
472
+ if a_better:
473
+ return gr.update(visible=True, value="Confirm A is better")
474
+ elif b_better:
475
+ return gr.update(visible=True, value="Confirm B is better")
476
+ elif neither:
477
+ return gr.update(visible=True, value="Confirm Neither is good")
478
+ elif tie:
479
+ return gr.update(visible=True, value="Confirm Tie")
480
+ return gr.update(visible=False)
481
+
482
+ # Initialize the interface
483
+ demo.load(
484
+ initialize,
485
+ outputs=[
486
+ source_image,
487
+ image_a,
488
+ image_b,
489
+ instruction,
490
+ simplified_instruction,
491
+ model_info,
492
+ state,
493
+ selected_verdict,
494
+ a_better_selected,
495
+ b_better_selected,
496
+ neither_selected,
497
+ tie_selected,
498
+ post_id_display,
499
+ simplified_instruction_debug,
500
+ ],
501
+ )
502
 
503
+ # Handle first step button clicks
504
+ a_better_btn.click(
505
+ lambda state: select_verdict("A is better", state),
506
+ inputs=[state],
507
+ outputs=[
508
+ selected_verdict,
509
+ a_better_selected,
510
+ b_better_selected,
511
+ neither_selected,
512
+ tie_selected,
513
+ ],
514
+ ).then(
515
+ update_button_styles,
516
+ inputs=[selected_verdict],
517
+ outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
518
+ )
519
 
520
+ b_better_btn.click(
521
+ lambda state: select_verdict("B is better", state),
522
+ inputs=[state],
523
+ outputs=[
524
+ selected_verdict,
525
+ a_better_selected,
526
+ b_better_selected,
527
+ neither_selected,
528
+ tie_selected,
529
+ ],
530
+ ).then(
531
+ update_button_styles,
532
+ inputs=[selected_verdict],
533
+ outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
534
+ )
535
 
536
+ neither_btn.click(
537
+ lambda state: select_verdict("Neither is good", state),
538
+ inputs=[state],
539
+ outputs=[
540
+ selected_verdict,
541
+ a_better_selected,
542
+ b_better_selected,
543
+ neither_selected,
544
+ tie_selected,
545
+ ],
546
+ ).then(
547
+ update_button_styles,
548
+ inputs=[selected_verdict],
549
+ outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
550
+ )
551
+
552
+ tie_btn.click(
553
+ lambda state: select_verdict("Tie", state),
554
+ inputs=[state],
555
+ outputs=[
556
+ selected_verdict,
557
+ a_better_selected,
558
+ b_better_selected,
559
+ neither_selected,
560
+ tie_selected,
561
+ ],
562
+ ).then(
563
+ update_button_styles,
564
+ inputs=[selected_verdict],
565
+ outputs=[a_better_btn, b_better_btn, neither_btn, tie_btn],
566
+ )
567
 
568
+ # Update confirm button visibility when selection changes
569
+ for checkbox in [
 
 
 
 
 
 
 
 
570
  a_better_selected,
571
  b_better_selected,
572
  neither_selected,
573
  tie_selected,
574
+ ]:
575
+ checkbox.change(
576
+ update_confirm_visibility,
577
+ inputs=[
578
+ a_better_selected,
579
+ b_better_selected,
580
+ neither_selected,
581
+ tie_selected,
582
+ ],
583
+ outputs=[confirm_btn],
584
+ )
585
+
586
+ # Handle confirmation button click
587
+ confirm_btn.click(
588
+ lambda verdict, state: evaluate(verdict, state),
589
+ inputs=[selected_verdict, state],
590
+ outputs=[
591
+ source_image,
592
+ image_a,
593
+ image_b,
594
+ instruction,
595
+ simplified_instruction,
596
+ model_info,
597
+ state,
598
+ selected_verdict,
599
+ a_better_selected,
600
+ b_better_selected,
601
+ neither_selected,
602
+ tie_selected,
603
+ a_better_btn,
604
+ b_better_btn,
605
+ neither_btn,
606
+ tie_btn,
607
+ post_id_display,
608
+ simplified_instruction_debug,
609
+ ],
610
+ )
611
 
612
+ # Handle page visibility
613
+ def update_page_visibility(page_num):
614
+ """Return visibility updates for each page column"""
615
+ return [
616
+ gr.update(visible=(page_num == 1)), # page1
617
+ gr.update(visible=(page_num == 2)), # page2
618
+ gr.update(visible=(page_num == 3)), # page3
619
+ gr.update(visible=(page_num == 4)), # main_ui
620
+ ]
621
+
622
+ # Connect button clicks to page navigation
623
+ start_btn.click(
624
+ handle_username_submit,
625
+ inputs=[username_input, current_page],
626
+ outputs=[current_page, username_input],
627
+ ).then(
628
+ update_page_visibility,
629
+ inputs=[current_page],
630
+ outputs=[page1, page2, page3, main_ui], # Control column visibility
631
+ ).then(
632
+ lambda x: gr.update(visible=x is None),
633
+ inputs=[current_page],
634
+ outputs=[validation_text],
635
+ )
636
+
637
+ next_btn1.click(
638
+ advance_page,
639
+ inputs=[current_page],
640
+ outputs=current_page,
641
+ ).then(
642
+ update_page_visibility,
643
+ inputs=[current_page],
644
+ outputs=[page1, page2, page3, main_ui], # Control column visibility
645
+ )
646
+
647
+ next_btn2.click(
648
+ advance_page,
649
+ inputs=[current_page],
650
+ outputs=current_page,
651
+ ).then(
652
+ update_page_visibility,
653
+ inputs=[current_page],
654
+ outputs=[page1, page2, page3, main_ui], # Control column visibility
655
+ ).then(
656
+ initialize,
657
  outputs=[
658
  source_image,
659
  image_a,
 
667
  b_better_selected,
668
  neither_selected,
669
  tie_selected,
 
 
 
 
670
  post_id_display,
671
  simplified_instruction_debug,
672
  ],