Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import torch
|
|
| 4 |
from PIL import Image
|
| 5 |
import utils
|
| 6 |
|
|
|
|
|
|
|
| 7 |
class Model:
|
| 8 |
def __init__(self, name, path, prefix):
|
| 9 |
self.name = name
|
|
@@ -32,16 +34,19 @@ last_mode = "txt2img"
|
|
| 32 |
current_model = models[1]
|
| 33 |
current_model_path = current_model.path
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if torch.cuda.is_available():
|
| 47 |
pipe = pipe.to("cuda")
|
|
@@ -76,7 +81,7 @@ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, g
|
|
| 76 |
if model_path != current_model_path or last_mode != "txt2img":
|
| 77 |
current_model_path = model_path
|
| 78 |
|
| 79 |
-
if current_model == models[0]:
|
| 80 |
pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
|
| 81 |
else:
|
| 82 |
pipe.to("cpu")
|
|
@@ -97,7 +102,7 @@ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, g
|
|
| 97 |
height = height,
|
| 98 |
generator = generator)
|
| 99 |
|
| 100 |
-
return result
|
| 101 |
|
| 102 |
def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator=None):
|
| 103 |
|
|
@@ -107,7 +112,7 @@ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, w
|
|
| 107 |
if model_path != current_model_path or last_mode != "img2img":
|
| 108 |
current_model_path = model_path
|
| 109 |
|
| 110 |
-
if current_model == models[0]:
|
| 111 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
|
| 112 |
else:
|
| 113 |
pipe.to("cpu")
|
|
@@ -132,7 +137,13 @@ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, w
|
|
| 132 |
height = height,
|
| 133 |
generator = generator)
|
| 134 |
|
| 135 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
css = """
|
| 138 |
<style>
|
|
@@ -178,7 +189,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 178 |
Demo for multiple fine-tuned Stable Diffusion models, trained on different styles: <br>
|
| 179 |
<a href="https://huggingface.co/nitrosocke/Arcane-Diffusion">Arcane</a>, <a href="https://huggingface.co/nitrosocke/archer-diffusion">Archer</a>, <a href="https://huggingface.co/nitrosocke/elden-ring-diffusion">Elden Ring</a>, <a href="https://huggingface.co/nitrosocke/spider-verse-diffusion">Spider-Verse</a>, <a href="https://huggingface.co/nitrosocke/modern-disney-diffusion">Modern Disney</a>, <a href="https://huggingface.co/nitrosocke/classic-anim-diffusion">Classic Disney</a>, <a href="https://huggingface.co/hakurei/waifu-diffusion">Waifu</a>, <a href="https://huggingface.co/lambdalabs/sd-pokemon-diffusers">Pokémon</a>, <a href="https://huggingface.co/AstraliteHeart/pony-diffusion">Pony Diffusion</a>, <a href="https://huggingface.co/nousr/robo-diffusion">Robo Diffusion</a>, <a href="https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion">Cyberpunk Anime</a>, <a href="https://huggingface.co/dallinmackay/Tron-Legacy-diffusion">Tron Legacy</a> + any other custom Diffusers 🧨 SD model hosted on HuggingFace 🤗.
|
| 180 |
</p>
|
| 181 |
-
|
|
|
|
| 182 |
</p>
|
| 183 |
</div>
|
| 184 |
"""
|
|
@@ -232,5 +244,21 @@ with gr.Blocks(css=css) as demo:
|
|
| 232 |
prompt.submit(inference, inputs=inputs, outputs=image_out)
|
| 233 |
generate.click(inference, inputs=inputs, outputs=image_out)
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import utils
|
| 6 |
|
| 7 |
+
is_colab = utils.is_google_colab()
|
| 8 |
+
|
| 9 |
class Model:
|
| 10 |
def __init__(self, name, path, prefix):
|
| 11 |
self.name = name
|
|
|
|
| 34 |
current_model = models[1]
|
| 35 |
current_model_path = current_model.path
|
| 36 |
|
| 37 |
+
if is_colab:
|
| 38 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16)
|
| 39 |
+
|
| 40 |
+
else: # download all models
|
| 41 |
+
vae = AutoencoderKL.from_pretrained(current_model.path, subfolder="vae", torch_dtype=torch.float16)
|
| 42 |
+
for model in models[1:]:
|
| 43 |
+
try:
|
| 44 |
+
unet = UNet2DConditionModel.from_pretrained(model.path, subfolder="unet", torch_dtype=torch.float16)
|
| 45 |
+
model.pipe_t2i = StableDiffusionPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
|
| 46 |
+
model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
|
| 47 |
+
except:
|
| 48 |
+
models.remove(model)
|
| 49 |
+
pipe = models[1].pipe_t2i
|
| 50 |
|
| 51 |
if torch.cuda.is_available():
|
| 52 |
pipe = pipe.to("cuda")
|
|
|
|
| 81 |
if model_path != current_model_path or last_mode != "txt2img":
|
| 82 |
current_model_path = model_path
|
| 83 |
|
| 84 |
+
if is_colab or current_model == models[0]:
|
| 85 |
pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
|
| 86 |
else:
|
| 87 |
pipe.to("cpu")
|
|
|
|
| 102 |
height = height,
|
| 103 |
generator = generator)
|
| 104 |
|
| 105 |
+
return replace_nsfw_images(result)
|
| 106 |
|
| 107 |
def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator=None):
|
| 108 |
|
|
|
|
| 112 |
if model_path != current_model_path or last_mode != "img2img":
|
| 113 |
current_model_path = model_path
|
| 114 |
|
| 115 |
+
if is_colab or current_model == models[0]:
|
| 116 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
|
| 117 |
else:
|
| 118 |
pipe.to("cpu")
|
|
|
|
| 137 |
height = height,
|
| 138 |
generator = generator)
|
| 139 |
|
| 140 |
+
return replace_nsfw_images(result)
|
| 141 |
+
|
| 142 |
+
def replace_nsfw_images(results):
|
| 143 |
+
for i in range(len(results.images)):
|
| 144 |
+
if results.nsfw_content_detected[i]:
|
| 145 |
+
results.images[i] = Image.open("nsfw.png")
|
| 146 |
+
return results.images[0]
|
| 147 |
|
| 148 |
css = """
|
| 149 |
<style>
|
|
|
|
| 189 |
Demo for multiple fine-tuned Stable Diffusion models, trained on different styles: <br>
|
| 190 |
<a href="https://huggingface.co/nitrosocke/Arcane-Diffusion">Arcane</a>, <a href="https://huggingface.co/nitrosocke/archer-diffusion">Archer</a>, <a href="https://huggingface.co/nitrosocke/elden-ring-diffusion">Elden Ring</a>, <a href="https://huggingface.co/nitrosocke/spider-verse-diffusion">Spider-Verse</a>, <a href="https://huggingface.co/nitrosocke/modern-disney-diffusion">Modern Disney</a>, <a href="https://huggingface.co/nitrosocke/classic-anim-diffusion">Classic Disney</a>, <a href="https://huggingface.co/hakurei/waifu-diffusion">Waifu</a>, <a href="https://huggingface.co/lambdalabs/sd-pokemon-diffusers">Pokémon</a>, <a href="https://huggingface.co/AstraliteHeart/pony-diffusion">Pony Diffusion</a>, <a href="https://huggingface.co/nousr/robo-diffusion">Robo Diffusion</a>, <a href="https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion">Cyberpunk Anime</a>, <a href="https://huggingface.co/dallinmackay/Tron-Legacy-diffusion">Tron Legacy</a> + any other custom Diffusers 🧨 SD model hosted on HuggingFace 🤗.
|
| 191 |
</p>
|
| 192 |
+
<p>Don't want to wait in queue? <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>
|
| 193 |
+
Running on <b>{device}</b>{(" in a <b>Google Colab</b>." if is_colab else "")}
|
| 194 |
</p>
|
| 195 |
</div>
|
| 196 |
"""
|
|
|
|
| 244 |
prompt.submit(inference, inputs=inputs, outputs=image_out)
|
| 245 |
generate.click(inference, inputs=inputs, outputs=image_out)
|
| 246 |
|
| 247 |
+
ex = gr.Examples([
|
| 248 |
+
[models[1].name, "jason bateman disassembling the demon core", 7.5, 50],
|
| 249 |
+
[models[4].name, "portrait of dwayne johnson", 7.0, 75],
|
| 250 |
+
[models[5].name, "portrait of a beautiful alyx vance half life", 10, 50],
|
| 251 |
+
[models[6].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0, 45],
|
| 252 |
+
[models[5].name, "fantasy portrait painting, digital art", 4.0, 30],
|
| 253 |
+
], [model_name, prompt, guidance, steps, seed], image_out, inference, cache_examples=False)
|
| 254 |
+
|
| 255 |
+
gr.Markdown('''
|
| 256 |
+
Models by [@nitrosocke](https://huggingface.co/nitrosocke), [@haruu1367](https://twitter.com/haruu1367), [@Helixngc7293](https://twitter.com/DGSpitzer) and others. ❤️<br>
|
| 257 |
+
Space by: [](https://twitter.com/hahahahohohe)
|
| 258 |
+
|
| 259 |
+

|
| 260 |
+
''')
|
| 261 |
+
|
| 262 |
+
if not is_colab:
|
| 263 |
+
demo.queue(concurrency_count=1)
|
| 264 |
+
demo.launch(debug=is_colab, share=is_colab)
|