Spaces:
Runtime error
Runtime error
Commit
·
3e004db
0
Parent(s):
Duplicate from anzorq/finetuned_diffusion
Browse filesCo-authored-by: AQ <[email protected]>
- .gitattributes +33 -0
- README.md +14 -0
- app.py +298 -0
- nsfw.png +0 -0
- requirements.txt +8 -0
- utils.py +6 -0
.gitattributes
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Finetuned Diffusion
|
| 3 |
+
emoji: 🪄🖼️
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.6
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: mit
|
| 11 |
+
duplicated_from: anzorq/finetuned_diffusion
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import AutoencoderKL, UNet2DConditionModel, StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
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=""):
|
| 15 |
+
self.name = name
|
| 16 |
+
self.path = path
|
| 17 |
+
self.prefix = prefix
|
| 18 |
+
self.pipe_t2i = None
|
| 19 |
+
self.pipe_i2i = None
|
| 20 |
+
|
| 21 |
+
models = [
|
| 22 |
+
Model("Arcane", "nitrosocke/Arcane-Diffusion", "arcane style "),
|
| 23 |
+
Model("Archer", "nitrosocke/archer-diffusion", "archer style "),
|
| 24 |
+
Model("Modern Disney", "nitrosocke/mo-di-diffusion", "modern disney style "),
|
| 25 |
+
Model("Classic Disney", "nitrosocke/classic-anim-diffusion", "classic disney style "),
|
| 26 |
+
Model("Loving Vincent (Van Gogh)", "dallinmackay/Van-Gogh-diffusion", "lvngvncnt "),
|
| 27 |
+
Model("Redshift renderer (Cinema4D)", "nitrosocke/redshift-diffusion", "redshift style "),
|
| 28 |
+
Model("Midjourney v4 style", "prompthero/midjourney-v4-diffusion", "mdjrny-v4 style "),
|
| 29 |
+
Model("Waifu", "hakurei/waifu-diffusion"),
|
| 30 |
+
Model("Cyberpunk Anime", "DGSpitzer/Cyberpunk-Anime-Diffusion", "dgs illustration style "),
|
| 31 |
+
Model("Elden Ring", "nitrosocke/elden-ring-diffusion", "elden ring style "),
|
| 32 |
+
Model("TrinArt v2", "naclbit/trinart_stable_diffusion_v2"),
|
| 33 |
+
Model("Spider-Verse", "nitrosocke/spider-verse-diffusion", "spiderverse style "),
|
| 34 |
+
Model("Balloon Art", "Fictiverse/Stable_Diffusion_BalloonArt_Model", "BalloonArt "),
|
| 35 |
+
Model("Tron Legacy", "dallinmackay/Tron-Legacy-diffusion", "trnlgcy "),
|
| 36 |
+
Model("Pokémon", "lambdalabs/sd-pokemon-diffusers"),
|
| 37 |
+
Model("Pony Diffusion", "AstraliteHeart/pony-diffusion"),
|
| 38 |
+
Model("Robo Diffusion", "nousr/robo-diffusion"),
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
scheduler = DPMSolverMultistepScheduler(
|
| 42 |
+
beta_start=0.00085,
|
| 43 |
+
beta_end=0.012,
|
| 44 |
+
beta_schedule="scaled_linear",
|
| 45 |
+
num_train_timesteps=1000,
|
| 46 |
+
trained_betas=None,
|
| 47 |
+
predict_epsilon=True,
|
| 48 |
+
thresholding=False,
|
| 49 |
+
algorithm_type="dpmsolver++",
|
| 50 |
+
solver_type="midpoint",
|
| 51 |
+
lower_order_final=True,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
custom_model = None
|
| 55 |
+
if is_colab:
|
| 56 |
+
models.insert(0, Model("Custom model"))
|
| 57 |
+
custom_model = models[0]
|
| 58 |
+
|
| 59 |
+
last_mode = "txt2img"
|
| 60 |
+
current_model = models[1] if is_colab else models[0]
|
| 61 |
+
current_model_path = current_model.path
|
| 62 |
+
|
| 63 |
+
if is_colab:
|
| 64 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16, scheduler=scheduler, safety_checker=lambda images, clip_input: (images, False))
|
| 65 |
+
|
| 66 |
+
else: # download all models
|
| 67 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16, scheduler=scheduler)
|
| 68 |
+
# print(f"{datetime.datetime.now()} Downloading vae...")
|
| 69 |
+
# vae = AutoencoderKL.from_pretrained(current_model.path, subfolder="vae", torch_dtype=torch.float16)
|
| 70 |
+
# for model in models:
|
| 71 |
+
# try:
|
| 72 |
+
# print(f"{datetime.datetime.now()} Downloading {model.name} model...")
|
| 73 |
+
# unet = UNet2DConditionModel.from_pretrained(model.path, subfolder="unet", torch_dtype=torch.float16)
|
| 74 |
+
# model.pipe_t2i = StableDiffusionPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16, scheduler=scheduler)
|
| 75 |
+
# model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16, scheduler=scheduler)
|
| 76 |
+
# except Exception as e:
|
| 77 |
+
# print(f"{datetime.datetime.now()} Failed to load model " + model.name + ": " + str(e))
|
| 78 |
+
# models.remove(model)
|
| 79 |
+
# pipe = models[0].pipe_t2i
|
| 80 |
+
|
| 81 |
+
if torch.cuda.is_available():
|
| 82 |
+
pipe = pipe.to("cuda")
|
| 83 |
+
|
| 84 |
+
device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
|
| 85 |
+
|
| 86 |
+
def error_str(error, title="Error"):
|
| 87 |
+
return f"""#### {title}
|
| 88 |
+
{error}""" if error else ""
|
| 89 |
+
|
| 90 |
+
def custom_model_changed(path):
|
| 91 |
+
models[0].path = path
|
| 92 |
+
global current_model
|
| 93 |
+
current_model = models[0]
|
| 94 |
+
|
| 95 |
+
def on_model_change(model_name):
|
| 96 |
+
|
| 97 |
+
prefix = "Enter prompt. \"" + next((m.prefix for m in models if m.name == model_name), None) + "\" is prefixed automatically" if model_name != models[0].name else "Don't forget to use the custom model prefix in the prompt!"
|
| 98 |
+
|
| 99 |
+
return gr.update(visible = model_name == models[0].name), gr.update(placeholder=prefix)
|
| 100 |
+
|
| 101 |
+
def inference(model_name, prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
|
| 102 |
+
|
| 103 |
+
print(psutil.virtual_memory()) # print memory usage
|
| 104 |
+
|
| 105 |
+
global current_model
|
| 106 |
+
for model in models:
|
| 107 |
+
if model.name == model_name:
|
| 108 |
+
current_model = model
|
| 109 |
+
model_path = current_model.path
|
| 110 |
+
|
| 111 |
+
generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
if img is not None:
|
| 115 |
+
return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator), None
|
| 116 |
+
else:
|
| 117 |
+
return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator), None
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return None, error_str(e)
|
| 120 |
+
|
| 121 |
+
def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator):
|
| 122 |
+
|
| 123 |
+
print(f"{datetime.datetime.now()} txt_to_img, model: {current_model.name}")
|
| 124 |
+
|
| 125 |
+
global last_mode
|
| 126 |
+
global pipe
|
| 127 |
+
global current_model_path
|
| 128 |
+
if model_path != current_model_path or last_mode != "txt2img":
|
| 129 |
+
current_model_path = model_path
|
| 130 |
+
|
| 131 |
+
if is_colab or current_model == custom_model:
|
| 132 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16, scheduler=scheduler, safety_checker=lambda images, clip_input: (images, False))
|
| 133 |
+
else:
|
| 134 |
+
pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16, scheduler=scheduler)
|
| 135 |
+
# pipe = pipe.to("cpu")
|
| 136 |
+
# pipe = current_model.pipe_t2i
|
| 137 |
+
|
| 138 |
+
if torch.cuda.is_available():
|
| 139 |
+
pipe = pipe.to("cuda")
|
| 140 |
+
last_mode = "txt2img"
|
| 141 |
+
|
| 142 |
+
prompt = current_model.prefix + prompt
|
| 143 |
+
result = pipe(
|
| 144 |
+
prompt,
|
| 145 |
+
negative_prompt = neg_prompt,
|
| 146 |
+
# num_images_per_prompt=n_images,
|
| 147 |
+
num_inference_steps = int(steps),
|
| 148 |
+
guidance_scale = guidance,
|
| 149 |
+
width = width,
|
| 150 |
+
height = height,
|
| 151 |
+
generator = generator)
|
| 152 |
+
|
| 153 |
+
return replace_nsfw_images(result)
|
| 154 |
+
|
| 155 |
+
def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator):
|
| 156 |
+
|
| 157 |
+
print(f"{datetime.datetime.now()} img_to_img, model: {model_path}")
|
| 158 |
+
|
| 159 |
+
global last_mode
|
| 160 |
+
global pipe
|
| 161 |
+
global current_model_path
|
| 162 |
+
if model_path != current_model_path or last_mode != "img2img":
|
| 163 |
+
current_model_path = model_path
|
| 164 |
+
|
| 165 |
+
if is_colab or current_model == custom_model:
|
| 166 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16, scheduler=scheduler, safety_checker=lambda images, clip_input: (images, False))
|
| 167 |
+
else:
|
| 168 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16, scheduler=scheduler)
|
| 169 |
+
# pipe = pipe.to("cpu")
|
| 170 |
+
# pipe = current_model.pipe_i2i
|
| 171 |
+
|
| 172 |
+
if torch.cuda.is_available():
|
| 173 |
+
pipe = pipe.to("cuda")
|
| 174 |
+
last_mode = "img2img"
|
| 175 |
+
|
| 176 |
+
prompt = current_model.prefix + prompt
|
| 177 |
+
ratio = min(height / img.height, width / img.width)
|
| 178 |
+
img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
|
| 179 |
+
result = pipe(
|
| 180 |
+
prompt,
|
| 181 |
+
negative_prompt = neg_prompt,
|
| 182 |
+
# num_images_per_prompt=n_images,
|
| 183 |
+
init_image = img,
|
| 184 |
+
num_inference_steps = int(steps),
|
| 185 |
+
strength = strength,
|
| 186 |
+
guidance_scale = guidance,
|
| 187 |
+
width = width,
|
| 188 |
+
height = height,
|
| 189 |
+
generator = generator)
|
| 190 |
+
|
| 191 |
+
return replace_nsfw_images(result)
|
| 192 |
+
|
| 193 |
+
def replace_nsfw_images(results):
|
| 194 |
+
|
| 195 |
+
if is_colab:
|
| 196 |
+
return results.images[0]
|
| 197 |
+
|
| 198 |
+
for i in range(len(results.images)):
|
| 199 |
+
if results.nsfw_content_detected[i]:
|
| 200 |
+
results.images[i] = Image.open("nsfw.png")
|
| 201 |
+
return results.images[0]
|
| 202 |
+
|
| 203 |
+
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}
|
| 204 |
+
"""
|
| 205 |
+
with gr.Blocks(css=css) as demo:
|
| 206 |
+
gr.HTML(
|
| 207 |
+
f"""
|
| 208 |
+
<div class="finetuned-diffusion-div">
|
| 209 |
+
<div>
|
| 210 |
+
<h1>Finetuned Diffusion</h1>
|
| 211 |
+
</div>
|
| 212 |
+
<p>
|
| 213 |
+
Demo for multiple fine-tuned Stable Diffusion models, trained on different styles: <br>
|
| 214 |
+
<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/mo-di-diffusion">Modern Disney</a>, <a href="https://huggingface.co/nitrosocke/classic-anim-diffusion">Classic Disney</a>, <a href="https://huggingface.co/dallinmackay/Van-Gogh-diffusion">Loving Vincent (Van Gogh)</a>, <a href="https://huggingface.co/nitrosocke/redshift-diffusion">Redshift renderer (Cinema4D)</a>, <a href="https://huggingface.co/prompthero/midjourney-v4-diffusion">Midjourney v4 style</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>, <a href="https://huggingface.co/Fictiverse/Stable_Diffusion_BalloonArt_Model">Balloon Art</a> + in colab notebook you can load any other Diffusers 🧨 SD model hosted on HuggingFace 🤗.
|
| 215 |
+
</p>
|
| 216 |
+
<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>
|
| 217 |
+
Running on <b>{device}</b>{(" in a <b>Google Colab</b>." if is_colab else "")}
|
| 218 |
+
</p>
|
| 219 |
+
<p>You can also duplicate this space and upgrade to gpu by going to settings: <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>
|
| 220 |
+
</div>
|
| 221 |
+
"""
|
| 222 |
+
)
|
| 223 |
+
with gr.Row():
|
| 224 |
+
|
| 225 |
+
with gr.Column(scale=55):
|
| 226 |
+
with gr.Group():
|
| 227 |
+
model_name = gr.Dropdown(label="Model", choices=[m.name for m in models], value=current_model.name)
|
| 228 |
+
with gr.Box(visible=False) as custom_model_group:
|
| 229 |
+
custom_model_path = gr.Textbox(label="Custom model path", placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion", interactive=True)
|
| 230 |
+
gr.HTML("<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>")
|
| 231 |
+
|
| 232 |
+
with gr.Row():
|
| 233 |
+
prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="Enter prompt. Style applied automatically").style(container=False)
|
| 234 |
+
generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
image_out = gr.Image(height=512)
|
| 238 |
+
# gallery = gr.Gallery(
|
| 239 |
+
# label="Generated images", show_label=False, elem_id="gallery"
|
| 240 |
+
# ).style(grid=[1], height="auto")
|
| 241 |
+
error_output = gr.Markdown()
|
| 242 |
+
|
| 243 |
+
with gr.Column(scale=45):
|
| 244 |
+
with gr.Tab("Options"):
|
| 245 |
+
with gr.Group():
|
| 246 |
+
neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
|
| 247 |
+
|
| 248 |
+
# n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
|
| 249 |
+
|
| 250 |
+
with gr.Row():
|
| 251 |
+
guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
|
| 252 |
+
steps = gr.Slider(label="Steps", value=25, minimum=2, maximum=75, step=1)
|
| 253 |
+
|
| 254 |
+
with gr.Row():
|
| 255 |
+
width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
|
| 256 |
+
height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
|
| 257 |
+
|
| 258 |
+
seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
|
| 259 |
+
|
| 260 |
+
with gr.Tab("Image to image"):
|
| 261 |
+
with gr.Group():
|
| 262 |
+
image = gr.Image(label="Image", height=256, tool="editor", type="pil")
|
| 263 |
+
strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
|
| 264 |
+
|
| 265 |
+
if is_colab:
|
| 266 |
+
model_name.change(on_model_change, inputs=model_name, outputs=[custom_model_group, prompt], queue=False)
|
| 267 |
+
custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
|
| 268 |
+
# n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
|
| 269 |
+
|
| 270 |
+
inputs = [model_name, prompt, guidance, steps, width, height, seed, image, strength, neg_prompt]
|
| 271 |
+
outputs = [image_out, error_output]
|
| 272 |
+
prompt.submit(inference, inputs=inputs, outputs=outputs)
|
| 273 |
+
generate.click(inference, inputs=inputs, outputs=outputs)
|
| 274 |
+
|
| 275 |
+
ex = gr.Examples([
|
| 276 |
+
[models[7].name, "tiny cute and adorable kitten adventurer dressed in a warm overcoat with survival gear on a winters day", 7.5, 50],
|
| 277 |
+
[models[4].name, "portrait of dwayne johnson", 7.0, 75],
|
| 278 |
+
[models[5].name, "portrait of a beautiful alyx vance half life", 10, 50],
|
| 279 |
+
[models[6].name, "Aloy from Horizon: Zero Dawn, half body portrait, smooth, detailed armor, beautiful face, illustration", 7.0, 45],
|
| 280 |
+
[models[5].name, "fantasy portrait painting, digital art", 4.0, 30],
|
| 281 |
+
], inputs=[model_name, prompt, guidance, steps, seed], outputs=outputs, fn=inference, cache_examples=False)
|
| 282 |
+
|
| 283 |
+
gr.HTML("""
|
| 284 |
+
<div style="border-top: 1px solid #303030;">
|
| 285 |
+
<br>
|
| 286 |
+
<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>
|
| 287 |
+
<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><br>
|
| 288 |
+
<p>Space by: <a href="https://twitter.com/hahahahohohe"><img src="https://img.shields.io/twitter/follow/hahahahohohe?label=%40anzorq&style=social" alt="Twitter Follow"></a></p><br>
|
| 289 |
+
<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>
|
| 290 |
+
<p><img src="https://visitor-badge.glitch.me/badge?page_id=anzorq.finetuned_diffusion" alt="visitors"></p>
|
| 291 |
+
</div>
|
| 292 |
+
""")
|
| 293 |
+
|
| 294 |
+
print(f"Space built in {time.time() - start_time:.2f} seconds")
|
| 295 |
+
|
| 296 |
+
if not is_colab:
|
| 297 |
+
demo.queue(concurrency_count=1)
|
| 298 |
+
demo.launch(debug=is_colab, share=is_colab)
|
nsfw.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
| 2 |
+
torch
|
| 3 |
+
git+https://github.com/huggingface/diffusers.git
|
| 4 |
+
transformers
|
| 5 |
+
scipy
|
| 6 |
+
ftfy
|
| 7 |
+
accelerate
|
| 8 |
+
psutil
|
utils.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def is_google_colab():
|
| 2 |
+
try:
|
| 3 |
+
import google.colab
|
| 4 |
+
return True
|
| 5 |
+
except:
|
| 6 |
+
return False
|