Upload giga_App.py
Browse files- giga_App.py +62 -22
giga_App.py
CHANGED
|
@@ -6,8 +6,16 @@ import torch
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
from pathlib import Path
|
|
|
|
| 9 |
import argparse
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Force CPU usage
|
| 12 |
torch.set_default_tensor_type(torch.FloatTensor)
|
| 13 |
|
|
@@ -21,21 +29,29 @@ aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
|
|
| 21 |
# Restore original torch.load
|
| 22 |
torch.load = original_load
|
| 23 |
|
| 24 |
-
def process_single_image(input_image_path):
|
| 25 |
if input_image_path is None:
|
| 26 |
raise gr.Error("Please provide an image to upscale.")
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
| 29 |
pil_image = Image.open(input_image_path)
|
| 30 |
|
| 31 |
-
# Upscale the
|
| 32 |
start_time = time.time()
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
processing_time = time.time() - start_time
|
| 35 |
-
|
| 36 |
print(f"Processing time: {processing_time:.2f} seconds")
|
| 37 |
|
| 38 |
-
# Save the upscaled image
|
| 39 |
output_folder = "outputs"
|
| 40 |
os.makedirs(output_folder, exist_ok=True)
|
| 41 |
|
|
@@ -50,9 +66,11 @@ def process_single_image(input_image_path):
|
|
| 50 |
|
| 51 |
upscaled_image.save(output_path)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
def process_batch(input_folder, output_folder=None):
|
| 56 |
if not input_folder:
|
| 57 |
raise gr.Error("Please provide an input folder path.")
|
| 58 |
|
|
@@ -60,18 +78,24 @@ def process_batch(input_folder, output_folder=None):
|
|
| 60 |
output_folder = "outputs"
|
| 61 |
|
| 62 |
os.makedirs(output_folder, exist_ok=True)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
total_files = len(input_files)
|
| 66 |
processed_files = 0
|
| 67 |
results = []
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
for filename in input_files:
|
| 70 |
input_path = os.path.join(input_folder, filename)
|
| 71 |
pil_image = Image.open(input_path)
|
| 72 |
|
| 73 |
start_time = time.time()
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
processing_time = time.time() - start_time
|
| 76 |
|
| 77 |
output_filename = os.path.splitext(filename)[0] + ".png"
|
|
@@ -85,17 +109,17 @@ def process_batch(input_folder, output_folder=None):
|
|
| 85 |
upscaled_image.save(output_path)
|
| 86 |
|
| 87 |
processed_files += 1
|
| 88 |
-
print(f"Processed {processed_files}/{total_files}: {filename} in {processing_time:.2f} seconds")
|
| 89 |
-
|
| 90 |
results.append(output_path)
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
|
| 95 |
-
title = """<h1 align="center">AuraSR Giga Upscaler
|
| 96 |
<p><center>AuraSR: new open source super-resolution upscaler based on GigaGAN. Works perfect on some images and fails on some images so give it a try</center></p>
|
| 97 |
<p><center>Works very fast and very VRAM friendly</center></p>
|
| 98 |
-
<h2 align="center">Latest version on : <a href="https://www.patreon.com/posts/110060645">https://www.patreon.com/posts/110060645</a></
|
| 99 |
"""
|
| 100 |
|
| 101 |
def create_demo():
|
|
@@ -106,27 +130,43 @@ def create_demo():
|
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
input_image = gr.Image(label="Input Image", type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
process_btn = gr.Button(value="Upscale Image", variant="primary")
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
output_gallery = gr.Gallery(label="Before / After", columns=2)
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
| 113 |
process_btn.click(
|
| 114 |
fn=process_single_image,
|
| 115 |
-
inputs=[input_image],
|
| 116 |
-
outputs=output_gallery
|
| 117 |
)
|
| 118 |
|
| 119 |
with gr.Tab("Batch Processing"):
|
| 120 |
with gr.Row():
|
| 121 |
input_folder = gr.Textbox(label="Input Folder Path")
|
| 122 |
output_folder = gr.Textbox(label="Output Folder Path (Optional)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
batch_process_btn = gr.Button(value="Process Batch", variant="primary")
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
| 126 |
batch_process_btn.click(
|
| 127 |
fn=process_batch,
|
| 128 |
-
inputs=[input_folder, output_folder],
|
| 129 |
-
outputs=
|
| 130 |
)
|
| 131 |
|
| 132 |
return demo
|
|
|
|
| 6 |
import os
|
| 7 |
import time
|
| 8 |
from pathlib import Path
|
| 9 |
+
import platform
|
| 10 |
import argparse
|
| 11 |
|
| 12 |
+
def open_folder():
|
| 13 |
+
open_folder_path = os.path.abspath("outputs")
|
| 14 |
+
if platform.system() == "Windows":
|
| 15 |
+
os.startfile(open_folder_path)
|
| 16 |
+
elif platform.system() == "Linux":
|
| 17 |
+
os.system(f'xdg-open "{open_folder_path}"')
|
| 18 |
+
|
| 19 |
# Force CPU usage
|
| 20 |
torch.set_default_tensor_type(torch.FloatTensor)
|
| 21 |
|
|
|
|
| 29 |
# Restore original torch.load
|
| 30 |
torch.load = original_load
|
| 31 |
|
| 32 |
+
def process_single_image(input_image_path, reduce_seams):
|
| 33 |
if input_image_path is None:
|
| 34 |
raise gr.Error("Please provide an image to upscale.")
|
| 35 |
|
| 36 |
+
# Send an initial progress update.
|
| 37 |
+
yield [[], "Starting upscaling..."]
|
| 38 |
+
|
| 39 |
+
# Load the image.
|
| 40 |
pil_image = Image.open(input_image_path)
|
| 41 |
|
| 42 |
+
# Upscale using the chosen method.
|
| 43 |
start_time = time.time()
|
| 44 |
+
if reduce_seams:
|
| 45 |
+
# Using upscale_4x_overlapped to reduce seam artifacts.
|
| 46 |
+
print("using reduce seams")
|
| 47 |
+
upscaled_image = aura_sr.upscale_4x_overlapped(pil_image)
|
| 48 |
+
else:
|
| 49 |
+
# Default upscaling method.
|
| 50 |
+
upscaled_image = aura_sr.upscale_4x(pil_image)
|
| 51 |
processing_time = time.time() - start_time
|
|
|
|
| 52 |
print(f"Processing time: {processing_time:.2f} seconds")
|
| 53 |
|
| 54 |
+
# Save the upscaled image.
|
| 55 |
output_folder = "outputs"
|
| 56 |
os.makedirs(output_folder, exist_ok=True)
|
| 57 |
|
|
|
|
| 66 |
|
| 67 |
upscaled_image.save(output_path)
|
| 68 |
|
| 69 |
+
# Send the final progress update along with the before/after gallery.
|
| 70 |
+
yield [[input_image_path, output_path],
|
| 71 |
+
f"Upscaling complete in {processing_time:.2f} seconds"]
|
| 72 |
|
| 73 |
+
def process_batch(input_folder, output_folder=None, reduce_seams=False):
|
| 74 |
if not input_folder:
|
| 75 |
raise gr.Error("Please provide an input folder path.")
|
| 76 |
|
|
|
|
| 78 |
output_folder = "outputs"
|
| 79 |
|
| 80 |
os.makedirs(output_folder, exist_ok=True)
|
| 81 |
+
input_files = [f for f in os.listdir(input_folder) if f.lower().endswith(
|
| 82 |
+
('.png', '.jpg', '.jpeg', '.bmp', '.tiff'))]
|
| 83 |
total_files = len(input_files)
|
| 84 |
processed_files = 0
|
| 85 |
results = []
|
| 86 |
|
| 87 |
+
# Initial progress update.
|
| 88 |
+
yield [results, "Starting batch processing..."]
|
| 89 |
+
|
| 90 |
for filename in input_files:
|
| 91 |
input_path = os.path.join(input_folder, filename)
|
| 92 |
pil_image = Image.open(input_path)
|
| 93 |
|
| 94 |
start_time = time.time()
|
| 95 |
+
if reduce_seams:
|
| 96 |
+
upscaled_image = aura_sr.upscale_4x_overlapped(pil_image)
|
| 97 |
+
else:
|
| 98 |
+
upscaled_image = aura_sr.upscale_4x(pil_image)
|
| 99 |
processing_time = time.time() - start_time
|
| 100 |
|
| 101 |
output_filename = os.path.splitext(filename)[0] + ".png"
|
|
|
|
| 109 |
upscaled_image.save(output_path)
|
| 110 |
|
| 111 |
processed_files += 1
|
|
|
|
|
|
|
| 112 |
results.append(output_path)
|
| 113 |
+
# Yield progress update after processing each image.
|
| 114 |
+
yield [results, f"Processed {processed_files}/{total_files}: {filename} in {processing_time:.2f} seconds"]
|
| 115 |
|
| 116 |
+
# Final update.
|
| 117 |
+
yield [results, f"Batch processing complete. {processed_files} images processed."]
|
| 118 |
|
| 119 |
+
title = """<h1 align="center">AuraSR Giga Upscaler V2 by SECourses - Upscales to 4x</h1>
|
| 120 |
<p><center>AuraSR: new open source super-resolution upscaler based on GigaGAN. Works perfect on some images and fails on some images so give it a try</center></p>
|
| 121 |
<p><center>Works very fast and very VRAM friendly</center></p>
|
| 122 |
+
<h2 align="center">Latest version on : <a href="https://www.patreon.com/posts/110060645">https://www.patreon.com/posts/110060645</a></h2>
|
| 123 |
"""
|
| 124 |
|
| 125 |
def create_demo():
|
|
|
|
| 130 |
with gr.Row():
|
| 131 |
with gr.Column(scale=1):
|
| 132 |
input_image = gr.Image(label="Input Image", type="filepath")
|
| 133 |
+
reduce_seams = gr.Checkbox(
|
| 134 |
+
label="Reduce Seam Artifacts",
|
| 135 |
+
value=False,
|
| 136 |
+
info="upscale_4x upscales the image in tiles that do not overlap. This can result in seams. Use upscale_4x_overlapped to reduce seams. This will double the time upscaling by taking an additional pass and averaging the results."
|
| 137 |
+
)
|
| 138 |
process_btn = gr.Button(value="Upscale Image", variant="primary")
|
| 139 |
with gr.Column(scale=1):
|
| 140 |
output_gallery = gr.Gallery(label="Before / After", columns=2)
|
| 141 |
+
progress_text = gr.Markdown("Progress messages will appear here.")
|
| 142 |
+
btn_open_outputs = gr.Button("Open Outputs Folder", variant="primary")
|
| 143 |
+
btn_open_outputs.click(fn=open_folder)
|
| 144 |
|
| 145 |
+
# The function now yields two outputs: a gallery and a progress message.
|
| 146 |
process_btn.click(
|
| 147 |
fn=process_single_image,
|
| 148 |
+
inputs=[input_image, reduce_seams],
|
| 149 |
+
outputs=[output_gallery, progress_text]
|
| 150 |
)
|
| 151 |
|
| 152 |
with gr.Tab("Batch Processing"):
|
| 153 |
with gr.Row():
|
| 154 |
input_folder = gr.Textbox(label="Input Folder Path")
|
| 155 |
output_folder = gr.Textbox(label="Output Folder Path (Optional)")
|
| 156 |
+
reduce_seams_batch = gr.Checkbox(
|
| 157 |
+
label="Reduce Seam Artifacts",
|
| 158 |
+
value=False,
|
| 159 |
+
info="upscale_4x upscales the image in tiles that do not overlap. This can result in seams. Use upscale_4x_overlapped to reduce seams. This will double the time upscaling by taking an additional pass and averaging the results."
|
| 160 |
+
)
|
| 161 |
batch_process_btn = gr.Button(value="Process Batch", variant="primary")
|
| 162 |
+
with gr.Column():
|
| 163 |
+
output_gallery_batch = gr.Gallery(label="Processed Images")
|
| 164 |
+
progress_text_batch = gr.Markdown("Progress messages will appear here.")
|
| 165 |
|
| 166 |
batch_process_btn.click(
|
| 167 |
fn=process_batch,
|
| 168 |
+
inputs=[input_folder, output_folder, reduce_seams_batch],
|
| 169 |
+
outputs=[output_gallery_batch, progress_text_batch]
|
| 170 |
)
|
| 171 |
|
| 172 |
return demo
|