Spaces:
Runtime error
Runtime error
Commit
·
673ba3d
1
Parent(s):
f53298a
Update app.py
Browse files
app.py
CHANGED
@@ -52,19 +52,6 @@ models = [
|
|
52 |
Model("TrinArt v2", "naclbit/trinart_stable_diffusion_v2"),
|
53 |
Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
|
54 |
]
|
55 |
-
|
56 |
-
scheduler = DPMSolverMultistepScheduler(
|
57 |
-
beta_start=0.00085,
|
58 |
-
beta_end=0.012,
|
59 |
-
beta_schedule="scaled_linear",
|
60 |
-
num_train_timesteps=1000,
|
61 |
-
trained_betas=None,
|
62 |
-
predict_epsilon=True,
|
63 |
-
thresholding=False,
|
64 |
-
algorithm_type="dpmsolver++",
|
65 |
-
solver_type="midpoint",
|
66 |
-
lower_order_final=True,
|
67 |
-
)
|
68 |
|
69 |
custom_model = None
|
70 |
if is_colab:
|
@@ -76,23 +63,20 @@ current_model = models[1] if is_colab else models[0]
|
|
76 |
current_model_path = current_model.path
|
77 |
|
78 |
if is_colab:
|
79 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
# models.remove(model)
|
94 |
-
# pipe = models[0].pipe_t2i
|
95 |
-
|
96 |
if torch.cuda.is_available():
|
97 |
pipe = pipe.to("cuda")
|
98 |
|
@@ -113,7 +97,7 @@ def on_model_change(model_name):
|
|
113 |
|
114 |
return gr.update(visible = model_name == models[0].name), gr.update(placeholder=prefix)
|
115 |
|
116 |
-
def inference(model_name, prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
|
117 |
|
118 |
print(psutil.virtual_memory()) # print memory usage
|
119 |
|
@@ -127,13 +111,13 @@ def inference(model_name, prompt, guidance, steps, width=512, height=512, seed=0
|
|
127 |
|
128 |
try:
|
129 |
if img is not None:
|
130 |
-
return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
|
131 |
else:
|
132 |
-
return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator), None
|
133 |
except Exception as e:
|
134 |
return None, error_str(e)
|
135 |
|
136 |
-
def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator):
|
137 |
|
138 |
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
139 |
|
@@ -144,9 +128,18 @@ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, g
|
|
144 |
current_model_path = model_path
|
145 |
|
146 |
if is_colab or current_model == custom_model:
|
147 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
148 |
else:
|
149 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
|
|
|
|
|
|
|
|
150 |
# pipe = pipe.to("cpu")
|
151 |
# pipe = current_model.pipe_t2i
|
152 |
|
@@ -158,7 +151,7 @@ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, g
|
|
158 |
result = pipe(
|
159 |
prompt,
|
160 |
negative_prompt = neg_prompt,
|
161 |
-
|
162 |
num_inference_steps = int(steps),
|
163 |
guidance_scale = guidance,
|
164 |
width = width,
|
@@ -167,7 +160,7 @@ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, g
|
|
167 |
|
168 |
return replace_nsfw_images(result)
|
169 |
|
170 |
-
def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
171 |
|
172 |
print(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
|
173 |
|
@@ -178,9 +171,18 @@ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, w
|
|
178 |
current_model_path = model_path
|
179 |
|
180 |
if is_colab or current_model == custom_model:
|
181 |
-
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
182 |
else:
|
183 |
-
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
|
|
|
|
|
|
|
|
184 |
# pipe = pipe.to("cpu")
|
185 |
# pipe = current_model.pipe_i2i
|
186 |
|
@@ -194,13 +196,13 @@ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, w
|
|
194 |
result = pipe(
|
195 |
prompt,
|
196 |
negative_prompt = neg_prompt,
|
197 |
-
|
198 |
init_image = img,
|
199 |
num_inference_steps = int(steps),
|
200 |
strength = strength,
|
201 |
guidance_scale = guidance,
|
202 |
-
width = width,
|
203 |
-
height = height,
|
204 |
generator = generator)
|
205 |
|
206 |
return replace_nsfw_images(result)
|
@@ -208,12 +210,12 @@ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, w
|
|
208 |
def replace_nsfw_images(results):
|
209 |
|
210 |
if is_colab:
|
211 |
-
return results.images
|
212 |
|
213 |
for i in range(len(results.images)):
|
214 |
if results.nsfw_content_detected[i]:
|
215 |
results.images[i] = Image.open("nsfw.png")
|
216 |
-
return results.images
|
217 |
|
218 |
css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
|
219 |
"""
|
@@ -231,7 +233,8 @@ with gr.Blocks(css=css) as demo:
|
|
231 |
<p>You can skip the queue and load custom models in the colab: <a href="https://colab.research.google.com/gist/qunash/42112fb104509c24fd3aa6d1c11dd6e0/copy-of-fine-tuned-diffusion-gradio.ipynb"><img data-canonical-src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667"></a></p>
|
232 |
Running on <b>{device}</b>{(" in a <b>Google Colab</b>." if is_colab else "")}
|
233 |
</p>
|
234 |
-
<p>You can also duplicate this space and upgrade to gpu by going to settings
|
|
|
235 |
</div>
|
236 |
"""
|
237 |
)
|
@@ -249,10 +252,9 @@ with gr.Blocks(css=css) as demo:
|
|
249 |
generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
|
250 |
|
251 |
|
252 |
-
image_out = gr.Image(height=512)
|
253 |
-
|
254 |
-
|
255 |
-
# ).style(grid=[1], height="auto")
|
256 |
error_output = gr.Markdown()
|
257 |
|
258 |
with gr.Column(scale=45):
|
@@ -260,7 +262,7 @@ with gr.Blocks(css=css) as demo:
|
|
260 |
with gr.Group():
|
261 |
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
262 |
|
263 |
-
|
264 |
|
265 |
with gr.Row():
|
266 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
@@ -282,27 +284,29 @@ with gr.Blocks(css=css) as demo:
|
|
282 |
custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
|
283 |
# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
|
284 |
|
285 |
-
inputs = [model_name, prompt, guidance, steps, width, height, seed, image, strength, neg_prompt]
|
286 |
-
outputs = [
|
287 |
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
288 |
generate.click(inference, inputs=inputs, outputs=outputs)
|
289 |
|
290 |
ex = gr.Examples([
|
291 |
-
[models[7].name, "tiny cute and adorable kitten adventurer dressed in a warm overcoat with survival gear on a winters day", 7.5,
|
292 |
-
[models[4].name, "portrait of dwayne johnson", 7.0,
|
293 |
-
[models[5].name, "portrait of a beautiful alyx vance half life", 10,
|
294 |
-
[models[6].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0,
|
295 |
-
[models[5].name, "fantasy portrait painting, digital art", 4.0,
|
296 |
-
], inputs=[model_name, prompt, guidance, steps
|
297 |
|
298 |
gr.HTML("""
|
299 |
<div style="border-top: 1px solid #303030;">
|
300 |
<br>
|
301 |
<p>Models by <a href="https://huggingface.co/nitrosocke">@nitrosocke</a>, <a href="https://twitter.com/haruu1367">@haruu1367</a>, <a href="https://twitter.com/DGSpitzer">@Helixngc7293</a>, <a href="https://twitter.com/dal_mack">@dal_mack</a>, <a href="https://twitter.com/prompthero">@prompthero</a> and others. ❤️</p>
|
302 |
-
<p>This space uses the <a href="https://github.com/LuChengTHU/dpm-solver">DPM-Solver++</a> sampler by <a href="https://arxiv.org/abs/2206.00927">Cheng Lu, et al.</a>.</p
|
303 |
-
<p>Space by
|
304 |
-
<a href="https://
|
305 |
-
<
|
|
|
|
|
306 |
</div>
|
307 |
""")
|
308 |
|
|
|
52 |
Model("TrinArt v2", "naclbit/trinart_stable_diffusion_v2"),
|
53 |
Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
|
54 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
custom_model = None
|
57 |
if is_colab:
|
|
|
63 |
current_model_path = current_model.path
|
64 |
|
65 |
if is_colab:
|
66 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
67 |
+
current_model.path,
|
68 |
+
torch_dtype=torch.float16,
|
69 |
+
scheduler=DPMSolverMultistepScheduler.from_pretrained(current_model.path, subfolder="scheduler"),
|
70 |
+
safety_checker=lambda images, clip_input: (images, False)
|
71 |
+
)
|
72 |
+
|
73 |
+
else:
|
74 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
75 |
+
current_model.path,
|
76 |
+
torch_dtype=torch.float16,
|
77 |
+
scheduler=DPMSolverMultistepScheduler.from_pretrained(current_model.path, subfolder="scheduler")
|
78 |
+
)
|
79 |
+
|
|
|
|
|
|
|
80 |
if torch.cuda.is_available():
|
81 |
pipe = pipe.to("cuda")
|
82 |
|
|
|
97 |
|
98 |
return gr.update(visible = model_name == models[0].name), gr.update(placeholder=prefix)
|
99 |
|
100 |
+
def inference(model_name, prompt, guidance, steps, n_images=1, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
|
101 |
|
102 |
print(psutil.virtual_memory()) # print memory usage
|
103 |
|
|
|
111 |
|
112 |
try:
|
113 |
if img is not None:
|
114 |
+
return img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator), None
|
115 |
else:
|
116 |
+
return txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width, height, generator), None
|
117 |
except Exception as e:
|
118 |
return None, error_str(e)
|
119 |
|
120 |
+
def txt_to_img(model_path, prompt, n_images, neg_prompt, guidance, steps, width, height, generator):
|
121 |
|
122 |
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
123 |
|
|
|
128 |
current_model_path = model_path
|
129 |
|
130 |
if is_colab or current_model == custom_model:
|
131 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
132 |
+
current_model_path,
|
133 |
+
torch_dtype=torch.float16,
|
134 |
+
scheduler=DPMSolverMultistepScheduler.from_pretrained(current_model.path, subfolder="scheduler"),
|
135 |
+
safety_checker=lambda images, clip_input: (images, False)
|
136 |
+
)
|
137 |
else:
|
138 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
139 |
+
current_model_path,
|
140 |
+
torch_dtype=torch.float16,
|
141 |
+
scheduler=DPMSolverMultistepScheduler.from_pretrained(current_model.path, subfolder="scheduler")
|
142 |
+
)
|
143 |
# pipe = pipe.to("cpu")
|
144 |
# pipe = current_model.pipe_t2i
|
145 |
|
|
|
151 |
result = pipe(
|
152 |
prompt,
|
153 |
negative_prompt = neg_prompt,
|
154 |
+
num_images_per_prompt=n_images,
|
155 |
num_inference_steps = int(steps),
|
156 |
guidance_scale = guidance,
|
157 |
width = width,
|
|
|
160 |
|
161 |
return replace_nsfw_images(result)
|
162 |
|
163 |
+
def img_to_img(model_path, prompt, n_images, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
164 |
|
165 |
print(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
|
166 |
|
|
|
171 |
current_model_path = model_path
|
172 |
|
173 |
if is_colab or current_model == custom_model:
|
174 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
175 |
+
current_model_path,
|
176 |
+
torch_dtype=torch.float16,
|
177 |
+
scheduler=DPMSolverMultistepScheduler.from_pretrained(current_model.path, subfolder="scheduler"),
|
178 |
+
safety_checker=lambda images, clip_input: (images, False)
|
179 |
+
)
|
180 |
else:
|
181 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
182 |
+
current_model_path,
|
183 |
+
torch_dtype=torch.float16,
|
184 |
+
scheduler=DPMSolverMultistepScheduler.from_pretrained(current_model.path, subfolder="scheduler")
|
185 |
+
)
|
186 |
# pipe = pipe.to("cpu")
|
187 |
# pipe = current_model.pipe_i2i
|
188 |
|
|
|
196 |
result = pipe(
|
197 |
prompt,
|
198 |
negative_prompt = neg_prompt,
|
199 |
+
num_images_per_prompt=n_images,
|
200 |
init_image = img,
|
201 |
num_inference_steps = int(steps),
|
202 |
strength = strength,
|
203 |
guidance_scale = guidance,
|
204 |
+
# width = width,
|
205 |
+
# height = height,
|
206 |
generator = generator)
|
207 |
|
208 |
return replace_nsfw_images(result)
|
|
|
210 |
def replace_nsfw_images(results):
|
211 |
|
212 |
if is_colab:
|
213 |
+
return results.images
|
214 |
|
215 |
for i in range(len(results.images)):
|
216 |
if results.nsfw_content_detected[i]:
|
217 |
results.images[i] = Image.open("nsfw.png")
|
218 |
+
return results.images
|
219 |
|
220 |
css = """.finetuned-diffusion-div div{display:inline-flex;align-items:center;gap:.8rem;font-size:1.75rem}.finetuned-diffusion-div div h1{font-weight:900;margin-bottom:7px}.finetuned-diffusion-div p{margin-bottom:10px;font-size:94%}a{text-decoration:underline}.tabs{margin-top:0;margin-bottom:0}#gallery{min-height:20rem}
|
221 |
"""
|
|
|
233 |
<p>You can skip the queue and load custom models in the colab: <a href="https://colab.research.google.com/gist/qunash/42112fb104509c24fd3aa6d1c11dd6e0/copy-of-fine-tuned-diffusion-gradio.ipynb"><img data-canonical-src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667"></a></p>
|
234 |
Running on <b>{device}</b>{(" in a <b>Google Colab</b>." if is_colab else "")}
|
235 |
</p>
|
236 |
+
<p>You can also duplicate this space and upgrade to gpu by going to settings:<br>
|
237 |
+
<a style="display:inline-block" href="https://huggingface.co/spaces/anzorq/finetuned_diffusion?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></p>
|
238 |
</div>
|
239 |
"""
|
240 |
)
|
|
|
252 |
generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
|
253 |
|
254 |
|
255 |
+
# image_out = gr.Image(height=512)
|
256 |
+
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
257 |
+
|
|
|
258 |
error_output = gr.Markdown()
|
259 |
|
260 |
with gr.Column(scale=45):
|
|
|
262 |
with gr.Group():
|
263 |
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
264 |
|
265 |
+
n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
|
266 |
|
267 |
with gr.Row():
|
268 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
|
|
284 |
custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
|
285 |
# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
|
286 |
|
287 |
+
inputs = [model_name, prompt, guidance, steps, n_images, width, height, seed, image, strength, neg_prompt]
|
288 |
+
outputs = [gallery, error_output]
|
289 |
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
290 |
generate.click(inference, inputs=inputs, outputs=outputs)
|
291 |
|
292 |
ex = gr.Examples([
|
293 |
+
[models[7].name, "tiny cute and adorable kitten adventurer dressed in a warm overcoat with survival gear on a winters day", 7.5, 25],
|
294 |
+
[models[4].name, "portrait of dwayne johnson", 7.0, 35],
|
295 |
+
[models[5].name, "portrait of a beautiful alyx vance half life", 10, 25],
|
296 |
+
[models[6].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0, 30],
|
297 |
+
[models[5].name, "fantasy portrait painting, digital art", 4.0, 20],
|
298 |
+
], inputs=[model_name, prompt, guidance, steps], outputs=outputs, fn=inference, cache_examples=False)
|
299 |
|
300 |
gr.HTML("""
|
301 |
<div style="border-top: 1px solid #303030;">
|
302 |
<br>
|
303 |
<p>Models by <a href="https://huggingface.co/nitrosocke">@nitrosocke</a>, <a href="https://twitter.com/haruu1367">@haruu1367</a>, <a href="https://twitter.com/DGSpitzer">@Helixngc7293</a>, <a href="https://twitter.com/dal_mack">@dal_mack</a>, <a href="https://twitter.com/prompthero">@prompthero</a> and others. ❤️</p>
|
304 |
+
<p>This space uses the <a href="https://github.com/LuChengTHU/dpm-solver">DPM-Solver++</a> sampler by <a href="https://arxiv.org/abs/2206.00927">Cheng Lu, et al.</a>.</p>
|
305 |
+
<p>Space by:<br>
|
306 |
+
<a href="https://twitter.com/hahahahohohe"><img src="https://img.shields.io/twitter/follow/hahahahohohe?label=%40anzorq&style=social" alt="Twitter Follow"></a><br>
|
307 |
+
<a href="https://github.com/qunash"><img alt="GitHub followers" src="https://img.shields.io/github/followers/qunash?style=social" alt="Github Follow"></a></p><br><br>
|
308 |
+
<a href="https://www.buymeacoffee.com/anzorq" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 45px !important;width: 162px !important;" ></a><br><br>
|
309 |
+
<p><img src="https://visitor-badge.glitch.me/badge?page_id=anzorq.finetuned_diffusion" alt="visitors"></p>
|
310 |
</div>
|
311 |
""")
|
312 |
|