VOIDER commited on
Commit
7711b92
·
verified ·
1 Parent(s): 899e479

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -22
app.py CHANGED
@@ -430,33 +430,164 @@ def create_interface():
430
  progress_html = gr.HTML(label="Progress") # Keep progress_html if you want to show initial progress
431
  output_html = gr.HTML(label="Evaluation Results")
432
 
433
- def process_images_and_update(files): # Renamed and simplified
434
  global global_results
435
  file_paths = [f.name for f in files]
436
- total = len(file_paths)
437
- progress_html_content = "" # Initialize progress content
438
-
439
- if not file_paths: # Handle no files uploaded
440
  global_results = []
441
- return progress_html_content, evaluator.generate_html_table([]) # Empty table
442
-
443
- progress_html_content = ""
444
- for i, file_path in enumerate(file_paths):
445
- percent = (i / total) * 100
446
- progress_bar = f"""
447
- <div>
448
- <p>Processing {os.path.basename(file_path)}</p>
449
- <progress value="{percent}" max="100"></progress>
450
- <p>{percent:.1f}% complete</p>
451
  </div>
452
  """
453
- progress_html_content = progress_bar # Update progress content
454
- yield progress_html_content, gr.update() # Yield progress update
455
- # No need to process and sort here, just evaluate
456
- global_results = evaluator.process_images_evaluation(file_paths) # Evaluate all images and store
457
- sorted_results = evaluator.sort_results(global_results, sort_by="Final Score") # Initial sort by Final Score
458
- html_table = evaluator.generate_html_table(sorted_results)
459
- yield "<p>Processing complete</p>", html_table # Final progress and table
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
 
461
  def update_table_sort(sort_by_column): # New function for sorting update
462
  global global_results
 
430
  progress_html = gr.HTML(label="Progress") # Keep progress_html if you want to show initial progress
431
  output_html = gr.HTML(label="Evaluation Results")
432
 
433
+ def process_images_and_update(files):
434
  global global_results
435
  file_paths = [f.name for f in files]
436
+ total_files = len(file_paths)
437
+ results = []
438
+
439
+ if not file_paths:
440
  global_results = []
441
+ yield "<p>No files uploaded.</p>", gr.update()
442
+ return
443
+
444
+ # Helper function to generate a styled progress bar HTML snippet.
445
+ def generate_progress_bar(percentage):
446
+ return f"""
447
+ <div style="background-color: #ddd; border-radius: 5px; width: 100%; margin: 10px 0;">
448
+ <div style="width: {percentage:.1f}%; background-color: #4CAF50; text-align: center; padding: 5px 0; border-radius: 5px;">
449
+ {percentage:.1f}%
450
+ </div>
451
  </div>
452
  """
453
+
454
+ total_models = 4 # Total number of model steps per image.
455
+
456
+ for i, file_path in enumerate(file_paths):
457
+ file_name = os.path.basename(file_path)
458
+ try:
459
+ img = Image.open(file_path).convert("RGB")
460
+ except Exception as e:
461
+ yield f"<p>Error opening {file_name}: {e}</p>", gr.update()
462
+ continue
463
+
464
+ # Update overall progress before starting this image.
465
+ overall_percent = (i / total_files) * 100
466
+ overall_bar = generate_progress_bar(overall_percent)
467
+ progress_html = f"""
468
+ <p>{file_name}: Starting evaluation...</p>
469
+ <p>Overall Progress:</p>
470
+ {overall_bar}
471
+ """
472
+ yield progress_html, gr.update()
473
+
474
+ # === Model Step 1: Aesthetic Shadow ===
475
+ model_index = 0
476
+ sub_percent = ((model_index + 1) / total_models) * 100
477
+ sub_bar = generate_progress_bar(sub_percent)
478
+ progress_html = f"""
479
+ <p>{file_name}: Evaluating <strong>Aesthetic Shadow</strong>...</p>
480
+ <p>Current Image Progress:</p>
481
+ {sub_bar}
482
+ <p>Overall Progress:</p>
483
+ {overall_bar}
484
+ """
485
+ yield progress_html, gr.update()
486
+ try:
487
+ shadow_result = evaluator.aesthetic_shadow(images=[img])[0]
488
+ hq_score = [p for p in shadow_result if p['label'] == 'hq'][0]['score']
489
+ aesthetic_shadow_score = np.clip(hq_score * 10.0, 0.0, 10.0)
490
+ except Exception as e:
491
+ print(f"Error in Aesthetic Shadow for {file_name}: {e}")
492
+ aesthetic_shadow_score = None
493
+ yield f"<p>{file_name}: <strong>Aesthetic Shadow</strong> evaluation complete.</p>", gr.update()
494
+
495
+ # === Model Step 2: Waifu Scorer ===
496
+ model_index = 1
497
+ sub_percent = ((model_index + 1) / total_models) * 100
498
+ sub_bar = generate_progress_bar(sub_percent)
499
+ progress_html = f"""
500
+ <p>{file_name}: Evaluating <strong>Waifu Scorer</strong>...</p>
501
+ <p>Current Image Progress:</p>
502
+ {sub_bar}
503
+ <p>Overall Progress:</p>
504
+ {overall_bar}
505
+ """
506
+ yield progress_html, gr.update()
507
+ try:
508
+ waifu_score = evaluator.waifu_scorer([img])[0]
509
+ waifu_score = np.clip(waifu_score, 0.0, 10.0)
510
+ except Exception as e:
511
+ print(f"Error in Waifu Scorer for {file_name}: {e}")
512
+ waifu_score = None
513
+ yield f"<p>{file_name}: <strong>Waifu Scorer</strong> evaluation complete.</p>", gr.update()
514
+
515
+ # === Model Step 3: Aesthetic Predictor V2.5 ===
516
+ model_index = 2
517
+ sub_percent = ((model_index + 1) / total_models) * 100
518
+ sub_bar = generate_progress_bar(sub_percent)
519
+ progress_html = f"""
520
+ <p>{file_name}: Evaluating <strong>Aesthetic Predictor V2.5</strong>...</p>
521
+ <p>Current Image Progress:</p>
522
+ {sub_bar}
523
+ <p>Overall Progress:</p>
524
+ {overall_bar}
525
+ """
526
+ yield progress_html, gr.update()
527
+ try:
528
+ v2_5_score = evaluator.aesthetic_predictor_v2_5.inference(img)
529
+ v2_5_score = float(np.round(np.clip(v2_5_score, 0.0, 10.0), 4))
530
+ except Exception as e:
531
+ print(f"Error in Aesthetic Predictor V2.5 for {file_name}: {e}")
532
+ v2_5_score = None
533
+ yield f"<p>{file_name}: <strong>Aesthetic Predictor V2.5</strong> evaluation complete.</p>", gr.update()
534
+
535
+ # === Model Step 4: Anime Aesthetic ===
536
+ model_index = 3
537
+ sub_percent = ((model_index + 1) / total_models) * 100
538
+ sub_bar = generate_progress_bar(sub_percent)
539
+ progress_html = f"""
540
+ <p>{file_name}: Evaluating <strong>Anime Aesthetic</strong>...</p>
541
+ <p>Current Image Progress:</p>
542
+ {sub_bar}
543
+ <p>Overall Progress:</p>
544
+ {overall_bar}
545
+ """
546
+ yield progress_html, gr.update()
547
+ try:
548
+ img_array = np.array(img)
549
+ anime_score = predict_anime_aesthetic(img_array, evaluator.anime_aesthetic)
550
+ anime_score = np.clip(anime_score * 10.0, 0.0, 10.0)
551
+ except Exception as e:
552
+ print(f"Error in Anime Aesthetic for {file_name}: {e}")
553
+ anime_score = None
554
+ yield f"<p>{file_name}: <strong>Anime Aesthetic</strong> evaluation complete.</p>", gr.update()
555
+
556
+ # === Final Score Calculation and Results Collection ===
557
+ valid_scores = [v for v in [aesthetic_shadow_score, waifu_score, v2_5_score, anime_score] if v is not None]
558
+ final_score = np.clip(np.mean(valid_scores), 0.0, 10.0) if valid_scores else None
559
+
560
+ # Create a thumbnail and store the evaluation results.
561
+ thumbnail = img.copy()
562
+ thumbnail.thumbnail((200, 200))
563
+ img_base64 = evaluator.image_to_base64(thumbnail)
564
+ result = {
565
+ 'file_name': file_name,
566
+ 'img_data': img_base64,
567
+ 'aesthetic_shadow': aesthetic_shadow_score,
568
+ 'waifu_scorer': waifu_score,
569
+ 'aesthetic_predictor_v2_5': v2_5_score,
570
+ 'anime_aesthetic': anime_score,
571
+ 'final_score': final_score
572
+ }
573
+ results.append(result)
574
+
575
+ # Update overall progress for the processed file.
576
+ overall_percent = ((i + 1) / total_files) * 100
577
+ overall_bar = generate_progress_bar(overall_percent)
578
+ progress_html = f"""
579
+ <p>{file_name}: Evaluation complete. ({i + 1}/{total_files} images processed.)</p>
580
+ <p>Overall Progress:</p>
581
+ {overall_bar}
582
+ """
583
+ # Sort the results by Final Score and update the table.
584
+ sorted_results = evaluator.sort_results(results.copy(), sort_by="Final Score")
585
+ html_table = evaluator.generate_html_table(sorted_results)
586
+ yield progress_html, html_table
587
+
588
+ # Sort final results by Final Score.
589
+ global_results = evaluator.sort_results(results, sort_by="Final Score")
590
+ yield "<p>All images processed.</p>", evaluator.generate_html_table(global_results)
591
 
592
  def update_table_sort(sort_by_column): # New function for sorting update
593
  global global_results