Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,68 @@
|
|
| 1 |
from diffusers import StableDiffusionPipeline
|
| 2 |
from diffusers import StableDiffusionImg2ImgPipeline
|
| 3 |
-
from diffusers import AutoencoderKL, UNet2DConditionModel
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
models = [
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
]
|
| 21 |
|
| 22 |
-
prompt_prefixes = {
|
| 23 |
-
models[0]: "arcane style ",
|
| 24 |
-
models[1]: "archer style ",
|
| 25 |
-
models[2]: "elden ring style ",
|
| 26 |
-
models[3]: "spiderverse style ",
|
| 27 |
-
models[4]: "modern disney style ",
|
| 28 |
-
models[5]: "",
|
| 29 |
-
models[6]: "",
|
| 30 |
-
models[7]: "",
|
| 31 |
-
models[8]: "",
|
| 32 |
-
models[9]: "",
|
| 33 |
-
models[10]: "dgs illustration style ",
|
| 34 |
-
models[11]: "herge_style ",
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
current_model = models[0]
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
unet = UNet2DConditionModel.from_pretrained(model, subfolder="unet", torch_dtype=torch.float16)
|
| 42 |
-
pipe = StableDiffusionPipeline.from_pretrained(model, unet=unet, vae=vae, torch_dtype=torch.float16)
|
| 43 |
-
pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model, unet=unet, vae=vae, torch_dtype=torch.float16)
|
| 44 |
-
pipes.append({"name":model, "pipeline":pipe, "pipeline_i2i":pipe_i2i})
|
| 45 |
|
| 46 |
|
| 47 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 48 |
|
| 49 |
-
def inference(
|
| 50 |
|
| 51 |
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
| 52 |
|
| 53 |
if img is not None:
|
| 54 |
-
return img_to_img(
|
| 55 |
else:
|
| 56 |
-
return txt_to_img(
|
| 57 |
|
| 58 |
-
def txt_to_img(
|
| 59 |
|
| 60 |
global current_model
|
| 61 |
global pipe
|
| 62 |
-
if
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
prompt = prompt_prefixes[current_model] + prompt
|
| 74 |
results = pipe(
|
| 75 |
prompt,
|
| 76 |
negative_prompt=neg_prompt,
|
|
@@ -87,19 +79,18 @@ def img_to_img(model, prompt, neg_prompt, img, strength, guidance, steps, width,
|
|
| 87 |
|
| 88 |
global current_model
|
| 89 |
global pipe
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
pipe = pipe.to("cuda")
|
| 100 |
-
|
| 101 |
-
prompt = prompt_prefixes[current_model] + prompt
|
| 102 |
-
ratio = min(height / img.height, width / img.width)
|
| 103 |
img = img.resize((int(img.width * ratio), int(img.height * ratio)))
|
| 104 |
results = pipe(
|
| 105 |
prompt,
|
|
@@ -115,7 +106,6 @@ def img_to_img(model, prompt, neg_prompt, img, strength, guidance, steps, width,
|
|
| 115 |
image = results.images[0] if not results.nsfw_content_detected[0] else Image.open("nsfw.png")
|
| 116 |
return image
|
| 117 |
|
| 118 |
-
|
| 119 |
css = """
|
| 120 |
<style>
|
| 121 |
.finetuned-diffusion-div {
|
|
@@ -144,7 +134,7 @@ css = """
|
|
| 144 |
"""
|
| 145 |
with gr.Blocks(css=css) as demo:
|
| 146 |
gr.HTML(
|
| 147 |
-
"""
|
| 148 |
<div class="finetuned-diffusion-div">
|
| 149 |
<div>
|
| 150 |
<h1>Finetuned Diffusion</h1>
|
|
@@ -152,26 +142,29 @@ with gr.Blocks(css=css) as demo:
|
|
| 152 |
<p>
|
| 153 |
Demo for multiple fine-tuned Stable Diffusion models, trained on different styles: <br>
|
| 154 |
<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">Spiderverse</a>, <a href="https://huggingface.co/nitrosocke/modern-disney-diffusion">Modern Disney</a>, <a href="https://huggingface.co/hakurei/waifu-diffusion">Waifu</a>, <a href="https://huggingface.co/lambdalabs/sd-pokemon-diffusers">Pokemon</a>, <a href="https://huggingface.co/yuk/fuyuko-waifu-diffusion">Fuyuko Waifu</a>, <a href="https://huggingface.co/AstraliteHeart/pony-diffusion">Pony</a>, <a href="https://huggingface.co/sd-dreambooth-library/herge-style">Hergé (Tintin)</a>, <a href="https://huggingface.co/nousr/robo-diffusion">Robo</a>, <a href="https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion">Cyberpunk Anime</a>
|
|
|
|
|
|
|
|
|
|
| 155 |
</p>
|
| 156 |
</div>
|
| 157 |
"""
|
| 158 |
)
|
|
|
|
| 159 |
with gr.Row():
|
| 160 |
|
| 161 |
with gr.Column():
|
| 162 |
-
|
| 163 |
prompt = gr.Textbox(label="Prompt", placeholder="Style prefix is applied automatically")
|
| 164 |
run = gr.Button(value="Run")
|
| 165 |
-
gr.Markdown(f"Running on: {device}")
|
| 166 |
-
|
| 167 |
-
with gr.Tab("Options"):
|
| 168 |
|
|
|
|
| 169 |
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
| 170 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
| 171 |
-
steps = gr.Slider(label="Steps", value=50, maximum=100, minimum=2)
|
| 172 |
-
width = gr.Slider(label="Width", value=512, maximum=
|
| 173 |
-
height = gr.Slider(label="Height", value=512, maximum=
|
| 174 |
seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
|
|
|
|
| 175 |
with gr.Tab("Image to image"):
|
| 176 |
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
| 177 |
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
|
@@ -179,16 +172,18 @@ with gr.Blocks(css=css) as demo:
|
|
| 179 |
with gr.Column():
|
| 180 |
image_out = gr.Image(height=512)
|
| 181 |
|
| 182 |
-
inputs = [
|
| 183 |
prompt.submit(inference, inputs=inputs, outputs=image_out)
|
| 184 |
run.click(inference, inputs=inputs, outputs=image_out)
|
|
|
|
| 185 |
gr.Examples([
|
| 186 |
-
[models[0], "jason bateman disassembling the demon core", 7.5, 50],
|
| 187 |
-
[models[3], "portrait of dwayne johnson", 7.0, 75],
|
| 188 |
-
[models[4], "portrait of a beautiful alyx vance half life", 10, 50],
|
| 189 |
-
[models[5], "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7, 45],
|
| 190 |
-
[models[4], "fantasy portrait painting, digital art", 4, 30],
|
| 191 |
-
], [
|
|
|
|
| 192 |
gr.Markdown('''
|
| 193 |
Models by [@nitrosocke](https://huggingface.co/nitrosocke), [@Helixngc7293](https://twitter.com/DGSpitzer) and others. ❤️<br>
|
| 194 |
Space by: [](https://twitter.com/hahahahohohe)
|
|
@@ -196,5 +191,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 196 |

|
| 197 |
''')
|
| 198 |
|
| 199 |
-
|
| 200 |
-
demo.
|
|
|
|
|
|
| 1 |
from diffusers import StableDiffusionPipeline
|
| 2 |
from diffusers import StableDiffusionImg2ImgPipeline
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import utils
|
| 7 |
+
|
| 8 |
+
is_colab = utils.is_google_colab()
|
| 9 |
+
|
| 10 |
+
max_width = 832
|
| 11 |
+
max_height = 832
|
| 12 |
+
|
| 13 |
+
class Model:
|
| 14 |
+
def __init__(self, name, path, prefix):
|
| 15 |
+
self.name = name
|
| 16 |
+
self.path = path
|
| 17 |
+
self.prefix = prefix
|
| 18 |
|
| 19 |
models = [
|
| 20 |
+
Model("Arcane", "nitrosocke/Arcane-Diffusion", "arcane style "),
|
| 21 |
+
Model("Archer", "nitrosocke/archer-diffusion", "archer style "),
|
| 22 |
+
Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
|
| 23 |
+
Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
|
| 24 |
+
Model("Modern Disney", "nitrosocke/modern-disney-diffusion", "modern disney style "),
|
| 25 |
+
Model("Classic Disney", "nitrosocke/classic-anim-diffusion", ""),
|
| 26 |
+
Model("Waifu", "hakurei/waifu-diffusion", ""),
|
| 27 |
+
Model("Pokémon", "lambdalabs/sd-pokemon-diffusers", ""),
|
| 28 |
+
Model("Fuyuko Waifu", "yuk/fuyuko-waifu-diffusion", ""),
|
| 29 |
+
Model("Pony Diffusion", "AstraliteHeart/pony-diffusion", ""),
|
| 30 |
+
Model("Robo Diffusion", "nousr/robo-diffusion", ""),
|
| 31 |
+
Model("Cyberpunk Anime", "DGSpitzer/Cyberpunk-Anime-Diffusion", "dgs illustration style "),
|
| 32 |
+
Model("Hergé Style", "sd-dreambooth-library/herge-style", "herge_style "),
|
| 33 |
]
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
current_model = models[0]
|
| 36 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16)
|
| 37 |
+
if torch.cuda.is_available():
|
| 38 |
+
pipe = pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 42 |
|
| 43 |
+
def inference(model_name, prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
|
| 44 |
|
| 45 |
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
| 46 |
|
| 47 |
if img is not None:
|
| 48 |
+
return img_to_img(model_name, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator)
|
| 49 |
else:
|
| 50 |
+
return txt_to_img(model_name, prompt, neg_prompt, guidance, steps, width, height, generator)
|
| 51 |
|
| 52 |
+
def txt_to_img(model_name, prompt, neg_prompt, guidance, steps, width, height, generator=None):
|
| 53 |
|
| 54 |
global current_model
|
| 55 |
global pipe
|
| 56 |
+
if model_name != current_model.name:
|
| 57 |
+
for model in models:
|
| 58 |
+
if model.name == model_name:
|
| 59 |
+
current_model = model
|
| 60 |
+
|
| 61 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16)
|
| 62 |
+
if torch.cuda.is_available():
|
| 63 |
+
pipe = pipe.to("cuda")
|
| 64 |
+
|
| 65 |
+
prompt = current_model.prefix + prompt
|
|
|
|
|
|
|
| 66 |
results = pipe(
|
| 67 |
prompt,
|
| 68 |
negative_prompt=neg_prompt,
|
|
|
|
| 79 |
|
| 80 |
global current_model
|
| 81 |
global pipe
|
| 82 |
+
if model_name != current_model.name:
|
| 83 |
+
for model in models:
|
| 84 |
+
if model.name == model_name:
|
| 85 |
+
current_model = model
|
| 86 |
+
|
| 87 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16)
|
| 88 |
|
| 89 |
+
if torch.cuda.is_available():
|
| 90 |
+
pipe = pipe.to("cuda")
|
| 91 |
+
|
| 92 |
+
prompt = current_model.prefix + prompt
|
| 93 |
+
ratio = min(max_height / img.height, max_width / img.width)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
img = img.resize((int(img.width * ratio), int(img.height * ratio)))
|
| 95 |
results = pipe(
|
| 96 |
prompt,
|
|
|
|
| 106 |
image = results.images[0] if not results.nsfw_content_detected[0] else Image.open("nsfw.png")
|
| 107 |
return image
|
| 108 |
|
|
|
|
| 109 |
css = """
|
| 110 |
<style>
|
| 111 |
.finetuned-diffusion-div {
|
|
|
|
| 134 |
"""
|
| 135 |
with gr.Blocks(css=css) as demo:
|
| 136 |
gr.HTML(
|
| 137 |
+
f"""
|
| 138 |
<div class="finetuned-diffusion-div">
|
| 139 |
<div>
|
| 140 |
<h1>Finetuned Diffusion</h1>
|
|
|
|
| 142 |
<p>
|
| 143 |
Demo for multiple fine-tuned Stable Diffusion models, trained on different styles: <br>
|
| 144 |
<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">Spiderverse</a>, <a href="https://huggingface.co/nitrosocke/modern-disney-diffusion">Modern Disney</a>, <a href="https://huggingface.co/hakurei/waifu-diffusion">Waifu</a>, <a href="https://huggingface.co/lambdalabs/sd-pokemon-diffusers">Pokemon</a>, <a href="https://huggingface.co/yuk/fuyuko-waifu-diffusion">Fuyuko Waifu</a>, <a href="https://huggingface.co/AstraliteHeart/pony-diffusion">Pony</a>, <a href="https://huggingface.co/sd-dreambooth-library/herge-style">Hergé (Tintin)</a>, <a href="https://huggingface.co/nousr/robo-diffusion">Robo</a>, <a href="https://huggingface.co/DGSpitzer/Cyberpunk-Anime-Diffusion">Cyberpunk Anime</a>
|
| 145 |
+
</p> <br>
|
| 146 |
+
<p>
|
| 147 |
+
Running on <b>{device}</b>
|
| 148 |
</p>
|
| 149 |
</div>
|
| 150 |
"""
|
| 151 |
)
|
| 152 |
+
# gr.Markdown(f"Running on: {device}", elem_id="markdown_device")
|
| 153 |
with gr.Row():
|
| 154 |
|
| 155 |
with gr.Column():
|
| 156 |
+
model_name = gr.Dropdown(label="Model", choices=[m.name for m in models], value=models[0].name)
|
| 157 |
prompt = gr.Textbox(label="Prompt", placeholder="Style prefix is applied automatically")
|
| 158 |
run = gr.Button(value="Run")
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
with gr.Tab("Options"):
|
| 161 |
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
| 162 |
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
| 163 |
+
steps = gr.Slider(label="Steps", value=50, maximum=100, minimum=2, step=1)
|
| 164 |
+
width = gr.Slider(label="Width", value=512, maximum=max_width, minimum=64, step=8)
|
| 165 |
+
height = gr.Slider(label="Height", value=512, maximum=max_height, minimum=64, step=8)
|
| 166 |
seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
|
| 167 |
+
|
| 168 |
with gr.Tab("Image to image"):
|
| 169 |
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
| 170 |
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
|
|
|
| 172 |
with gr.Column():
|
| 173 |
image_out = gr.Image(height=512)
|
| 174 |
|
| 175 |
+
inputs = [model_name, prompt, guidance, steps, width, height, seed, image, strength, neg_prompt]
|
| 176 |
prompt.submit(inference, inputs=inputs, outputs=image_out)
|
| 177 |
run.click(inference, inputs=inputs, outputs=image_out)
|
| 178 |
+
|
| 179 |
gr.Examples([
|
| 180 |
+
[models[0].name, "jason bateman disassembling the demon core", 7.5, 50],
|
| 181 |
+
[models[3].name, "portrait of dwayne johnson", 7.0, 75],
|
| 182 |
+
[models[4].name, "portrait of a beautiful alyx vance half life", 10, 50],
|
| 183 |
+
[models[5].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0, 45],
|
| 184 |
+
[models[4].name, "fantasy portrait painting, digital art", 4.0, 30],
|
| 185 |
+
], [model_name, prompt, guidance, steps, seed], image_out, inference, cache_examples=not is_colab and torch.cuda.is_available())
|
| 186 |
+
|
| 187 |
gr.Markdown('''
|
| 188 |
Models by [@nitrosocke](https://huggingface.co/nitrosocke), [@Helixngc7293](https://twitter.com/DGSpitzer) and others. ❤️<br>
|
| 189 |
Space by: [](https://twitter.com/hahahahohohe)
|
|
|
|
| 191 |

|
| 192 |
''')
|
| 193 |
|
| 194 |
+
if not is_colab:
|
| 195 |
+
demo.queue()
|
| 196 |
+
demo.launch(debug=is_colab)
|