Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,9 +6,13 @@ import utils
|
|
6 |
import datetime
|
7 |
import time
|
8 |
import psutil
|
|
|
|
|
9 |
|
10 |
start_time = time.time()
|
11 |
is_colab = utils.is_google_colab()
|
|
|
|
|
12 |
|
13 |
class Model:
|
14 |
def __init__(self, name, path="", prefix=""):
|
@@ -76,6 +80,14 @@ def error_str(error, title="Error"):
|
|
76 |
return f"""#### {title}
|
77 |
{error}""" if error else ""
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def custom_model_changed(path):
|
80 |
models[0].path = path
|
81 |
global current_model
|
@@ -87,8 +99,17 @@ def on_model_change(model_name):
|
|
87 |
|
88 |
return gr.update(visible = model_name == models[0].name), gr.update(placeholder=prefix)
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def inference(model_name, prompt, guidance, steps, n_images=1, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
|
91 |
|
|
|
|
|
92 |
print(psutil.virtual_memory()) # print memory usage
|
93 |
|
94 |
global current_model
|
@@ -97,17 +118,21 @@ def inference(model_name, prompt, guidance, steps, n_images=1, width=512, height
|
|
97 |
current_model = model
|
98 |
model_path = current_model.path
|
99 |
|
100 |
-
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
|
|
|
|
|
|
|
|
101 |
|
102 |
try:
|
103 |
if img is not None:
|
104 |
-
return img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator), None
|
105 |
else:
|
106 |
-
return txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width, height, generator), None
|
107 |
except Exception as e:
|
108 |
return None, error_str(e)
|
109 |
|
110 |
-
def txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width, height, generator):
|
111 |
|
112 |
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
113 |
|
@@ -117,6 +142,8 @@ def txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width,
|
|
117 |
if model_path != current_model_path or last_mode != "txt2img":
|
118 |
current_model_path = model_path
|
119 |
|
|
|
|
|
120 |
if is_colab or current_model == custom_model:
|
121 |
pipe = StableDiffusionPipeline.from_pretrained(
|
122 |
current_model_path,
|
@@ -147,11 +174,14 @@ def txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width,
|
|
147 |
guidance_scale = guidance,
|
148 |
width = width,
|
149 |
height = height,
|
150 |
-
generator = generator
|
|
|
|
|
|
|
151 |
|
152 |
return replace_nsfw_images(result)
|
153 |
|
154 |
-
def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
155 |
|
156 |
print(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
|
157 |
|
@@ -161,6 +191,8 @@ def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance
|
|
161 |
if model_path != current_model_path or last_mode != "img2img":
|
162 |
current_model_path = model_path
|
163 |
|
|
|
|
|
164 |
if is_colab or current_model == custom_model:
|
165 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
166 |
current_model_path,
|
@@ -195,7 +227,10 @@ def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance
|
|
195 |
guidance_scale = guidance,
|
196 |
# width = width,
|
197 |
# height = height,
|
198 |
-
generator = generator
|
|
|
|
|
|
|
199 |
|
200 |
return replace_nsfw_images(result)
|
201 |
|
@@ -246,7 +281,8 @@ with gr.Blocks(css="style.css") as demo:
|
|
246 |
|
247 |
# image_out = gr.Image(height=512)
|
248 |
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
249 |
-
|
|
|
250 |
error_output = gr.Markdown()
|
251 |
|
252 |
with gr.Column(scale=45):
|
@@ -258,7 +294,7 @@ with gr.Blocks(css="style.css") as demo:
|
|
258 |
|
259 |
with gr.Row():
|
260 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
261 |
-
steps = gr.Slider(label="Steps", value=
|
262 |
|
263 |
with gr.Row():
|
264 |
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
|
@@ -272,9 +308,10 @@ with gr.Blocks(css="style.css") as demo:
|
|
272 |
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
273 |
|
274 |
if is_colab:
|
275 |
-
|
276 |
-
|
277 |
# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
|
|
|
278 |
|
279 |
inputs = [model_name, prompt, guidance, steps, n_images, width, height, seed, image, strength, neg_prompt]
|
280 |
outputs = [gallery, error_output]
|
@@ -302,8 +339,10 @@ with gr.Blocks(css="style.css") as demo:
|
|
302 |
</div>
|
303 |
""")
|
304 |
|
|
|
|
|
305 |
print(f"Space built in {time.time() - start_time:.2f} seconds")
|
306 |
|
307 |
-
if not is_colab:
|
308 |
-
|
309 |
demo.launch(debug=is_colab, share=is_colab)
|
|
|
6 |
import datetime
|
7 |
import time
|
8 |
import psutil
|
9 |
+
import random
|
10 |
+
|
11 |
|
12 |
start_time = time.time()
|
13 |
is_colab = utils.is_google_colab()
|
14 |
+
state = None
|
15 |
+
current_steps = 25
|
16 |
|
17 |
class Model:
|
18 |
def __init__(self, name, path="", prefix=""):
|
|
|
80 |
return f"""#### {title}
|
81 |
{error}""" if error else ""
|
82 |
|
83 |
+
def update_state(new_state):
|
84 |
+
global state
|
85 |
+
state = new_state
|
86 |
+
|
87 |
+
def update_state_info(old_state):
|
88 |
+
if state and state != old_state:
|
89 |
+
return gr.update(value=state)
|
90 |
+
|
91 |
def custom_model_changed(path):
|
92 |
models[0].path = path
|
93 |
global current_model
|
|
|
99 |
|
100 |
return gr.update(visible = model_name == models[0].name), gr.update(placeholder=prefix)
|
101 |
|
102 |
+
def on_steps_change(steps):
|
103 |
+
global current_steps
|
104 |
+
current_steps = steps
|
105 |
+
|
106 |
+
def pipe_callback(step: int, timestep: int, latents: torch.FloatTensor):
|
107 |
+
update_state(f"{step}/{current_steps} steps")#\nTime left, sec: {timestep/100:.0f}")
|
108 |
+
|
109 |
def inference(model_name, prompt, guidance, steps, n_images=1, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
|
110 |
|
111 |
+
update_state(" ")
|
112 |
+
|
113 |
print(psutil.virtual_memory()) # print memory usage
|
114 |
|
115 |
global current_model
|
|
|
118 |
current_model = model
|
119 |
model_path = current_model.path
|
120 |
|
121 |
+
# generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
122 |
+
if seed == 0:
|
123 |
+
seed = random.randint(0, 2147483647)
|
124 |
+
|
125 |
+
generator = torch.Generator('cuda').manual_seed(seed)
|
126 |
|
127 |
try:
|
128 |
if img is not None:
|
129 |
+
return img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator, seed), None
|
130 |
else:
|
131 |
+
return txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width, height, generator, seed), None
|
132 |
except Exception as e:
|
133 |
return None, error_str(e)
|
134 |
|
135 |
+
def txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width, height, generator, seed):
|
136 |
|
137 |
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
138 |
|
|
|
142 |
if model_path != current_model_path or last_mode != "txt2img":
|
143 |
current_model_path = model_path
|
144 |
|
145 |
+
update_state("Loading text-to-image model...")
|
146 |
+
|
147 |
if is_colab or current_model == custom_model:
|
148 |
pipe = StableDiffusionPipeline.from_pretrained(
|
149 |
current_model_path,
|
|
|
174 |
guidance_scale = guidance,
|
175 |
width = width,
|
176 |
height = height,
|
177 |
+
generator = generator,
|
178 |
+
callback=pipe_callback)
|
179 |
+
|
180 |
+
update_state(f"Done. Seed: {seed}")
|
181 |
|
182 |
return replace_nsfw_images(result)
|
183 |
|
184 |
+
def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator, seed):
|
185 |
|
186 |
print(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
|
187 |
|
|
|
191 |
if model_path != current_model_path or last_mode != "img2img":
|
192 |
current_model_path = model_path
|
193 |
|
194 |
+
update_state("Loading image-to-image model...")
|
195 |
+
|
196 |
if is_colab or current_model == custom_model:
|
197 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
198 |
current_model_path,
|
|
|
227 |
guidance_scale = guidance,
|
228 |
# width = width,
|
229 |
# height = height,
|
230 |
+
generator = generator,
|
231 |
+
callback=pipe_callback)
|
232 |
+
|
233 |
+
update_state(f"Done. Seed: {seed}")
|
234 |
|
235 |
return replace_nsfw_images(result)
|
236 |
|
|
|
281 |
|
282 |
# image_out = gr.Image(height=512)
|
283 |
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
284 |
+
|
285 |
+
state_info = gr.Textbox(label="State", show_label=False, max_lines=2).style(container=False)
|
286 |
error_output = gr.Markdown()
|
287 |
|
288 |
with gr.Column(scale=45):
|
|
|
294 |
|
295 |
with gr.Row():
|
296 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
297 |
+
steps = gr.Slider(label="Steps", value=current_steps, minimum=2, maximum=75, step=1)
|
298 |
|
299 |
with gr.Row():
|
300 |
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
|
|
|
308 |
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
309 |
|
310 |
if is_colab:
|
311 |
+
model_name.change(on_model_change, inputs=model_name, outputs=[custom_model_group, prompt], queue=False)
|
312 |
+
custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
|
313 |
# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
|
314 |
+
steps.change(on_steps_change, inputs=[steps], outputs=[], queue=False)
|
315 |
|
316 |
inputs = [model_name, prompt, guidance, steps, n_images, width, height, seed, image, strength, neg_prompt]
|
317 |
outputs = [gallery, error_output]
|
|
|
339 |
</div>
|
340 |
""")
|
341 |
|
342 |
+
demo.load(update_state_info, inputs=state_info, outputs=state_info, every=0.5, show_progress=False)
|
343 |
+
|
344 |
print(f"Space built in {time.time() - start_time:.2f} seconds")
|
345 |
|
346 |
+
# if not is_colab:
|
347 |
+
demo.queue(concurrency_count=1)
|
348 |
demo.launch(debug=is_colab, share=is_colab)
|