Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -350,40 +350,59 @@ with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
|
| 350 |
num_steps,
|
| 351 |
guidance_scale,
|
| 352 |
seed,
|
|
|
|
| 353 |
]
|
| 354 |
with gr.Row():
|
| 355 |
helper_text = gr.Markdown("## Tap and hold (on mobile) to save the image.", visible=True)
|
| 356 |
|
| 357 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
@gr.on(triggers=[image.upload, prompt.submit, run_button.click], inputs=config,
|
| 359 |
outputs=result, show_progress="minimal")
|
| 360 |
def auto_process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 361 |
image_resolution, preprocess_resolution, num_steps, guidance_scale,
|
| 362 |
-
seed, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
# Call the core processing function
|
| 364 |
return process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 365 |
image_resolution, preprocess_resolution, num_steps, guidance_scale,
|
| 366 |
-
|
| 367 |
|
| 368 |
@gr.on(triggers=[use_ai_button.click], inputs=[result] + config, outputs=[image, result],
|
| 369 |
show_progress="minimal")
|
| 370 |
def submit(previous_result, image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 371 |
image_resolution, preprocess_resolution, num_steps, guidance_scale, seed,
|
| 372 |
-
progress=gr.Progress(track_tqdm=True)):
|
| 373 |
# First, yield the previous result to update the input image immediately
|
| 374 |
yield previous_result, gr.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
# Then, process the new input image
|
| 376 |
new_result = process_image(previous_result, style_selection, prompt, a_prompt,
|
| 377 |
n_prompt, num_images, image_resolution,
|
| 378 |
-
preprocess_resolution, num_steps, guidance_scale,
|
|
|
|
| 379 |
# Finally, yield the new result
|
| 380 |
yield previous_result, new_result
|
| 381 |
|
|
|
|
| 382 |
@gr.on(triggers=[image.upload, use_ai_button.click, run_button.click], inputs=None,
|
| 383 |
outputs=[run_button, use_ai_button], show_progress="hidden")
|
| 384 |
def turn_buttons_off():
|
| 385 |
return gr.update(visible=False), gr.update(visible=False)
|
| 386 |
|
|
|
|
| 387 |
@gr.on(triggers=[result.change], inputs=None, outputs=[use_ai_button, run_button],
|
| 388 |
show_progress="hidden")
|
| 389 |
def turn_buttons_on():
|
|
@@ -404,59 +423,30 @@ def process_image(
|
|
| 404 |
preprocess_resolution,
|
| 405 |
num_steps,
|
| 406 |
guidance_scale,
|
| 407 |
-
seed,
|
| 408 |
):
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
current_seed = seed
|
| 419 |
-
|
| 420 |
-
# to potentially override the seed slider value before passing it to `process_image`.
|
| 421 |
-
# The `randomize_seed_fn` was defined but not called in the provided Gradio code snippet.
|
| 422 |
-
# Let's stick to the provided Gradio code's logic which seems to just use the seed slider value.
|
| 423 |
-
# If randomization is needed, it should be handled in the event handler before calling process_image.
|
| 424 |
-
# However, the original `process_image` function *itself* had `seed = random.randint(0, MAX_SEED)`.
|
| 425 |
-
# This means the seed slider was ignored! Let's fix that and use the seed slider value,
|
| 426 |
-
# applying randomization if the checkbox is checked in the event handler.
|
| 427 |
-
|
| 428 |
-
# Re-adding randomize_seed to the inputs list `config` and event handler functions
|
| 429 |
-
# Then applying randomization logic in the event handler before calling process_image
|
| 430 |
-
|
| 431 |
-
# *** Correction: Re-reading the original Gradio code snippet provided at the very beginning,
|
| 432 |
-
# the `randomize_seed_fn` was defined but not used. The `process_image` function
|
| 433 |
-
# *inside* the `if gr.NO_RELOAD:` block *did* have `seed = random.randint(0, MAX_SEED)`
|
| 434 |
-
# at the beginning, effectively ignoring the input seed slider value unless randomize_seed_fn
|
| 435 |
-
# was somehow implicitly called or the logic was elsewhere.
|
| 436 |
-
# Let's assume the intention was to use the seed slider, with an option to randomize.
|
| 437 |
-
# The `randomize_seed_fn` should be called in the Gradio event handlers.
|
| 438 |
-
|
| 439 |
-
# Let's modify the Gradio event handlers to use randomize_seed_fn
|
| 440 |
-
|
| 441 |
-
# *** Another Correction: The `process_image` function signature in the original Gradio code
|
| 442 |
-
# included `seed` as a parameter, but the first line inside the function was `seed = random.randint(0, MAX_SEED)`.
|
| 443 |
-
# This is contradictory. It implies the input `seed` was always overwritten by a random value.
|
| 444 |
-
# Let's assume the *intent* was to use the input `seed` unless `randomize_seed` was True.
|
| 445 |
-
# The `randomize_seed_fn` should be called in the event handler to get the final seed value.
|
| 446 |
-
|
| 447 |
-
# Let's modify the event handlers to call randomize_seed_fn and pass the result as `seed` to `process_image`.
|
| 448 |
-
# The `process_image` function itself should *not* randomize the seed internally if a seed is passed.
|
| 449 |
-
|
| 450 |
-
# *** Final Decision: Let's revert `process_image` to its signature from the original Gradio code,
|
| 451 |
-
# but remove the internal `seed = random.randint(0, MAX_SEED)` line.
|
| 452 |
-
# The randomization logic will be added to the Gradio event handlers using `randomize_seed_fn`.
|
| 453 |
-
# The `config` list and event handlers will need `randomize_seed` as an input.
|
| 454 |
-
|
| 455 |
-
# Re-adding randomize_seed to config and event handler signatures
|
| 456 |
-
# Adding call to randomize_seed_fn in event handlers
|
| 457 |
-
|
| 458 |
-
# --- Start of process_image logic ---
|
| 459 |
-
# Seed handling moved to event handlers
|
| 460 |
|
| 461 |
if preprocessor.name != "NormalBae":
|
| 462 |
preprocessor.load("NormalBae")
|
|
@@ -481,13 +471,10 @@ def process_image(
|
|
| 481 |
negative_prompt = str(n_prompt)
|
| 482 |
print(f"Using prompt: {full_prompt}")
|
| 483 |
print(f"Using negative prompt: {negative_prompt}")
|
| 484 |
-
print(f"Using seed: {
|
| 485 |
|
| 486 |
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 487 |
|
| 488 |
-
# Use the generator created with the potentially randomized seed
|
| 489 |
-
generator = torch.cuda.manual_seed(seed) if torch.cuda.is_available() else torch.manual_seed(seed)
|
| 490 |
-
|
| 491 |
with torch.no_grad():
|
| 492 |
initial_result = pipe(
|
| 493 |
prompt=full_prompt,
|
|
@@ -525,76 +512,6 @@ def process_image(
|
|
| 525 |
print(f"Error saving or uploading image: {e}")
|
| 526 |
|
| 527 |
return initial_result
|
| 528 |
-
# --- End of process_image logic ---
|
| 529 |
-
|
| 530 |
-
# Re-adding randomize_seed_fn
|
| 531 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 532 |
-
if randomize_seed:
|
| 533 |
-
seed = random.randint(0, MAX_SEED)
|
| 534 |
-
return seed
|
| 535 |
-
|
| 536 |
-
# Re-adding randomize_seed to config list
|
| 537 |
-
config = [
|
| 538 |
-
image,
|
| 539 |
-
style_selection,
|
| 540 |
-
prompt,
|
| 541 |
-
a_prompt,
|
| 542 |
-
n_prompt,
|
| 543 |
-
num_images,
|
| 544 |
-
image_resolution,
|
| 545 |
-
preprocess_resolution,
|
| 546 |
-
num_steps,
|
| 547 |
-
guidance_scale,
|
| 548 |
-
seed,
|
| 549 |
-
randomize_seed, # Added randomize_seed to config
|
| 550 |
-
]
|
| 551 |
-
|
| 552 |
-
# Re-defining Gradio Event Handling Functions with randomize_seed input and seed randomization logic
|
| 553 |
-
@gr.on(triggers=[image.upload, prompt.submit, run_button.click], inputs=config,
|
| 554 |
-
outputs=result, show_progress="minimal")
|
| 555 |
-
def auto_process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 556 |
-
image_resolution, preprocess_resolution, num_steps, guidance_scale,
|
| 557 |
-
seed, randomize_seed, progress=gr.Progress(track_tqdm=True)): # Added randomize_seed
|
| 558 |
-
# Apply seed randomization
|
| 559 |
-
processed_seed = randomize_seed_fn(seed, randomize_seed)
|
| 560 |
-
print(f"Using processed seed: {processed_seed}") # Debug print
|
| 561 |
-
|
| 562 |
-
# Call the core processing function with the processed seed
|
| 563 |
-
return process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 564 |
-
image_resolution, preprocess_resolution, num_steps, guidance_scale,
|
| 565 |
-
processed_seed) # Pass processed_seed
|
| 566 |
-
|
| 567 |
-
@gr.on(triggers=[use_ai_button.click], inputs=[result] + config, outputs=[image, result],
|
| 568 |
-
show_progress="minimal")
|
| 569 |
-
def submit(previous_result, image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 570 |
-
image_resolution, preprocess_resolution, num_steps, guidance_scale, seed,
|
| 571 |
-
randomize_seed, progress=gr.Progress(track_tqdm=True)): # Added randomize_seed
|
| 572 |
-
# First, yield the previous result to update the input image immediately
|
| 573 |
-
yield previous_result, gr.update()
|
| 574 |
-
|
| 575 |
-
# Apply seed randomization
|
| 576 |
-
processed_seed = randomize_seed_fn(seed, randomize_seed)
|
| 577 |
-
print(f"Using processed seed: {processed_seed}") # Debug print
|
| 578 |
-
|
| 579 |
-
# Then, process the new input image
|
| 580 |
-
new_result = process_image(previous_result, style_selection, prompt, a_prompt,
|
| 581 |
-
n_prompt, num_images, image_resolution,
|
| 582 |
-
preprocess_resolution, num_steps, guidance_scale,
|
| 583 |
-
processed_seed) # Pass processed_seed
|
| 584 |
-
# Finally, yield the new result
|
| 585 |
-
yield previous_result, new_result
|
| 586 |
-
|
| 587 |
-
# Turn off buttons when processing - These functions remain the same
|
| 588 |
-
@gr.on(triggers=[image.upload, use_ai_button.click, run_button.click], inputs=None,
|
| 589 |
-
outputs=[run_button, use_ai_button], show_progress="hidden")
|
| 590 |
-
def turn_buttons_off():
|
| 591 |
-
return gr.update(visible=False), gr.update(visible=False)
|
| 592 |
-
|
| 593 |
-
# Turn on buttons when processing is complete - These functions remain the same
|
| 594 |
-
@gr.on(triggers=[result.change], inputs=None, outputs=[use_ai_button, run_button],
|
| 595 |
-
show_progress="hidden")
|
| 596 |
-
def turn_buttons_on():
|
| 597 |
-
return gr.update(visible=True), gr.update(visible=True)
|
| 598 |
|
| 599 |
|
| 600 |
# Launch the Gradio app
|
|
|
|
| 350 |
num_steps,
|
| 351 |
guidance_scale,
|
| 352 |
seed,
|
| 353 |
+
randomize_seed, # Added randomize_seed to config
|
| 354 |
]
|
| 355 |
with gr.Row():
|
| 356 |
helper_text = gr.Markdown("## Tap and hold (on mobile) to save the image.", visible=True)
|
| 357 |
|
| 358 |
+
# Re-adding randomize_seed_fn inside the Blocks context
|
| 359 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 360 |
+
if randomize_seed:
|
| 361 |
+
seed = random.randint(0, MAX_SEED)
|
| 362 |
+
return seed
|
| 363 |
+
|
| 364 |
+
# Gradio Event Handling Functions - MOVED INSIDE gr.Blocks context
|
| 365 |
@gr.on(triggers=[image.upload, prompt.submit, run_button.click], inputs=config,
|
| 366 |
outputs=result, show_progress="minimal")
|
| 367 |
def auto_process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 368 |
image_resolution, preprocess_resolution, num_steps, guidance_scale,
|
| 369 |
+
seed, randomize_seed, progress=gr.Progress(track_tqdm=True)): # Added randomize_seed
|
| 370 |
+
# Apply seed randomization
|
| 371 |
+
processed_seed = randomize_seed_fn(seed, randomize_seed)
|
| 372 |
+
print(f"Using processed seed: {processed_seed}") # Debug print
|
| 373 |
+
|
| 374 |
# Call the core processing function
|
| 375 |
return process_image(image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 376 |
image_resolution, preprocess_resolution, num_steps, guidance_scale,
|
| 377 |
+
processed_seed) # Pass processed_seed
|
| 378 |
|
| 379 |
@gr.on(triggers=[use_ai_button.click], inputs=[result] + config, outputs=[image, result],
|
| 380 |
show_progress="minimal")
|
| 381 |
def submit(previous_result, image, style_selection, prompt, a_prompt, n_prompt, num_images,
|
| 382 |
image_resolution, preprocess_resolution, num_steps, guidance_scale, seed,
|
| 383 |
+
randomize_seed, progress=gr.Progress(track_tqdm=True)): # Added randomize_seed
|
| 384 |
# First, yield the previous result to update the input image immediately
|
| 385 |
yield previous_result, gr.update()
|
| 386 |
+
|
| 387 |
+
# Apply seed randomization
|
| 388 |
+
processed_seed = randomize_seed_fn(seed, randomize_seed)
|
| 389 |
+
print(f"Using processed seed: {processed_seed}") # Debug print
|
| 390 |
+
|
| 391 |
# Then, process the new input image
|
| 392 |
new_result = process_image(previous_result, style_selection, prompt, a_prompt,
|
| 393 |
n_prompt, num_images, image_resolution,
|
| 394 |
+
preprocess_resolution, num_steps, guidance_scale,
|
| 395 |
+
processed_seed) # Pass processed_seed
|
| 396 |
# Finally, yield the new result
|
| 397 |
yield previous_result, new_result
|
| 398 |
|
| 399 |
+
# Turn off buttons when processing - These functions remain the same
|
| 400 |
@gr.on(triggers=[image.upload, use_ai_button.click, run_button.click], inputs=None,
|
| 401 |
outputs=[run_button, use_ai_button], show_progress="hidden")
|
| 402 |
def turn_buttons_off():
|
| 403 |
return gr.update(visible=False), gr.update(visible=False)
|
| 404 |
|
| 405 |
+
# Turn on buttons when processing is complete - These functions remain the same
|
| 406 |
@gr.on(triggers=[result.change], inputs=None, outputs=[use_ai_button, run_button],
|
| 407 |
show_progress="hidden")
|
| 408 |
def turn_buttons_on():
|
|
|
|
| 423 |
preprocess_resolution,
|
| 424 |
num_steps,
|
| 425 |
guidance_scale,
|
| 426 |
+
seed, # Now receives the potentially randomized seed
|
| 427 |
):
|
| 428 |
+
"""
|
| 429 |
+
Processes an input image to generate a new image based on style and prompts.
|
| 430 |
+
|
| 431 |
+
Args:
|
| 432 |
+
image: Input PIL Image.
|
| 433 |
+
style_selection: Name of the design style to apply.
|
| 434 |
+
prompt: Custom design prompt.
|
| 435 |
+
a_prompt: Additional positive prompt.
|
| 436 |
+
n_prompt: Negative prompt.
|
| 437 |
+
num_images: Number of images to generate (currently only 1 supported by pipeline).
|
| 438 |
+
image_resolution: Resolution for the output image.
|
| 439 |
+
preprocess_resolution: Resolution for the preprocessor.
|
| 440 |
+
num_steps: Number of inference steps.
|
| 441 |
+
guidance_scale: Guidance scale for the diffusion process.
|
| 442 |
+
seed: Random seed for reproducibility.
|
| 443 |
+
|
| 444 |
+
Returns:
|
| 445 |
+
A PIL Image of the generated result.
|
| 446 |
+
"""
|
| 447 |
+
# Use the seed passed from the event handler
|
| 448 |
current_seed = seed
|
| 449 |
+
generator = torch.cuda.manual_seed(current_seed) if torch.cuda.is_available() else torch.manual_seed(current_seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
if preprocessor.name != "NormalBae":
|
| 452 |
preprocessor.load("NormalBae")
|
|
|
|
| 471 |
negative_prompt = str(n_prompt)
|
| 472 |
print(f"Using prompt: {full_prompt}")
|
| 473 |
print(f"Using negative prompt: {negative_prompt}")
|
| 474 |
+
print(f"Using seed: {current_seed}")
|
| 475 |
|
| 476 |
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 477 |
|
|
|
|
|
|
|
|
|
|
| 478 |
with torch.no_grad():
|
| 479 |
initial_result = pipe(
|
| 480 |
prompt=full_prompt,
|
|
|
|
| 512 |
print(f"Error saving or uploading image: {e}")
|
| 513 |
|
| 514 |
return initial_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
|
| 517 |
# Launch the Gradio app
|