Spaces:
Running
Running
File size: 24,488 Bytes
f0fcc66 9e5447d f0fcc66 6165359 f0fcc66 6165359 f0fcc66 e70b261 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 b64eca0 f0fcc66 b64eca0 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 6165359 f0fcc66 9e5447d 6165359 9e5447d 6165359 9e5447d 6165359 9e5447d f0fcc66 6165359 fd31dbe f0fcc66 6165359 f0fcc66 9e5447d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
import torch
import gradio as gr
from diffusers import FluxPipeline, FluxTransformer2DModel
import gc
import random
import glob
from pathlib import Path
from PIL import Image
import os
import time
import json
from fasteners import InterProcessLock
import spaces
AGG_FILE = Path(__file__).parent / "agg_stats.json"
LOCK_FILE = AGG_FILE.with_suffix(".lock")
def _load_agg_stats() -> dict:
if AGG_FILE.exists():
with open(AGG_FILE, "r") as f:
try:
return json.load(f)
except json.JSONDecodeError:
print(f"Warning: {AGG_FILE} is corrupted. Starting with empty stats.")
return {"8-bit": {"attempts": 0, "correct": 0}, "4-bit": {"attempts": 0, "correct": 0}}
return {"8-bit": {"attempts": 0, "correct": 0},
"4-bit": {"attempts": 0, "correct": 0}}
def _save_agg_stats(stats: dict) -> None:
with InterProcessLock(str(LOCK_FILE)):
with open(AGG_FILE, "w") as f:
json.dump(stats, f, indent=2)
USER_STATS_FILE = Path(__file__).parent / "user_stats.json"
USER_STATS_LOCK_FILE = USER_STATS_FILE.with_suffix(".lock")
def _load_user_stats() -> dict:
if USER_STATS_FILE.exists():
with open(USER_STATS_FILE, "r") as f:
try:
return json.load(f)
except json.JSONDecodeError:
print(f"Warning: {USER_STATS_FILE} is corrupted. Starting with empty user stats.")
return {}
return {}
def _save_user_stats(stats: dict) -> None:
with InterProcessLock(str(USER_STATS_LOCK_FILE)):
with open(USER_STATS_FILE, "w") as f:
json.dump(stats, f, indent=2)
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {DEVICE}")
DEFAULT_HEIGHT = 1024
DEFAULT_WIDTH = 1024
DEFAULT_GUIDANCE_SCALE = 3.5
DEFAULT_NUM_INFERENCE_STEPS = 15
DEFAULT_MAX_SEQUENCE_LENGTH = 512
HF_TOKEN = os.environ.get("HF_ACCESS_TOKEN")
CACHED_PIPES = {}
def load_bf16_pipeline():
print("Loading BF16 pipeline...")
MODEL_ID = "black-forest-labs/FLUX.1-dev"
if MODEL_ID in CACHED_PIPES:
return CACHED_PIPES[MODEL_ID]
start_time = time.time()
try:
pipe = FluxPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16,
token=HF_TOKEN
)
pipe.to(DEVICE)
end_time = time.time()
mem_reserved = torch.cuda.memory_reserved(0)/1024**3 if DEVICE == "cuda" else 0
print(f"BF16 Pipeline loaded in {end_time - start_time:.2f}s. Memory reserved: {mem_reserved:.2f} GB")
CACHED_PIPES[MODEL_ID] = pipe
return pipe
except Exception as e:
print(f"Error loading BF16 pipeline: {e}")
raise
def load_bnb_8bit_pipeline():
print("Loading 8-bit BNB pipeline...")
MODEL_ID = "derekl35/FLUX.1-dev-bnb-8bit"
if MODEL_ID in CACHED_PIPES:
return CACHED_PIPES[MODEL_ID]
start_time = time.time()
try:
pipe = FluxPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16
)
pipe.to(DEVICE)
# pipe.enable_model_cpu_offload()
end_time = time.time()
mem_reserved = torch.cuda.memory_reserved(0)/1024**3 if DEVICE == "cuda" else 0
print(f"8-bit BNB pipeline loaded in {end_time - start_time:.2f}s. Memory reserved: {mem_reserved:.2f} GB")
CACHED_PIPES[MODEL_ID] = pipe
return pipe
except Exception as e:
print(f"Error loading 8-bit BNB pipeline: {e}")
raise
def load_bnb_4bit_pipeline():
print("Loading 4-bit BNB pipeline...")
MODEL_ID = "derekl35/FLUX.1-dev-nf4"
if MODEL_ID in CACHED_PIPES:
return CACHED_PIPES[MODEL_ID]
start_time = time.time()
try:
pipe = FluxPipeline.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16
)
pipe.to(DEVICE)
# pipe.enable_model_cpu_offload()
end_time = time.time()
mem_reserved = torch.cuda.memory_reserved(0)/1024**3 if DEVICE == "cuda" else 0
print(f"4-bit BNB pipeline loaded in {end_time - start_time:.2f}s. Memory reserved: {mem_reserved:.2f} GB")
CACHED_PIPES[MODEL_ID] = pipe
return pipe
except Exception as e:
print(f"Error loading 4-bit BNB pipeline: {e}")
raise
@spaces.GPU(duration=240)
def generate_images(prompt, quantization_choice, progress=gr.Progress(track_tqdm=True)):
if not prompt:
return None, {}, gr.update(value="Please enter a prompt.", interactive=False), gr.update(choices=[], value=None), gr.update(interactive=True), gr.update(interactive=True)
if not quantization_choice:
return None, {}, gr.update(value="Please select a quantization method.", interactive=False), gr.update(choices=[], value=None), gr.update(interactive=True), gr.update(interactive=True)
if quantization_choice == "8-bit":
quantized_load_func = load_bnb_8bit_pipeline
quantized_label = "Quantized (8-bit)"
elif quantization_choice == "4-bit":
quantized_load_func = load_bnb_4bit_pipeline
quantized_label = "Quantized (4-bit)"
else:
return None, {}, gr.update(value="Invalid quantization choice.", interactive=False), gr.update(choices=[], value=None), gr.update(interactive=True), gr.update(interactive=True)
model_configs = [
("Original", load_bf16_pipeline),
(quantized_label, quantized_load_func),
]
results = []
pipe_kwargs = {
"prompt": prompt,
"height": DEFAULT_HEIGHT,
"width": DEFAULT_WIDTH,
"guidance_scale": DEFAULT_GUIDANCE_SCALE,
"num_inference_steps": DEFAULT_NUM_INFERENCE_STEPS,
"max_sequence_length": DEFAULT_MAX_SEQUENCE_LENGTH,
}
seed = random.getrandbits(64)
print(f"Using seed: {seed}")
for i, (label, load_func) in enumerate(model_configs):
progress(i / len(model_configs), desc=f"Loading {label} model...")
print(f"\n--- Loading {label} Model ---")
load_start_time = time.time()
try:
current_pipe = load_func()
load_end_time = time.time()
print(f"{label} model loaded in {load_end_time - load_start_time:.2f} seconds.")
progress((i + 0.5) / len(model_configs), desc=f"Generating with {label} model...")
print(f"--- Generating with {label} Model ---")
gen_start_time = time.time()
image_list = current_pipe(**pipe_kwargs, generator=torch.manual_seed(seed)).images
image = image_list[0]
gen_end_time = time.time()
results.append({"label": label, "image": image})
print(f"--- Finished Generation with {label} Model in {gen_end_time - gen_start_time:.2f} seconds ---")
mem_reserved = torch.cuda.memory_reserved(0)/1024**3 if DEVICE == "cuda" else 0
print(f"Memory reserved: {mem_reserved:.2f} GB")
except Exception as e:
print(f"Error during {label} model processing: {e}")
return None, {}, gr.update(value=f"Error processing {label} model: {e}", interactive=False), gr.update(choices=[], value=None), gr.update(interactive=True), gr.update(interactive=True)
if len(results) != len(model_configs):
return None, {}, gr.update(value="Failed to generate images for all model types.", interactive=False), gr.update(choices=[], value=None), gr.update(interactive=True), gr.update(interactive=True)
shuffled_results = results.copy()
random.shuffle(shuffled_results)
shuffled_data_for_gallery = [(res["image"], f"Image {i+1}") for i, res in enumerate(shuffled_results)]
correct_mapping = {i: res["label"] for i, res in enumerate(shuffled_results)}
print("Correct mapping (hidden):", correct_mapping)
return shuffled_data_for_gallery, correct_mapping, "Generation complete! Make your guess.", None, gr.update(interactive=True), gr.update(interactive=True)
def check_guess(user_guess, correct_mapping_state):
if not isinstance(correct_mapping_state, dict) or not correct_mapping_state:
return "Please generate images first (state is empty or invalid)."
if user_guess is None:
return "Please select which image you think is quantized."
quantized_image_index = -1
quantized_label_actual = ""
for index, label in correct_mapping_state.items():
if "Quantized" in label:
quantized_image_index = index
quantized_label_actual = label
break
if quantized_image_index == -1:
return "Error: Could not find the quantized image in the mapping data."
correct_guess_label = f"Image {quantized_image_index + 1}"
if user_guess == correct_guess_label:
feedback = f"Correct! {correct_guess_label} used the {quantized_label_actual} model."
else:
feedback = f"Incorrect. The quantized image ({quantized_label_actual}) was {correct_guess_label}."
return feedback
EXAMPLE_DIR = Path(__file__).parent / "examples"
EXAMPLES = [
{
"prompt": "A photorealistic portrait of an astronaut on Mars",
"files": ["astronauts_seed_6456306350371904162.png", "astronauts_bnb_8bit.png"],
"quantized_idx": 1,
"quant_method": "bnb 8-bit",
},
{
"prompt": "Water-color painting of a cat wearing sunglasses",
"files": ["watercolor_cat_bnb_8bit.png", "watercolor_cat_seed_14269059182221286790.png"],
"quantized_idx": 0,
"quant_method": "bnb 8-bit",
},
# {
# "prompt": "Neo-tokyo cyberpunk cityscape at night, rain-soaked streets, 8-K",
# "files": ["cyber_city_q.jpg", "cyber_city.jpg"],
# "quantized_idx": 0,
# },
]
def load_example(idx):
ex = EXAMPLES[idx]
imgs = [Image.open(EXAMPLE_DIR / f) for f in ex["files"]]
gallery_items = [(img, f"Image {i+1}") for i, img in enumerate(imgs)]
mapping = {i: (f"Quantized {ex['quant_method']}" if i == ex["quantized_idx"] else "Original")
for i in range(2)}
return gallery_items, mapping, f"{ex['prompt']}"
def _accuracy_string(correct: int, attempts: int) -> tuple[str, float]:
if attempts:
pct = 100 * correct / attempts
return f"{pct:.1f}%", pct
return "N/A", -1.0
def _add_medals(user_rows):
MEDALS = {0: "🥇 ", 1: "🥈 ", 2: "🥉 "}
return [
[MEDALS.get(i, "") + row[0], *row[1:]]
for i, row in enumerate(user_rows)
]
def update_leaderboards_data():
agg = _load_agg_stats()
quant_rows = []
for method, stats in agg.items():
acc_str, acc_val = _accuracy_string(stats["correct"], stats["attempts"])
quant_rows.append([
method,
stats["correct"],
stats["attempts"],
acc_str
])
quant_rows.sort(key=lambda r: r[1]/r[2] if r[2] != 0 else 1e9)
user_stats = _load_user_stats()
user_rows = []
for user, st in user_stats.items():
acc_str, acc_val = _accuracy_string(st["total_correct"], st["total_attempts"])
user_rows.append([user, st["total_correct"], st["total_attempts"], acc_str])
user_rows.sort(key=lambda r: (-float(r[3].rstrip('%')) if r[3] != "N/A" else float('-inf'), -r[2]))
user_rows = _add_medals(user_rows)
return quant_rows, user_rows
quant_df = gr.DataFrame(
headers=["Method", "Correct Guesses", "Total Attempts", "Detectability %"],
interactive=False, col_count=(4, "fixed")
)
user_df = gr.DataFrame(
headers=["User", "Correct Guesses", "Total Attempts", "Accuracy %"],
interactive=False, col_count=(4, "fixed")
)
with gr.Blocks(title="FLUX Quantization Challenge", theme=gr.themes.Soft()) as demo:
gr.Markdown("# FLUX Model Quantization Challenge")
with gr.Tabs():
with gr.TabItem("Challenge"):
gr.Markdown(
"Compare the original FLUX.1-dev (BF16) model against a quantized version (4-bit or 8-bit). "
"Enter a prompt, choose the quantization method, and generate two images. "
"The images will be shuffled, can you spot which one was quantized?"
)
gr.Markdown("### Examples")
ex_selector = gr.Radio(
choices=[f"Example {i+1}" for i in range(len(EXAMPLES))],
label="Choose an example prompt",
interactive=True,
)
gr.Markdown("### …or create your own comparison")
with gr.Row():
prompt_input = gr.Textbox(label="Enter Prompt", scale=3)
quantization_choice_radio = gr.Radio(
choices=["8-bit", "4-bit"],
label="Select Quantization",
value="8-bit",
scale=1
)
generate_button = gr.Button("Generate & Compare", variant="primary", scale=1)
output_gallery = gr.Gallery(
label="Generated Images",
columns=2,
height=606,
object_fit="contain",
allow_preview=True,
show_label=True,
)
gr.Markdown("### Which image used the selected quantization method?")
with gr.Row():
image1_btn = gr.Button("Image 1")
image2_btn = gr.Button("Image 2")
feedback_box = gr.Textbox(label="Feedback", interactive=False, lines=1)
with gr.Row():
session_score_box = gr.Textbox(label="Your accuracy this session", interactive=False)
with gr.Row(equal_height=False):
username_input = gr.Textbox(
label="Enter Your Name for Leaderboard",
placeholder="YourName",
visible=False,
interactive=True,
scale=2
)
add_score_button = gr.Button(
"Add My Score to Leaderboard",
visible=False,
variant="secondary",
scale=1
)
add_score_feedback = gr.Textbox(
label="Leaderboard Update",
visible=False,
interactive=False,
lines=1
)
correct_mapping_state = gr.State({})
session_stats_state = gr.State(
{"8-bit": {"attempts": 0, "correct": 0},
"4-bit": {"attempts": 0, "correct": 0}}
)
is_example_state = gr.State(False)
has_added_score_state = gr.State(False)
def _load_example(sel):
idx = int(sel.split()[-1]) - 1
gallery_items, mapping, prompt = load_example(idx)
quant_data, user_data = update_leaderboards_data()
return gallery_items, mapping, prompt, True, quant_data, user_data
ex_selector.change(
fn=_load_example,
inputs=ex_selector,
outputs=[output_gallery, correct_mapping_state, prompt_input, is_example_state, quant_df, user_df],
).then(
lambda: (gr.update(interactive=True), gr.update(interactive=True)),
outputs=[image1_btn, image2_btn],
)
generate_button.click(
fn=generate_images,
inputs=[prompt_input, quantization_choice_radio],
outputs=[output_gallery, correct_mapping_state]
).then(
lambda: (False, # for is_example_state
False, # for has_added_score_state
gr.update(visible=False, value="", interactive=True), # username_input reset
gr.update(visible=False), # add_score_button reset
gr.update(visible=False, value="")), # add_score_feedback reset
outputs=[is_example_state,
has_added_score_state,
username_input,
add_score_button,
add_score_feedback]
).then(
lambda: (gr.update(interactive=True),
gr.update(interactive=True),
""),
outputs=[image1_btn, image2_btn, feedback_box],
)
def choose(choice_string, mapping, session_stats, is_example, has_added_score_curr):
feedback = check_guess(choice_string, mapping)
quant_label = next(label for label in mapping.values() if "Quantized" in label)
quant_key = "8-bit" if "8-bit" in quant_label else "4-bit"
got_it_right = "Correct!" in feedback
sess = session_stats.copy()
if not is_example and not has_added_score_curr:
sess[quant_key]["attempts"] += 1
if got_it_right:
sess[quant_key]["correct"] += 1
session_stats = sess
AGG_STATS = _load_agg_stats()
AGG_STATS[quant_key]["attempts"] += 1
if got_it_right:
AGG_STATS[quant_key]["correct"] += 1
_save_agg_stats(AGG_STATS)
def _fmt(d):
a, c = d["attempts"], d["correct"]
pct = 100 * c / a if a else 0
return f"{c} / {a} ({pct:.1f}%)"
session_msg = ", ".join(
f"{k}: {_fmt(v)}" for k, v in sess.items()
)
current_agg_stats = _load_agg_stats()
global_msg = ", ".join(
f"{k}: {_fmt(v)}" for k, v in current_agg_stats.items()
)
username_input_update = gr.update(visible=False, interactive=True)
add_score_button_update = gr.update(visible=False)
# Keep existing feedback if score was already added and feedback is visible
current_feedback_text = add_score_feedback.value if hasattr(add_score_feedback, 'value') and add_score_feedback.value else ""
add_score_feedback_update = gr.update(visible=has_added_score_curr, value=current_feedback_text)
session_total_attempts = sum(stats["attempts"] for stats in sess.values())
if not is_example and not has_added_score_curr:
if session_total_attempts >= 1 : # Show button if more than 1 attempt
username_input_update = gr.update(visible=True, interactive=True)
add_score_button_update = gr.update(visible=True, interactive=True)
add_score_feedback_update = gr.update(visible=False, value="")
else: # Less than 1 attempts, keep hidden
username_input_update = gr.update(visible=False, value=username_input.value if hasattr(username_input, 'value') else "")
add_score_button_update = gr.update(visible=False)
add_score_feedback_update = gr.update(visible=False, value="")
elif has_added_score_curr:
username_input_update = gr.update(visible=True, interactive=False, value=username_input.value if hasattr(username_input, 'value') else "")
add_score_button_update = gr.update(visible=True, interactive=False)
add_score_feedback_update = gr.update(visible=True)
# disable the buttons so the user can't vote twice
quant_data, user_data = update_leaderboards_data() # Get updated leaderboard data
return (feedback,
gr.update(interactive=False),
gr.update(interactive=False),
session_msg,
session_stats,
quant_data,
user_data,
username_input_update,
add_score_button_update,
add_score_feedback_update)
image1_btn.click(
fn=lambda mapping, sess, is_ex, has_added: choose("Image 1", mapping, sess, is_ex, has_added),
inputs=[correct_mapping_state, session_stats_state, is_example_state, has_added_score_state],
outputs=[feedback_box, image1_btn, image2_btn,
session_score_box, session_stats_state,
quant_df, user_df,
username_input, add_score_button, add_score_feedback],
)
image2_btn.click(
fn=lambda mapping, sess, is_ex, has_added: choose("Image 2", mapping, sess, is_ex, has_added),
inputs=[correct_mapping_state, session_stats_state, is_example_state, has_added_score_state],
outputs=[feedback_box, image1_btn, image2_btn,
session_score_box, session_stats_state,
quant_df, user_df,
username_input, add_score_button, add_score_feedback],
)
def handle_add_score_to_leaderboard(username_str, current_session_stats_dict):
if not username_str or not username_str.strip():
return ("Username is required.", # Feedback for add_score_feedback
gr.update(interactive=True), # username_input
gr.update(interactive=True), # add_score_button
False, # has_added_score_state
None, None) # quant_df, user_df
user_stats = _load_user_stats()
user_key = username_str.strip()
session_total_correct = sum(stats["correct"] for stats in current_session_stats_dict.values())
session_total_attempts = sum(stats["attempts"] for stats in current_session_stats_dict.values())
if session_total_attempts == 0:
return ("No attempts made in this session to add to leaderboard.",
gr.update(interactive=True),
gr.update(interactive=True),
False, None, None)
if user_key in user_stats:
user_stats[user_key]["total_correct"] += session_total_correct
user_stats[user_key]["total_attempts"] += session_total_attempts
else:
user_stats[user_key] = {
"total_correct": session_total_correct,
"total_attempts": session_total_attempts
}
_save_user_stats(user_stats)
new_quant_data, new_user_data = update_leaderboards_data()
feedback_msg = f"Score for '{user_key}' submitted to leaderboard!"
return (feedback_msg, # To add_score_feedback
gr.update(interactive=False), # username_input
gr.update(interactive=False), # add_score_button
True, # has_added_score_state (set to true)
new_quant_data, # To quant_df
new_user_data) # To user_df
add_score_button.click(
fn=handle_add_score_to_leaderboard,
inputs=[username_input, session_stats_state],
outputs=[add_score_feedback, username_input, add_score_button, has_added_score_state, quant_df, user_df]
)
with gr.TabItem("Leaderboard"):
gr.Markdown("## Quantization Method Leaderboard *(Lower % ⇒ harder to detect)*")
quant_df = gr.DataFrame(
headers=["Method", "Correct Guesses", "Total Attempts", "Detectability %"],
interactive=False, col_count=(4, "fixed")
)
gr.Markdown("## User Leaderboard *(Higher % ⇒ better spotter)*")
user_df = gr.DataFrame(
headers=["User", "Correct Guesses", "Total Attempts", "Accuracy %"],
interactive=False, col_count=(4, "fixed")
)
demo.load(update_leaderboards_data, outputs=[quant_df, user_df])
if __name__ == "__main__":
demo.launch(share=True) |