Spaces:
Runtime error
Runtime error
Image Guidance Scale
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ import time
|
|
| 10 |
import math
|
| 11 |
import random
|
| 12 |
import imageio
|
| 13 |
-
from PIL import
|
| 14 |
import torch
|
| 15 |
|
| 16 |
max_64_bit_int = 2**63 - 1
|
|
@@ -29,6 +29,7 @@ def check(
|
|
| 29 |
denoising_steps,
|
| 30 |
num_inference_steps,
|
| 31 |
guidance_scale,
|
|
|
|
| 32 |
randomize_seed,
|
| 33 |
seed,
|
| 34 |
progress = gr.Progress()):
|
|
@@ -45,6 +46,7 @@ def pix2pix(
|
|
| 45 |
denoising_steps,
|
| 46 |
num_inference_steps,
|
| 47 |
guidance_scale,
|
|
|
|
| 48 |
randomize_seed,
|
| 49 |
seed,
|
| 50 |
progress = gr.Progress()):
|
|
@@ -55,6 +57,7 @@ def pix2pix(
|
|
| 55 |
denoising_steps,
|
| 56 |
num_inference_steps,
|
| 57 |
guidance_scale,
|
|
|
|
| 58 |
randomize_seed,
|
| 59 |
seed
|
| 60 |
)
|
|
@@ -73,6 +76,9 @@ def pix2pix(
|
|
| 73 |
if guidance_scale is None:
|
| 74 |
guidance_scale = 5
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
if randomize_seed:
|
| 77 |
seed = random.randint(0, max_64_bit_int)
|
| 78 |
|
|
@@ -90,7 +96,9 @@ def pix2pix(
|
|
| 90 |
except:
|
| 91 |
raise gr.Error("Can't open input image. You can try to first save your image in another format (.webp, .png, .jpeg, .bmp...).")
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = "white")
|
| 95 |
|
| 96 |
limitation = "";
|
|
@@ -101,7 +109,7 @@ def pix2pix(
|
|
| 101 |
output_width = math.floor(output_width * factor)
|
| 102 |
output_height = math.floor(output_height * factor)
|
| 103 |
|
| 104 |
-
limitation = " Due to technical limitation, the image have been downscaled.";
|
| 105 |
|
| 106 |
# Width and height must be multiple of 8
|
| 107 |
output_width = output_width - (output_width % 8)
|
|
@@ -118,10 +126,14 @@ def pix2pix(
|
|
| 118 |
mask_image = mask_image,
|
| 119 |
num_inference_steps = num_inference_steps,
|
| 120 |
guidance_scale = guidance_scale,
|
|
|
|
| 121 |
denoising_steps = denoising_steps,
|
| 122 |
show_progress_bar = True
|
| 123 |
).images[0]
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
end = time.time()
|
| 126 |
secondes = int(end - start)
|
| 127 |
minutes = secondes // 60
|
|
@@ -164,8 +176,9 @@ with gr.Blocks() as interface:
|
|
| 164 |
with gr.Accordion("Advanced options", open = False):
|
| 165 |
negative_prompt = gr.Textbox(label = 'Negative prompt', placeholder = 'Describe what you do NOT want to see in the image', value = 'Watermark')
|
| 166 |
denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 0, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
|
| 167 |
-
num_inference_steps = gr.Slider(minimum = 10, maximum =
|
| 168 |
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 5, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
|
|
|
|
| 169 |
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed (not working, always checked)", value = True, info = "If checked, result is always different")
|
| 170 |
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed (if not randomized)")
|
| 171 |
|
|
@@ -181,6 +194,7 @@ with gr.Blocks() as interface:
|
|
| 181 |
denoising_steps,
|
| 182 |
num_inference_steps,
|
| 183 |
guidance_scale,
|
|
|
|
| 184 |
randomize_seed,
|
| 185 |
seed
|
| 186 |
], outputs = [], queue = False, show_progress = False).success(pix2pix, inputs = [
|
|
@@ -190,6 +204,7 @@ with gr.Blocks() as interface:
|
|
| 190 |
denoising_steps,
|
| 191 |
num_inference_steps,
|
| 192 |
guidance_scale,
|
|
|
|
| 193 |
randomize_seed,
|
| 194 |
seed
|
| 195 |
], outputs = [
|
|
@@ -205,6 +220,7 @@ with gr.Blocks() as interface:
|
|
| 205 |
denoising_steps,
|
| 206 |
num_inference_steps,
|
| 207 |
guidance_scale,
|
|
|
|
| 208 |
randomize_seed,
|
| 209 |
seed
|
| 210 |
],
|
|
@@ -220,6 +236,7 @@ with gr.Blocks() as interface:
|
|
| 220 |
1,
|
| 221 |
20,
|
| 222 |
5,
|
|
|
|
| 223 |
True,
|
| 224 |
42
|
| 225 |
],
|
|
@@ -230,6 +247,7 @@ with gr.Blocks() as interface:
|
|
| 230 |
1,
|
| 231 |
20,
|
| 232 |
5,
|
|
|
|
| 233 |
True,
|
| 234 |
42
|
| 235 |
],
|
|
@@ -240,6 +258,7 @@ with gr.Blocks() as interface:
|
|
| 240 |
1,
|
| 241 |
20,
|
| 242 |
5,
|
|
|
|
| 243 |
True,
|
| 244 |
42
|
| 245 |
],
|
|
|
|
| 10 |
import math
|
| 11 |
import random
|
| 12 |
import imageio
|
| 13 |
+
from PIL import Image, ImageFilter
|
| 14 |
import torch
|
| 15 |
|
| 16 |
max_64_bit_int = 2**63 - 1
|
|
|
|
| 29 |
denoising_steps,
|
| 30 |
num_inference_steps,
|
| 31 |
guidance_scale,
|
| 32 |
+
image_guidance_scale,
|
| 33 |
randomize_seed,
|
| 34 |
seed,
|
| 35 |
progress = gr.Progress()):
|
|
|
|
| 46 |
denoising_steps,
|
| 47 |
num_inference_steps,
|
| 48 |
guidance_scale,
|
| 49 |
+
image_guidance_scale,
|
| 50 |
randomize_seed,
|
| 51 |
seed,
|
| 52 |
progress = gr.Progress()):
|
|
|
|
| 57 |
denoising_steps,
|
| 58 |
num_inference_steps,
|
| 59 |
guidance_scale,
|
| 60 |
+
image_guidance_scale,
|
| 61 |
randomize_seed,
|
| 62 |
seed
|
| 63 |
)
|
|
|
|
| 76 |
if guidance_scale is None:
|
| 77 |
guidance_scale = 5
|
| 78 |
|
| 79 |
+
if image_guidance_scale is None:
|
| 80 |
+
image_guidance_scale = 1.5
|
| 81 |
+
|
| 82 |
if randomize_seed:
|
| 83 |
seed = random.randint(0, max_64_bit_int)
|
| 84 |
|
|
|
|
| 96 |
except:
|
| 97 |
raise gr.Error("Can't open input image. You can try to first save your image in another format (.webp, .png, .jpeg, .bmp...).")
|
| 98 |
|
| 99 |
+
original_height, original_width, dummy_channel = np.array(input_image).shape
|
| 100 |
+
output_width = original_width
|
| 101 |
+
output_height = original_height
|
| 102 |
mask_image = Image.new(mode = input_image.mode, size = (output_width, output_height), color = "white")
|
| 103 |
|
| 104 |
limitation = "";
|
|
|
|
| 109 |
output_width = math.floor(output_width * factor)
|
| 110 |
output_height = math.floor(output_height * factor)
|
| 111 |
|
| 112 |
+
limitation = " Due to technical limitation, the image have been downscaled and then upscaled.";
|
| 113 |
|
| 114 |
# Width and height must be multiple of 8
|
| 115 |
output_width = output_width - (output_width % 8)
|
|
|
|
| 126 |
mask_image = mask_image,
|
| 127 |
num_inference_steps = num_inference_steps,
|
| 128 |
guidance_scale = guidance_scale,
|
| 129 |
+
image_guidance_scale = image_guidance_scale,
|
| 130 |
denoising_steps = denoising_steps,
|
| 131 |
show_progress_bar = True
|
| 132 |
).images[0]
|
| 133 |
|
| 134 |
+
if limitation != "":
|
| 135 |
+
output_image = output_image.resize((original_width, original_height))
|
| 136 |
+
|
| 137 |
end = time.time()
|
| 138 |
secondes = int(end - start)
|
| 139 |
minutes = secondes // 60
|
|
|
|
| 176 |
with gr.Accordion("Advanced options", open = False):
|
| 177 |
negative_prompt = gr.Textbox(label = 'Negative prompt', placeholder = 'Describe what you do NOT want to see in the image', value = 'Watermark')
|
| 178 |
denoising_steps = gr.Slider(minimum = 0, maximum = 1000, value = 0, step = 1, label = "Denoising", info = "lower=irrelevant result, higher=relevant result")
|
| 179 |
+
num_inference_steps = gr.Slider(minimum = 10, maximum = 500, value = 20, step = 1, label = "Number of inference steps", info = "lower=faster, higher=image quality")
|
| 180 |
guidance_scale = gr.Slider(minimum = 1, maximum = 13, value = 5, step = 0.1, label = "Classifier-Free Guidance Scale", info = "lower=image quality, higher=follow the prompt")
|
| 181 |
+
image_guidance_scale = gr.Slider(minimum = 1, value = 1.5, step = 0.1, label = "Image Guidance Scale", info = "lower=image quality, higher=follow the image")
|
| 182 |
randomize_seed = gr.Checkbox(label = "\U0001F3B2 Randomize seed (not working, always checked)", value = True, info = "If checked, result is always different")
|
| 183 |
seed = gr.Slider(minimum = 0, maximum = max_64_bit_int, step = 1, randomize = True, label = "Seed (if not randomized)")
|
| 184 |
|
|
|
|
| 194 |
denoising_steps,
|
| 195 |
num_inference_steps,
|
| 196 |
guidance_scale,
|
| 197 |
+
image_guidance_scale,
|
| 198 |
randomize_seed,
|
| 199 |
seed
|
| 200 |
], outputs = [], queue = False, show_progress = False).success(pix2pix, inputs = [
|
|
|
|
| 204 |
denoising_steps,
|
| 205 |
num_inference_steps,
|
| 206 |
guidance_scale,
|
| 207 |
+
image_guidance_scale,
|
| 208 |
randomize_seed,
|
| 209 |
seed
|
| 210 |
], outputs = [
|
|
|
|
| 220 |
denoising_steps,
|
| 221 |
num_inference_steps,
|
| 222 |
guidance_scale,
|
| 223 |
+
image_guidance_scale,
|
| 224 |
randomize_seed,
|
| 225 |
seed
|
| 226 |
],
|
|
|
|
| 236 |
1,
|
| 237 |
20,
|
| 238 |
5,
|
| 239 |
+
1.5,
|
| 240 |
True,
|
| 241 |
42
|
| 242 |
],
|
|
|
|
| 247 |
1,
|
| 248 |
20,
|
| 249 |
5,
|
| 250 |
+
1.5,
|
| 251 |
True,
|
| 252 |
42
|
| 253 |
],
|
|
|
|
| 258 |
1,
|
| 259 |
20,
|
| 260 |
5,
|
| 261 |
+
1.5,
|
| 262 |
True,
|
| 263 |
42
|
| 264 |
],
|