1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "provenance" : [],
7
+ "gpuType" : " T4" ,
8
+ "private_outputs" : true ,
9
+ "authorship_tag" : " ABX9TyOiUWRZBuOHzRrD41UDa1vL" ,
10
+ "include_colab_link" : true
11
+ },
12
+ "kernelspec" : {
13
+ "name" : " python3" ,
14
+ "display_name" : " Python 3"
15
+ },
16
+ "language_info" : {
17
+ "name" : " python"
18
+ },
19
+ "accelerator" : " GPU"
20
+ },
21
+ "cells" : [
22
+ {
23
+ "cell_type" : " markdown" ,
24
+ "metadata" : {
25
+ "id" : " view-in-github" ,
26
+ "colab_type" : " text"
27
+ },
28
+ "source" : [
29
+ " <a href=\" https://colab.research.google.com/github/ruchira-net/css-loader/blob/master/Stable_Diffusion.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
30
+ ]
31
+ },
32
+ {
33
+ "cell_type" : " code" ,
34
+ "source" : [],
35
+ "metadata" : {
36
+ "id" : " 3he-zWjkTirt"
37
+ },
38
+ "execution_count" : null ,
39
+ "outputs" : []
40
+ },
41
+ {
42
+ "cell_type" : " code" ,
43
+ "execution_count" : null ,
44
+ "metadata" : {
45
+ "id" : " aSGhPjR3Er-N"
46
+ },
47
+ "outputs" : [],
48
+ "source" : [
49
+ " #@title 👇 Installing dependencies { display-mode: \" form\" }\n " ,
50
+ " #@markdown ---\n " ,
51
+ " #@markdown Make sure to select **GPU** as the runtime type:<br/>\n " ,
52
+ " #@markdown *Runtime->Change Runtime Type->Under Hardware accelerator, select GPU*\n " ,
53
+ " #@markdown\n " ,
54
+ " #@markdown ---\n " ,
55
+ " \n " ,
56
+ " !pip -q install torch torchvision tor diffusers transformers accelerate scipy safetensors xformers mediapy ipywidgets==7.7.1\n "
57
+ ]
58
+ },
59
+ {
60
+ "cell_type" : " code" ,
61
+ "source" : [
62
+ " #@title 👇 Selecting Model { form-width: \" 20%\" , display-mode: \" form\" }\n " ,
63
+ " #@markdown ---\n " ,
64
+ " #@markdown - **Select Model** - A list of Stable Diffusion models to choose from.\n " ,
65
+ " #@markdown - **Select Sampler** - A list of schedulers to choose from. Default is EulerAncestralScheduler.\n " ,
66
+ " #@markdown - **Safety Checker** - Enable/Disable uncensored content\n " ,
67
+ " #@markdown\n " ,
68
+ " #@markdown ---\n " ,
69
+ " \n " ,
70
+ " from diffusers import StableDiffusionPipeline, EulerAncestralDiscreteScheduler\n " ,
71
+ " from diffusers.models import AutoencoderKL\n " ,
72
+ " import torch\n " ,
73
+ " import ipywidgets as widgets\n " ,
74
+ " import importlib\n " ,
75
+ " from PIL import Image\n " ,
76
+ " \n " ,
77
+ " #Enable third party widget support\n " ,
78
+ " from google.colab import output\n " ,
79
+ " output.enable_custom_widget_manager()\n " ,
80
+ " \n " ,
81
+ " #Pipe\n " ,
82
+ " pipe = None\n " ,
83
+ " \n " ,
84
+ " #Models\n " ,
85
+ " select_model = widgets.Dropdown(\n " ,
86
+ " options=[\n " ,
87
+ " (\" Dreamlike Photoreal 2.0\" , \" stabilityai/stable-diffusion-xl-base-1.0\" )\n " ,
88
+ " ],\n " ,
89
+ " description=\" Select Model:\"\n " ,
90
+ " )\n " ,
91
+ " \n " ,
92
+ " #Schedulers\n " ,
93
+ " select_sampler = widgets.Dropdown(\n " ,
94
+ " options=[\n " ,
95
+ " \" EulerAncestralDiscreteScheduler\" ,\n " ,
96
+ " \" EulerDiscreteScheduler\" ,\n " ,
97
+ " \" UniPCMultistepScheduler\" ,\n " ,
98
+ " \" DDIMScheduler\"\n " ,
99
+ " ],\n " ,
100
+ " description=\" Select Schedular:\"\n " ,
101
+ " )\n " ,
102
+ " select_sampler.style.description_width = \" auto\"\n " ,
103
+ " \n " ,
104
+ " #Safety Checker\n " ,
105
+ " safety_check = widgets.Checkbox(\n " ,
106
+ " value=False,\n " ,
107
+ " description=\" Enable Safety Check\" ,\n " ,
108
+ " layout=widgets.Layout(margin=\" 0px 0px 0px -85px\" )\n " ,
109
+ " )\n " ,
110
+ " \n " ,
111
+ " #Output\n " ,
112
+ " out = widgets.Output()\n " ,
113
+ " \n " ,
114
+ " #Apply Settings\n " ,
115
+ " apply_btn = widgets.Button(\n " ,
116
+ " description=\" Apply\" ,\n " ,
117
+ " button_style=\" info\"\n " ,
118
+ " )\n " ,
119
+ " \n " ,
120
+ " \n " ,
121
+ " #Get scheduler\n " ,
122
+ " def get_scheduler(name):\n " ,
123
+ " \n " ,
124
+ " match name:\n " ,
125
+ " \n " ,
126
+ " case \" EulerAncestralDiscreteScheduler\" :\n " ,
127
+ " return EulerAncestralDiscreteScheduler.from_pretrained(select_model.value, subfolder=\" scheduler\" )\n " ,
128
+ " \n " ,
129
+ " #Run pipeline\n " ,
130
+ " def pipeline(p):\n " ,
131
+ " \n " ,
132
+ " global pipe\n " ,
133
+ " \n " ,
134
+ " out.clear_output()\n " ,
135
+ " apply_btn.disabled = True\n " ,
136
+ " \n " ,
137
+ " with out:\n " ,
138
+ " \n " ,
139
+ " print(\" Running, please wait...\" )\n " ,
140
+ " \n " ,
141
+ " pipe = StableDiffusionPipeline.from_pretrained(\n " ,
142
+ " select_model.value,\n " ,
143
+ " scheduler=get_scheduler(select_sampler.value),\n " ,
144
+ " torch_dtype=torch.float16,\n " ,
145
+ " vae=AutoencoderKL.from_pretrained(\" stabilityai/sd-vae-ft-mse\" , torch_dtype=torch.float16).to(\" cuda\" )\n " ,
146
+ " ).to(\" cuda\" )\n " ,
147
+ " \n " ,
148
+ " pipe.safety_checker = None\n " ,
149
+ " \n " ,
150
+ " pipe.enable_xformers_memory_efficient_attention()\n " ,
151
+ " \n " ,
152
+ " print(\" Finished!\" )\n " ,
153
+ " \n " ,
154
+ " apply_btn.disabled = False\n " ,
155
+ " \n " ,
156
+ " \n " ,
157
+ " #Display\n " ,
158
+ " apply_btn.on_click(pipeline)\n " ,
159
+ " \n " ,
160
+ " widgets.VBox(\n " ,
161
+ " [\n " ,
162
+ " widgets.HTML(value=\" <h2>Configure Pipeline</h2>\" ),\n " ,
163
+ " select_model, select_sampler, safety_check, apply_btn, out\n " ,
164
+ " ]\n " ,
165
+ " )\n " ,
166
+ " \n " ,
167
+ " #call pipeline method from here\n " ,
168
+ " pipeline(None)"
169
+ ],
170
+ "metadata" : {
171
+ "id" : " b_0UKcp3E9no"
172
+ },
173
+ "execution_count" : null ,
174
+ "outputs" : []
175
+ },
176
+ {
177
+ "cell_type" : " code" ,
178
+ "source" : [
179
+ " %env PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True"
180
+ ],
181
+ "metadata" : {
182
+ "id" : " ef0CwmfkmKOg"
183
+ },
184
+ "execution_count" : null ,
185
+ "outputs" : []
186
+ },
187
+ {
188
+ "cell_type" : " code" ,
189
+ "source" : [
190
+ " #@title 👇 Generating Images { form-width: \" 20%\" , display-mode: \" form\" }\n " ,
191
+ " #@markdown ---\n " ,
192
+ " #@markdown - **Prompt** - Description of the image\n " ,
193
+ " #@markdown - **Negative Prompt** - Things you don't want to see or ignore in the image\n " ,
194
+ " #@markdown - **Steps** - Number of denoising steps. Higher steps may lead to better results but takes longer time to generate the image. Default is `30`.\n " ,
195
+ " #@markdown - **CFG** - Guidance scale ranging from `0` to `20`. Lower values allow the AI to be more creative and less strict at following the prompt. Default is `7.5`.\n " ,
196
+ " #@markdown - **Seed** - A random value that controls image generation. The same seed and prompt produce the same images. Set `-1` for using random seed values.\n " ,
197
+ " #@markdown ---\n " ,
198
+ " import ipywidgets as widgets, mediapy, random\n " ,
199
+ " import IPython.display\n " ,
200
+ " \n " ,
201
+ " \n " ,
202
+ " #PARAMETER WIDGETS\n " ,
203
+ " width = \" 300px\"\n " ,
204
+ " \n " ,
205
+ " prompt = widgets.Textarea(\n " ,
206
+ " value=\" (photorealistic (HDR):2), (gorgeous:1.1), (only one:1.4) \" ,\n " ,
207
+ " placeholder=\" Enter prompt\" ,\n " ,
208
+ " #description=\" Prompt:\" ,\n " ,
209
+ " rows=5,\n " ,
210
+ " layout=widgets.Layout(width=\" 600px\" )\n " ,
211
+ " )\n " ,
212
+ " \n " ,
213
+ " camera = widgets.Dropdown(\n " ,
214
+ " options=[('leica summicron 35mm f2.0', ' leica summicron 35mm f2.0'), ('sony a7ii', ' sony a7ii'), ('kodak potra', ' kodak potra')],\n " ,
215
+ " value=' leica summicron 35mm f2.0',\n " ,
216
+ " description=\" Camera:\" ,\n " ,
217
+ " layout=widgets.Layout(width=width)\n " ,
218
+ " )\n " ,
219
+ " \n " ,
220
+ " lighting = widgets.Dropdown(\n " ,
221
+ " options=[('romantic lighting', ' romantic lighting,'), ('dramatic lighting', ' dramatic lighting,')],\n " ,
222
+ " value=' dramatic lighting,',\n " ,
223
+ " description=\" Lighting:\" ,\n " ,
224
+ " layout=widgets.Layout(width=width)\n " ,
225
+ " )\n " ,
226
+ " \n " ,
227
+ " neg_prompt = widgets.Textarea(\n " ,
228
+ " value=\" disfigured, bad lips, ugly lips, bad nose, ugly nose, bad face, ugly face, bad mouth, ugly mouth, beard, bad teeth, ugly teeth, ((grayscale)), cross-eyed, uneven eyes, bad ears, ugly ears, deformed, malformed limbs, bad anatomy, bad hands, three hands, three legs, bad arms, missing legs, missing arms, poorly drawn face, bad face, fused face, cloned face, worst face, three crus, extra crus, fused crus, worst feet, three feet, fused feet, fused thigh, three thigh, fused thigh, extra thigh, worst thigh, missing fingers, extra fingers, ugly fingers, long fingers, horn, extra eyes, huge eyes, 2girl, amputation, disconnected limbs, cartoon, cg, 3d, anime, unreal, animate, masculine, fat, chubby, (tamil:1.7), (red dot:2), (red mark:1.9), (pottu:1.8), (bindi:1.6), paintings, sketches, lowres, ((monochrome)), heavy make-up, (worst quality:1.6), (low quality:1.5), body hair, jewellary, topless, multiple people, blurry, duplicate bodies\" ,\n " ,
229
+ " placeholder=\" Enter negative prompt\" ,\n " ,
230
+ " #description=\" Negative Prompt:\" ,\n " ,
231
+ " rows=5,\n " ,
232
+ " layout=widgets.Layout(width=\" 600px\" )\n " ,
233
+ " )\n " ,
234
+ " \n " ,
235
+ " num_images = widgets.IntText(\n " ,
236
+ " value=4,\n " ,
237
+ " description=\" Images:\" ,\n " ,
238
+ " layout=widgets.Layout(width=width),\n " ,
239
+ " )\n " ,
240
+ " \n " ,
241
+ " steps = widgets.IntText(\n " ,
242
+ " value=35,\n " ,
243
+ " description=\" Steps:\" ,\n " ,
244
+ " layout=widgets.Layout(width=width)\n " ,
245
+ " )\n " ,
246
+ " \n " ,
247
+ " CFG = widgets.FloatText(\n " ,
248
+ " value=7,\n " ,
249
+ " description=\" CFG:\" ,\n " ,
250
+ " layout=widgets.Layout(width=width)\n " ,
251
+ " )\n " ,
252
+ " \n " ,
253
+ " img_height = widgets.Dropdown(\n " ,
254
+ " options=[('512px', 512), ('768px', 768), ('720px', 720)],\n " ,
255
+ " value=720,\n " ,
256
+ " description=\" Height:\" ,\n " ,
257
+ " layout=widgets.Layout(width=width)\n " ,
258
+ " )\n " ,
259
+ " \n " ,
260
+ " img_width = widgets.Dropdown(\n " ,
261
+ " options=[('512px', 512), ('768px', 768), ('1280px', 1280)],\n " ,
262
+ " value=1280,\n " ,
263
+ " description=\" Width:\" ,\n " ,
264
+ " layout=widgets.Layout(width=width)\n " ,
265
+ " )\n " ,
266
+ " \n " ,
267
+ " random_seed = widgets.IntText(\n " ,
268
+ " value=-1,\n " ,
269
+ " description=\" Seed:\" ,\n " ,
270
+ " layout=widgets.Layout(width=width),\n " ,
271
+ " disabled=False\n " ,
272
+ " )\n " ,
273
+ " \n " ,
274
+ " generate = widgets.Button(\n " ,
275
+ " description=\" Generate\" ,\n " ,
276
+ " disabled=False,\n " ,
277
+ " button_style=\" primary\"\n " ,
278
+ " )\n " ,
279
+ " \n " ,
280
+ " display_imgs = widgets.Output()\n " ,
281
+ " \n " ,
282
+ " \n " ,
283
+ " #RUN\n " ,
284
+ " def generate_img(i):\n " ,
285
+ " \n " ,
286
+ " #Clear output\n " ,
287
+ " display_imgs.clear_output()\n " ,
288
+ " generate.disabled = True\n " ,
289
+ " \n " ,
290
+ " #Calculate seed\n " ,
291
+ " seed = random.randint(0, 2147483647) if random_seed.value == -1 else random_seed.value\n " ,
292
+ " \n " ,
293
+ " with display_imgs:\n " ,
294
+ " \n " ,
295
+ " print(f\" Prompt:\\ n{prompt.value + lighting.value + camera.value}\" )\n " ,
296
+ " \n " ,
297
+ " images = pipe(\n " ,
298
+ " prompt.value + lighting.value + camera.value,\n " ,
299
+ " height = img_height.value,\n " ,
300
+ " width = img_width.value,\n " ,
301
+ " num_inference_steps = steps.value,\n " ,
302
+ " guidance_scale = CFG.value,\n " ,
303
+ " num_images_per_prompt = num_images.value,\n " ,
304
+ " negative_prompt = neg_prompt.value,\n " ,
305
+ " generator = torch.Generator(\" cuda\" ).manual_seed(seed),\n " ,
306
+ " ).images\n " ,
307
+ " \n " ,
308
+ " mediapy.show_images(images)\n " ,
309
+ " \n " ,
310
+ " print(f\" Seed:\\ n{seed}\" )\n " ,
311
+ " \n " ,
312
+ " generate.disabled = False\n " ,
313
+ " \n " ,
314
+ " #Display\n " ,
315
+ " generate.on_click(generate_img)\n " ,
316
+ " \n " ,
317
+ " widgets.VBox(\n " ,
318
+ " [\n " ,
319
+ " widgets.AppLayout(\n " ,
320
+ " header=widgets.VBox(\n " ,
321
+ " [camera, lighting]\n " ,
322
+ " ),\n " ,
323
+ " left_sidebar=widgets.VBox(\n " ,
324
+ " [num_images, steps, CFG, img_height, img_width, random_seed]\n " ,
325
+ " ),\n " ,
326
+ " center=widgets.VBox(\n " ,
327
+ " [prompt, neg_prompt, generate]\n " ,
328
+ " ),\n " ,
329
+ " right_sidebar=None,\n " ,
330
+ " footer=None\n " ,
331
+ " ),\n " ,
332
+ " display_imgs\n " ,
333
+ " ]\n " ,
334
+ " )"
335
+ ],
336
+ "metadata" : {
337
+ "id" : " -g-Q5UGcGATS"
338
+ },
339
+ "execution_count" : null ,
340
+ "outputs" : []
341
+ }
342
+ ]
343
+ }
0 commit comments