Spaces:
Running
on
Zero
Running
on
Zero
update
Browse files
app.py
CHANGED
|
@@ -54,6 +54,12 @@ import utils
|
|
| 54 |
from Waifu2x.magnify import ImageMagnifier
|
| 55 |
from RealESRGANv030.interface import realEsrgan
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
sys.path.append(os.path.dirname(__file__)) # Ensure current directory is in path
|
| 58 |
os.environ["PYTHONPATH"] = os.path.dirname(__file__)
|
| 59 |
|
|
@@ -62,7 +68,7 @@ APP_TITLE = "๐จ Super Resolution Anime Diffusion"
|
|
| 62 |
APP_DESCRIPTION = """
|
| 63 |
Generate high-quality anime images with automatic super resolution enhancement.
|
| 64 |
Combines Stable Diffusion models with advanced upscaling techniques (RealESRGAN & Waifu2x).
|
| 65 |
-
|
| 66 |
"""
|
| 67 |
|
| 68 |
CONTRIBUTION_INFO = """
|
|
@@ -401,6 +407,9 @@ def txt_to_img(
|
|
| 401 |
# Add model prefix
|
| 402 |
full_prompt = f"{current_model.prefix}, {prompt}" if current_model.prefix else prompt
|
| 403 |
|
|
|
|
|
|
|
|
|
|
| 404 |
result = pipe(
|
| 405 |
full_prompt,
|
| 406 |
negative_prompt=neg_prompt,
|
|
@@ -465,11 +474,14 @@ def img_to_img(
|
|
| 465 |
progress(0.3, desc="Processing input image...")
|
| 466 |
|
| 467 |
ratio = min(height / img.height, width / img.width)
|
| 468 |
-
img = img.resize((int(img.width * ratio), int(img.height * ratio)),
|
| 469 |
|
| 470 |
# Add model prefix
|
| 471 |
full_prompt = f"{current_model.prefix}, {prompt}" if current_model.prefix else prompt
|
| 472 |
|
|
|
|
|
|
|
|
|
|
| 473 |
if progress:
|
| 474 |
progress(0.4, desc="Transforming image...")
|
| 475 |
|
|
@@ -510,7 +522,12 @@ def load_example_images() -> List[str]:
|
|
| 510 |
example_images.append(f_img)
|
| 511 |
return example_images[:12] # Limit examples
|
| 512 |
|
| 513 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
custom_css = """
|
| 515 |
.gradio-container {
|
| 516 |
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
|
@@ -519,13 +536,26 @@ custom_css = """
|
|
| 519 |
background: #f8fafc;
|
| 520 |
}
|
| 521 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
.header-section {
|
| 523 |
text-align: center;
|
| 524 |
background: #ffffff;
|
| 525 |
color: #111827;
|
| 526 |
padding: 1.75rem;
|
| 527 |
border-radius: 14px;
|
| 528 |
-
margin-bottom: 1.
|
| 529 |
border: 1px solid #e5e7eb;
|
| 530 |
box-shadow: 0 4px 16px rgba(17,24,39,0.06);
|
| 531 |
}
|
|
@@ -571,13 +601,16 @@ def create_interface():
|
|
| 571 |
|
| 572 |
with gr.Blocks(title=APP_TITLE, css=custom_css) as demo:
|
| 573 |
|
| 574 |
-
# Header
|
| 575 |
with gr.Row():
|
| 576 |
gr.HTML(f"""
|
| 577 |
-
<div
|
| 578 |
-
|
| 579 |
-
<
|
| 580 |
-
|
|
|
|
|
|
|
|
|
|
| 581 |
</div>
|
| 582 |
""")
|
| 583 |
|
|
@@ -590,8 +623,20 @@ def create_interface():
|
|
| 590 |
with gr.Column():
|
| 591 |
gr.Markdown(CONTRIBUTION_INFO, elem_classes=["info-card"])
|
| 592 |
|
| 593 |
-
#
|
| 594 |
with gr.Tabs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
# Text-to-Image Tab
|
| 597 |
with gr.TabItem("๐จ Text-to-Image"):
|
|
@@ -749,22 +794,11 @@ def create_interface():
|
|
| 749 |
output_image_img2img = gr.Image(label="Transformed Image", type="pil")
|
| 750 |
output_info_img2img = gr.Markdown("Upload an image to transform! ๐ผ๏ธ")
|
| 751 |
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
refresh_gallery_btn = gr.Button("๐ Refresh Gallery", variant="secondary")
|
| 758 |
-
|
| 759 |
-
example_gallery = gr.Gallery(
|
| 760 |
-
value=load_example_images(),
|
| 761 |
-
label="Results Gallery",
|
| 762 |
-
show_label=False,
|
| 763 |
-
columns=4,
|
| 764 |
-
height="auto"
|
| 765 |
-
)
|
| 766 |
-
|
| 767 |
-
# Event handlers
|
| 768 |
|
| 769 |
# Model changes
|
| 770 |
model_dropdown.change(
|
|
@@ -835,12 +869,6 @@ def create_interface():
|
|
| 835 |
outputs=[output_image_img2img, output_info_img2img]
|
| 836 |
)
|
| 837 |
|
| 838 |
-
# Gallery refresh
|
| 839 |
-
refresh_gallery_btn.click(
|
| 840 |
-
fn=load_example_images,
|
| 841 |
-
outputs=[example_gallery]
|
| 842 |
-
)
|
| 843 |
-
|
| 844 |
return demo
|
| 845 |
|
| 846 |
if __name__ == "__main__":
|
|
|
|
| 54 |
from Waifu2x.magnify import ImageMagnifier
|
| 55 |
from RealESRGANv030.interface import realEsrgan
|
| 56 |
|
| 57 |
+
# Pillow resampling constant compatibility
|
| 58 |
+
try:
|
| 59 |
+
RESAMPLE_LANCZOS = Image.Resampling.LANCZOS # Pillow >=9.1.0
|
| 60 |
+
except AttributeError: # Pillow <9.1.0 fallback
|
| 61 |
+
RESAMPLE_LANCZOS = getattr(Image, "LANCZOS", Image.BICUBIC)
|
| 62 |
+
|
| 63 |
sys.path.append(os.path.dirname(__file__)) # Ensure current directory is in path
|
| 64 |
os.environ["PYTHONPATH"] = os.path.dirname(__file__)
|
| 65 |
|
|
|
|
| 68 |
APP_DESCRIPTION = """
|
| 69 |
Generate high-quality anime images with automatic super resolution enhancement.
|
| 70 |
Combines Stable Diffusion models with advanced upscaling techniques (RealESRGAN & Waifu2x).
|
| 71 |
+
[Duplicte](https://huggingface.co/spaces/yangheng95/SuperResolutionAnimeDiffusion/duplicate) this Space to run locally for full functionality and performance.
|
| 72 |
"""
|
| 73 |
|
| 74 |
CONTRIBUTION_INFO = """
|
|
|
|
| 407 |
# Add model prefix
|
| 408 |
full_prompt = f"{current_model.prefix}, {prompt}" if current_model.prefix else prompt
|
| 409 |
|
| 410 |
+
if pipe is None:
|
| 411 |
+
raise RuntimeError("Model pipeline not initialized. Please re-launch or check model loading.")
|
| 412 |
+
|
| 413 |
result = pipe(
|
| 414 |
full_prompt,
|
| 415 |
negative_prompt=neg_prompt,
|
|
|
|
| 474 |
progress(0.3, desc="Processing input image...")
|
| 475 |
|
| 476 |
ratio = min(height / img.height, width / img.width)
|
| 477 |
+
img = img.resize((int(img.width * ratio), int(img.height * ratio)), RESAMPLE_LANCZOS)
|
| 478 |
|
| 479 |
# Add model prefix
|
| 480 |
full_prompt = f"{current_model.prefix}, {prompt}" if current_model.prefix else prompt
|
| 481 |
|
| 482 |
+
if pipe is None:
|
| 483 |
+
raise RuntimeError("Model pipeline not initialized. Please re-launch or check model loading.")
|
| 484 |
+
|
| 485 |
if progress:
|
| 486 |
progress(0.4, desc="Transforming image...")
|
| 487 |
|
|
|
|
| 522 |
example_images.append(f_img)
|
| 523 |
return example_images[:12] # Limit examples
|
| 524 |
|
| 525 |
+
# Professional user advisory (displayed prominently)
|
| 526 |
+
USAGE_ADVISORY = (
|
| 527 |
+
"<div class='usage-advisory'>For full functionality and improved performance, duplicate this Space or clone the repository and run it locally (preferably with a GPU-enabled environment). This ensures unrestricted execution, optimal inference speed, and persistent storage of generated outputs.</div>"
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
# Custom CSS for styling (base block)
|
| 531 |
custom_css = """
|
| 532 |
.gradio-container {
|
| 533 |
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
|
|
|
|
| 536 |
background: #f8fafc;
|
| 537 |
}
|
| 538 |
|
| 539 |
+
.usage-advisory {
|
| 540 |
+
text-align: center;
|
| 541 |
+
background: #fff7ed;
|
| 542 |
+
color: #7c2d12;
|
| 543 |
+
padding: 0.85rem 1rem;
|
| 544 |
+
border-radius: 10px;
|
| 545 |
+
margin: 0 0 1.25rem 0;
|
| 546 |
+
border: 1px solid #fdba74;
|
| 547 |
+
font-size: 0.95rem;
|
| 548 |
+
line-height: 1.35rem;
|
| 549 |
+
box-shadow: 0 2px 6px rgba(0,0,0,0.04);
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
.header-section {
|
| 553 |
text-align: center;
|
| 554 |
background: #ffffff;
|
| 555 |
color: #111827;
|
| 556 |
padding: 1.75rem;
|
| 557 |
border-radius: 14px;
|
| 558 |
+
margin-bottom: 1.0rem;
|
| 559 |
border: 1px solid #e5e7eb;
|
| 560 |
box-shadow: 0 4px 16px rgba(17,24,39,0.06);
|
| 561 |
}
|
|
|
|
| 601 |
|
| 602 |
with gr.Blocks(title=APP_TITLE, css=custom_css) as demo:
|
| 603 |
|
| 604 |
+
# Header + Advisory
|
| 605 |
with gr.Row():
|
| 606 |
gr.HTML(f"""
|
| 607 |
+
<div style='width:100%'>
|
| 608 |
+
{USAGE_ADVISORY}
|
| 609 |
+
<div class="header-section">
|
| 610 |
+
<h1 style="font-size: 2.25rem; margin-bottom: 0.5rem;">{APP_TITLE}</h1>
|
| 611 |
+
<p style="font-size: 1.05rem; margin-bottom: 0.75rem; color:#374151;">{APP_DESCRIPTION}</p>
|
| 612 |
+
<div style="font-size: 0.95rem; color:#6b7280;">{get_system_info()}</div>
|
| 613 |
+
</div>
|
| 614 |
</div>
|
| 615 |
""")
|
| 616 |
|
|
|
|
| 623 |
with gr.Column():
|
| 624 |
gr.Markdown(CONTRIBUTION_INFO, elem_classes=["info-card"])
|
| 625 |
|
| 626 |
+
# Tabs (Gallery first so it is the default visible page)
|
| 627 |
with gr.Tabs():
|
| 628 |
+
# Gallery Tab (default)
|
| 629 |
+
with gr.TabItem("๐ผ๏ธ Gallery"):
|
| 630 |
+
gr.Markdown("### Generated Images")
|
| 631 |
+
with gr.Row():
|
| 632 |
+
refresh_gallery_btn = gr.Button("Refresh Gallery", variant="secondary")
|
| 633 |
+
example_gallery = gr.Gallery(
|
| 634 |
+
value=load_example_images(),
|
| 635 |
+
label="Results Gallery",
|
| 636 |
+
show_label=False,
|
| 637 |
+
columns=4,
|
| 638 |
+
height="auto"
|
| 639 |
+
)
|
| 640 |
|
| 641 |
# Text-to-Image Tab
|
| 642 |
with gr.TabItem("๐จ Text-to-Image"):
|
|
|
|
| 794 |
output_image_img2img = gr.Image(label="Transformed Image", type="pil")
|
| 795 |
output_info_img2img = gr.Markdown("Upload an image to transform! ๐ผ๏ธ")
|
| 796 |
|
| 797 |
+
# Event handlers (retain existing logic, ensure gallery refresh binding retained)
|
| 798 |
+
refresh_gallery_btn.click(
|
| 799 |
+
fn=load_example_images,
|
| 800 |
+
outputs=[example_gallery]
|
| 801 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 802 |
|
| 803 |
# Model changes
|
| 804 |
model_dropdown.change(
|
|
|
|
| 869 |
outputs=[output_image_img2img, output_info_img2img]
|
| 870 |
)
|
| 871 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 872 |
return demo
|
| 873 |
|
| 874 |
if __name__ == "__main__":
|