Riccardo Giorato commited on
Commit
247b595
1 Parent(s): 6dc6ba5

setup diffusion playground

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. README.md +2 -2
  3. app.py +246 -0
  4. nsfw.png +3 -0
  5. requirements.txt +6 -0
  6. utils.py +6 -0
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ *.png filter=lfs diff=lfs merge=lfs -text
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Playground Diffusion
3
- emoji: 🏃
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
@@ -10,4 +10,4 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Playground Diffusion
3
+ emoji: 🎮
4
  colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
 
10
  license: mit
11
  ---
12
 
13
+ This Space is based on [anzorq/finetuned_diffusion](https://huggingface.co/spaces/anzorq/finetuned_diffusion), go and support them and thank them for their open source work!
app.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import AutoencoderKL, UNet2DConditionModel, StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
2
+ import gradio as gr
3
+ import torch
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
12
+ self.path = path
13
+ self.prefix = prefix
14
+ self.pipe_t2i = None
15
+ self.pipe_i2i = None
16
+
17
+ models = [
18
+ Model("Beeple", "riccardogiorato/beeple-diffusion", "beeple style "),
19
+ ]
20
+
21
+ last_mode = "txt2img"
22
+ current_model = models[1]
23
+ current_model_path = current_model.path
24
+
25
+ if is_colab:
26
+ pipe = StableDiffusionPipeline.from_pretrained(current_model.path, torch_dtype=torch.float16)
27
+
28
+ else: # download all models
29
+ vae = AutoencoderKL.from_pretrained(current_model.path, subfolder="vae", torch_dtype=torch.float16)
30
+ for model in models[1:]:
31
+ try:
32
+ unet = UNet2DConditionModel.from_pretrained(model.path, subfolder="unet", torch_dtype=torch.float16)
33
+ model.pipe_t2i = StableDiffusionPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
34
+ model.pipe_i2i = StableDiffusionImg2ImgPipeline.from_pretrained(model.path, unet=unet, vae=vae, torch_dtype=torch.float16)
35
+ except:
36
+ models.remove(model)
37
+ pipe = models[1].pipe_t2i
38
+
39
+ if torch.cuda.is_available():
40
+ pipe = pipe.to("cuda")
41
+
42
+ device = "GPU 🔥" if torch.cuda.is_available() else "CPU 🥶"
43
+
44
+ def custom_model_changed(path):
45
+ models[0].path = path
46
+ global current_model
47
+ current_model = models[0]
48
+
49
+ def inference(model_name, prompt, guidance, steps, width=512, height=512, seed=0, img=None, strength=0.5, neg_prompt=""):
50
+
51
+ global current_model
52
+ for model in models:
53
+ if model.name == model_name:
54
+ current_model = model
55
+ model_path = current_model.path
56
+
57
+ generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
58
+
59
+ if img is not None:
60
+ return img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator)
61
+ else:
62
+ return txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator)
63
+
64
+ def txt_to_img(model_path, prompt, neg_prompt, guidance, steps, width, height, generator=None):
65
+
66
+ global last_mode
67
+ global pipe
68
+ global current_model_path
69
+ if model_path != current_model_path or last_mode != "txt2img":
70
+ current_model_path = model_path
71
+
72
+ if is_colab or current_model == models[0]:
73
+ pipe = StableDiffusionPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
74
+ else:
75
+ pipe.to("cpu")
76
+ pipe = current_model.pipe_t2i
77
+
78
+ if torch.cuda.is_available():
79
+ pipe = pipe.to("cuda")
80
+ last_mode = "txt2img"
81
+
82
+ prompt = current_model.prefix + prompt
83
+ result = pipe(
84
+ prompt,
85
+ negative_prompt = neg_prompt,
86
+ # num_images_per_prompt=n_images,
87
+ num_inference_steps = int(steps),
88
+ guidance_scale = guidance,
89
+ width = width,
90
+ height = height,
91
+ generator = generator)
92
+
93
+ return replace_nsfw_images(result)
94
+
95
+ def img_to_img(model_path, prompt, neg_prompt, img, strength, guidance, steps, width, height, generator=None):
96
+
97
+ global last_mode
98
+ global pipe
99
+ global current_model_path
100
+ if model_path != current_model_path or last_mode != "img2img":
101
+ current_model_path = model_path
102
+
103
+ if is_colab or current_model == models[0]:
104
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(current_model_path, torch_dtype=torch.float16)
105
+ else:
106
+ pipe.to("cpu")
107
+ pipe = current_model.pipe_i2i
108
+
109
+ if torch.cuda.is_available():
110
+ pipe = pipe.to("cuda")
111
+ last_mode = "img2img"
112
+
113
+ prompt = current_model.prefix + prompt
114
+ ratio = min(height / img.height, width / img.width)
115
+ img = img.resize((int(img.width * ratio), int(img.height * ratio)), Image.LANCZOS)
116
+ result = pipe(
117
+ prompt,
118
+ negative_prompt = neg_prompt,
119
+ # num_images_per_prompt=n_images,
120
+ init_image = img,
121
+ num_inference_steps = int(steps),
122
+ strength = strength,
123
+ guidance_scale = guidance,
124
+ width = width,
125
+ height = height,
126
+ generator = generator)
127
+
128
+ return replace_nsfw_images(result)
129
+
130
+ def replace_nsfw_images(results):
131
+ for i in range(len(results.images)):
132
+ if results.nsfw_content_detected[i]:
133
+ results.images[i] = Image.open("nsfw.png")
134
+ return results.images[0]
135
+
136
+ css = """
137
+ <style>
138
+ .finetuned-diffusion-div {
139
+ text-align: center;
140
+ max-width: 700px;
141
+ margin: 0 auto;
142
+ }
143
+ .finetuned-diffusion-div div {
144
+ display: inline-flex;
145
+ align-items: center;
146
+ gap: 0.8rem;
147
+ font-size: 1.75rem;
148
+ }
149
+ .finetuned-diffusion-div div h1 {
150
+ font-weight: 900;
151
+ margin-bottom: 7px;
152
+ }
153
+ .finetuned-diffusion-div p {
154
+ margin-bottom: 10px;
155
+ font-size: 94%;
156
+ }
157
+ .finetuned-diffusion-div p a {
158
+ text-decoration: underline;
159
+ }
160
+ .tabs {
161
+ margin-top: 0px;
162
+ margin-bottom: 0px;
163
+ }
164
+ #gallery {
165
+ min-height: 20rem;
166
+ }
167
+ </style>
168
+ """
169
+ with gr.Blocks(css=css) as demo:
170
+ gr.HTML(
171
+ f"""
172
+ <div class="finetuned-diffusion-div">
173
+ <div>
174
+ <h1>Finetuned Diffusion</h1>
175
+ </div>
176
+ <p>
177
+ Demo for multiple fine-tuned Stable Diffusion models, trained on different styles: <br>
178
+ <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 🤗.
179
+ </p>
180
+ <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>
181
+ Running on <b>{device}</b>{(" in a <b>Google Colab</b>." if is_colab else "")}
182
+ </p>
183
+ </div>
184
+ """
185
+ )
186
+ with gr.Row():
187
+
188
+ with gr.Column(scale=55):
189
+ with gr.Group():
190
+ model_name = gr.Dropdown(label="Model", choices=[m.name for m in models], value=current_model.name)
191
+ with gr.Box(visible=False) as custom_model_group:
192
+ custom_model_path = gr.Textbox(label="Custom model path", placeholder="Path to model, e.g. nitrosocke/Arcane-Diffusion", interactive=True)
193
+ gr.HTML("<div><font size='2'>Custom models have to be downloaded first, so give it some time.</font></div>")
194
+
195
+ with gr.Row():
196
+ prompt = gr.Textbox(label="Prompt", show_label=False, max_lines=2,placeholder="Enter prompt. Style applied automatically").style(container=False)
197
+ generate = gr.Button(value="Generate").style(rounded=(False, True, True, False))
198
+
199
+
200
+ image_out = gr.Image(height=512)
201
+ # gallery = gr.Gallery(
202
+ # label="Generated images", show_label=False, elem_id="gallery"
203
+ # ).style(grid=[1], height="auto")
204
+
205
+ with gr.Column(scale=45):
206
+ with gr.Tab("Options"):
207
+ with gr.Group():
208
+ neg_prompt = gr.Textbox(label="Negative prompt", placeholder="What to exclude from the image")
209
+
210
+ # n_images = gr.Slider(label="Images", value=1, minimum=1, maximum=4, step=1)
211
+
212
+ with gr.Row():
213
+ guidance = gr.Slider(label="Guidance scale", value=7.5, maximum=15)
214
+ steps = gr.Slider(label="Steps", value=50, minimum=2, maximum=100, step=1)
215
+
216
+ with gr.Row():
217
+ width = gr.Slider(label="Width", value=512, minimum=64, maximum=1024, step=8)
218
+ height = gr.Slider(label="Height", value=512, minimum=64, maximum=1024, step=8)
219
+
220
+ seed = gr.Slider(0, 2147483647, label='Seed (0 = random)', value=0, step=1)
221
+
222
+ with gr.Tab("Image to image"):
223
+ with gr.Group():
224
+ image = gr.Image(label="Image", height=256, tool="editor", type="pil")
225
+ strength = gr.Slider(label="Transformation strength", minimum=0, maximum=1, step=0.01, value=0.5)
226
+
227
+ model_name.change(lambda x: gr.update(visible = x == models[0].name), inputs=model_name, outputs=custom_model_group)
228
+ custom_model_path.change(custom_model_changed, inputs=custom_model_path, outputs=None)
229
+ # n_images.change(lambda n: gr.Gallery().style(grid=[2 if n > 1 else 1], height="auto"), inputs=n_images, outputs=gallery)
230
+
231
+ inputs = [model_name, prompt, guidance, steps, width, height, seed, image, strength, neg_prompt]
232
+ prompt.submit(inference, inputs=inputs, outputs=image_out)
233
+ generate.click(inference, inputs=inputs, outputs=image_out)
234
+
235
+ ex = gr.Examples([
236
+ [models[1].name, "Neon techno-magic robot with spear pierces an ancient beast, hyperrealism, no blur, 4k resolution, ultra detailed", 7.5, 50],
237
+ ], [model_name, prompt, guidance, steps, seed], image_out, inference, cache_examples=False)
238
+
239
+ gr.Markdown('''
240
+ Models by [@riccardogiorato](https://huggingface.co/riccardogiorato) <br>
241
+ Space by: [![Twitter Follow](https://img.shields.io/twitter/follow/riccardogiorato?style=social)](https://twitter.com/riccardogiorato)
242
+ ''')
243
+
244
+ if not is_colab:
245
+ demo.queue(concurrency_count=1)
246
+ demo.launch(debug=is_colab, share=is_colab)
nsfw.png ADDED

Git LFS Details

  • SHA256: cb68cb6ac27278f5eea6aac7eb78d0cd17ae14bb37d6f74a0cbf691d7362009f
  • Pointer size: 130 Bytes
  • Size of remote file: 84.1 kB
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch
3
+ diffusers
4
+ transformers
5
+ scipy
6
+ ftfy
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