local inference enhancements
Browse files
app.py
CHANGED
@@ -1,26 +1,57 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
|
|
|
|
4 |
|
5 |
# import spaces #[uncomment to use ZeroGPU]
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
8 |
|
|
|
|
|
|
|
|
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
|
11 |
|
12 |
if torch.cuda.is_available():
|
13 |
torch_dtype = torch.float16
|
14 |
else:
|
15 |
torch_dtype = torch.float32
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
pipe = pipe.to(device)
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
21 |
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
-
|
24 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
25 |
def infer(
|
26 |
prompt,
|
@@ -38,6 +69,9 @@ def infer(
|
|
38 |
|
39 |
generator = torch.Generator().manual_seed(seed)
|
40 |
|
|
|
|
|
|
|
41 |
image = pipe(
|
42 |
prompt=prompt,
|
43 |
negative_prompt=negative_prompt,
|
@@ -46,6 +80,7 @@ def infer(
|
|
46 |
width=width,
|
47 |
height=height,
|
48 |
generator=generator,
|
|
|
49 |
).images[0]
|
50 |
|
51 |
return image, seed
|
@@ -64,9 +99,14 @@ css = """
|
|
64 |
}
|
65 |
"""
|
66 |
|
67 |
-
with gr.Blocks(css=css) as
|
68 |
with gr.Column(elem_id="col-container"):
|
69 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Row():
|
72 |
prompt = gr.Text(
|
@@ -102,18 +142,18 @@ with gr.Blocks(css=css) as demo_cpu:
|
|
102 |
with gr.Row():
|
103 |
width = gr.Slider(
|
104 |
label="Width",
|
105 |
-
minimum=
|
106 |
maximum=MAX_IMAGE_SIZE,
|
107 |
step=32,
|
108 |
-
value=
|
109 |
)
|
110 |
|
111 |
height = gr.Slider(
|
112 |
label="Height",
|
113 |
-
minimum=
|
114 |
maximum=MAX_IMAGE_SIZE,
|
115 |
step=32,
|
116 |
-
value=
|
117 |
)
|
118 |
|
119 |
with gr.Row():
|
@@ -122,7 +162,7 @@ with gr.Blocks(css=css) as demo_cpu:
|
|
122 |
minimum=0.0,
|
123 |
maximum=10.0,
|
124 |
step=0.1,
|
125 |
-
value=0.0,
|
126 |
)
|
127 |
|
128 |
num_inference_steps = gr.Slider(
|
@@ -130,7 +170,7 @@ with gr.Blocks(css=css) as demo_cpu:
|
|
130 |
minimum=1,
|
131 |
maximum=50,
|
132 |
step=1,
|
133 |
-
value=
|
134 |
)
|
135 |
|
136 |
gr.Examples(examples=examples, inputs=[prompt])
|
@@ -151,9 +191,9 @@ with gr.Blocks(css=css) as demo_cpu:
|
|
151 |
)
|
152 |
|
153 |
|
154 |
-
demo_inference = gr.load(
|
155 |
|
156 |
-
demo = gr.TabbedInterface([demo_inference,
|
157 |
|
158 |
if __name__ == "__main__":
|
159 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
|
7 |
# import spaces #[uncomment to use ZeroGPU]
|
8 |
+
from diffusers import DiffusionPipeline, StableDiffusionPipeline, schedulers
|
9 |
import torch
|
10 |
|
11 |
+
MODEL_REPO_ID = os.environ.get('MODEL_REPO_ID', 'myxlmynx/cyberrealistic_classic40')
|
12 |
+
MODEL_REPO_LOCAL = os.environ.get('MODEL_REPO_LOCAL', '')
|
13 |
+
MODEL_REPO_NAME = os.environ.get('MODEL_REPO_NAME', 'CyberRealistic Classic 4.0')
|
14 |
+
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
+
print("Running on " + device)
|
17 |
|
18 |
if torch.cuda.is_available():
|
19 |
torch_dtype = torch.float16
|
20 |
else:
|
21 |
torch_dtype = torch.float32
|
22 |
|
23 |
+
print("Loading " + MODEL_REPO_ID)
|
24 |
+
if MODEL_REPO_LOCAL and Path(MODEL_REPO_LOCAL).is_file():
|
25 |
+
pipe = StableDiffusionPipeline.from_single_file(MODEL_REPO_LOCAL, torch_dtype=torch_dtype)
|
26 |
+
else:
|
27 |
+
pipe = DiffusionPipeline.from_pretrained(MODEL_REPO_ID, torch_dtype=torch_dtype)
|
28 |
+
|
29 |
+
extra_inference_parameters = {}
|
30 |
+
|
31 |
+
# add accel LoRA to boost generation speed
|
32 |
+
pipe.load_lora_weights("wangfuyun/PCM_Weights",
|
33 |
+
subfolder='sd15', weight_name='pcm_sd15_smallcfg_2step_converted.safetensors',
|
34 |
+
adapter_name='pcm_smallcfg_2step')
|
35 |
+
pipe.set_adapters(['pcm_smallcfg_2step'], adapter_weights=[1.0])
|
36 |
+
pipe.fuse_lora()
|
37 |
+
|
38 |
+
# for very low step counts with PCM
|
39 |
+
#pipe.scheduler = schedulers.DDIMScheduler(timestep_spacing='trailing',
|
40 |
+
# clip_sample=False, set_alpha_to_one=False)
|
41 |
+
pipe.scheduler = schedulers.TCDScheduler()
|
42 |
+
extra_inference_parameters['eta'] = 0.3
|
43 |
+
#pipe.scheduler = schedulers.LCMScheduler()
|
44 |
+
#pipe.scheduler = schedulers.EulerAncestralDiscreteScheduler()
|
45 |
+
|
46 |
+
# lib default will fry the image
|
47 |
+
default_guidance_scale = 1
|
48 |
+
|
49 |
pipe = pipe.to(device)
|
50 |
|
51 |
MAX_SEED = np.iinfo(np.int32).max
|
52 |
+
MIN_IMAGE_SIZE = 128
|
53 |
MAX_IMAGE_SIZE = 1024
|
54 |
|
|
|
55 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
56 |
def infer(
|
57 |
prompt,
|
|
|
69 |
|
70 |
generator = torch.Generator().manual_seed(seed)
|
71 |
|
72 |
+
if guidance_scale == 0:
|
73 |
+
guidance_scale = default_guidance_scale
|
74 |
+
|
75 |
image = pipe(
|
76 |
prompt=prompt,
|
77 |
negative_prompt=negative_prompt,
|
|
|
80 |
width=width,
|
81 |
height=height,
|
82 |
generator=generator,
|
83 |
+
**extra_inference_parameters
|
84 |
).images[0]
|
85 |
|
86 |
return image, seed
|
|
|
99 |
}
|
100 |
"""
|
101 |
|
102 |
+
with gr.Blocks(css=css) as demo_device:
|
103 |
with gr.Column(elem_id="col-container"):
|
104 |
+
gr.Markdown("# " + MODEL_REPO_NAME + " - on " + device.upper())
|
105 |
+
|
106 |
+
if device == 'cpu':
|
107 |
+
gr.Markdown("Note: running on CPU, generation will be very slow. Expect at least" +
|
108 |
+
" a minute for minimal parameters (512x512 image, guidance <= 1, <=4 steps).\n" +
|
109 |
+
"It's also on a single queue, so clone this space for experimenting with it.")
|
110 |
|
111 |
with gr.Row():
|
112 |
prompt = gr.Text(
|
|
|
142 |
with gr.Row():
|
143 |
width = gr.Slider(
|
144 |
label="Width",
|
145 |
+
minimum=MIN_IMAGE_SIZE,
|
146 |
maximum=MAX_IMAGE_SIZE,
|
147 |
step=32,
|
148 |
+
value=512,
|
149 |
)
|
150 |
|
151 |
height = gr.Slider(
|
152 |
label="Height",
|
153 |
+
minimum=MIN_IMAGE_SIZE,
|
154 |
maximum=MAX_IMAGE_SIZE,
|
155 |
step=32,
|
156 |
+
value=768,
|
157 |
)
|
158 |
|
159 |
with gr.Row():
|
|
|
162 |
minimum=0.0,
|
163 |
maximum=10.0,
|
164 |
step=0.1,
|
165 |
+
value=0.0,
|
166 |
)
|
167 |
|
168 |
num_inference_steps = gr.Slider(
|
|
|
170 |
minimum=1,
|
171 |
maximum=50,
|
172 |
step=1,
|
173 |
+
value=3,
|
174 |
)
|
175 |
|
176 |
gr.Examples(examples=examples, inputs=[prompt])
|
|
|
191 |
)
|
192 |
|
193 |
|
194 |
+
demo_inference = gr.load(MODEL_REPO_ID, title=MODEL_REPO_NAME, src='models')
|
195 |
|
196 |
+
demo = gr.TabbedInterface([demo_inference, demo_device], ["Inference API", device.upper()])
|
197 |
|
198 |
if __name__ == "__main__":
|
199 |
demo.launch()
|