MonsterMMORPG
commited on
Commit
•
08bd837
1
Parent(s):
7a2767c
Upload 2 files
Browse files- giga_App.py +140 -0
- giga_requirements.txt +7 -0
giga_App.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
from aura_sr import AuraSR
|
5 |
+
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 |
+
|
14 |
+
# Override torch.load to always use CPU
|
15 |
+
original_load = torch.load
|
16 |
+
torch.load = lambda *args, **kwargs: original_load(*args, **kwargs, map_location=torch.device('cpu'))
|
17 |
+
|
18 |
+
# Initialize the AuraSR model
|
19 |
+
aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
|
20 |
+
|
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 |
+
# Load the image
|
29 |
+
pil_image = Image.open(input_image_path)
|
30 |
+
|
31 |
+
# Upscale the image using AuraSR
|
32 |
+
start_time = time.time()
|
33 |
+
upscaled_image = aura_sr.upscale_4x(pil_image)
|
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 |
+
|
42 |
+
input_filename = os.path.basename(input_image_path)
|
43 |
+
output_filename = os.path.splitext(input_filename)[0]
|
44 |
+
output_path = os.path.join(output_folder, output_filename + ".png")
|
45 |
+
|
46 |
+
counter = 1
|
47 |
+
while os.path.exists(output_path):
|
48 |
+
output_path = os.path.join(output_folder, f"{output_filename}_{counter:04d}.png")
|
49 |
+
counter += 1
|
50 |
+
|
51 |
+
upscaled_image.save(output_path)
|
52 |
+
|
53 |
+
return [input_image_path, output_path]
|
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 |
+
|
59 |
+
if not output_folder:
|
60 |
+
output_folder = "outputs"
|
61 |
+
|
62 |
+
os.makedirs(output_folder, exist_ok=True)
|
63 |
+
|
64 |
+
input_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff'))]
|
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 |
+
upscaled_image = aura_sr.upscale_4x(pil_image)
|
75 |
+
processing_time = time.time() - start_time
|
76 |
+
|
77 |
+
output_filename = os.path.splitext(filename)[0] + ".png"
|
78 |
+
output_path = os.path.join(output_folder, output_filename)
|
79 |
+
|
80 |
+
counter = 1
|
81 |
+
while os.path.exists(output_path):
|
82 |
+
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}_{counter:04d}.png")
|
83 |
+
counter += 1
|
84 |
+
|
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 |
+
print(f"Batch processing complete. {processed_files} images processed.")
|
93 |
+
return results
|
94 |
+
|
95 |
+
title = """<h1 align="center">AuraSR Giga Upscaler V1 by SECourses - Upscales to 4x</h1>
|
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></h1>
|
99 |
+
"""
|
100 |
+
|
101 |
+
def create_demo():
|
102 |
+
with gr.Blocks() as demo:
|
103 |
+
gr.HTML(title)
|
104 |
+
|
105 |
+
with gr.Tab("Single Image"):
|
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 |
+
output_gallery = gr.Gallery(label="Processed Images")
|
125 |
+
|
126 |
+
batch_process_btn.click(
|
127 |
+
fn=process_batch,
|
128 |
+
inputs=[input_folder, output_folder],
|
129 |
+
outputs=output_gallery
|
130 |
+
)
|
131 |
+
|
132 |
+
return demo
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
parser = argparse.ArgumentParser(description="AuraSR Image Upscaling")
|
136 |
+
parser.add_argument("--share", action="store_true", help="Create a publicly shareable link")
|
137 |
+
args = parser.parse_args()
|
138 |
+
|
139 |
+
demo = create_demo()
|
140 |
+
demo.launch(debug=True, inbrowser=True, share=args.share)
|
giga_requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
--extra-index-url https://download.pytorch.org/whl/cu118
|
2 |
+
aura-sr
|
3 |
+
gradio-imageslider
|
4 |
+
gradio
|
5 |
+
torch==2.2.0
|
6 |
+
torchvision==0.17.0
|
7 |
+
numpy==1.26.4
|