Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import tempfile
|
| 4 |
-
import
|
| 5 |
-
from io import BytesIO, StringIO
|
| 6 |
-
import csv
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
import logging
|
| 9 |
|
| 10 |
import cv2
|
| 11 |
import numpy as np
|
|
|
|
| 12 |
import torch
|
| 13 |
import onnxruntime as rt
|
| 14 |
from PIL import Image
|
| 15 |
import gradio as gr
|
| 16 |
-
from transformers import pipeline
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if isinstance(images, list):
|
| 41 |
-
num_images = len(images)
|
| 42 |
-
return {"pixel_values": torch.randn(num_images, 3, 224, 224)}
|
| 43 |
-
else:
|
| 44 |
-
return {"pixel_values": torch.randn(1, 3, 224, 224)}
|
| 45 |
-
class MockModel:
|
| 46 |
-
def __init__(self): self._parameters = {"dummy": torch.nn.Parameter(torch.empty(0))}
|
| 47 |
-
def __call__(self, pixel_values):
|
| 48 |
-
bs = pixel_values.shape[0]
|
| 49 |
class Output:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
class MLP(torch.nn.Module):
|
|
|
|
| 67 |
def __init__(self, input_size: int, batch_norm: bool = True):
|
| 68 |
super().__init__()
|
| 69 |
self.input_size = input_size
|
| 70 |
-
layers =
|
| 71 |
torch.nn.Linear(self.input_size, 2048), torch.nn.ReLU(),
|
| 72 |
torch.nn.BatchNorm1d(2048) if batch_norm else torch.nn.Identity(), torch.nn.Dropout(0.3),
|
| 73 |
torch.nn.Linear(2048, 512), torch.nn.ReLU(),
|
|
@@ -78,916 +81,461 @@ class MLP(torch.nn.Module):
|
|
| 78 |
torch.nn.BatchNorm1d(128) if batch_norm else torch.nn.Identity(), torch.nn.Dropout(0.1),
|
| 79 |
torch.nn.Linear(128, 32), torch.nn.ReLU(),
|
| 80 |
torch.nn.Linear(32, 1)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 85 |
-
return self.layers(x)
|
| 86 |
-
|
| 87 |
|
| 88 |
-
class
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
self.device = device
|
| 92 |
-
self.
|
| 93 |
-
self.
|
| 94 |
-
self.
|
| 95 |
-
self.
|
| 96 |
-
self.mlp = None
|
| 97 |
-
|
| 98 |
-
try:
|
| 99 |
-
import clip # Dynamically import clip
|
| 100 |
-
if model_path is None:
|
| 101 |
-
model_path = "Eugeoter/waifu-scorer-v3/model.pth"
|
| 102 |
-
if self.verbose: logger.info(f"WaifuScorer model path not provided. Using default: {model_path}")
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
# Assuming model_path like "user/repo/file.pth" for hf_hub_download
|
| 107 |
-
parts = model_path.split("/")
|
| 108 |
-
if len(parts) >= 3:
|
| 109 |
-
repo_id_parts = parts[:-1]
|
| 110 |
-
filename = parts[-1]
|
| 111 |
-
repo_id_str = "/".join(repo_id_parts)
|
| 112 |
-
model_path_resolved = hf_hub_download(repo_id=repo_id_str, filename=filename, cache_dir=cache_dir)
|
| 113 |
-
else: # try as repo_id and assume model.pth or common name
|
| 114 |
-
model_path_resolved = hf_hub_download(repo_id=model_path, filename="model.pth", cache_dir=cache_dir) # fallback filename
|
| 115 |
-
except Exception as e:
|
| 116 |
-
logger.error(f"Failed to download WaifuScorer model from HF Hub ({model_path}): {e}")
|
| 117 |
-
# Try a more specific default if the generic one failed
|
| 118 |
-
logger.info("Attempting to download specific WaifuScorer model Eugeoter/waifu-scorer-v3/model.pth")
|
| 119 |
-
model_path_resolved = hf_hub_download("Eugeoter/waifu-scorer-v3", "model.pth", cache_dir=cache_dir)
|
| 120 |
-
model_path = model_path_resolved
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
self.mlp = MLP(input_size=768)
|
| 126 |
-
if
|
| 127 |
from safetensors.torch import load_file
|
| 128 |
-
state_dict = load_file(
|
| 129 |
else:
|
| 130 |
-
state_dict = torch.load(
|
| 131 |
-
|
| 132 |
-
# Adjust keys if necessary (e.g. if saved from DataParallel)
|
| 133 |
-
if any(key.startswith("module.") for key in state_dict.keys()):
|
| 134 |
-
state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()}
|
| 135 |
-
|
| 136 |
self.mlp.load_state_dict(state_dict)
|
| 137 |
-
self.mlp.to(
|
| 138 |
-
self.mlp.eval()
|
| 139 |
|
| 140 |
-
|
| 141 |
-
self.
|
| 142 |
-
|
| 143 |
except ImportError:
|
| 144 |
-
|
| 145 |
except Exception as e:
|
| 146 |
-
|
| 147 |
|
| 148 |
@torch.no_grad()
|
| 149 |
-
def
|
| 150 |
-
if not self.
|
| 151 |
-
return [None] * len(images)
|
| 152 |
|
| 153 |
-
if not images:
|
| 154 |
-
return []
|
| 155 |
-
|
| 156 |
original_n = len(images)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
|
| 160 |
try:
|
| 161 |
-
image_tensors = [self.
|
| 162 |
-
|
| 163 |
-
image_features = self.clip_model.encode_image(image_batch)
|
| 164 |
-
|
| 165 |
norm = image_features.norm(p=2, dim=-1, keepdim=True)
|
| 166 |
-
norm
|
| 167 |
-
im_emb = (image_features / norm).to(device=self.device, dtype=
|
| 168 |
|
| 169 |
predictions = self.mlp(im_emb)
|
| 170 |
-
scores = predictions.clamp(0, 10).cpu().numpy().
|
| 171 |
return scores[:original_n]
|
| 172 |
except Exception as e:
|
| 173 |
-
|
| 174 |
return [None] * original_n
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
|
| 187 |
@torch.no_grad()
|
| 188 |
-
def
|
| 189 |
-
if not images
|
| 190 |
-
return []
|
| 191 |
try:
|
| 192 |
-
|
| 193 |
-
pixel_values = self.
|
| 194 |
-
if self.device == 'cuda' and torch.cuda.is_available():
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
scores = [scores_tensor.item()]
|
| 200 |
-
else:
|
| 201 |
-
scores = scores_tensor.tolist()
|
| 202 |
-
return [round(max(0.0, min(s, 10.0)), 4) for s in scores] # Clip and round
|
| 203 |
except Exception as e:
|
| 204 |
-
|
| 205 |
return [None] * len(images)
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
model_path = hf_hub_download(repo_id="skytnt/anime-aesthetic", filename="model.onnx", cache_dir=cache_dir)
|
| 210 |
-
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if torch.cuda.is_available() else ['CPUExecutionProvider']
|
| 211 |
-
session = rt.InferenceSession(model_path, providers=providers)
|
| 212 |
-
logger.info(f"Anime Aesthetic ONNX model loaded with providers: {session.get_providers()}")
|
| 213 |
-
return session
|
| 214 |
-
except Exception as e:
|
| 215 |
-
logger.error(f"Failed to load Anime Aesthetic ONNX model: {e}")
|
| 216 |
-
return None
|
| 217 |
-
|
| 218 |
-
def preprocess_anime_aesthetic_batch(images_pil: list[Image.Image], target_size: int = 768) -> np.ndarray | None:
|
| 219 |
-
if not images_pil:
|
| 220 |
-
return None
|
| 221 |
-
batch_canvases = []
|
| 222 |
-
try:
|
| 223 |
-
for img_pil in images_pil:
|
| 224 |
-
img_np = np.array(img_pil.convert("RGB")).astype(np.float32) / 255.0
|
| 225 |
-
h, w = img_np.shape[:2]
|
| 226 |
-
if h > w:
|
| 227 |
-
new_h, new_w = target_size, int(target_size * w / h)
|
| 228 |
-
else:
|
| 229 |
-
new_h, new_w = int(target_size * h / w), target_size
|
| 230 |
-
|
| 231 |
-
resized = cv2.resize(img_np, (new_w, new_h), interpolation=cv2.INTER_AREA)
|
| 232 |
-
canvas = np.zeros((target_size, target_size, 3), dtype=np.float32)
|
| 233 |
-
pad_h = (target_size - new_h) // 2
|
| 234 |
-
pad_w = (target_size - new_w) // 2
|
| 235 |
-
canvas[pad_h:pad_h+new_h, pad_w:pad_w+new_w] = resized
|
| 236 |
-
batch_canvases.append(canvas)
|
| 237 |
-
|
| 238 |
-
input_tensor_batch = np.array(batch_canvases, dtype=np.float32) # (N, H, W, C)
|
| 239 |
-
input_tensor_batch = np.transpose(input_tensor_batch, (0, 3, 1, 2)) # (N, C, H, W)
|
| 240 |
-
return input_tensor_batch
|
| 241 |
-
except Exception as e:
|
| 242 |
-
logger.error(f"Error during Anime Aesthetic preprocessing: {e}")
|
| 243 |
-
return None
|
| 244 |
-
|
| 245 |
-
#####################################
|
| 246 |
-
# Image Evaluation Tool #
|
| 247 |
-
#####################################
|
| 248 |
-
|
| 249 |
-
class ModelManager:
|
| 250 |
-
def __init__(self, cache_dir: str = None):
|
| 251 |
-
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 252 |
-
logger.info(f"Using device: {self.device}")
|
| 253 |
-
self.cache_dir = cache_dir
|
| 254 |
-
self.models = {}
|
| 255 |
-
self.model_configs = {}
|
| 256 |
-
self._load_all_models()
|
| 257 |
-
|
| 258 |
-
self.processing_queue: asyncio.Queue = asyncio.Queue()
|
| 259 |
-
self.worker_task = None
|
| 260 |
-
self._temp_files_to_clean = [] # For CSV files
|
| 261 |
-
|
| 262 |
-
def _load_all_models(self):
|
| 263 |
-
logger.info("Loading Aesthetic Shadow model...")
|
| 264 |
-
try:
|
| 265 |
-
self.models["aesthetic_shadow"] = pipeline("image-classification", model="NeoChen1024/aesthetic-shadow-v2-backup", device=0 if self.device == 'cuda' else -1)
|
| 266 |
-
self.model_configs["aesthetic_shadow"] = {"name": "Aesthetic Shadow", "process_func": self._process_aesthetic_shadow}
|
| 267 |
-
logger.info("Aesthetic Shadow model loaded.")
|
| 268 |
-
except Exception as e:
|
| 269 |
-
logger.error(f"Failed to load Aesthetic Shadow model: {e}")
|
| 270 |
-
|
| 271 |
-
logger.info("Loading Waifu Scorer model...")
|
| 272 |
-
try:
|
| 273 |
-
ws = WaifuScorer(device=self.device, cache_dir=self.cache_dir, verbose=True)
|
| 274 |
-
if ws.available:
|
| 275 |
-
self.models["waifu_scorer"] = ws
|
| 276 |
-
self.model_configs["waifu_scorer"] = {"name": "Waifu Scorer", "process_func": self._process_waifu_scorer}
|
| 277 |
-
logger.info("Waifu Scorer model loaded.")
|
| 278 |
-
else:
|
| 279 |
-
logger.warning("Waifu Scorer model is not available.")
|
| 280 |
-
except Exception as e:
|
| 281 |
-
logger.error(f"Failed to load Waifu Scorer model: {e}")
|
| 282 |
-
|
| 283 |
-
logger.info("Loading Aesthetic Predictor V2.5...")
|
| 284 |
-
try:
|
| 285 |
-
ap_v25 = AestheticPredictorV2_5_Wrapper(device=self.device)
|
| 286 |
-
self.models["aesthetic_predictor_v2_5"] = ap_v25
|
| 287 |
-
self.model_configs["aesthetic_predictor_v2_5"] = {"name": "Aesthetic V2.5", "process_func": self._process_aesthetic_predictor_v2_5}
|
| 288 |
-
logger.info("Aesthetic Predictor V2.5 loaded.")
|
| 289 |
-
except Exception as e:
|
| 290 |
-
logger.error(f"Failed to load Aesthetic Predictor V2.5: {e}")
|
| 291 |
-
|
| 292 |
-
logger.info("Loading Anime Aesthetic model...")
|
| 293 |
try:
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
logger.warning("Anime Aesthetic ONNX model failed to load and will be unavailable.")
|
| 301 |
except Exception as e:
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
logger.info("Async worker started.")
|
| 311 |
-
|
| 312 |
-
async def _worker(self):
|
| 313 |
-
while True:
|
| 314 |
-
request = await self.processing_queue.get()
|
| 315 |
-
if request is None: # Shutdown signal
|
| 316 |
-
self.processing_queue.task_done()
|
| 317 |
-
logger.info("Async worker received shutdown signal.")
|
| 318 |
-
break
|
| 319 |
-
|
| 320 |
-
future = request.get('future')
|
| 321 |
-
try:
|
| 322 |
-
if request['type'] == 'run_evaluation_generator':
|
| 323 |
-
# The generator itself is created here and returned via future
|
| 324 |
-
# The Gradio callback will iterate over it
|
| 325 |
-
gen = self.run_evaluation_generator(**request['params'])
|
| 326 |
-
future.set_result(gen)
|
| 327 |
-
else:
|
| 328 |
-
logger.warning(f"Unknown request type in worker: {request.get('type')}")
|
| 329 |
-
if future: future.set_exception(ValueError("Unknown request type"))
|
| 330 |
-
except Exception as e:
|
| 331 |
-
logger.error(f"Error in worker processing request: {e}", exc_info=True)
|
| 332 |
-
if future: future.set_exception(e)
|
| 333 |
-
finally:
|
| 334 |
-
self.processing_queue.task_done()
|
| 335 |
-
|
| 336 |
-
async def submit_evaluation_request(self, file_paths, auto_batch, manual_batch_size, selected_model_keys):
|
| 337 |
-
await self.start_worker_if_not_running()
|
| 338 |
-
future = asyncio.Future()
|
| 339 |
-
request_item = {
|
| 340 |
-
'type': 'run_evaluation_generator',
|
| 341 |
-
'params': {
|
| 342 |
-
'file_paths': file_paths,
|
| 343 |
-
'auto_batch': auto_batch,
|
| 344 |
-
'manual_batch_size': manual_batch_size,
|
| 345 |
-
'selected_model_keys': selected_model_keys,
|
| 346 |
-
},
|
| 347 |
-
'future': future
|
| 348 |
-
}
|
| 349 |
-
await self.processing_queue.put(request_item)
|
| 350 |
-
return await future # Future resolves to the async generator
|
| 351 |
-
|
| 352 |
-
def auto_tune_batch_size(self, images: list[Image.Image], selected_model_keys: list[str]) -> int:
|
| 353 |
-
if not images or not selected_model_keys:
|
| 354 |
-
return 1
|
| 355 |
|
| 356 |
-
|
| 357 |
-
max_possible_batch = len(images)
|
| 358 |
-
test_image_pil = [images[0].copy()] # A list containing one PIL image, copy to avoid issues with transforms
|
| 359 |
-
|
| 360 |
-
logger.info(f"Auto-tuning batch size with selected models: {selected_model_keys}")
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
logger.debug(f"Testing batch size: {batch_size}")
|
| 367 |
-
if "aesthetic_shadow" in selected_model_keys and "aesthetic_shadow" in self.models:
|
| 368 |
-
_ = self.models["aesthetic_shadow"](current_test_batch, batch_size=batch_size)
|
| 369 |
-
if "waifu_scorer" in selected_model_keys and "waifu_scorer" in self.models:
|
| 370 |
-
_ = self.models["waifu_scorer"](current_test_batch)
|
| 371 |
-
if "aesthetic_predictor_v2_5" in selected_model_keys and "aesthetic_predictor_v2_5" in self.models:
|
| 372 |
-
_ = self.models["aesthetic_predictor_v2_5"].inference(current_test_batch)
|
| 373 |
-
if "anime_aesthetic" in selected_model_keys and "anime_aesthetic" in self.models:
|
| 374 |
-
processed_input = preprocess_anime_aesthetic_batch(current_test_batch)
|
| 375 |
-
if processed_input is None: raise ValueError("Anime aesthetic preprocessing failed for test batch")
|
| 376 |
-
_ = self.models["anime_aesthetic"].run(None, {"img": processed_input})
|
| 377 |
-
|
| 378 |
-
optimal_batch_size = batch_size # This batch size worked
|
| 379 |
-
if batch_size * 2 > max_possible_batch : # If next step exceeds max, current is best fit
|
| 380 |
-
if max_possible_batch > batch_size: # Check if we can exactly fit max_possible_batch
|
| 381 |
-
# Test max_possible_batch one last time if it's > current batch_size and < batch_size*2
|
| 382 |
-
pass # Current optimal_batch_size is good, or we can check max_possible_batch specifically
|
| 383 |
-
break
|
| 384 |
-
batch_size *= 2
|
| 385 |
-
|
| 386 |
-
except Exception as e: # Typically torch.cuda.OutOfMemoryError or similar
|
| 387 |
-
logger.warning(f"Auto-tune failed at batch size {batch_size} for at least one model: {e}")
|
| 388 |
-
break # Current optimal_batch_size is the largest that worked before this failure
|
| 389 |
-
|
| 390 |
-
# Cap the batch size for very large numbers of images / powerful GPUs
|
| 391 |
-
final_optimal_batch = min(optimal_batch_size, max_possible_batch, 64)
|
| 392 |
-
logger.info(f"Optimal batch size determined: {final_optimal_batch}")
|
| 393 |
-
return max(1, final_optimal_batch)
|
| 394 |
-
|
| 395 |
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
def _log(msg):
|
| 401 |
-
log_messages.append(msg)
|
| 402 |
-
logger.info(msg)
|
| 403 |
-
|
| 404 |
-
_log("Starting image evaluation...")
|
| 405 |
-
yield {"type": "log_update", "messages": log_messages[-20:]} # Show last 20 logs
|
| 406 |
-
yield {"type": "progress", "value": 0.0, "desc": "Initiating..."}
|
| 407 |
-
|
| 408 |
-
images_pil = []
|
| 409 |
-
file_names = []
|
| 410 |
-
for f_path_str in file_paths:
|
| 411 |
try:
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
file_names.append(p.name)
|
| 416 |
-
_log(f"Loaded image: {p.name}")
|
| 417 |
except Exception as e:
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
if not images_pil:
|
| 423 |
-
_log("No valid images loaded. Aborting.")
|
| 424 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 425 |
-
yield {"type": "progress", "value": 1.0, "desc": "No images loaded"}
|
| 426 |
-
yield {"type": "final_results_state", "data": []} # ensure state is empty
|
| 427 |
-
return
|
| 428 |
-
|
| 429 |
-
actual_batch_size = 1
|
| 430 |
-
if auto_batch:
|
| 431 |
-
_log("Auto-tuning batch size...")
|
| 432 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 433 |
-
yield {"type": "progress", "value": 0.05, "desc": "Auto-tuning batch size..."}
|
| 434 |
-
actual_batch_size = self.auto_tune_batch_size(images_pil, selected_model_keys)
|
| 435 |
-
_log(f"Auto-detected batch size: {actual_batch_size}")
|
| 436 |
-
else:
|
| 437 |
-
actual_batch_size = int(manual_batch_size) if manual_batch_size > 0 else 1
|
| 438 |
-
_log(f"Using manual batch size: {actual_batch_size}")
|
| 439 |
-
|
| 440 |
-
yield {"type": "batch_size_update", "value": actual_batch_size}
|
| 441 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 442 |
-
|
| 443 |
-
all_results_for_state = [] # Full data for gr.State
|
| 444 |
-
dataframe_rows_so_far = [] # Data for gr.DataFrame (PIL images, strings, numbers)
|
| 445 |
-
|
| 446 |
-
total_images = len(images_pil)
|
| 447 |
-
processed_count = 0
|
| 448 |
-
|
| 449 |
-
for i in range(0, total_images, actual_batch_size):
|
| 450 |
-
batch_images_pil = images_pil[i:i+actual_batch_size]
|
| 451 |
-
batch_file_names = file_names[i:i+actual_batch_size]
|
| 452 |
-
num_in_batch = len(batch_images_pil)
|
| 453 |
-
_log(f"Processing batch {i//actual_batch_size + 1}/{ (total_images + actual_batch_size -1) // actual_batch_size }: images {i+1} to {i+num_in_batch}")
|
| 454 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 455 |
-
|
| 456 |
-
batch_model_scores = {key: [None] * num_in_batch for key in self.model_configs.keys()}
|
| 457 |
-
|
| 458 |
-
for model_key in selected_model_keys:
|
| 459 |
-
if model_key in self.models and model_key in self.model_configs:
|
| 460 |
-
_log(f" Running {self.model_configs[model_key]['name']} for batch...")
|
| 461 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 462 |
-
try:
|
| 463 |
-
scores = await self.model_configs[model_key]['process_func'](batch_images_pil)
|
| 464 |
-
batch_model_scores[model_key] = scores
|
| 465 |
-
_log(f" {self.model_configs[model_key]['name']} scores: {scores}")
|
| 466 |
-
except Exception as e:
|
| 467 |
-
_log(f" Error processing batch with {self.model_configs[model_key]['name']}: {e}")
|
| 468 |
-
batch_model_scores[model_key] = [None] * num_in_batch # Ensure it's list of Nones
|
| 469 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 470 |
-
|
| 471 |
-
# Assemble results for this batch
|
| 472 |
-
current_batch_df_rows = []
|
| 473 |
-
for j in range(num_in_batch):
|
| 474 |
-
result_item_state = {'file_name': batch_file_names[j]} # For gr.State
|
| 475 |
-
|
| 476 |
-
# For DataFrame: [PIL.Image, filename, score1, score2, ..., final_score]
|
| 477 |
-
thumbnail = batch_images_pil[j].copy()
|
| 478 |
-
thumbnail.thumbnail((150, 150)) # Create thumbnail
|
| 479 |
-
result_item_df_row = [thumbnail, batch_file_names[j]]
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
current_image_scores = []
|
| 483 |
-
for model_key in self.model_configs.keys(): # Iterate in defined order for consistency
|
| 484 |
-
score = batch_model_scores[model_key][j]
|
| 485 |
-
result_item_state[model_key] = score # For gr.State
|
| 486 |
-
if model_key in selected_model_keys: # Only add to DF if selected
|
| 487 |
-
result_item_df_row.append(f"{score:.4f}" if isinstance(score, (float, int)) else "N/A")
|
| 488 |
-
if isinstance(score, (float, int)) and model_key in selected_model_keys:
|
| 489 |
-
current_image_scores.append(score)
|
| 490 |
-
|
| 491 |
-
final_score = None
|
| 492 |
-
if current_image_scores:
|
| 493 |
-
final_score_val = float(np.mean([s for s in current_image_scores if s is not None]))
|
| 494 |
-
final_score = float(np.clip(final_score_val, 0.0, 10.0))
|
| 495 |
-
|
| 496 |
-
result_item_state['final_score'] = final_score
|
| 497 |
-
result_item_df_row.append(f"{final_score:.4f}" if final_score is not None else "N/A")
|
| 498 |
-
|
| 499 |
-
all_results_for_state.append(result_item_state)
|
| 500 |
-
current_batch_df_rows.append(result_item_df_row)
|
| 501 |
-
|
| 502 |
-
dataframe_rows_so_far.extend(current_batch_df_rows)
|
| 503 |
-
|
| 504 |
-
processed_count += num_in_batch
|
| 505 |
-
progress_value = processed_count / total_images
|
| 506 |
-
yield {"type": "partial_results_df_rows", "data": dataframe_rows_so_far, "selected_model_keys": selected_model_keys}
|
| 507 |
-
yield {"type": "progress", "value": progress_value, "desc": f"Processed {processed_count}/{total_images}"}
|
| 508 |
-
|
| 509 |
-
_log("All images processed.")
|
| 510 |
-
yield {"type": "log_update", "messages": log_messages[-20:]}
|
| 511 |
-
yield {"type": "progress", "value": 1.0, "desc": "Completed!"}
|
| 512 |
-
yield {"type": "final_results_state", "data": all_results_for_state}
|
| 513 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
if not model: return [None] * len(batch_images)
|
| 518 |
-
results = model(batch_images, batch_size=len(batch_images)) # Assuming pipeline can take batch_size hint
|
| 519 |
scores = []
|
| 520 |
-
for res_group in results: # Results might be List[List[Dict]] or List[Dict]
|
| 521 |
-
# Handle both single image and batch results from pipeline
|
| 522 |
-
current_res_list = res_group if isinstance(res_group, list) else [res_group]
|
| 523 |
-
try:
|
| 524 |
-
hq_score_item = next(p for p in current_res_list if p['label'] == 'hq')
|
| 525 |
-
score = float(np.clip(hq_score_item['score'] * 10.0, 0.0, 10.0))
|
| 526 |
-
except (StopIteration, KeyError, TypeError):
|
| 527 |
-
score = None
|
| 528 |
-
scores.append(score)
|
| 529 |
-
return scores
|
| 530 |
-
|
| 531 |
-
async def _process_waifu_scorer(self, batch_images: list[Image.Image]) -> list[float | None]:
|
| 532 |
-
model = self.models.get("waifu_scorer")
|
| 533 |
-
if not model: return [None] * len(batch_images)
|
| 534 |
-
raw_scores = model(batch_images)
|
| 535 |
-
return [float(np.clip(s, 0.0, 10.0)) if s is not None else None for s in raw_scores]
|
| 536 |
-
|
| 537 |
-
async def _process_aesthetic_predictor_v2_5(self, batch_images: list[Image.Image]) -> list[float | None]:
|
| 538 |
-
model = self.models.get("aesthetic_predictor_v2_5")
|
| 539 |
-
if not model: return [None] * len(batch_images)
|
| 540 |
-
# Already returns clipped & rounded scores or Nones
|
| 541 |
-
return model.inference(batch_images)
|
| 542 |
-
|
| 543 |
-
async def _process_anime_aesthetic(self, batch_images: list[Image.Image]) -> list[float | None]:
|
| 544 |
-
model = self.models.get("anime_aesthetic")
|
| 545 |
-
if not model: return [None] * len(batch_images)
|
| 546 |
-
|
| 547 |
-
input_data = preprocess_anime_aesthetic_batch(batch_images)
|
| 548 |
-
if input_data is None:
|
| 549 |
-
return [None] * len(batch_images)
|
| 550 |
-
|
| 551 |
try:
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
except Exception as e:
|
| 556 |
-
|
| 557 |
-
return [None] * len(
|
|
|
|
| 558 |
|
| 559 |
-
|
| 560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
try:
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
logger.warning("Worker task did not finish in time. Cancelling...")
|
| 571 |
-
self.worker_task.cancel()
|
| 572 |
except Exception as e:
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
os.remove(f_path)
|
| 596 |
-
logger.info(f"Removed temp file: {f_path}")
|
| 597 |
-
except OSError as e:
|
| 598 |
-
logger.error(f"Error removing temp file {f_path}: {e}")
|
| 599 |
-
self._temp_files_to_clean.clear()
|
| 600 |
-
logger.info("Cleanup finished.")
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
#####################################
|
| 604 |
-
# Interface #
|
| 605 |
-
#####################################
|
| 606 |
-
|
| 607 |
-
# Initialize ModelManager once
|
| 608 |
-
model_manager = ModelManager(cache_dir=".model_cache")
|
| 609 |
-
|
| 610 |
-
def create_interface():
|
| 611 |
-
# Define model choices based on ModelManager's loaded models
|
| 612 |
-
# Filter out models that failed to load
|
| 613 |
-
AVAILABLE_MODEL_KEYS = [k for k in model_manager.model_configs.keys() if k in model_manager.models]
|
| 614 |
-
AVAILABLE_MODEL_NAMES_MAP = {k: model_manager.model_configs[k]['name'] for k in AVAILABLE_MODEL_KEYS}
|
| 615 |
|
| 616 |
-
#
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
with gr.Blocks(theme=gr.themes.
|
| 621 |
-
gr.Markdown(""
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
""")
|
| 626 |
-
|
| 627 |
-
# Stores full processing results (list of dicts)
|
| 628 |
-
# Dict keys: 'file_name', 'final_score', and all model_keys with their scores
|
| 629 |
-
# This state is the source of truth for regenerating table and CSV
|
| 630 |
-
results_state = gr.State([])
|
| 631 |
-
# Stores current list of selected model keys (e.g., ['waifu_scorer', 'anime_aesthetic'])
|
| 632 |
-
selected_models_state = gr.State(AVAILABLE_MODEL_KEYS)
|
| 633 |
-
# Stores current log messages as a list
|
| 634 |
-
log_messages_state = gr.State([])
|
| 635 |
|
| 636 |
with gr.Row():
|
| 637 |
-
with gr.Column(scale=1):
|
| 638 |
-
|
|
|
|
|
|
|
| 639 |
|
| 640 |
-
|
| 641 |
-
gr.
|
| 642 |
-
|
| 643 |
-
else:
|
| 644 |
-
model_checkboxes = gr.CheckboxGroup(
|
| 645 |
-
choices=MODEL_CHOICES_FOR_CHECKBOX,
|
| 646 |
-
label="Select Models",
|
| 647 |
-
value=AVAILABLE_MODEL_KEYS, # Default to all available selected
|
| 648 |
-
info="Choose models for evaluation. Final score is an average of selected model scores."
|
| 649 |
-
)
|
| 650 |
-
|
| 651 |
-
auto_batch_checkbox = gr.Checkbox(label="Automatic Batch Size Detection", value=True)
|
| 652 |
-
batch_size_input = gr.Number(label="Manual Batch Size", value=8, minimum=1, precision=0, interactive=False) # Interactive based on auto_batch_checkbox
|
| 653 |
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
|
| 662 |
-
# Initial headers for DataFrame; will be updated dynamically
|
| 663 |
-
initial_df_headers = ['Image', 'File Name'] + [AVAILABLE_MODEL_NAMES_MAP[k] for k in AVAILABLE_MODEL_KEYS] + ['Final Score']
|
| 664 |
results_dataframe = gr.DataFrame(
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
interactive=True, # Enables sorting by clicking headers
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
wrap=True,
|
| 672 |
)
|
| 673 |
-
|
| 674 |
-
download_file_provider = gr.File(label="Download Link", visible=False)
|
| 675 |
-
|
| 676 |
-
# --- Callback Functions ---
|
| 677 |
-
def update_batch_size_interactive(auto_detect_enabled: bool):
|
| 678 |
-
return gr.Number.update(interactive=not auto_detect_enabled)
|
| 679 |
-
|
| 680 |
-
async def handle_process_images_ui(
|
| 681 |
-
files_list: list[gr. rýchle.TempFile] | None, # Gradio File objects
|
| 682 |
-
auto_batch_flag: bool,
|
| 683 |
-
manual_batch_val: int,
|
| 684 |
-
selected_model_keys_from_ui: list[str],
|
| 685 |
-
# Gradio will pass the gr.Progress instance automatically by type hinting
|
| 686 |
-
# Ensure the name 'progress_tracker_instance' matches an output component if you want to update it by dict key
|
| 687 |
-
# Otherwise, use the positional argument `progress`
|
| 688 |
-
progress_instance: gr.Progress
|
| 689 |
-
):
|
| 690 |
-
if not files_list:
|
| 691 |
-
yield {
|
| 692 |
-
log_output: "No files uploaded. Please select images first.",
|
| 693 |
-
progress_tracker: gr.Progress(0.0, "Idle. No files."),
|
| 694 |
-
results_dataframe: gr.DataFrame.update(value=None), # Clear table
|
| 695 |
-
results_state: [],
|
| 696 |
-
selected_models_state: selected_model_keys_from_ui,
|
| 697 |
-
log_messages_state: ["No files uploaded. Please select images first."]
|
| 698 |
-
}
|
| 699 |
-
return
|
| 700 |
|
| 701 |
-
|
| 702 |
-
|
|
|
|
| 703 |
|
| 704 |
-
|
| 705 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
# Call the ModelManager's generator
|
| 710 |
-
# The progress_instance is implicitly passed by Gradio to this function
|
| 711 |
-
# The ModelManager generator will then use it via its own parameter `progress_tracker_instance`
|
| 712 |
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
|
|
|
|
|
|
|
|
|
| 717 |
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
outputs_to_yield = {}
|
| 723 |
-
if event["type"] == "log_update":
|
| 724 |
-
current_log_list = event["messages"]
|
| 725 |
-
outputs_to_yield[log_output] = "\n".join(current_log_list)
|
| 726 |
-
elif event["type"] == "progress":
|
| 727 |
-
# Update progress bar directly using the passed instance
|
| 728 |
-
progress_instance(event["value"], desc=event.get("desc"))
|
| 729 |
-
elif event["type"] == "batch_size_update":
|
| 730 |
-
outputs_to_yield[batch_size_input] = gr.Number.update(value=event["value"])
|
| 731 |
-
elif event["type"] == "partial_results_df_rows":
|
| 732 |
-
# data is list of lists for DataFrame rows
|
| 733 |
-
# selected_model_keys used to generate current headers
|
| 734 |
-
dynamic_headers = ['Image', 'File Name'] + \
|
| 735 |
-
[AVAILABLE_MODEL_NAMES_MAP[k] for k in event["selected_model_keys"] if k in AVAILABLE_MODEL_NAMES_MAP] + \
|
| 736 |
-
['Final Score']
|
| 737 |
-
dataframe_update_value = pd.DataFrame(event["data"], columns=dynamic_headers) if event["data"] else None
|
| 738 |
-
outputs_to_yield[results_dataframe] = gr.DataFrame.update(value=dataframe_update_value, headers=dynamic_headers)
|
| 739 |
-
|
| 740 |
-
elif event["type"] == "final_results_state":
|
| 741 |
-
final_results_for_app_state = event["data"]
|
| 742 |
-
|
| 743 |
-
if outputs_to_yield: # Only yield if there's something to update
|
| 744 |
-
yield outputs_to_yield
|
| 745 |
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
|
|
|
|
|
|
| 752 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
-
def handle_clear_results_ui():
|
| 755 |
-
# Clear files, logs, table, progress, and internal states
|
| 756 |
return {
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
progress_tracker: gr.Progress(0.0, "Idle"),
|
| 761 |
-
results_state: [],
|
| 762 |
-
# selected_models_state: AVAILABLE_MODEL_KEYS, # Optionally reset model selection
|
| 763 |
-
batch_size_input: gr.Number.update(value=8), # Reset batch size
|
| 764 |
-
log_messages_state: ["Results cleared."]
|
| 765 |
}
|
| 766 |
-
|
| 767 |
-
# Function to re-render DataFrame and update states when model selection changes
|
| 768 |
-
def handle_model_selection_or_state_change_ui(
|
| 769 |
-
current_selected_keys: list[str],
|
| 770 |
-
current_full_results: list[dict]
|
| 771 |
-
):
|
| 772 |
-
if not current_full_results: # No data to process
|
| 773 |
-
dynamic_headers = ['Image', 'File Name'] + \
|
| 774 |
-
[AVAILABLE_MODEL_NAMES_MAP[k] for k in current_selected_keys if k in AVAILABLE_MODEL_NAMES_MAP] + \
|
| 775 |
-
['Final Score']
|
| 776 |
-
return {
|
| 777 |
-
results_dataframe: gr.DataFrame.update(value=None, headers=dynamic_headers),
|
| 778 |
-
selected_models_state: current_selected_keys,
|
| 779 |
-
results_state: current_full_results # pass through if empty
|
| 780 |
-
}
|
| 781 |
-
|
| 782 |
-
new_df_rows = []
|
| 783 |
-
updated_full_results = []
|
| 784 |
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
scores_to_avg = []
|
| 788 |
-
for mk in current_selected_keys:
|
| 789 |
-
if mk in res_item_dict and isinstance(res_item_dict[mk], (float, int)):
|
| 790 |
-
scores_to_avg.append(res_item_dict[mk])
|
| 791 |
-
|
| 792 |
-
new_final_score = None
|
| 793 |
-
if scores_to_avg:
|
| 794 |
-
new_final_score_val = float(np.mean(scores_to_avg))
|
| 795 |
-
new_final_score = float(np.clip(new_final_score_val, 0.0, 10.0))
|
| 796 |
-
|
| 797 |
-
# Update the item in results_state
|
| 798 |
-
res_item_dict['final_score'] = new_final_score
|
| 799 |
-
updated_full_results.append(res_item_dict.copy()) # Store updated item
|
| 800 |
-
|
| 801 |
-
# Prepare row for DataFrame
|
| 802 |
-
# Find the corresponding image (this assumes images are not stored in results_state, which they aren't)
|
| 803 |
-
# For simplicity, we'll need to re-generate thumbnails if we want them in this update path.
|
| 804 |
-
# A robust way: results_state stores paths or minimal data to re-fetch/re-create thumbnails.
|
| 805 |
-
# Current implementation of `run_evaluation_generator` directly yields DF rows with PIL images.
|
| 806 |
-
# If `handle_model_selection_change_ui` is to re-generate the DF from `results_state`,
|
| 807 |
-
# `results_state` items would need to include enough info for `Image.open` and `thumbnail`.
|
| 808 |
-
# This is a complex part if we want perfect dynamic DF regeneration with images.
|
| 809 |
-
# For now, let's assume `results_state` stores `PIL.Image` thumbnails if this path is critical.
|
| 810 |
-
# The `run_evaluation_generator` stores dicts without PIL image objects in `all_results_for_state`.
|
| 811 |
-
# This means `handle_model_selection_change_ui` cannot easily reconstruct the 'Image' column.
|
| 812 |
-
#
|
| 813 |
-
# SIMPLIFICATION: When model selection changes, we only update scores in the existing DataFrame
|
| 814 |
-
# if possible, or we re-calculate and re-populate. The current code path re-creates rows.
|
| 815 |
-
# To do this properly, `results_state` items should perhaps include original image path or cached thumbnail.
|
| 816 |
-
#
|
| 817 |
-
# Let's make results_state store {'file_path': ..., 'thumbnail_pil': ..., scores...}
|
| 818 |
-
# This needs `run_evaluation_generator` to save file_path and thumbnail_pil to `all_results_for_state`.
|
| 819 |
-
# Assume `results_state` items now contain 'thumbnail_pil' and other scores.
|
| 820 |
-
|
| 821 |
-
# If 'thumbnail_pil' is not in res_item_dict (because it wasn't saved that way), this will fail.
|
| 822 |
-
# This path requires results_state to contain PIL image data for the 'Image' column.
|
| 823 |
-
# The current 'run_evaluation_generator' does not save PIL images into `all_results_for_state`.
|
| 824 |
-
# It only creates them for immediate DataFrame update.
|
| 825 |
-
# This function needs to be re-thought if full DF reconstruction with images is needed here.
|
| 826 |
-
|
| 827 |
-
# Let's assume results_state IS NOT used to rebuild the image column.
|
| 828 |
-
# The change handler for model_checkboxes will mostly affect the *calculation* of final_score
|
| 829 |
-
# and *visibility* of columns if we were dynamically adding/removing them.
|
| 830 |
-
# Gradio's DataFrame doesn't easily hide/show columns; we change headers and data.
|
| 831 |
-
|
| 832 |
-
# Rebuild row for DF:
|
| 833 |
-
df_row = [res_item_dict.get('thumbnail_pil_placeholder', "N/A"), res_item_dict['file_name']]
|
| 834 |
-
for mk_cfg in AVAILABLE_MODEL_KEYS: # All possible models to maintain column order
|
| 835 |
-
if mk_cfg in current_selected_keys: # If this model is currently selected for display
|
| 836 |
-
score = res_item_dict.get(mk_cfg)
|
| 837 |
-
df_row.append(f"{score:.4f}" if isinstance(score, (float, int)) else "N/A")
|
| 838 |
-
# If not selected, this column won't even be in dynamic_headers.
|
| 839 |
-
df_row.append(f"{new_final_score:.4f}" if new_final_score is not None else "N/A")
|
| 840 |
-
new_df_rows.append(df_row)
|
| 841 |
-
|
| 842 |
-
dynamic_headers = ['Image', 'File Name'] + \
|
| 843 |
-
[AVAILABLE_MODEL_NAMES_MAP[k] for k in current_selected_keys if k in AVAILABLE_MODEL_NAMES_MAP] + \
|
| 844 |
-
['Final Score']
|
| 845 |
-
|
| 846 |
-
import pandas as pd
|
| 847 |
-
df_value = pd.DataFrame(new_df_rows, columns=dynamic_headers) if new_df_rows else None
|
| 848 |
-
|
| 849 |
return {
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
}
|
| 854 |
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
return gr.File.update(value=None, visible=False)
|
| 860 |
-
|
| 861 |
-
# Use StringIO to build CSV in memory
|
| 862 |
-
csv_output = StringIO()
|
| 863 |
-
# Define fieldnames: Filename, selected model scores, Final Score
|
| 864 |
-
fieldnames = ['File Name'] + \
|
| 865 |
-
[AVAILABLE_MODEL_NAMES_MAP[k] for k in current_selected_keys if k in AVAILABLE_MODEL_NAMES_MAP] + \
|
| 866 |
-
['Final Score']
|
| 867 |
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
score_val = res_item.get(key)
|
| 880 |
-
row_to_write[model_display_name] = f"{score_val:.4f}" if isinstance(score_val, (float, int)) else "N/A"
|
| 881 |
-
writer.writerow(row_to_write)
|
| 882 |
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
|
|
|
|
|
|
|
|
|
| 890 |
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
return gr.File.update(value=temp_file_path, visible=True, label="results.csv")
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
# --- Wire up components ---
|
| 897 |
-
auto_batch_checkbox.change(
|
| 898 |
-
fn=update_batch_size_interactive,
|
| 899 |
-
inputs=[auto_batch_checkbox],
|
| 900 |
-
outputs=[batch_size_input]
|
| 901 |
-
)
|
| 902 |
|
| 903 |
-
|
| 904 |
-
if model_checkboxes:
|
| 905 |
-
process_btn.click(
|
| 906 |
-
fn=handle_process_images_ui,
|
| 907 |
-
inputs=[input_images, auto_batch_checkbox, batch_size_input, model_checkboxes],
|
| 908 |
-
outputs=[
|
| 909 |
-
log_output, progress_tracker, results_dataframe, batch_size_input,
|
| 910 |
-
results_state, selected_models_state, log_messages_state # Ensure all yielded components are listed
|
| 911 |
-
]
|
| 912 |
-
)
|
| 913 |
-
# When model selection changes, update the displayed table and internal states
|
| 914 |
-
model_checkboxes.change(
|
| 915 |
-
fn=handle_model_selection_or_state_change_ui,
|
| 916 |
-
inputs=[model_checkboxes, results_state], # Takes current selection and full results data
|
| 917 |
-
outputs=[results_dataframe, selected_models_state, results_state]
|
| 918 |
-
)
|
| 919 |
|
| 920 |
-
|
| 921 |
-
fn=handle_clear_results_ui,
|
| 922 |
-
outputs=[
|
| 923 |
-
input_images, log_output, results_dataframe, progress_tracker,
|
| 924 |
-
results_state, batch_size_input, log_messages_state # model_checkboxes could be reset too if needed
|
| 925 |
-
]
|
| 926 |
-
)
|
| 927 |
|
| 928 |
-
|
| 929 |
-
fn=
|
| 930 |
-
inputs=[
|
| 931 |
-
outputs=[
|
| 932 |
)
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
return {selected_models_state: AVAILABLE_MODEL_KEYS, log_messages_state: ["Application loaded. Ready."]}
|
| 940 |
-
|
| 941 |
-
demo.load(
|
| 942 |
-
fn=initial_load_setup,
|
| 943 |
-
outputs=[selected_models_state, log_messages_state]
|
| 944 |
)
|
| 945 |
-
# Register cleanup function
|
| 946 |
-
demo.unload(model_manager.cleanup)
|
| 947 |
-
|
| 948 |
-
|
| 949 |
-
gr.Markdown("""
|
| 950 |
-
### Notes
|
| 951 |
-
- **Model Selection**: Dynamically choose models for evaluation. The 'Final Score' and displayed columns update accordingly.
|
| 952 |
-
- **Native Table**: Results are shown in a native Gradio DataFrame, allowing sorting by clicking column headers.
|
| 953 |
-
- **Batching**: Automatic batch size detection is enabled by default. You can switch to manual batch sizing.
|
| 954 |
-
- **CSV Export**: Download the current results (respecting selected models for columns) as a CSV file.
|
| 955 |
-
- **Asynchronous Processing**: Image evaluation runs in the background, providing live updates for logs and progress.
|
| 956 |
-
""")
|
| 957 |
-
return demo
|
| 958 |
|
|
|
|
|
|
|
|
|
|
| 959 |
|
| 960 |
if __name__ == "__main__":
|
| 961 |
-
#
|
| 962 |
-
|
| 963 |
-
#
|
| 964 |
-
# For general Hugging Face model loading, 'transformers'.
|
| 965 |
-
# OpenCV ('cv2') for image manipulation: 'opencv-python'.
|
| 966 |
-
# And of course 'torch', 'numpy', 'Pillow', 'gradio'.
|
| 967 |
-
|
| 968 |
-
# Create a dummy aesthetic_predictor_v2_5.py if it doesn't exist for the stub to work
|
| 969 |
-
# (or ensure the real one is present)
|
| 970 |
-
if not Path("aesthetic_predictor_v2_5.py").exists():
|
| 971 |
-
stub_content = """
|
| 972 |
-
# Placeholder for aesthetic_predictor_v2_5.py
|
| 973 |
-
# This file needs to contain the actual 'convert_v2_5_from_siglip' function.
|
| 974 |
-
# The main script uses a basic stub if this file is missing or fails to import.
|
| 975 |
-
# print("aesthetic_predictor_v2_5.py placeholder executed")
|
| 976 |
-
def convert_v2_5_from_siglip(*args, **kwargs):
|
| 977 |
-
raise NotImplementedError("This is a placeholder. Implement convert_v2_5_from_siglip here or ensure the main script's stub is used.")
|
| 978 |
-
"""
|
| 979 |
-
# Only write if you are sure, or better, let user handle this dependency.
|
| 980 |
-
# For this exercise, we assume the main script's internal stub is sufficient if the file is missing.
|
| 981 |
-
pass
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
# It's important that the ModelManager is initialized before create_interface() is called,
|
| 985 |
-
# as create_interface() relies on model_manager.model_configs.
|
| 986 |
-
# This is already handled by placing `model_manager = ModelManager()` globally.
|
| 987 |
-
|
| 988 |
-
app_interface = create_interface()
|
| 989 |
-
app_interface.queue().launch(debug=True, share=False) # Enable queue for async operations
|
| 990 |
-
|
| 991 |
-
# Ensure cleanup is called on exit if demo.unload isn't fully effective in all environments
|
| 992 |
-
import atexit
|
| 993 |
-
atexit.register(model_manager.cleanup)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import io
|
| 3 |
import tempfile
|
| 4 |
+
import shutil # Kept for potential future use, but not actively used for now.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
+
import pandas as pd
|
| 9 |
import torch
|
| 10 |
import onnxruntime as rt
|
| 11 |
from PIL import Image
|
| 12 |
import gradio as gr
|
| 13 |
+
from transformers import pipeline
|
| 14 |
from huggingface_hub import hf_hub_download
|
| 15 |
|
| 16 |
+
# Assuming aesthetic_predictor_v2_5.py is in the same directory or Python path.
|
| 17 |
+
# If it's not available, the AestheticPredictorV25 model will fail to load.
|
| 18 |
+
# For this example, a mock will be used if the real import fails.
|
| 19 |
+
try:
|
| 20 |
+
from aesthetic_predictor_v2_5 import convert_v2_5_from_siglip
|
| 21 |
+
except ImportError:
|
| 22 |
+
print("Warning: aesthetic_predictor_v2_5.py not found. Using a mock for AestheticPredictorV25.")
|
| 23 |
+
def convert_v2_5_from_siglip(low_cpu_mem_usage=True, trust_remote_code=True):
|
| 24 |
+
# This is a mock.
|
| 25 |
+
mock_model_output = torch.randn(1, 1) # Represents logits for a single image
|
| 26 |
+
|
| 27 |
+
class MockModel(torch.nn.Module):
|
| 28 |
+
def __init__(self):
|
| 29 |
+
super().__init__()
|
| 30 |
+
self.dummy_param = torch.nn.Parameter(torch.empty(0)) # To have a device property
|
| 31 |
+
|
| 32 |
+
def forward(self, pixel_values):
|
| 33 |
+
# Return something that has .logits
|
| 34 |
+
# Batch size from pixel_values
|
| 35 |
+
batch_size = pixel_values.size(0)
|
| 36 |
+
# Create a namedtuple or simple class to mimic HuggingFace output object with .logits
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
class Output:
|
| 38 |
+
pass
|
| 39 |
+
output = Output()
|
| 40 |
+
output.logits = torch.randn(batch_size, 1).to(self.dummy_param.device)
|
| 41 |
+
return output
|
| 42 |
+
|
| 43 |
+
def to(self, device_or_dtype): # Simplified .to()
|
| 44 |
+
if isinstance(device_or_dtype, torch.dtype):
|
| 45 |
+
# In a real scenario, handle dtype conversion
|
| 46 |
+
pass
|
| 47 |
+
elif isinstance(device_or_dtype, str) or isinstance(device_or_dtype, torch.device):
|
| 48 |
+
self.dummy_param = torch.nn.Parameter(torch.empty(0, device=device_or_dtype)) # Move dummy param to device
|
| 49 |
+
return self
|
| 50 |
+
|
| 51 |
+
def cuda(self): # Mock .cuda()
|
| 52 |
+
return self.to(torch.device('cuda'))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
mock_model_instance = MockModel()
|
| 56 |
+
|
| 57 |
+
# Mock preprocessor that returns a dict with "pixel_values"
|
| 58 |
+
mock_preprocessor = lambda images, return_tensors: {"pixel_values": torch.randn(len(images) if isinstance(images, list) else 1, 3, 224, 224)}
|
| 59 |
+
return mock_model_instance, mock_preprocessor
|
| 60 |
+
|
| 61 |
+
# --- Configuration ---
|
| 62 |
+
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 63 |
+
DTYPE_WAIFU = torch.float32 # Specific dtype for WaifuScorer's MLP
|
| 64 |
+
CACHE_DIR = None # Set to a path string to use a specific Hugging Face cache directory, e.g., "./hf_cache"
|
| 65 |
+
|
| 66 |
+
# --- Model Definitions ---
|
| 67 |
|
| 68 |
class MLP(torch.nn.Module):
|
| 69 |
+
"""Custom MLP for WaifuScorer."""
|
| 70 |
def __init__(self, input_size: int, batch_norm: bool = True):
|
| 71 |
super().__init__()
|
| 72 |
self.input_size = input_size
|
| 73 |
+
self.layers = torch.nn.Sequential(
|
| 74 |
torch.nn.Linear(self.input_size, 2048), torch.nn.ReLU(),
|
| 75 |
torch.nn.BatchNorm1d(2048) if batch_norm else torch.nn.Identity(), torch.nn.Dropout(0.3),
|
| 76 |
torch.nn.Linear(2048, 512), torch.nn.ReLU(),
|
|
|
|
| 81 |
torch.nn.BatchNorm1d(128) if batch_norm else torch.nn.Identity(), torch.nn.Dropout(0.1),
|
| 82 |
torch.nn.Linear(128, 32), torch.nn.ReLU(),
|
| 83 |
torch.nn.Linear(32, 1)
|
| 84 |
+
)
|
| 85 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor: return self.layers(x)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
class BaseImageScorer:
|
| 88 |
+
"""Abstract base class for image scorers."""
|
| 89 |
+
def __init__(self, model_key: str, model_display_name: str, device: str = DEVICE, verbose: bool = False):
|
| 90 |
+
self.model_key = model_key
|
| 91 |
+
self.model_display_name = model_display_name
|
| 92 |
self.device = device
|
| 93 |
+
self.verbose = verbose
|
| 94 |
+
self.model = None
|
| 95 |
+
self.preprocessor = None
|
| 96 |
+
self._load_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
def _load_model(self): raise NotImplementedError
|
| 99 |
+
def predict(self, images: list[Image.Image]) -> list[float | None]: raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
def __call__(self, images: list[Image.Image]) -> list[float | None]:
|
| 102 |
+
if not self.model:
|
| 103 |
+
if self.verbose: print(f"{self.model_display_name} model not loaded.")
|
| 104 |
+
return [None] * len(images)
|
| 105 |
+
|
| 106 |
+
rgb_images = [img.convert("RGB") if img.mode != "RGB" else img for img in images]
|
| 107 |
+
return self.predict(rgb_images)
|
| 108 |
|
| 109 |
+
class WaifuScorerModel(BaseImageScorer):
|
| 110 |
+
def _load_model(self):
|
| 111 |
+
try:
|
| 112 |
+
import clip
|
| 113 |
+
model_hf_path = "Eugeoter/waifu-scorer-v3/model.pth" # Default path
|
| 114 |
+
|
| 115 |
+
repo_id, filename = os.path.split(model_hf_path)
|
| 116 |
+
actual_model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=CACHE_DIR)
|
| 117 |
+
if self.verbose: print(f"Loading WaifuScorer MLP from: {actual_model_path}")
|
| 118 |
|
| 119 |
+
self.mlp = MLP(input_size=768) # ViT-L/14 embedding size
|
| 120 |
+
if actual_model_path.endswith(".safetensors"):
|
| 121 |
from safetensors.torch import load_file
|
| 122 |
+
state_dict = load_file(actual_model_path, device=self.device)
|
| 123 |
else:
|
| 124 |
+
state_dict = torch.load(actual_model_path, map_location=self.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
self.mlp.load_state_dict(state_dict)
|
| 126 |
+
self.mlp.to(self.device).eval()
|
|
|
|
| 127 |
|
| 128 |
+
if self.verbose: print("Loading CLIP model ViT-L/14 for WaifuScorer.")
|
| 129 |
+
self.model, self.preprocessor = clip.load("ViT-L/14", device=self.device) # self.model is CLIP model
|
| 130 |
+
self.model.eval()
|
| 131 |
except ImportError:
|
| 132 |
+
if self.verbose: print("CLIP library not found. WaifuScorer will not be available.")
|
| 133 |
except Exception as e:
|
| 134 |
+
if self.verbose: print(f"Error loading WaifuScorer ({self.model_display_name}): {e}")
|
| 135 |
|
| 136 |
@torch.no_grad()
|
| 137 |
+
def predict(self, images: list[Image.Image]) -> list[float | None]:
|
| 138 |
+
if not self.model or not self.mlp: return [None] * len(images)
|
|
|
|
| 139 |
|
|
|
|
|
|
|
|
|
|
| 140 |
original_n = len(images)
|
| 141 |
+
processed_images = list(images)
|
| 142 |
+
if original_n == 1: processed_images.append(images[0]) # Duplicate for single image batch
|
| 143 |
|
| 144 |
try:
|
| 145 |
+
image_tensors = torch.cat([self.preprocessor(img).unsqueeze(0) for img in processed_images]).to(self.device)
|
| 146 |
+
image_features = self.model.encode_image(image_tensors)
|
|
|
|
|
|
|
| 147 |
norm = image_features.norm(p=2, dim=-1, keepdim=True)
|
| 148 |
+
norm[norm == 0] = 1e-6 # Avoid division by zero, use small epsilon
|
| 149 |
+
im_emb = (image_features / norm).to(device=self.device, dtype=DTYPE_WAIFU)
|
| 150 |
|
| 151 |
predictions = self.mlp(im_emb)
|
| 152 |
+
scores = predictions.clamp(0, 10).cpu().numpy().flatten().tolist()
|
| 153 |
return scores[:original_n]
|
| 154 |
except Exception as e:
|
| 155 |
+
if self.verbose: print(f"Error during {self.model_display_name} prediction: {e}")
|
| 156 |
return [None] * original_n
|
| 157 |
|
| 158 |
+
class AestheticPredictorV25(BaseImageScorer):
|
| 159 |
+
def _load_model(self):
|
| 160 |
+
try:
|
| 161 |
+
if self.verbose: print(f"Loading {self.model_display_name}...")
|
| 162 |
+
self.model, self.preprocessor = convert_v2_5_from_siglip(low_cpu_mem_usage=True, trust_remote_code=True)
|
| 163 |
+
# Model's .to() method should handle dtype (e.g. bfloat16) and device.
|
| 164 |
+
self.model = self.model.to(self.device)
|
| 165 |
+
if self.device == 'cuda' and torch.cuda.is_available() and hasattr(self.model, 'to'): # some models might need explicit dtype
|
| 166 |
+
self.model = self.model.to(torch.bfloat16)
|
| 167 |
+
self.model.eval()
|
| 168 |
+
except Exception as e:
|
| 169 |
+
if self.verbose: print(f"Error loading {self.model_display_name}: {e}")
|
| 170 |
|
| 171 |
@torch.no_grad()
|
| 172 |
+
def predict(self, images: list[Image.Image]) -> list[float | None]:
|
| 173 |
+
if not self.model or not self.preprocessor: return [None] * len(images)
|
|
|
|
| 174 |
try:
|
| 175 |
+
inputs = self.preprocessor(images=images, return_tensors="pt")
|
| 176 |
+
pixel_values = inputs["pixel_values"].to(self.model.dummy_param.device if hasattr(self.model, 'dummy_param') else self.device) # Use model's device
|
| 177 |
+
if self.device == 'cuda' and torch.cuda.is_available() and pixel_values.dtype != torch.bfloat16 : # Match dtype if model changed it
|
| 178 |
+
pixel_values = pixel_values.to(torch.bfloat16)
|
| 179 |
+
|
| 180 |
+
output = self.model(pixel_values)
|
| 181 |
+
scores_tensor = output.logits if hasattr(output, 'logits') else output
|
| 182 |
+
scores = scores_tensor.squeeze().float().cpu().numpy()
|
| 183 |
|
| 184 |
+
scores_list = [float(np.round(np.clip(s, 0.0, 10.0), 4)) for s in np.atleast_1d(scores)]
|
| 185 |
+
return scores_list
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
except Exception as e:
|
| 187 |
+
if self.verbose: print(f"Error during {self.model_display_name} prediction: {e}")
|
| 188 |
return [None] * len(images)
|
| 189 |
|
| 190 |
+
class AnimeAestheticONNX(BaseImageScorer):
|
| 191 |
+
def _load_model(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
try:
|
| 193 |
+
if self.verbose: print(f"Loading {self.model_display_name} (ONNX)...")
|
| 194 |
+
model_path = hf_hub_download(repo_id="skytnt/anime-aesthetic", filename="model.onnx", cache_dir=CACHE_DIR)
|
| 195 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if self.device == 'cuda' else ['CPUExecutionProvider']
|
| 196 |
+
valid_providers = [p for p in providers if p in rt.get_available_providers()] or ['CPUExecutionProvider']
|
| 197 |
+
self.model = rt.InferenceSession(model_path, providers=valid_providers)
|
| 198 |
+
if self.verbose: print(f"{self.model_display_name} loaded with providers: {self.model.get_providers()}")
|
|
|
|
| 199 |
except Exception as e:
|
| 200 |
+
if self.verbose: print(f"Error loading {self.model_display_name}: {e}")
|
| 201 |
+
|
| 202 |
+
def _preprocess_image(self, img: Image.Image) -> np.ndarray:
|
| 203 |
+
img_np = np.array(img).astype(np.float32) / 255.0
|
| 204 |
+
s = 768
|
| 205 |
+
h, w = img_np.shape[:2]
|
| 206 |
+
r = min(s/h, s/w)
|
| 207 |
+
new_h, new_w = int(h*r), int(w*r)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
resized = cv2.resize(img_np, (new_w, new_h), interpolation=cv2.INTER_AREA if r < 1 else cv2.INTER_LANCZOS4)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
+
canvas = np.zeros((s, s, 3), dtype=np.float32) # Fill with black
|
| 212 |
+
pad_h, pad_w = (s - new_h) // 2, (s - new_w) // 2
|
| 213 |
+
canvas[pad_h:pad_h+new_h, pad_w:pad_w+new_w] = resized
|
| 214 |
+
return np.transpose(canvas, (2, 0, 1))[np.newaxis, :]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
def predict(self, images: list[Image.Image]) -> list[float | None]:
|
| 217 |
+
if not self.model: return [None] * len(images)
|
| 218 |
+
scores = []
|
| 219 |
+
for img in images:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
try:
|
| 221 |
+
input_tensor = self._preprocess_image(img)
|
| 222 |
+
pred = self.model.run(None, {"img": input_tensor})[0].item()
|
| 223 |
+
scores.append(float(np.clip(pred * 10.0, 0.0, 10.0)))
|
|
|
|
|
|
|
| 224 |
except Exception as e:
|
| 225 |
+
if self.verbose: print(f"Error predicting with {self.model_display_name} for one image: {e}")
|
| 226 |
+
scores.append(None)
|
| 227 |
+
return scores
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
class AestheticShadowPipeline(BaseImageScorer):
|
| 230 |
+
def _load_model(self):
|
| 231 |
+
try:
|
| 232 |
+
if self.verbose: print(f"Loading {self.model_display_name} pipeline...")
|
| 233 |
+
pipeline_device = 0 if self.device == 'cuda' else -1
|
| 234 |
+
self.model = pipeline("image-classification", model="NeoChen1024/aesthetic-shadow-v2-backup", device=pipeline_device)
|
| 235 |
+
except Exception as e:
|
| 236 |
+
if self.verbose: print(f"Error loading {self.model_display_name}: {e}")
|
| 237 |
|
| 238 |
+
def predict(self, images: list[Image.Image]) -> list[float | None]:
|
| 239 |
+
if not self.model: return [None] * len(images)
|
|
|
|
|
|
|
| 240 |
scores = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
try:
|
| 242 |
+
pipeline_results = self.model(images, top_k=None) # Assuming pipeline handles batching
|
| 243 |
+
|
| 244 |
+
# Ensure consistent output structure from pipeline (List[List[Dict]] vs List[Dict])
|
| 245 |
+
if images and pipeline_results and not isinstance(pipeline_results[0], list):
|
| 246 |
+
pipeline_results = [pipeline_results]
|
| 247 |
+
|
| 248 |
+
for res_set in pipeline_results:
|
| 249 |
+
try:
|
| 250 |
+
hq_score_dict = next(p for p in res_set if p['label'] == 'hq')
|
| 251 |
+
scores.append(float(np.clip(hq_score_dict['score'] * 10.0, 0.0, 10.0)))
|
| 252 |
+
except (StopIteration, TypeError, KeyError): scores.append(None)
|
| 253 |
except Exception as e:
|
| 254 |
+
if self.verbose: print(f"Error during {self.model_display_name} prediction: {e}")
|
| 255 |
+
return [None] * len(images) # All None if batch fails
|
| 256 |
+
return scores
|
| 257 |
|
| 258 |
+
# --- Model Management ---
|
| 259 |
+
MODEL_REGISTRY = {
|
| 260 |
+
"aesthetic_shadow": {"class": AestheticShadowPipeline, "name": "Aesthetic Shadow"},
|
| 261 |
+
"waifu_scorer": {"class": WaifuScorerModel, "name": "Waifu Scorer"},
|
| 262 |
+
"aesthetic_predictor_v2_5": {"class": AestheticPredictorV25, "name": "Aesthetic V2.5"},
|
| 263 |
+
"anime_aesthetic": {"class": AnimeAestheticONNX, "name": "Anime Score"},
|
| 264 |
+
}
|
| 265 |
+
LOADED_MODELS = {} # Populated at startup
|
| 266 |
+
|
| 267 |
+
def initialize_models(verbose_loading=False):
|
| 268 |
+
print(f"Using device: {DEVICE}")
|
| 269 |
+
print("Initializing models...")
|
| 270 |
+
for key, config in MODEL_REGISTRY.items():
|
| 271 |
+
LOADED_MODELS[key] = config["class"](key, config['name'], device=DEVICE, verbose=verbose_loading)
|
| 272 |
+
print("Model initialization complete.")
|
| 273 |
+
|
| 274 |
+
# --- Core Logic ---
|
| 275 |
+
@torch.no_grad()
|
| 276 |
+
def auto_tune_batch_size(images: list[Image.Image], selected_model_keys: list[str],
|
| 277 |
+
initial_bs: int = 1, max_bs_limit: int = 64, verbose: bool = False) -> int:
|
| 278 |
+
if not images or not selected_model_keys: return initial_bs
|
| 279 |
+
if verbose: print("Auto-tuning batch size...")
|
| 280 |
+
|
| 281 |
+
test_image = images[0]
|
| 282 |
+
active_models = [LOADED_MODELS[key] for key in selected_model_keys if key in LOADED_MODELS and LOADED_MODELS[key].model]
|
| 283 |
+
if not active_models: return initial_bs
|
| 284 |
|
| 285 |
+
bs = initial_bs
|
| 286 |
+
optimal_bs = initial_bs
|
| 287 |
+
while bs <= len(images) and bs <= max_bs_limit:
|
| 288 |
+
try:
|
| 289 |
+
batch_test_images = [test_image] * bs
|
| 290 |
+
for model in active_models:
|
| 291 |
+
if verbose: print(f" Testing {model.model_display_name} with batch size {bs}")
|
| 292 |
+
model.predict(batch_test_images)
|
| 293 |
+
if DEVICE == 'cuda': torch.cuda.empty_cache()
|
| 294 |
+
|
| 295 |
+
optimal_bs = bs
|
| 296 |
+
if bs == max_bs_limit: break
|
| 297 |
+
bs = min(bs * 2, max_bs_limit) # Try next power of 2 or max_bs_limit
|
| 298 |
+
except Exception as e: # Typically OOM or other runtime errors
|
| 299 |
+
if verbose: print(f" Failed at batch size {bs} ({type(e).__name__}). Optimal so far: {optimal_bs}. Error: {str(e)[:100]}")
|
| 300 |
+
break
|
| 301 |
+
if verbose: print(f"Auto-tuned batch size: {optimal_bs}")
|
| 302 |
+
return max(1, optimal_bs)
|
| 303 |
+
|
| 304 |
+
async def evaluate_images_core(
|
| 305 |
+
pil_images: list[Image.Image], file_names: list[str],
|
| 306 |
+
selected_model_keys: list[str], batch_size: int,
|
| 307 |
+
progress_tracker: gr.Progress
|
| 308 |
+
) -> tuple[pd.DataFrame, list[str]]:
|
| 309 |
+
|
| 310 |
+
logs = []
|
| 311 |
+
num_images = len(pil_images)
|
| 312 |
+
if num_images == 0: return pd.DataFrame(), ["No images to process."]
|
| 313 |
+
|
| 314 |
+
# Initialize results_data: list of dicts, one per image
|
| 315 |
+
results_data = [{'File Name': fn, 'Thumbnail': img.copy().resize((150,150)), 'Final Score': np.nan}
|
| 316 |
+
for fn, img in zip(file_names, pil_images)]
|
| 317 |
+
for r_dict in results_data: # Initialize all model score columns to NaN
|
| 318 |
+
for cfg in MODEL_REGISTRY.values(): r_dict[cfg['name']] = np.nan
|
| 319 |
+
|
| 320 |
+
progress_tracker(0, desc="Starting evaluation...")
|
| 321 |
+
total_models_to_run = len(selected_model_keys)
|
| 322 |
+
|
| 323 |
+
for model_idx, model_key in enumerate(selected_model_keys):
|
| 324 |
+
model = LOADED_MODELS.get(model_key)
|
| 325 |
+
if not model or not model.model:
|
| 326 |
+
logs.append(f"Skipping {MODEL_REGISTRY[model_key]['name']} (not loaded).")
|
| 327 |
+
continue
|
| 328 |
+
|
| 329 |
+
model_name = model.model_display_name
|
| 330 |
+
logs.append(f"Processing with {model_name}...")
|
| 331 |
+
|
| 332 |
+
current_img_offset = 0
|
| 333 |
+
for batch_start_idx in range(0, num_images, batch_size):
|
| 334 |
+
# Progress: (current_model_idx + fraction_of_current_model_done) / total_models_to_run
|
| 335 |
+
model_progress_fraction = (batch_start_idx / num_images)
|
| 336 |
+
overall_progress = (model_idx + model_progress_fraction) / total_models_to_run
|
| 337 |
+
progress_tracker(overall_progress, desc=f"{model_name} (Batch {batch_start_idx//batch_size + 1})")
|
| 338 |
+
|
| 339 |
+
batch_images = pil_images[batch_start_idx : batch_start_idx + batch_size]
|
| 340 |
try:
|
| 341 |
+
scores = model(batch_images) # Use __call__
|
| 342 |
+
for i, score in enumerate(scores):
|
| 343 |
+
results_data[current_img_offset + i][model_name] = score if score is not None else np.nan
|
|
|
|
|
|
|
| 344 |
except Exception as e:
|
| 345 |
+
logs.append(f"Error with {model_name} on batch: {e}")
|
| 346 |
+
current_img_offset += len(batch_images)
|
| 347 |
+
logs.append(f"Finished with {model_name}.")
|
| 348 |
+
|
| 349 |
+
# Calculate Final Scores
|
| 350 |
+
for i in range(num_images):
|
| 351 |
+
img_scores = [results_data[i][MODEL_REGISTRY[mk]['name']] for mk in selected_model_keys
|
| 352 |
+
if pd.notna(results_data[i].get(MODEL_REGISTRY[mk]['name']))]
|
| 353 |
+
if img_scores:
|
| 354 |
+
results_data[i]['Final Score'] = float(np.clip(np.mean(img_scores), 0.0, 10.0))
|
| 355 |
+
|
| 356 |
+
df = pd.DataFrame(results_data)
|
| 357 |
+
# Define column order: Thumbnail, File Name, then model scores, then Final Score
|
| 358 |
+
ordered_cols = ['Thumbnail', 'File Name'] + \
|
| 359 |
+
[MODEL_REGISTRY[k]['name'] for k in MODEL_REGISTRY.keys() if MODEL_REGISTRY[k]['name'] in df.columns] + \
|
| 360 |
+
['Final Score']
|
| 361 |
+
df = df[[col for col in ordered_cols if col in df.columns]] # Ensure all columns exist
|
| 362 |
+
|
| 363 |
+
logs.append("Evaluation complete.")
|
| 364 |
+
progress_tracker(1.0, desc="Evaluation complete.")
|
| 365 |
+
return df, logs
|
| 366 |
+
|
| 367 |
+
def results_df_to_csv_bytes(df: pd.DataFrame, selected_model_display_names: list[str]) -> bytes | None:
|
| 368 |
+
if df.empty: return None
|
| 369 |
+
|
| 370 |
+
cols_for_csv = ['File Name', 'Final Score'] + \
|
| 371 |
+
[name for name in selected_model_display_names if name in df.columns and name not in cols_for_csv]
|
| 372 |
+
|
| 373 |
+
df_csv = df[cols_for_csv].copy()
|
| 374 |
+
for col in df_csv.select_dtypes(include=['float']).columns: # Format float scores
|
| 375 |
+
df_csv[col] = df_csv[col].apply(lambda x: f"{x:.4f}" if pd.notnull(x) else "N/A")
|
| 376 |
+
|
| 377 |
+
s_io = io.StringIO()
|
| 378 |
+
df_csv.to_csv(s_io, index=False)
|
| 379 |
+
return s_io.getvalue().encode('utf-8')
|
| 380 |
|
| 381 |
+
# --- Gradio Interface ---
|
| 382 |
+
def create_gradio_interface():
|
| 383 |
+
model_name_choices = [config['name'] for config in MODEL_REGISTRY.values()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
+
# Define column structure for DataFrame
|
| 386 |
+
initial_df_cols = ['Thumbnail', 'File Name'] + model_name_choices + ['Final Score']
|
| 387 |
+
initial_datatypes = ['image', 'str'] + ['number'] * (len(model_name_choices) + 1)
|
| 388 |
+
|
| 389 |
+
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
| 390 |
+
gr.Markdown("## ✨ Comprehensive Image Evaluation Tool ✨")
|
| 391 |
+
|
| 392 |
+
# For storing results DataFrame between interactions
|
| 393 |
+
results_state = gr.State(pd.DataFrame(columns=initial_df_cols))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
with gr.Row():
|
| 396 |
+
with gr.Column(scale=1, min_width=300):
|
| 397 |
+
gr.Markdown("#### Controls")
|
| 398 |
+
files_input = gr.Files(label="Upload Images", file_count="multiple", type="filepath")
|
| 399 |
+
models_checkbox_group = gr.CheckboxGroup(choices=model_name_choices, value=model_name_choices, label="Select Models")
|
| 400 |
|
| 401 |
+
with gr.Accordion("Batch Settings", open=False):
|
| 402 |
+
auto_batch_toggle = gr.Checkbox(label="Auto-detect Batch Size", value=True)
|
| 403 |
+
manual_batch_input = gr.Number(label="Manual Batch Size", value=4, minimum=1, step=1, interactive=False) # Interactive based on toggle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
+
evaluate_button = gr.Button("🚀 Evaluate Images", variant="primary")
|
| 406 |
+
with gr.Row():
|
| 407 |
+
clear_button = gr.Button("🧹 Clear")
|
| 408 |
+
download_button = gr.Button("💾 Download CSV")
|
| 409 |
+
|
| 410 |
+
# Hidden component for file download functionality
|
| 411 |
+
csv_file_output = gr.File(label="Download CSV File", visible=False)
|
| 412 |
+
|
| 413 |
+
with gr.Column(scale=3, min_width=600):
|
| 414 |
+
gr.Markdown("#### Results")
|
| 415 |
+
# Using gr.Slider for progress display
|
| 416 |
+
progress_slider = gr.Slider(label="Progress", minimum=0, maximum=1, value=0, interactive=False)
|
| 417 |
|
|
|
|
|
|
|
| 418 |
results_dataframe = gr.DataFrame(
|
| 419 |
+
label="Evaluation Scores",
|
| 420 |
+
headers=initial_df_cols,
|
| 421 |
+
datatype=initial_datatypes,
|
| 422 |
+
interactive=True, # Enables native sorting by clicking headers
|
| 423 |
+
height=500,
|
| 424 |
+
wrap=True
|
|
|
|
| 425 |
)
|
| 426 |
+
logs_textbox = gr.Textbox(label="Process Logs", lines=5, max_lines=10, interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
|
| 428 |
+
# --- Callbacks ---
|
| 429 |
+
def map_display_names_to_keys(display_names: list[str]) -> list[str]:
|
| 430 |
+
return [key for key, cfg in MODEL_REGISTRY.items() if cfg['name'] in display_names]
|
| 431 |
|
| 432 |
+
async def run_evaluation(uploaded_files, selected_model_names, auto_batch, manual_batch,
|
| 433 |
+
current_results_df, progress=gr.Progress(track_tqdm=True)):
|
| 434 |
+
if not uploaded_files:
|
| 435 |
+
return {
|
| 436 |
+
results_state: current_results_df, logs_textbox: "No files uploaded. Please upload images first.",
|
| 437 |
+
progress_slider: gr.update(value=0, label="Progress")
|
| 438 |
+
}
|
| 439 |
|
| 440 |
+
yield {logs_textbox: "Loading images...", progress_slider: gr.update(value=0.01, label="Loading images...")}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
+
pil_images, file_names = [], []
|
| 443 |
+
for f_obj in uploaded_files:
|
| 444 |
+
try:
|
| 445 |
+
pil_images.append(Image.open(f_obj.name).convert("RGB")) # f_obj.name is path for type="filepath"
|
| 446 |
+
file_names.append(os.path.basename(f_obj.name))
|
| 447 |
+
except Exception as e:
|
| 448 |
+
print(f"Error loading image {f_obj.name}: {e}") # Log to console
|
| 449 |
|
| 450 |
+
if not pil_images:
|
| 451 |
+
return {logs_textbox: "No valid images could be loaded.", progress_slider: gr.update(value=0, label="Error")}
|
| 452 |
+
|
| 453 |
+
selected_keys = map_display_names_to_keys(selected_model_names)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
+
batch_size_to_use = manual_batch
|
| 456 |
+
if auto_batch:
|
| 457 |
+
yield {logs_textbox: "Auto-tuning batch size...", progress_slider: gr.update(value=0.1, label="Auto-tuning...")}
|
| 458 |
+
batch_size_to_use = auto_tune_batch_size(pil_images, selected_keys, verbose=True)
|
| 459 |
+
yield {manual_batch_input: gr.update(value=batch_size_to_use)} # Update UI with detected size
|
| 460 |
+
|
| 461 |
+
yield {logs_textbox: f"Starting evaluation with batch size {batch_size_to_use}...",
|
| 462 |
+
progress_slider: gr.update(value=0.15, label=f"Evaluating (Batch: {batch_size_to_use})...")}
|
| 463 |
|
| 464 |
+
df_new_results, log_messages = await evaluate_images_core(
|
| 465 |
+
pil_images, file_names, selected_keys, batch_size_to_use, progress
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
# Sort by 'Final Score' descending by default before display
|
| 469 |
+
if not df_new_results.empty and 'Final Score' in df_new_results.columns:
|
| 470 |
+
df_new_results = df_new_results.sort_values(by='Final Score', ascending=False, na_position='last')
|
| 471 |
|
|
|
|
|
|
|
| 472 |
return {
|
| 473 |
+
results_state: df_new_results, results_dataframe: df_new_results,
|
| 474 |
+
logs_textbox: "\n".join(log_messages),
|
| 475 |
+
progress_slider: gr.update(value=1.0, label="Evaluation Complete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
+
def clear_all_outputs():
|
| 479 |
+
empty_df = pd.DataFrame(columns=initial_df_cols)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
return {
|
| 481 |
+
results_state: empty_df, results_dataframe: empty_df,
|
| 482 |
+
files_input: None, logs_textbox: "Outputs cleared.",
|
| 483 |
+
progress_slider: gr.update(value=0, label="Progress")
|
| 484 |
}
|
| 485 |
|
| 486 |
+
def download_csv_file(current_df, selected_names):
|
| 487 |
+
if current_df.empty:
|
| 488 |
+
gr.Warning("No results available to download.")
|
| 489 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
|
| 491 |
+
csv_data = results_df_to_csv_bytes(current_df, selected_names)
|
| 492 |
+
if csv_data:
|
| 493 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode='wb') as tmp_f:
|
| 494 |
+
tmp_f.write(csv_data)
|
| 495 |
+
gr.Info("CSV file prepared for download.")
|
| 496 |
+
return tmp_f.name
|
| 497 |
+
gr.Error("Failed to generate CSV.")
|
| 498 |
+
return None
|
| 499 |
+
|
| 500 |
+
def update_final_scores_on_model_select(selected_model_names, current_df):
|
| 501 |
+
if current_df.empty: return current_df
|
|
|
|
|
|
|
|
|
|
| 502 |
|
| 503 |
+
df_updated = current_df.copy()
|
| 504 |
+
selected_keys = map_display_names_to_keys(selected_model_names)
|
| 505 |
+
|
| 506 |
+
for i, row in df_updated.iterrows():
|
| 507 |
+
img_scores = [row[MODEL_REGISTRY[mk]['name']] for mk in selected_keys
|
| 508 |
+
if pd.notna(row.get(MODEL_REGISTRY[mk]['name']))]
|
| 509 |
+
if img_scores:
|
| 510 |
+
df_updated.loc[i, 'Final Score'] = float(np.clip(np.mean(img_scores), 0.0, 10.0))
|
| 511 |
+
else:
|
| 512 |
+
df_updated.loc[i, 'Final Score'] = np.nan
|
| 513 |
|
| 514 |
+
if 'Final Score' in df_updated.columns: # Re-sort
|
| 515 |
+
df_updated = df_updated.sort_values(by='Final Score', ascending=False, na_position='last')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
+
return {results_state: df_updated, results_dataframe: df_updated}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
|
| 519 |
+
auto_batch_toggle.change(lambda x: gr.update(interactive=not x), inputs=auto_batch_toggle, outputs=manual_batch_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
+
evaluate_button.click(
|
| 522 |
+
fn=run_evaluation,
|
| 523 |
+
inputs=[files_input, models_checkbox_group, auto_batch_toggle, manual_batch_input, results_state],
|
| 524 |
+
outputs=[results_state, results_dataframe, logs_textbox, manual_batch_input, progress_slider]
|
| 525 |
)
|
| 526 |
+
clear_button.click(fn=clear_all_outputs, outputs=[results_state, results_dataframe, files_input, logs_textbox, progress_slider])
|
| 527 |
+
download_button.click(fn=download_csv_file, inputs=[results_state, models_checkbox_group], outputs=csv_file_output)
|
| 528 |
+
models_checkbox_group.change(
|
| 529 |
+
fn=update_final_scores_on_model_select,
|
| 530 |
+
inputs=[models_checkbox_group, results_state],
|
| 531 |
+
outputs=[results_state, results_dataframe]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
+
# Initial load state for the DataFrame UI component
|
| 535 |
+
demo.load(lambda: pd.DataFrame(columns=initial_df_cols), outputs=[results_dataframe])
|
| 536 |
+
return demo
|
| 537 |
|
| 538 |
if __name__ == "__main__":
|
| 539 |
+
initialize_models(verbose_loading=True) # Load models once at startup
|
| 540 |
+
gradio_app = create_gradio_interface()
|
| 541 |
+
gradio_app.queue().launch(debug=False) # Enable queue for async ops, debug=True for more logs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|