Spaces:
Sleeping
Sleeping
lichorosario
commited on
Commit
•
523674a
1
Parent(s):
3a2e4dc
feat: Add image resizing functionality and reduce factor option
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
from gradio_client import Client, handle_file
|
5 |
from gradio_imageslider import ImageSlider
|
|
|
6 |
|
7 |
with open('loras.json', 'r') as f:
|
8 |
loras = json.load(f)
|
@@ -113,8 +115,21 @@ def update_selection(evt: gr.SelectData):
|
|
113 |
evt.index
|
114 |
)
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
global client_tile_upscaler
|
119 |
|
120 |
# if client_tile_upscaler is None:
|
@@ -125,6 +140,10 @@ def upscale_image(image, resolution, num_inference_steps, strength, hdr, guidanc
|
|
125 |
print(f"Failed to load custom model: {e}")
|
126 |
client_custom_model = None
|
127 |
raise gr.Error("Failed to load client for " + tile_upscaler_url)
|
|
|
|
|
|
|
|
|
128 |
|
129 |
try:
|
130 |
result = client_tile_upscaler.predict(
|
@@ -264,6 +283,7 @@ with gr.Blocks(css=css) as demo:
|
|
264 |
output_slider = ImageSlider(label="Before / After", type="numpy", show_download_button=False)
|
265 |
|
266 |
with gr.Accordion("Advanced Options", open=False):
|
|
|
267 |
upscale_resolution = gr.Slider(minimum=128, maximum=2048, value=1024, step=128, label="Resolution")
|
268 |
upscale_num_inference_steps = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of Inference Steps")
|
269 |
upscale_strength = gr.Slider(minimum=0, maximum=1, value=0.2, step=0.01, label="Strength")
|
@@ -297,7 +317,7 @@ with gr.Blocks(css=css) as demo:
|
|
297 |
outputs=[output_slider]
|
298 |
).then(
|
299 |
upscale_image,
|
300 |
-
[generated_image, upscale_resolution, upscale_num_inference_steps, upscale_strength, upscale_hdr, upscale_guidance_scale, upscale_controlnet_strength, upscale_scheduler_name],
|
301 |
output_slider
|
302 |
)
|
303 |
|
|
|
1 |
import os
|
2 |
+
import uuid
|
3 |
import gradio as gr
|
4 |
import json
|
5 |
from gradio_client import Client, handle_file
|
6 |
from gradio_imageslider import ImageSlider
|
7 |
+
from PIL import Image
|
8 |
|
9 |
with open('loras.json', 'r') as f:
|
10 |
loras = json.load(f)
|
|
|
115 |
evt.index
|
116 |
)
|
117 |
|
118 |
+
def resize_image(image_path, reduction_factor):
|
119 |
+
image = Image.open(image_path)
|
120 |
+
width, height = image.size
|
121 |
+
new_size = (width // reduction_factor, height // reduction_factor)
|
122 |
+
resized_image = image.resize(new_size)
|
123 |
+
return resized_image
|
124 |
|
125 |
+
|
126 |
+
def save_image(image):
|
127 |
+
unique_filename = f"resized_image_{uuid.uuid4().hex}.png"
|
128 |
+
image.save(unique_filename)
|
129 |
+
return unique_filename
|
130 |
+
|
131 |
+
|
132 |
+
def upscale_image(image, resolution, num_inference_steps, strength, hdr, guidance_scale, controlnet_strength, scheduler_name, reduce_factor):
|
133 |
global client_tile_upscaler
|
134 |
|
135 |
# if client_tile_upscaler is None:
|
|
|
140 |
print(f"Failed to load custom model: {e}")
|
141 |
client_custom_model = None
|
142 |
raise gr.Error("Failed to load client for " + tile_upscaler_url)
|
143 |
+
|
144 |
+
if (reduce_factor > 1):
|
145 |
+
image = resize_image(image, reduce_factor)
|
146 |
+
image = save_image(image)
|
147 |
|
148 |
try:
|
149 |
result = client_tile_upscaler.predict(
|
|
|
283 |
output_slider = ImageSlider(label="Before / After", type="numpy", show_download_button=False)
|
284 |
|
285 |
with gr.Accordion("Advanced Options", open=False):
|
286 |
+
upscale_reduce_factor = gr.Slide(minimum=1, maximum=10)
|
287 |
upscale_resolution = gr.Slider(minimum=128, maximum=2048, value=1024, step=128, label="Resolution")
|
288 |
upscale_num_inference_steps = gr.Slider(minimum=1, maximum=50, value=20, step=1, label="Number of Inference Steps")
|
289 |
upscale_strength = gr.Slider(minimum=0, maximum=1, value=0.2, step=0.01, label="Strength")
|
|
|
317 |
outputs=[output_slider]
|
318 |
).then(
|
319 |
upscale_image,
|
320 |
+
[generated_image, upscale_resolution, upscale_num_inference_steps, upscale_strength, upscale_hdr, upscale_guidance_scale, upscale_controlnet_strength, upscale_scheduler_name, upscale_reduce_factor],
|
321 |
output_slider
|
322 |
)
|
323 |
|