Spaces:
Runtime error
Runtime error
patrickvonplaten
commited on
Commit
·
e94f64d
1
Parent(s):
0711b9e
up
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from diffusers import DiffusionPipeline
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from PIL import Image
|
@@ -9,12 +9,16 @@ import random
|
|
9 |
|
10 |
|
11 |
start_time = time.time()
|
12 |
-
current_steps =
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
17 |
|
|
|
|
|
|
|
18 |
|
19 |
def error_str(error, title="Error"):
|
20 |
return (
|
@@ -27,17 +31,15 @@ def error_str(error, title="Error"):
|
|
27 |
|
28 |
def inference(
|
29 |
prompt,
|
30 |
-
|
|
|
|
|
31 |
steps,
|
32 |
-
n_images=1,
|
33 |
-
width=512,
|
34 |
-
height=512,
|
35 |
-
seed=0,
|
36 |
-
img=None,
|
37 |
-
strength=0.5,
|
38 |
neg_prompt="",
|
|
|
|
|
|
|
39 |
):
|
40 |
-
|
41 |
print(psutil.virtual_memory()) # print memory usage
|
42 |
|
43 |
if seed == 0:
|
@@ -46,63 +48,25 @@ def inference(
|
|
46 |
generator = torch.Generator("cuda").manual_seed(seed)
|
47 |
|
48 |
try:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
generator,
|
61 |
-
seed,
|
62 |
-
),
|
63 |
-
f"Done. Seed: {seed}",
|
64 |
)
|
|
|
|
|
|
|
65 |
except Exception as e:
|
66 |
return None, error_str(e)
|
67 |
|
68 |
|
69 |
-
def img_to_img(
|
70 |
-
prompt,
|
71 |
-
n_images,
|
72 |
-
neg_prompt,
|
73 |
-
img,
|
74 |
-
strength,
|
75 |
-
guidance,
|
76 |
-
steps,
|
77 |
-
width,
|
78 |
-
height,
|
79 |
-
generator,
|
80 |
-
seed,
|
81 |
-
):
|
82 |
-
pipe = PIPE
|
83 |
-
|
84 |
-
if torch.cuda.is_available():
|
85 |
-
pipe = pipe.to("cuda")
|
86 |
-
pipe.enable_xformers_memory_efficient_attention()
|
87 |
-
|
88 |
-
ratio = min(height / img.height, width / img.width)
|
89 |
-
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
90 |
-
|
91 |
-
result = pipe(
|
92 |
-
prompt,
|
93 |
-
negative_prompt=neg_prompt,
|
94 |
-
num_images_per_prompt=n_images,
|
95 |
-
image=img,
|
96 |
-
num_inference_steps=int(steps),
|
97 |
-
strength=strength,
|
98 |
-
guidance_scale=guidance,
|
99 |
-
generator=generator,
|
100 |
-
)
|
101 |
-
|
102 |
-
# return replace_nsfw_images(result)
|
103 |
-
return result.images
|
104 |
-
|
105 |
-
|
106 |
def replace_nsfw_images(results):
|
107 |
for i in range(len(results.images)):
|
108 |
if results.nsfw_content_detected[i]:
|
@@ -170,9 +134,6 @@ with gr.Blocks(css="style.css") as demo:
|
|
170 |
)
|
171 |
|
172 |
with gr.Row():
|
173 |
-
guidance = gr.Slider(
|
174 |
-
label="Guidance scale", value=7.5, maximum=15
|
175 |
-
)
|
176 |
steps = gr.Slider(
|
177 |
label="Steps",
|
178 |
value=current_steps,
|
@@ -197,25 +158,27 @@ with gr.Blocks(css="style.css") as demo:
|
|
197 |
image = gr.Image(
|
198 |
label="Image", height=256, tool="editor", type="pil"
|
199 |
)
|
200 |
-
|
201 |
-
label="
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
|
|
|
|
206 |
)
|
207 |
|
208 |
inputs = [
|
209 |
prompt,
|
210 |
-
|
|
|
|
|
211 |
steps,
|
212 |
-
|
213 |
width,
|
214 |
height,
|
215 |
seed,
|
216 |
-
image,
|
217 |
-
strength,
|
218 |
-
neg_prompt,
|
219 |
]
|
220 |
outputs = [gallery, error_output]
|
221 |
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
@@ -223,7 +186,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
223 |
|
224 |
ex = gr.Examples(
|
225 |
[],
|
226 |
-
inputs=[prompt,
|
227 |
outputs=outputs,
|
228 |
fn=inference,
|
229 |
cache_examples=True,
|
|
|
1 |
+
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from PIL import Image
|
|
|
9 |
|
10 |
|
11 |
start_time = time.time()
|
12 |
+
current_steps = 15
|
13 |
|
14 |
+
pipe = DiffusionPipeline.from_pretrained("timbrooks/instruct-pix2pix", torch_dtype=torch.float16, safety_checker=None)
|
15 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
16 |
|
17 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
18 |
|
19 |
+
if torch.cuda.is_available():
|
20 |
+
pipe = pipe.to("cuda")
|
21 |
+
|
22 |
|
23 |
def error_str(error, title="Error"):
|
24 |
return (
|
|
|
31 |
|
32 |
def inference(
|
33 |
prompt,
|
34 |
+
text_guidance_scale,
|
35 |
+
image_guidance_scale,
|
36 |
+
image,
|
37 |
steps,
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
neg_prompt="",
|
39 |
+
width=256,
|
40 |
+
height=256,
|
41 |
+
seed=0,
|
42 |
):
|
|
|
43 |
print(psutil.virtual_memory()) # print memory usage
|
44 |
|
45 |
if seed == 0:
|
|
|
48 |
generator = torch.Generator("cuda").manual_seed(seed)
|
49 |
|
50 |
try:
|
51 |
+
ratio = min(height / image.height, width / image.width)
|
52 |
+
image = image.resize((int(image.width * ratio), int(image.height * ratio)), Image.LANCZOS)
|
53 |
+
|
54 |
+
result = pipe(
|
55 |
+
prompt,
|
56 |
+
negative_prompt=neg_prompt,
|
57 |
+
image=image,
|
58 |
+
num_inference_steps=int(steps),
|
59 |
+
image_guidance_scale=image_guidance_scale,
|
60 |
+
guidance_scale=text_guidance_scale,
|
61 |
+
generator=generator,
|
|
|
|
|
|
|
|
|
62 |
)
|
63 |
+
|
64 |
+
# return replace_nsfw_images(result)
|
65 |
+
return result.images, f"Done. Seed: {seed}"
|
66 |
except Exception as e:
|
67 |
return None, error_str(e)
|
68 |
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def replace_nsfw_images(results):
|
71 |
for i in range(len(results.images)):
|
72 |
if results.nsfw_content_detected[i]:
|
|
|
134 |
)
|
135 |
|
136 |
with gr.Row():
|
|
|
|
|
|
|
137 |
steps = gr.Slider(
|
138 |
label="Steps",
|
139 |
value=current_steps,
|
|
|
158 |
image = gr.Image(
|
159 |
label="Image", height=256, tool="editor", type="pil"
|
160 |
)
|
161 |
+
text_guidance_scale = gr.Slider(
|
162 |
+
label="Text Guidance Scale", minimum=1.0, value=5.5, maximum=15, step=0.1
|
163 |
+
)
|
164 |
+
image_guidance_scale = gr.Slider(
|
165 |
+
label="Image Guidance Scale",
|
166 |
+
minimum=1.0,
|
167 |
+
maximum=15,
|
168 |
+
step=0.1,
|
169 |
+
value=1.5,
|
170 |
)
|
171 |
|
172 |
inputs = [
|
173 |
prompt,
|
174 |
+
text_guidance_scale,
|
175 |
+
image_guidance_scale,
|
176 |
+
image,
|
177 |
steps,
|
178 |
+
neg_prompt,
|
179 |
width,
|
180 |
height,
|
181 |
seed,
|
|
|
|
|
|
|
182 |
]
|
183 |
outputs = [gallery, error_output]
|
184 |
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
|
|
186 |
|
187 |
ex = gr.Examples(
|
188 |
[],
|
189 |
+
inputs=[prompt, text_guidance_scale, image_guidance_scale, image, steps],
|
190 |
outputs=outputs,
|
191 |
fn=inference,
|
192 |
cache_examples=True,
|