Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
from PIL import Image
|
4 |
import random
|
5 |
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
6 |
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
|
|
|
|
7 |
import cv2
|
8 |
import torch
|
9 |
import spaces
|
@@ -48,34 +51,31 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
48 |
MAX_IMAGE_SIZE = 1216
|
49 |
|
50 |
@spaces.GPU
|
51 |
-
def infer(image: Image, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps) -> Image:
|
52 |
|
53 |
-
width, height
|
54 |
ratio = np.sqrt(1024. * 1024. / (width * height))
|
55 |
new_width, new_height = int(width * ratio), int(height * ratio)
|
56 |
-
image = image.resize((new_width, new_height))
|
57 |
|
|
|
|
|
58 |
if randomize_seed:
|
59 |
seed = random.randint(0, MAX_SEED)
|
60 |
|
61 |
-
controlnet_img =
|
62 |
-
controlnet_img = nms(controlnet_img, 127, 3)
|
63 |
-
controlnet_img = cv2.GaussianBlur(controlnet_img, (0, 0), 3)
|
64 |
-
|
65 |
-
random_val = int(round(random.uniform(0.01, 0.10), 2) * 255)
|
66 |
-
controlnet_img[controlnet_img > random_val] = 255
|
67 |
-
controlnet_img[controlnet_img < 255] = 0
|
68 |
-
image = Image.fromarray(controlnet_img)
|
69 |
|
70 |
generator = torch.Generator().manual_seed(seed)
|
71 |
|
72 |
output_image = pipe(
|
73 |
prompt=prompt + ", masterpiece, best quality, very aesthetic, absurdres",
|
74 |
negative_prompt=negative_prompt,
|
|
|
|
|
75 |
guidance_scale=guidance_scale,
|
76 |
num_inference_steps=num_inference_steps,
|
77 |
-
width=
|
78 |
-
height=
|
79 |
generator=generator
|
80 |
).images[0]
|
81 |
|
@@ -135,7 +135,7 @@ with gr.Blocks(css=css) as demo:
|
|
135 |
minimum=256,
|
136 |
maximum=MAX_IMAGE_SIZE,
|
137 |
step=32,
|
138 |
-
value=832,
|
139 |
)
|
140 |
|
141 |
height = gr.Slider(
|
@@ -143,7 +143,7 @@ with gr.Blocks(css=css) as demo:
|
|
143 |
minimum=256,
|
144 |
maximum=MAX_IMAGE_SIZE,
|
145 |
step=32,
|
146 |
-
value=1216,
|
147 |
)
|
148 |
|
149 |
with gr.Row():
|
@@ -163,7 +163,7 @@ with gr.Blocks(css=css) as demo:
|
|
163 |
value=28,
|
164 |
)
|
165 |
|
166 |
-
run_button.click(
|
167 |
fn=infer,
|
168 |
inputs=[image, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
169 |
outputs=[result]
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import PIL.Image
|
4 |
from PIL import Image
|
5 |
import random
|
6 |
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
7 |
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
8 |
+
from diffusers.utils import load_image
|
9 |
+
|
10 |
import cv2
|
11 |
import torch
|
12 |
import spaces
|
|
|
51 |
MAX_IMAGE_SIZE = 1216
|
52 |
|
53 |
@spaces.GPU
|
54 |
+
def infer(image: PIL.Image.Image, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps) -> PIL.Image.Image:
|
55 |
|
56 |
+
width, height = image['composite'].size
|
57 |
ratio = np.sqrt(1024. * 1024. / (width * height))
|
58 |
new_width, new_height = int(width * ratio), int(height * ratio)
|
59 |
+
image = image['composite'].resize((new_width, new_height))
|
60 |
|
61 |
+
print(image)
|
62 |
+
|
63 |
if randomize_seed:
|
64 |
seed = random.randint(0, MAX_SEED)
|
65 |
|
66 |
+
controlnet_img = image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
generator = torch.Generator().manual_seed(seed)
|
69 |
|
70 |
output_image = pipe(
|
71 |
prompt=prompt + ", masterpiece, best quality, very aesthetic, absurdres",
|
72 |
negative_prompt=negative_prompt,
|
73 |
+
image=image,
|
74 |
+
controlnet_conditioning_scale=1.0,
|
75 |
guidance_scale=guidance_scale,
|
76 |
num_inference_steps=num_inference_steps,
|
77 |
+
width=new_width,
|
78 |
+
height=new_height,
|
79 |
generator=generator
|
80 |
).images[0]
|
81 |
|
|
|
135 |
minimum=256,
|
136 |
maximum=MAX_IMAGE_SIZE,
|
137 |
step=32,
|
138 |
+
value=1024,#832,
|
139 |
)
|
140 |
|
141 |
height = gr.Slider(
|
|
|
143 |
minimum=256,
|
144 |
maximum=MAX_IMAGE_SIZE,
|
145 |
step=32,
|
146 |
+
value=1024,#1216,
|
147 |
)
|
148 |
|
149 |
with gr.Row():
|
|
|
163 |
value=28,
|
164 |
)
|
165 |
|
166 |
+
run_button.click(lambda x: None, inputs=None, outputs=image_slider).then(
|
167 |
fn=infer,
|
168 |
inputs=[image, prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
169 |
outputs=[result]
|